blob: 290fdf090cd21072cbdca72525b5eee72ac67634 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart145e5a82020-01-27 16:23:12 -08004 * Copyright (C) 2017-2020 Broadcom. All Rights Reserved. The term *
James Smart3e21d1c2018-05-04 20:37:59 -07005 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
23
dea31012005-04-17 16:05:31 -050024#include <linux/blkdev.h>
25#include <linux/pci.h>
26#include <linux/interrupt.h>
27#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010029#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050030
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040031#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050032#include <scsi/scsi_cmnd.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040035#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040036#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040037#include <linux/aer.h>
Dick Kennedy86ee57a2020-06-30 14:49:55 -070038#include <linux/crash_dump.h>
James Smart1351e692018-02-22 08:18:43 -080039#ifdef CONFIG_X86
40#include <asm/set_memory.h>
41#endif
dea31012005-04-17 16:05:31 -050042
James Smartda0436e2009-05-22 14:51:39 -040043#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_hw.h"
45#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040046#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040047#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080050#include "lpfc_scsi.h"
51#include "lpfc_nvme.h"
dea31012005-04-17 16:05:31 -050052#include "lpfc_crtn.h"
53#include "lpfc_logmsg.h"
54#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050055#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040056#include "lpfc_vport.h"
James Smart61bda8f2016-10-13 15:06:05 -070057#include "lpfc_version.h"
dea31012005-04-17 16:05:31 -050058
59/* There are only four IOCB completion types. */
60typedef enum _lpfc_iocb_type {
61 LPFC_UNKNOWN_IOCB,
62 LPFC_UNSOL_IOCB,
63 LPFC_SOL_IOCB,
64 LPFC_ABORT_IOCB
65} lpfc_iocb_type;
66
James Smart4f774512009-05-22 14:52:35 -040067
68/* Provide function prototypes local to this module. */
69static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
70 uint32_t);
71static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040072 uint8_t *, uint32_t *);
73static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
74 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040075static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
76 struct hbq_dmabuf *);
James Smartae9e28f2017-05-15 15:20:51 -070077static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
78 struct hbq_dmabuf *dmabuf);
James Smart32517fc2019-01-28 11:14:33 -080079static bool lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba,
80 struct lpfc_queue *cq, struct lpfc_cqe *cqe);
James Smart895427b2017-02-12 13:52:30 -080081static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
James Smart8a9d2e82012-05-09 21:16:12 -040082 int);
Dick Kennedyf485c182017-09-29 17:34:34 -070083static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
James Smart32517fc2019-01-28 11:14:33 -080084 struct lpfc_queue *eq,
85 struct lpfc_eqe *eqe);
James Smarte8d3c3b2013-10-10 12:21:30 -040086static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
87static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart24c7c0a2019-09-21 20:58:58 -070088static struct lpfc_cqe *lpfc_sli4_cq_get(struct lpfc_queue *q);
89static void __lpfc_sli4_consume_cqe(struct lpfc_hba *phba,
90 struct lpfc_queue *cq,
91 struct lpfc_cqe *cqe);
James Smart05580562011-05-24 11:40:48 -040092
James Smart4f774512009-05-22 14:52:35 -040093static IOCB_t *
94lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
95{
96 return &iocbq->iocb;
97}
98
James Smart48f8fdb2018-05-04 20:37:51 -070099#if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
100/**
101 * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
102 * @srcp: Source memory pointer.
103 * @destp: Destination memory pointer.
104 * @cnt: Number of words required to be copied.
105 * Must be a multiple of sizeof(uint64_t)
106 *
107 * This function is used for copying data between driver memory
108 * and the SLI WQ. This function also changes the endianness
109 * of each word if native endianness is different from SLI
110 * endianness. This function can be called with or without
111 * lock.
112 **/
YueHaibingd7b761b2019-05-31 23:28:41 +0800113static void
James Smart48f8fdb2018-05-04 20:37:51 -0700114lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
115{
116 uint64_t *src = srcp;
117 uint64_t *dest = destp;
118 int i;
119
120 for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
121 *dest++ = *src++;
122}
123#else
124#define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
125#endif
126
James Smart4f774512009-05-22 14:52:35 -0400127/**
128 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
129 * @q: The Work Queue to operate on.
130 * @wqe: The work Queue Entry to put on the Work queue.
131 *
132 * This routine will copy the contents of @wqe to the next available entry on
133 * the @q. This function will then ring the Work Queue Doorbell to signal the
134 * HBA to start processing the Work Queue Entry. This function returns 0 if
135 * successful. If no entries are available on @q then this function will return
136 * -ENOMEM.
137 * The caller is expected to hold the hbalock when calling this routine.
138 **/
Dick Kennedycd22d602017-08-23 16:55:35 -0700139static int
James Smart205e8242018-03-05 12:04:03 -0800140lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -0400141{
James Smart2e90f4b2011-12-13 13:22:37 -0500142 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400143 struct lpfc_register doorbell;
144 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400145 uint32_t idx;
James Smart1351e692018-02-22 08:18:43 -0800146 uint32_t i = 0;
147 uint8_t *tmp;
James Smart5cc167d2018-06-26 08:24:23 -0700148 u32 if_type;
James Smart4f774512009-05-22 14:52:35 -0400149
James Smart2e90f4b2011-12-13 13:22:37 -0500150 /* sanity check on queue memory */
151 if (unlikely(!q))
152 return -ENOMEM;
James Smart9afbee32019-03-12 16:30:28 -0700153 temp_wqe = lpfc_sli4_qe(q, q->host_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500154
James Smart4f774512009-05-22 14:52:35 -0400155 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400156 idx = ((q->host_index + 1) % q->entry_count);
157 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400158 q->WQ_overflow++;
Dick Kennedycd22d602017-08-23 16:55:35 -0700159 return -EBUSY;
James Smartb84daac2012-08-03 12:35:13 -0400160 }
161 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400162 /* set consumption flag every once in a while */
James Smart32517fc2019-01-28 11:14:33 -0800163 if (!((q->host_index + 1) % q->notify_interval))
James Smartf0d9bcc2010-10-22 11:07:09 -0400164 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smart04673e32018-01-30 15:58:45 -0800165 else
166 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
James Smartfedd3b72011-02-16 12:39:24 -0500167 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
168 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart48f8fdb2018-05-04 20:37:51 -0700169 lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart1351e692018-02-22 08:18:43 -0800170 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
171 /* write to DPP aperture taking advatage of Combined Writes */
James Smart4c066192018-03-05 10:29:03 -0800172 tmp = (uint8_t *)temp_wqe;
173#ifdef __raw_writeq
James Smart1351e692018-02-22 08:18:43 -0800174 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
James Smart4c066192018-03-05 10:29:03 -0800175 __raw_writeq(*((uint64_t *)(tmp + i)),
176 q->dpp_regaddr + i);
177#else
178 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
179 __raw_writel(*((uint32_t *)(tmp + i)),
180 q->dpp_regaddr + i);
181#endif
James Smart1351e692018-02-22 08:18:43 -0800182 }
183 /* ensure WQE bcopy and DPP flushed before doorbell write */
James Smart6b3b3bd2016-12-19 15:07:30 -0800184 wmb();
James Smart4f774512009-05-22 14:52:35 -0400185
186 /* Update the host index before invoking device */
187 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400188
189 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400190
191 /* Ring Doorbell */
192 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500193 if (q->db_format == LPFC_DB_LIST_FORMAT) {
James Smart1351e692018-02-22 08:18:43 -0800194 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
195 bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
196 bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
197 bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
198 q->dpp_id);
199 bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
200 q->queue_id);
201 } else {
202 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
James Smart1351e692018-02-22 08:18:43 -0800203 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
James Smart5cc167d2018-06-26 08:24:23 -0700204
205 /* Leave bits <23:16> clear for if_type 6 dpp */
206 if_type = bf_get(lpfc_sli_intf_if_type,
207 &q->phba->sli4_hba.sli_intf);
208 if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
209 bf_set(lpfc_wq_db_list_fm_index, &doorbell,
210 host_index);
James Smart1351e692018-02-22 08:18:43 -0800211 }
James Smart962bc512013-01-03 15:44:00 -0500212 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
213 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
214 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
215 } else {
216 return -EINVAL;
217 }
218 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400219
220 return 0;
221}
222
223/**
224 * lpfc_sli4_wq_release - Updates internal hba index for WQ
225 * @q: The Work Queue to operate on.
226 * @index: The index to advance the hba index to.
227 *
228 * This routine will update the HBA index of a queue to reflect consumption of
229 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
230 * an entry the host calls this function to update the queue's internal
James Smart1543af32020-03-22 11:12:58 -0700231 * pointers.
James Smart4f774512009-05-22 14:52:35 -0400232 **/
James Smart1543af32020-03-22 11:12:58 -0700233static void
James Smart4f774512009-05-22 14:52:35 -0400234lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
235{
James Smart2e90f4b2011-12-13 13:22:37 -0500236 /* sanity check on queue memory */
237 if (unlikely(!q))
James Smart1543af32020-03-22 11:12:58 -0700238 return;
James Smart2e90f4b2011-12-13 13:22:37 -0500239
James Smart1543af32020-03-22 11:12:58 -0700240 q->hba_index = index;
James Smart4f774512009-05-22 14:52:35 -0400241}
242
243/**
244 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
245 * @q: The Mailbox Queue to operate on.
246 * @wqe: The Mailbox Queue Entry to put on the Work queue.
247 *
248 * This routine will copy the contents of @mqe to the next available entry on
249 * the @q. This function will then ring the Work Queue Doorbell to signal the
250 * HBA to start processing the Work Queue Entry. This function returns 0 if
251 * successful. If no entries are available on @q then this function will return
252 * -ENOMEM.
253 * The caller is expected to hold the hbalock when calling this routine.
254 **/
255static uint32_t
256lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
257{
James Smart2e90f4b2011-12-13 13:22:37 -0500258 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400259 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400260
James Smart2e90f4b2011-12-13 13:22:37 -0500261 /* sanity check on queue memory */
262 if (unlikely(!q))
263 return -ENOMEM;
James Smart9afbee32019-03-12 16:30:28 -0700264 temp_mqe = lpfc_sli4_qe(q, q->host_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500265
James Smart4f774512009-05-22 14:52:35 -0400266 /* If the host has not yet processed the next entry then we are done */
267 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
268 return -ENOMEM;
James Smart48f8fdb2018-05-04 20:37:51 -0700269 lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400270 /* Save off the mailbox pointer for completion */
271 q->phba->mbox = (MAILBOX_t *)temp_mqe;
272
273 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400274 q->host_index = ((q->host_index + 1) % q->entry_count);
275
276 /* Ring Doorbell */
277 doorbell.word0 = 0;
278 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
279 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
280 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400281 return 0;
282}
283
284/**
285 * lpfc_sli4_mq_release - Updates internal hba index for MQ
286 * @q: The Mailbox Queue to operate on.
287 *
288 * This routine will update the HBA index of a queue to reflect consumption of
289 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
290 * an entry the host calls this function to update the queue's internal
291 * pointers. This routine returns the number of entries that were consumed by
292 * the HBA.
293 **/
294static uint32_t
295lpfc_sli4_mq_release(struct lpfc_queue *q)
296{
James Smart2e90f4b2011-12-13 13:22:37 -0500297 /* sanity check on queue memory */
298 if (unlikely(!q))
299 return 0;
300
James Smart4f774512009-05-22 14:52:35 -0400301 /* Clear the mailbox pointer for completion */
302 q->phba->mbox = NULL;
303 q->hba_index = ((q->hba_index + 1) % q->entry_count);
304 return 1;
305}
306
307/**
308 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
309 * @q: The Event Queue to get the first valid EQE from
310 *
311 * This routine will get the first valid Event Queue Entry from @q, update
312 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
313 * the Queue (no more work to do), or the Queue is full of EQEs that have been
314 * processed, but not popped back to the HBA then this routine will return NULL.
315 **/
316static struct lpfc_eqe *
317lpfc_sli4_eq_get(struct lpfc_queue *q)
318{
James Smart2e90f4b2011-12-13 13:22:37 -0500319 struct lpfc_eqe *eqe;
320
321 /* sanity check on queue memory */
322 if (unlikely(!q))
323 return NULL;
James Smart9afbee32019-03-12 16:30:28 -0700324 eqe = lpfc_sli4_qe(q, q->host_index);
James Smart4f774512009-05-22 14:52:35 -0400325
326 /* If the next EQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800327 if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400328 return NULL;
James Smart27f344e2014-05-07 17:16:46 -0400329
330 /*
331 * insert barrier for instruction interlock : data from the hardware
332 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800333 * upon. Speculative instructions were allowing a bcopy at the start
334 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
335 * after our return, to copy data before the valid bit check above
336 * was done. As such, some of the copied data was stale. The barrier
337 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400338 */
339 mb();
James Smart4f774512009-05-22 14:52:35 -0400340 return eqe;
341}
342
343/**
James Smartba20c852012-08-03 12:36:52 -0400344 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
345 * @q: The Event Queue to disable interrupts
346 *
347 **/
James Smart92f3b322019-03-20 10:44:22 -0700348void
James Smartba20c852012-08-03 12:36:52 -0400349lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
350{
351 struct lpfc_register doorbell;
352
353 doorbell.word0 = 0;
354 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
355 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
356 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
357 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
358 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800359 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smartba20c852012-08-03 12:36:52 -0400360}
361
362/**
James Smart27d6ac02018-02-22 08:18:42 -0800363 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
364 * @q: The Event Queue to disable interrupts
365 *
366 **/
James Smart92f3b322019-03-20 10:44:22 -0700367void
James Smart27d6ac02018-02-22 08:18:42 -0800368lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
369{
370 struct lpfc_register doorbell;
371
372 doorbell.word0 = 0;
James Smartaad59d52018-09-10 10:30:47 -0700373 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
James Smart27d6ac02018-02-22 08:18:42 -0800374 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
375}
376
377/**
James Smart32517fc2019-01-28 11:14:33 -0800378 * lpfc_sli4_write_eq_db - write EQ DB for eqe's consumed or arm state
379 * @phba: adapter with EQ
James Smart4f774512009-05-22 14:52:35 -0400380 * @q: The Event Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800381 * @count: Number of elements that have been consumed
James Smart4f774512009-05-22 14:52:35 -0400382 * @arm: Indicates whether the host wants to arms this CQ.
383 *
James Smart32517fc2019-01-28 11:14:33 -0800384 * This routine will notify the HBA, by ringing the doorbell, that count
385 * number of EQEs have been processed. The @arm parameter indicates whether
386 * the queue should be rearmed when ringing the doorbell.
James Smart4f774512009-05-22 14:52:35 -0400387 **/
James Smart32517fc2019-01-28 11:14:33 -0800388void
389lpfc_sli4_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
390 uint32_t count, bool arm)
James Smart4f774512009-05-22 14:52:35 -0400391{
James Smart4f774512009-05-22 14:52:35 -0400392 struct lpfc_register doorbell;
393
James Smart2e90f4b2011-12-13 13:22:37 -0500394 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800395 if (unlikely(!q || (count == 0 && !arm)))
396 return;
James Smart4f774512009-05-22 14:52:35 -0400397
398 /* ring doorbell for number popped */
399 doorbell.word0 = 0;
400 if (arm) {
401 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
402 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
403 }
James Smart32517fc2019-01-28 11:14:33 -0800404 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
James Smart4f774512009-05-22 14:52:35 -0400405 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500406 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
407 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
408 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800409 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500410 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
411 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
James Smart9dd35422018-02-22 08:18:41 -0800412 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400413}
414
415/**
James Smart32517fc2019-01-28 11:14:33 -0800416 * lpfc_sli4_if6_write_eq_db - write EQ DB for eqe's consumed or arm state
417 * @phba: adapter with EQ
James Smart27d6ac02018-02-22 08:18:42 -0800418 * @q: The Event Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800419 * @count: Number of elements that have been consumed
James Smart27d6ac02018-02-22 08:18:42 -0800420 * @arm: Indicates whether the host wants to arms this CQ.
421 *
James Smart32517fc2019-01-28 11:14:33 -0800422 * This routine will notify the HBA, by ringing the doorbell, that count
423 * number of EQEs have been processed. The @arm parameter indicates whether
424 * the queue should be rearmed when ringing the doorbell.
James Smart27d6ac02018-02-22 08:18:42 -0800425 **/
James Smart32517fc2019-01-28 11:14:33 -0800426void
427lpfc_sli4_if6_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
428 uint32_t count, bool arm)
James Smart27d6ac02018-02-22 08:18:42 -0800429{
James Smart27d6ac02018-02-22 08:18:42 -0800430 struct lpfc_register doorbell;
431
432 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800433 if (unlikely(!q || (count == 0 && !arm)))
434 return;
James Smart27d6ac02018-02-22 08:18:42 -0800435
436 /* ring doorbell for number popped */
437 doorbell.word0 = 0;
438 if (arm)
439 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800440 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, count);
James Smart27d6ac02018-02-22 08:18:42 -0800441 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
442 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
443 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
444 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
445 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart32517fc2019-01-28 11:14:33 -0800446}
447
448static void
449__lpfc_sli4_consume_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
450 struct lpfc_eqe *eqe)
451{
452 if (!phba->sli4_hba.pc_sli4_params.eqav)
453 bf_set_le32(lpfc_eqe_valid, eqe, 0);
454
455 eq->host_index = ((eq->host_index + 1) % eq->entry_count);
456
457 /* if the index wrapped around, toggle the valid bit */
458 if (phba->sli4_hba.pc_sli4_params.eqav && !eq->host_index)
459 eq->qe_valid = (eq->qe_valid) ? 0 : 1;
460}
461
462static void
James Smart24c7c0a2019-09-21 20:58:58 -0700463lpfc_sli4_eqcq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
James Smart32517fc2019-01-28 11:14:33 -0800464{
James Smart24c7c0a2019-09-21 20:58:58 -0700465 struct lpfc_eqe *eqe = NULL;
466 u32 eq_count = 0, cq_count = 0;
467 struct lpfc_cqe *cqe = NULL;
468 struct lpfc_queue *cq = NULL, *childq = NULL;
469 int cqid = 0;
James Smart32517fc2019-01-28 11:14:33 -0800470
471 /* walk all the EQ entries and drop on the floor */
472 eqe = lpfc_sli4_eq_get(eq);
473 while (eqe) {
James Smart24c7c0a2019-09-21 20:58:58 -0700474 /* Get the reference to the corresponding CQ */
475 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
476 cq = NULL;
477
478 list_for_each_entry(childq, &eq->child_list, list) {
479 if (childq->queue_id == cqid) {
480 cq = childq;
481 break;
482 }
483 }
484 /* If CQ is valid, iterate through it and drop all the CQEs */
485 if (cq) {
486 cqe = lpfc_sli4_cq_get(cq);
487 while (cqe) {
488 __lpfc_sli4_consume_cqe(phba, cq, cqe);
489 cq_count++;
490 cqe = lpfc_sli4_cq_get(cq);
491 }
492 /* Clear and re-arm the CQ */
493 phba->sli4_hba.sli4_write_cq_db(phba, cq, cq_count,
494 LPFC_QUEUE_REARM);
495 cq_count = 0;
496 }
James Smart32517fc2019-01-28 11:14:33 -0800497 __lpfc_sli4_consume_eqe(phba, eq, eqe);
James Smart24c7c0a2019-09-21 20:58:58 -0700498 eq_count++;
James Smart32517fc2019-01-28 11:14:33 -0800499 eqe = lpfc_sli4_eq_get(eq);
500 }
501
502 /* Clear and re-arm the EQ */
James Smart24c7c0a2019-09-21 20:58:58 -0700503 phba->sli4_hba.sli4_write_eq_db(phba, eq, eq_count, LPFC_QUEUE_REARM);
James Smart32517fc2019-01-28 11:14:33 -0800504}
505
506static int
James Smart93a4d6f2019-11-04 16:57:05 -0800507lpfc_sli4_process_eq(struct lpfc_hba *phba, struct lpfc_queue *eq,
508 uint8_t rearm)
James Smart32517fc2019-01-28 11:14:33 -0800509{
510 struct lpfc_eqe *eqe;
511 int count = 0, consumed = 0;
512
513 if (cmpxchg(&eq->queue_claimed, 0, 1) != 0)
514 goto rearm_and_exit;
515
516 eqe = lpfc_sli4_eq_get(eq);
517 while (eqe) {
518 lpfc_sli4_hba_handle_eqe(phba, eq, eqe);
519 __lpfc_sli4_consume_eqe(phba, eq, eqe);
520
521 consumed++;
522 if (!(++count % eq->max_proc_limit))
523 break;
524
525 if (!(count % eq->notify_interval)) {
526 phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed,
527 LPFC_QUEUE_NOARM);
528 consumed = 0;
529 }
530
531 eqe = lpfc_sli4_eq_get(eq);
532 }
533 eq->EQ_processed += count;
534
535 /* Track the max number of EQEs processed in 1 intr */
536 if (count > eq->EQ_max_eqe)
537 eq->EQ_max_eqe = count;
538
Dick Kennedy164ba8d2020-05-01 14:43:03 -0700539 xchg(&eq->queue_claimed, 0);
James Smart32517fc2019-01-28 11:14:33 -0800540
541rearm_and_exit:
James Smart93a4d6f2019-11-04 16:57:05 -0800542 /* Always clear the EQ. */
543 phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed, rearm);
James Smart32517fc2019-01-28 11:14:33 -0800544
545 return count;
James Smart27d6ac02018-02-22 08:18:42 -0800546}
547
548/**
James Smart4f774512009-05-22 14:52:35 -0400549 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
550 * @q: The Completion Queue to get the first valid CQE from
551 *
552 * This routine will get the first valid Completion Queue Entry from @q, update
553 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
554 * the Queue (no more work to do), or the Queue is full of CQEs that have been
555 * processed, but not popped back to the HBA then this routine will return NULL.
556 **/
557static struct lpfc_cqe *
558lpfc_sli4_cq_get(struct lpfc_queue *q)
559{
560 struct lpfc_cqe *cqe;
561
James Smart2e90f4b2011-12-13 13:22:37 -0500562 /* sanity check on queue memory */
563 if (unlikely(!q))
564 return NULL;
James Smart9afbee32019-03-12 16:30:28 -0700565 cqe = lpfc_sli4_qe(q, q->host_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500566
James Smart4f774512009-05-22 14:52:35 -0400567 /* If the next CQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800568 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400569 return NULL;
James Smart27f344e2014-05-07 17:16:46 -0400570
571 /*
572 * insert barrier for instruction interlock : data from the hardware
573 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800574 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
575 * instructions allowing action on content before valid bit checked,
576 * add barrier here as well. May not be needed as "content" is a
577 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400578 */
579 mb();
James Smart4f774512009-05-22 14:52:35 -0400580 return cqe;
581}
582
James Smart32517fc2019-01-28 11:14:33 -0800583static void
584__lpfc_sli4_consume_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
585 struct lpfc_cqe *cqe)
586{
587 if (!phba->sli4_hba.pc_sli4_params.cqav)
588 bf_set_le32(lpfc_cqe_valid, cqe, 0);
589
590 cq->host_index = ((cq->host_index + 1) % cq->entry_count);
591
592 /* if the index wrapped around, toggle the valid bit */
593 if (phba->sli4_hba.pc_sli4_params.cqav && !cq->host_index)
594 cq->qe_valid = (cq->qe_valid) ? 0 : 1;
595}
596
James Smart4f774512009-05-22 14:52:35 -0400597/**
James Smart32517fc2019-01-28 11:14:33 -0800598 * lpfc_sli4_write_cq_db - write cq DB for entries consumed or arm state.
599 * @phba: the adapter with the CQ
James Smart4f774512009-05-22 14:52:35 -0400600 * @q: The Completion Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800601 * @count: the number of elements that were consumed
James Smart4f774512009-05-22 14:52:35 -0400602 * @arm: Indicates whether the host wants to arms this CQ.
603 *
James Smart32517fc2019-01-28 11:14:33 -0800604 * This routine will notify the HBA, by ringing the doorbell, that the
605 * CQEs have been processed. The @arm parameter specifies whether the
606 * queue should be rearmed when ringing the doorbell.
James Smart4f774512009-05-22 14:52:35 -0400607 **/
James Smart32517fc2019-01-28 11:14:33 -0800608void
609lpfc_sli4_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
610 uint32_t count, bool arm)
James Smart4f774512009-05-22 14:52:35 -0400611{
James Smart4f774512009-05-22 14:52:35 -0400612 struct lpfc_register doorbell;
613
James Smart2e90f4b2011-12-13 13:22:37 -0500614 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800615 if (unlikely(!q || (count == 0 && !arm)))
616 return;
James Smart4f774512009-05-22 14:52:35 -0400617
618 /* ring doorbell for number popped */
619 doorbell.word0 = 0;
620 if (arm)
621 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800622 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
James Smart4f774512009-05-22 14:52:35 -0400623 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500624 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
625 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
626 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800627 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400628}
629
630/**
James Smart32517fc2019-01-28 11:14:33 -0800631 * lpfc_sli4_if6_write_cq_db - write cq DB for entries consumed or arm state.
632 * @phba: the adapter with the CQ
James Smart27d6ac02018-02-22 08:18:42 -0800633 * @q: The Completion Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800634 * @count: the number of elements that were consumed
James Smart27d6ac02018-02-22 08:18:42 -0800635 * @arm: Indicates whether the host wants to arms this CQ.
636 *
James Smart32517fc2019-01-28 11:14:33 -0800637 * This routine will notify the HBA, by ringing the doorbell, that the
638 * CQEs have been processed. The @arm parameter specifies whether the
639 * queue should be rearmed when ringing the doorbell.
James Smart27d6ac02018-02-22 08:18:42 -0800640 **/
James Smart32517fc2019-01-28 11:14:33 -0800641void
642lpfc_sli4_if6_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
643 uint32_t count, bool arm)
James Smart27d6ac02018-02-22 08:18:42 -0800644{
James Smart27d6ac02018-02-22 08:18:42 -0800645 struct lpfc_register doorbell;
646
647 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800648 if (unlikely(!q || (count == 0 && !arm)))
649 return;
James Smart27d6ac02018-02-22 08:18:42 -0800650
651 /* ring doorbell for number popped */
652 doorbell.word0 = 0;
653 if (arm)
654 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800655 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, count);
James Smart27d6ac02018-02-22 08:18:42 -0800656 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
657 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart27d6ac02018-02-22 08:18:42 -0800658}
659
660/**
James Smart4f774512009-05-22 14:52:35 -0400661 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
662 * @q: The Header Receive Queue to operate on.
663 * @wqe: The Receive Queue Entry to put on the Receive queue.
664 *
665 * This routine will copy the contents of @wqe to the next available entry on
666 * the @q. This function will then ring the Receive Queue Doorbell to signal the
667 * HBA to start processing the Receive Queue Entry. This function returns the
668 * index that the rqe was copied to if successful. If no entries are available
669 * on @q then this function will return -ENOMEM.
670 * The caller is expected to hold the hbalock when calling this routine.
671 **/
James Smart895427b2017-02-12 13:52:30 -0800672int
James Smart4f774512009-05-22 14:52:35 -0400673lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
674 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
675{
James Smart2e90f4b2011-12-13 13:22:37 -0500676 struct lpfc_rqe *temp_hrqe;
677 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400678 struct lpfc_register doorbell;
James Smartcbc5de12017-12-08 17:18:04 -0800679 int hq_put_index;
680 int dq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400681
James Smart2e90f4b2011-12-13 13:22:37 -0500682 /* sanity check on queue memory */
683 if (unlikely(!hq) || unlikely(!dq))
684 return -ENOMEM;
James Smartcbc5de12017-12-08 17:18:04 -0800685 hq_put_index = hq->host_index;
686 dq_put_index = dq->host_index;
James Smart9afbee32019-03-12 16:30:28 -0700687 temp_hrqe = lpfc_sli4_qe(hq, hq_put_index);
688 temp_drqe = lpfc_sli4_qe(dq, dq_put_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500689
James Smart4f774512009-05-22 14:52:35 -0400690 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
691 return -EINVAL;
James Smartcbc5de12017-12-08 17:18:04 -0800692 if (hq_put_index != dq_put_index)
James Smart4f774512009-05-22 14:52:35 -0400693 return -EINVAL;
694 /* If the host has not yet processed the next entry then we are done */
James Smartcbc5de12017-12-08 17:18:04 -0800695 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400696 return -EBUSY;
James Smart48f8fdb2018-05-04 20:37:51 -0700697 lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
698 lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400699
700 /* Update the host index to point to the next slot */
James Smartcbc5de12017-12-08 17:18:04 -0800701 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
702 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700703 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400704
705 /* Ring The Header Receive Queue Doorbell */
James Smart32517fc2019-01-28 11:14:33 -0800706 if (!(hq->host_index % hq->notify_interval)) {
James Smart4f774512009-05-22 14:52:35 -0400707 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500708 if (hq->db_format == LPFC_DB_RING_FORMAT) {
709 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
James Smart32517fc2019-01-28 11:14:33 -0800710 hq->notify_interval);
James Smart962bc512013-01-03 15:44:00 -0500711 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
712 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
713 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
James Smart32517fc2019-01-28 11:14:33 -0800714 hq->notify_interval);
James Smart962bc512013-01-03 15:44:00 -0500715 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
716 hq->host_index);
717 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
718 } else {
719 return -EINVAL;
720 }
721 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400722 }
James Smartcbc5de12017-12-08 17:18:04 -0800723 return hq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400724}
725
726/**
727 * lpfc_sli4_rq_release - Updates internal hba index for RQ
728 * @q: The Header Receive Queue to operate on.
729 *
730 * This routine will update the HBA index of a queue to reflect consumption of
731 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
732 * consumed an entry the host calls this function to update the queue's
733 * internal pointers. This routine returns the number of entries that were
734 * consumed by the HBA.
735 **/
736static uint32_t
737lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
738{
James Smart2e90f4b2011-12-13 13:22:37 -0500739 /* sanity check on queue memory */
740 if (unlikely(!hq) || unlikely(!dq))
741 return 0;
742
James Smart4f774512009-05-22 14:52:35 -0400743 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
744 return 0;
745 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
746 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
747 return 1;
748}
749
James Smarte59058c2008-08-24 21:49:00 -0400750/**
James Smart3621a712009-04-06 18:47:14 -0400751 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400752 * @phba: Pointer to HBA context object.
753 * @pring: Pointer to driver SLI ring object.
754 *
755 * This function returns pointer to next command iocb entry
756 * in the command ring. The caller must hold hbalock to prevent
757 * other threads consume the next command iocb.
758 * SLI-2/SLI-3 provide different sized iocbs.
759 **/
James Smarted957682007-06-17 19:56:37 -0500760static inline IOCB_t *
761lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
762{
James Smart7e56aa22012-08-03 12:35:34 -0400763 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
764 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500765}
766
James Smarte59058c2008-08-24 21:49:00 -0400767/**
James Smart3621a712009-04-06 18:47:14 -0400768 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400769 * @phba: Pointer to HBA context object.
770 * @pring: Pointer to driver SLI ring object.
771 *
772 * This function returns pointer to next response iocb entry
773 * in the response ring. The caller must hold hbalock to make sure
774 * that no other thread consume the next response iocb.
775 * SLI-2/SLI-3 provide different sized iocbs.
776 **/
James Smarted957682007-06-17 19:56:37 -0500777static inline IOCB_t *
778lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
779{
James Smart7e56aa22012-08-03 12:35:34 -0400780 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
781 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500782}
783
James Smarte59058c2008-08-24 21:49:00 -0400784/**
James Smart3621a712009-04-06 18:47:14 -0400785 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400786 * @phba: Pointer to HBA context object.
787 *
788 * This function is called with hbalock held. This function
789 * allocates a new driver iocb object from the iocb pool. If the
790 * allocation is successful, it returns pointer to the newly
791 * allocated iocb object else it returns NULL.
792 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400793struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500794__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400795{
796 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
797 struct lpfc_iocbq * iocbq = NULL;
798
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100799 lockdep_assert_held(&phba->hbalock);
800
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400801 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400802 if (iocbq)
803 phba->iocb_cnt++;
804 if (phba->iocb_cnt > phba->iocb_max)
805 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400806 return iocbq;
807}
808
James Smarte59058c2008-08-24 21:49:00 -0400809/**
James Smartda0436e2009-05-22 14:51:39 -0400810 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
811 * @phba: Pointer to HBA context object.
812 * @xritag: XRI value.
813 *
814 * This function clears the sglq pointer from the array of acive
815 * sglq's. The xritag that is passed in is used to index into the
816 * array. Before the xritag can be used it needs to be adjusted
817 * by subtracting the xribase.
818 *
819 * Returns sglq ponter = success, NULL = Failure.
820 **/
James Smart895427b2017-02-12 13:52:30 -0800821struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400822__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
823{
James Smartda0436e2009-05-22 14:51:39 -0400824 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400825
826 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
827 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400828 return sglq;
829}
830
831/**
832 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
833 * @phba: Pointer to HBA context object.
834 * @xritag: XRI value.
835 *
836 * This function returns the sglq pointer from the array of acive
837 * sglq's. The xritag that is passed in is used to index into the
838 * array. Before the xritag can be used it needs to be adjusted
839 * by subtracting the xribase.
840 *
841 * Returns sglq ponter = success, NULL = Failure.
842 **/
James Smart0f65ff62010-02-26 14:14:23 -0500843struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400844__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
845{
James Smartda0436e2009-05-22 14:51:39 -0400846 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400847
848 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400849 return sglq;
850}
851
852/**
James Smart1151e3e2011-02-16 12:39:35 -0500853 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500854 * @phba: Pointer to HBA context object.
855 * @xritag: xri used in this exchange.
856 * @rrq: The RRQ to be cleared.
857 *
James Smart19ca7602010-11-20 23:11:55 -0500858 **/
James Smart1151e3e2011-02-16 12:39:35 -0500859void
860lpfc_clr_rrq_active(struct lpfc_hba *phba,
861 uint16_t xritag,
862 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500863{
James Smart1151e3e2011-02-16 12:39:35 -0500864 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500865
James Smart1151e3e2011-02-16 12:39:35 -0500866 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
867 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500868
869 /* The target DID could have been swapped (cable swap)
870 * we should use the ndlp from the findnode if it is
871 * available.
872 */
James Smart1151e3e2011-02-16 12:39:35 -0500873 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500874 ndlp = rrq->ndlp;
875
James Smart1151e3e2011-02-16 12:39:35 -0500876 if (!ndlp)
877 goto out;
878
James Smartcff261f2013-12-17 20:29:47 -0500879 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500880 rrq->send_rrq = 0;
881 rrq->xritag = 0;
882 rrq->rrq_stop_time = 0;
883 }
James Smart1151e3e2011-02-16 12:39:35 -0500884out:
James Smart19ca7602010-11-20 23:11:55 -0500885 mempool_free(rrq, phba->rrq_pool);
886}
887
888/**
889 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
890 * @phba: Pointer to HBA context object.
891 *
892 * This function is called with hbalock held. This function
893 * Checks if stop_time (ratov from setting rrq active) has
894 * been reached, if it has and the send_rrq flag is set then
895 * it will call lpfc_send_rrq. If the send_rrq flag is not set
896 * then it will just call the routine to clear the rrq and
897 * free the rrq resource.
898 * The timer is set to the next rrq that is going to expire before
899 * leaving the routine.
900 *
901 **/
902void
903lpfc_handle_rrq_active(struct lpfc_hba *phba)
904{
905 struct lpfc_node_rrq *rrq;
906 struct lpfc_node_rrq *nextrrq;
907 unsigned long next_time;
908 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500909 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500910
911 spin_lock_irqsave(&phba->hbalock, iflags);
912 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400913 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500914 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500915 &phba->active_rrq_list, list) {
916 if (time_after(jiffies, rrq->rrq_stop_time))
917 list_move(&rrq->list, &send_rrq);
918 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500919 next_time = rrq->rrq_stop_time;
920 }
921 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500922 if ((!list_empty(&phba->active_rrq_list)) &&
923 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500924 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500925 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
926 list_del(&rrq->list);
Bart Van Asscheffd43812019-03-28 11:06:17 -0700927 if (!rrq->send_rrq) {
James Smart1151e3e2011-02-16 12:39:35 -0500928 /* this call will free the rrq */
Bart Van Asscheffd43812019-03-28 11:06:17 -0700929 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
930 } else if (lpfc_send_rrq(phba, rrq)) {
James Smart1151e3e2011-02-16 12:39:35 -0500931 /* if we send the rrq then the completion handler
932 * will clear the bit in the xribitmap.
933 */
934 lpfc_clr_rrq_active(phba, rrq->xritag,
935 rrq);
936 }
937 }
James Smart19ca7602010-11-20 23:11:55 -0500938}
939
940/**
941 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
942 * @vport: Pointer to vport context object.
943 * @xri: The xri used in the exchange.
944 * @did: The targets DID for this exchange.
945 *
946 * returns NULL = rrq not found in the phba->active_rrq_list.
947 * rrq = rrq for this xri and target.
948 **/
949struct lpfc_node_rrq *
950lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
951{
952 struct lpfc_hba *phba = vport->phba;
953 struct lpfc_node_rrq *rrq;
954 struct lpfc_node_rrq *nextrrq;
955 unsigned long iflags;
956
957 if (phba->sli_rev != LPFC_SLI_REV4)
958 return NULL;
959 spin_lock_irqsave(&phba->hbalock, iflags);
960 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
961 if (rrq->vport == vport && rrq->xritag == xri &&
962 rrq->nlp_DID == did){
963 list_del(&rrq->list);
964 spin_unlock_irqrestore(&phba->hbalock, iflags);
965 return rrq;
966 }
967 }
968 spin_unlock_irqrestore(&phba->hbalock, iflags);
969 return NULL;
970}
971
972/**
973 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
974 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500975 * @ndlp: Pointer to the lpfc_node_list structure.
976 * If ndlp is NULL Remove all active RRQs for this vport from the
977 * phba->active_rrq_list and clear the rrq.
978 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500979 **/
980void
James Smart1151e3e2011-02-16 12:39:35 -0500981lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500982
983{
984 struct lpfc_hba *phba = vport->phba;
985 struct lpfc_node_rrq *rrq;
986 struct lpfc_node_rrq *nextrrq;
987 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500988 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500989
990 if (phba->sli_rev != LPFC_SLI_REV4)
991 return;
James Smart1151e3e2011-02-16 12:39:35 -0500992 if (!ndlp) {
993 lpfc_sli4_vport_delete_els_xri_aborted(vport);
994 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500995 }
James Smart1151e3e2011-02-16 12:39:35 -0500996 spin_lock_irqsave(&phba->hbalock, iflags);
997 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
998 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
999 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -05001000 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -05001001
1002 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1003 list_del(&rrq->list);
1004 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1005 }
James Smart19ca7602010-11-20 23:11:55 -05001006}
1007
1008/**
James Smart1151e3e2011-02-16 12:39:35 -05001009 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -05001010 * @phba: Pointer to HBA context object.
1011 * @ndlp: Targets nodelist pointer for this exchange.
1012 * @xritag the xri in the bitmap to test.
1013 *
James Smarte2a8be52019-05-06 17:26:47 -07001014 * This function returns:
1015 * 0 = rrq not active for this xri
1016 * 1 = rrq is valid for this xri.
James Smart19ca7602010-11-20 23:11:55 -05001017 **/
James Smart1151e3e2011-02-16 12:39:35 -05001018int
1019lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -05001020 uint16_t xritag)
1021{
James Smart19ca7602010-11-20 23:11:55 -05001022 if (!ndlp)
1023 return 0;
James Smartcff261f2013-12-17 20:29:47 -05001024 if (!ndlp->active_rrqs_xri_bitmap)
1025 return 0;
1026 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
Colin Ian King258f84f2019-02-12 15:29:45 +00001027 return 1;
James Smart19ca7602010-11-20 23:11:55 -05001028 else
1029 return 0;
1030}
1031
1032/**
1033 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1034 * @phba: Pointer to HBA context object.
1035 * @ndlp: nodelist pointer for this target.
1036 * @xritag: xri used in this exchange.
1037 * @rxid: Remote Exchange ID.
1038 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1039 *
1040 * This function takes the hbalock.
1041 * The active bit is always set in the active rrq xri_bitmap even
1042 * if there is no slot avaiable for the other rrq information.
1043 *
1044 * returns 0 rrq actived for this xri
1045 * < 0 No memory or invalid ndlp.
1046 **/
1047int
1048lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -05001049 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -05001050{
James Smart19ca7602010-11-20 23:11:55 -05001051 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -05001052 struct lpfc_node_rrq *rrq;
1053 int empty;
1054
1055 if (!ndlp)
1056 return -EINVAL;
1057
1058 if (!phba->cfg_enable_rrq)
1059 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001060
1061 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001062 if (phba->pport->load_flag & FC_UNLOADING) {
1063 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1064 goto out;
1065 }
1066
1067 /*
1068 * set the active bit even if there is no mem available.
1069 */
1070 if (NLP_CHK_FREE_REQ(ndlp))
1071 goto out;
1072
1073 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1074 goto out;
1075
James Smartcff261f2013-12-17 20:29:47 -05001076 if (!ndlp->active_rrqs_xri_bitmap)
1077 goto out;
1078
1079 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -05001080 goto out;
1081
James Smart19ca7602010-11-20 23:11:55 -05001082 spin_unlock_irqrestore(&phba->hbalock, iflags);
Dick Kennedy9dace1f2020-06-30 14:49:53 -07001083 rrq = mempool_alloc(phba->rrq_pool, GFP_ATOMIC);
James Smartb42c07c2012-01-18 16:25:55 -05001084 if (!rrq) {
1085 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1086 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1087 " DID:0x%x Send:%d\n",
1088 xritag, rxid, ndlp->nlp_DID, send_rrq);
1089 return -EINVAL;
1090 }
James Smarte5771b42013-03-01 16:37:14 -05001091 if (phba->cfg_enable_rrq == 1)
1092 rrq->send_rrq = send_rrq;
1093 else
1094 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001095 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001096 rrq->rrq_stop_time = jiffies +
1097 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001098 rrq->ndlp = ndlp;
1099 rrq->nlp_DID = ndlp->nlp_DID;
1100 rrq->vport = ndlp->vport;
1101 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001102 spin_lock_irqsave(&phba->hbalock, iflags);
1103 empty = list_empty(&phba->active_rrq_list);
1104 list_add_tail(&rrq->list, &phba->active_rrq_list);
1105 phba->hba_flag |= HBA_RRQ_ACTIVE;
1106 if (empty)
1107 lpfc_worker_wake_up(phba);
1108 spin_unlock_irqrestore(&phba->hbalock, iflags);
1109 return 0;
1110out:
1111 spin_unlock_irqrestore(&phba->hbalock, iflags);
1112 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1113 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1114 " DID:0x%x Send:%d\n",
1115 xritag, rxid, ndlp->nlp_DID, send_rrq);
1116 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001117}
1118
1119/**
James Smart895427b2017-02-12 13:52:30 -08001120 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001121 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -05001122 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001123 *
James Smarte2a8be52019-05-06 17:26:47 -07001124 * The driver calls this function with either the nvme ls ring lock
1125 * or the fc els ring lock held depending on the iocb usage. This function
1126 * gets a new driver sglq object from the sglq list. If the list is not empty
1127 * then it is successful, it returns pointer to the newly allocated sglq
1128 * object else it returns NULL.
James Smartda0436e2009-05-22 14:51:39 -04001129 **/
1130static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -08001131__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -04001132{
James Smart895427b2017-02-12 13:52:30 -08001133 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -04001134 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001135 struct lpfc_sglq *start_sglq = NULL;
James Smartc4908502019-01-28 11:14:28 -08001136 struct lpfc_io_buf *lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001137 struct lpfc_nodelist *ndlp;
James Smarte2a8be52019-05-06 17:26:47 -07001138 struct lpfc_sli_ring *pring = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001139 int found = 0;
1140
James Smarte2a8be52019-05-06 17:26:47 -07001141 if (piocbq->iocb_flag & LPFC_IO_NVME_LS)
1142 pring = phba->sli4_hba.nvmels_wq->pring;
1143 else
1144 pring = lpfc_phba_elsring(phba);
1145
1146 lockdep_assert_held(&pring->ring_lock);
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001147
James Smart19ca7602010-11-20 23:11:55 -05001148 if (piocbq->iocb_flag & LPFC_IO_FCP) {
James Smartc4908502019-01-28 11:14:28 -08001149 lpfc_cmd = (struct lpfc_io_buf *) piocbq->context1;
James Smart19ca7602010-11-20 23:11:55 -05001150 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001151 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001152 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001153 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001154 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1155 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1156 ndlp = NULL;
1157 else
1158 ndlp = piocbq->context_un.ndlp;
1159 } else {
James Smart19ca7602010-11-20 23:11:55 -05001160 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001161 }
James Smart19ca7602010-11-20 23:11:55 -05001162
James Smart895427b2017-02-12 13:52:30 -08001163 spin_lock(&phba->sli4_hba.sgl_list_lock);
1164 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001165 start_sglq = sglq;
1166 while (!found) {
1167 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001168 break;
James Smart895427b2017-02-12 13:52:30 -08001169 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1170 test_bit(sglq->sli4_lxritag,
1171 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001172 /* This xri has an rrq outstanding for this DID.
1173 * put it back in the list and get another xri.
1174 */
James Smart895427b2017-02-12 13:52:30 -08001175 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001176 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001177 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001178 struct lpfc_sglq, list);
1179 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001180 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001181 sglq = NULL;
1182 break;
1183 } else
1184 continue;
1185 }
1186 sglq->ndlp = ndlp;
1187 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001188 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001189 sglq->state = SGL_ALLOCATED;
1190 }
James Smart895427b2017-02-12 13:52:30 -08001191 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001192 return sglq;
1193}
1194
1195/**
James Smartf358dd02017-02-12 13:52:34 -08001196 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1197 * @phba: Pointer to HBA context object.
1198 * @piocb: Pointer to the iocbq.
1199 *
1200 * This function is called with the sgl_list lock held. This function
1201 * gets a new driver sglq object from the sglq list. If the
1202 * list is not empty then it is successful, it returns pointer to the newly
1203 * allocated sglq object else it returns NULL.
1204 **/
1205struct lpfc_sglq *
1206__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1207{
1208 struct list_head *lpfc_nvmet_sgl_list;
1209 struct lpfc_sglq *sglq = NULL;
1210
1211 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1212
1213 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1214
1215 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1216 if (!sglq)
1217 return NULL;
1218 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1219 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001220 return sglq;
1221}
1222
James Smarte59058c2008-08-24 21:49:00 -04001223/**
James Smart3621a712009-04-06 18:47:14 -04001224 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001225 * @phba: Pointer to HBA context object.
1226 *
1227 * This function is called with no lock held. This function
1228 * allocates a new driver iocb object from the iocb pool. If the
1229 * allocation is successful, it returns pointer to the newly
1230 * allocated iocb object else it returns NULL.
1231 **/
James Smart2e0fef82007-06-17 19:56:36 -05001232struct lpfc_iocbq *
1233lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001234{
James Smart2e0fef82007-06-17 19:56:36 -05001235 struct lpfc_iocbq * iocbq = NULL;
1236 unsigned long iflags;
1237
1238 spin_lock_irqsave(&phba->hbalock, iflags);
1239 iocbq = __lpfc_sli_get_iocbq(phba);
1240 spin_unlock_irqrestore(&phba->hbalock, iflags);
1241 return iocbq;
1242}
1243
James Smarte59058c2008-08-24 21:49:00 -04001244/**
James Smart4f774512009-05-22 14:52:35 -04001245 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1246 * @phba: Pointer to HBA context object.
1247 * @iocbq: Pointer to driver iocb object.
1248 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001249 * This function is called to release the driver iocb object
1250 * to the iocb pool. The iotag in the iocb object
James Smart4f774512009-05-22 14:52:35 -04001251 * does not change for each use of the iocb object. This function
1252 * clears all other fields of the iocb object when it is freed.
1253 * The sqlq structure that holds the xritag and phys and virtual
1254 * mappings for the scatter gather list is retrieved from the
1255 * active array of sglq. The get of the sglq pointer also clears
1256 * the entry in the array. If the status of the IO indiactes that
1257 * this IO was aborted then the sglq entry it put on the
1258 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1259 * IO has good status or fails for any other reason then the sglq
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001260 * entry is added to the free list (lpfc_els_sgl_list). The hbalock is
1261 * asserted held in the code path calling this routine.
James Smart4f774512009-05-22 14:52:35 -04001262 **/
1263static void
1264__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1265{
1266 struct lpfc_sglq *sglq;
1267 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001268 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001269 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001270
1271 if (iocbq->sli4_xritag == NO_XRI)
1272 sglq = NULL;
1273 else
James Smart6d368e52011-05-24 11:44:12 -04001274 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1275
James Smart0e9bb8d2013-03-01 16:35:12 -05001276
James Smart4f774512009-05-22 14:52:35 -04001277 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001278 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1279 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1280 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001281 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001282 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001283 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001284 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1285 spin_unlock_irqrestore(
1286 &phba->sli4_hba.sgl_list_lock, iflag);
1287 goto out;
1288 }
1289
James Smart895427b2017-02-12 13:52:30 -08001290 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001291 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1292 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001293 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1294 iflag);
James Smart341af102010-01-26 23:07:37 -05001295 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001296 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001297 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001298 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001299 } else {
James Smart895427b2017-02-12 13:52:30 -08001300 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1301 iflag);
James Smart4f774512009-05-22 14:52:35 -04001302 sglq->state = SGL_FREED;
1303 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001304 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001305 &phba->sli4_hba.lpfc_els_sgl_list);
1306 spin_unlock_irqrestore(
1307 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001308
1309 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001310 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001311 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001312 }
James Smart4f774512009-05-22 14:52:35 -04001313 }
1314
James Smartf358dd02017-02-12 13:52:34 -08001315out:
James Smart4f774512009-05-22 14:52:35 -04001316 /*
1317 * Clean all volatile data fields, preserve iotag and node struct.
1318 */
1319 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001320 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001321 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001322 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1323 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001324 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1325}
1326
James Smart2a9bf3d2010-06-07 15:24:45 -04001327
James Smart4f774512009-05-22 14:52:35 -04001328/**
James Smart3772a992009-05-22 14:50:54 -04001329 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1330 * @phba: Pointer to HBA context object.
1331 * @iocbq: Pointer to driver iocb object.
1332 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001333 * This function is called to release the driver iocb object to the
1334 * iocb pool. The iotag in the iocb object does not change for each
1335 * use of the iocb object. This function clears all other fields of
1336 * the iocb object when it is freed. The hbalock is asserted held in
1337 * the code path calling this routine.
James Smart3772a992009-05-22 14:50:54 -04001338 **/
1339static void
1340__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1341{
1342 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1343
James Smart0e9bb8d2013-03-01 16:35:12 -05001344 /*
James Smart3772a992009-05-22 14:50:54 -04001345 * Clean all volatile data fields, preserve iotag and node struct.
1346 */
1347 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1348 iocbq->sli4_xritag = NO_XRI;
1349 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1350}
1351
1352/**
James Smart3621a712009-04-06 18:47:14 -04001353 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001354 * @phba: Pointer to HBA context object.
1355 * @iocbq: Pointer to driver iocb object.
1356 *
1357 * This function is called with hbalock held to release driver
1358 * iocb object to the iocb pool. The iotag in the iocb object
1359 * does not change for each use of the iocb object. This function
1360 * clears all other fields of the iocb object when it is freed.
1361 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001362static void
James Smart2e0fef82007-06-17 19:56:36 -05001363__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1364{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001365 lockdep_assert_held(&phba->hbalock);
1366
James Smart3772a992009-05-22 14:50:54 -04001367 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001368 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001369}
1370
James Smarte59058c2008-08-24 21:49:00 -04001371/**
James Smart3621a712009-04-06 18:47:14 -04001372 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001373 * @phba: Pointer to HBA context object.
1374 * @iocbq: Pointer to driver iocb object.
1375 *
1376 * This function is called with no lock held to release the iocb to
1377 * iocb pool.
1378 **/
James Smart2e0fef82007-06-17 19:56:36 -05001379void
1380lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1381{
1382 unsigned long iflags;
1383
1384 /*
1385 * Clean all volatile data fields, preserve iotag and node struct.
1386 */
1387 spin_lock_irqsave(&phba->hbalock, iflags);
1388 __lpfc_sli_release_iocbq(phba, iocbq);
1389 spin_unlock_irqrestore(&phba->hbalock, iflags);
1390}
1391
James Smarte59058c2008-08-24 21:49:00 -04001392/**
James Smarta257bf92009-04-06 18:48:10 -04001393 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1394 * @phba: Pointer to HBA context object.
1395 * @iocblist: List of IOCBs.
1396 * @ulpstatus: ULP status in IOCB command field.
1397 * @ulpWord4: ULP word-4 in IOCB command field.
1398 *
1399 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1400 * on the list by invoking the complete callback function associated with the
1401 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1402 * fields.
1403 **/
1404void
1405lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1406 uint32_t ulpstatus, uint32_t ulpWord4)
1407{
1408 struct lpfc_iocbq *piocb;
1409
1410 while (!list_empty(iocblist)) {
1411 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smart84f2ddf2019-08-14 16:56:55 -07001412 if (!piocb->iocb_cmpl) {
1413 if (piocb->iocb_flag & LPFC_IO_NVME)
1414 lpfc_nvme_cancel_iocb(phba, piocb);
1415 else
1416 lpfc_sli_release_iocbq(phba, piocb);
1417 } else {
James Smarta257bf92009-04-06 18:48:10 -04001418 piocb->iocb.ulpStatus = ulpstatus;
1419 piocb->iocb.un.ulpWord[4] = ulpWord4;
1420 (piocb->iocb_cmpl) (phba, piocb, piocb);
1421 }
1422 }
1423 return;
1424}
1425
1426/**
James Smart3621a712009-04-06 18:47:14 -04001427 * lpfc_sli_iocb_cmd_type - Get the iocb type
1428 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001429 *
1430 * This function is called by ring event handler function to get the iocb type.
1431 * This function translates the iocb command to an iocb command type used to
1432 * decide the final disposition of each completed IOCB.
1433 * The function returns
1434 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1435 * LPFC_SOL_IOCB if it is a solicited iocb completion
1436 * LPFC_ABORT_IOCB if it is an abort iocb
1437 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1438 *
1439 * The caller is not required to hold any lock.
1440 **/
dea31012005-04-17 16:05:31 -05001441static lpfc_iocb_type
1442lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1443{
1444 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1445
1446 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1447 return 0;
1448
1449 switch (iocb_cmnd) {
1450 case CMD_XMIT_SEQUENCE_CR:
1451 case CMD_XMIT_SEQUENCE_CX:
1452 case CMD_XMIT_BCAST_CN:
1453 case CMD_XMIT_BCAST_CX:
1454 case CMD_ELS_REQUEST_CR:
1455 case CMD_ELS_REQUEST_CX:
1456 case CMD_CREATE_XRI_CR:
1457 case CMD_CREATE_XRI_CX:
1458 case CMD_GET_RPI_CN:
1459 case CMD_XMIT_ELS_RSP_CX:
1460 case CMD_GET_RPI_CR:
1461 case CMD_FCP_IWRITE_CR:
1462 case CMD_FCP_IWRITE_CX:
1463 case CMD_FCP_IREAD_CR:
1464 case CMD_FCP_IREAD_CX:
1465 case CMD_FCP_ICMND_CR:
1466 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001467 case CMD_FCP_TSEND_CX:
1468 case CMD_FCP_TRSP_CX:
1469 case CMD_FCP_TRECEIVE_CX:
1470 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001471 case CMD_ADAPTER_MSG:
1472 case CMD_ADAPTER_DUMP:
1473 case CMD_XMIT_SEQUENCE64_CR:
1474 case CMD_XMIT_SEQUENCE64_CX:
1475 case CMD_XMIT_BCAST64_CN:
1476 case CMD_XMIT_BCAST64_CX:
1477 case CMD_ELS_REQUEST64_CR:
1478 case CMD_ELS_REQUEST64_CX:
1479 case CMD_FCP_IWRITE64_CR:
1480 case CMD_FCP_IWRITE64_CX:
1481 case CMD_FCP_IREAD64_CR:
1482 case CMD_FCP_IREAD64_CX:
1483 case CMD_FCP_ICMND64_CR:
1484 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001485 case CMD_FCP_TSEND64_CX:
1486 case CMD_FCP_TRSP64_CX:
1487 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001488 case CMD_GEN_REQUEST64_CR:
1489 case CMD_GEN_REQUEST64_CX:
1490 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001491 case DSSCMD_IWRITE64_CR:
1492 case DSSCMD_IWRITE64_CX:
1493 case DSSCMD_IREAD64_CR:
1494 case DSSCMD_IREAD64_CX:
Dick Kennedyc93764a2020-06-30 14:49:49 -07001495 case CMD_SEND_FRAME:
dea31012005-04-17 16:05:31 -05001496 type = LPFC_SOL_IOCB;
1497 break;
1498 case CMD_ABORT_XRI_CN:
1499 case CMD_ABORT_XRI_CX:
1500 case CMD_CLOSE_XRI_CN:
1501 case CMD_CLOSE_XRI_CX:
1502 case CMD_XRI_ABORTED_CX:
1503 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001504 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001505 type = LPFC_ABORT_IOCB;
1506 break;
1507 case CMD_RCV_SEQUENCE_CX:
1508 case CMD_RCV_ELS_REQ_CX:
1509 case CMD_RCV_SEQUENCE64_CX:
1510 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001511 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001512 case CMD_IOCB_RCV_SEQ64_CX:
1513 case CMD_IOCB_RCV_ELS64_CX:
1514 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001515 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001516 type = LPFC_UNSOL_IOCB;
1517 break;
James Smart3163f722008-02-08 18:50:25 -05001518 case CMD_IOCB_XMIT_MSEQ64_CR:
1519 case CMD_IOCB_XMIT_MSEQ64_CX:
1520 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1521 case CMD_IOCB_RCV_ELS_LIST64_CX:
1522 case CMD_IOCB_CLOSE_EXTENDED_CN:
1523 case CMD_IOCB_ABORT_EXTENDED_CN:
1524 case CMD_IOCB_RET_HBQE64_CN:
1525 case CMD_IOCB_FCP_IBIDIR64_CR:
1526 case CMD_IOCB_FCP_IBIDIR64_CX:
1527 case CMD_IOCB_FCP_ITASKMGT64_CX:
1528 case CMD_IOCB_LOGENTRY_CN:
1529 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1530 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001531 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001532 type = LPFC_UNKNOWN_IOCB;
1533 break;
dea31012005-04-17 16:05:31 -05001534 default:
1535 type = LPFC_UNKNOWN_IOCB;
1536 break;
1537 }
1538
1539 return type;
1540}
1541
James Smarte59058c2008-08-24 21:49:00 -04001542/**
James Smart3621a712009-04-06 18:47:14 -04001543 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001544 * @phba: Pointer to HBA context object.
1545 *
1546 * This function is called from SLI initialization code
1547 * to configure every ring of the HBA's SLI interface. The
1548 * caller is not required to hold any lock. This function issues
1549 * a config_ring mailbox command for each ring.
1550 * This function returns zero if successful else returns a negative
1551 * error code.
1552 **/
dea31012005-04-17 16:05:31 -05001553static int
James Smarted957682007-06-17 19:56:37 -05001554lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001555{
1556 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001557 LPFC_MBOXQ_t *pmb;
1558 MAILBOX_t *pmbox;
1559 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001560
James Smarted957682007-06-17 19:56:37 -05001561 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1562 if (!pmb)
1563 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001564 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001565 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001566 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001567 lpfc_config_ring(phba, i, pmb);
1568 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1569 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001571 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001572 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1573 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001574 rc, pmbox->mbxCommand,
1575 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001576 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001577 ret = -ENXIO;
1578 break;
dea31012005-04-17 16:05:31 -05001579 }
1580 }
James Smarted957682007-06-17 19:56:37 -05001581 mempool_free(pmb, phba->mbox_mem_pool);
1582 return ret;
dea31012005-04-17 16:05:31 -05001583}
1584
James Smarte59058c2008-08-24 21:49:00 -04001585/**
James Smart3621a712009-04-06 18:47:14 -04001586 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001587 * @phba: Pointer to HBA context object.
1588 * @pring: Pointer to driver SLI ring object.
1589 * @piocb: Pointer to the driver iocb object.
1590 *
James Smarte2a8be52019-05-06 17:26:47 -07001591 * The driver calls this function with the hbalock held for SLI3 ports or
1592 * the ring lock held for SLI4 ports. The function adds the
James Smarte59058c2008-08-24 21:49:00 -04001593 * new iocb to txcmplq of the given ring. This function always returns
1594 * 0. If this function is called for ELS ring, this function checks if
1595 * there is a vport associated with the ELS command. This function also
1596 * starts els_tmofunc timer if this is an ELS command.
1597 **/
dea31012005-04-17 16:05:31 -05001598static int
James Smart2e0fef82007-06-17 19:56:36 -05001599lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1600 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001601{
James Smarte2a8be52019-05-06 17:26:47 -07001602 if (phba->sli_rev == LPFC_SLI_REV4)
1603 lockdep_assert_held(&pring->ring_lock);
1604 else
1605 lockdep_assert_held(&phba->hbalock);
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001606
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001607 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001608
dea31012005-04-17 16:05:31 -05001609 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001610 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08001611 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001612
James Smart92d7f7b2007-06-17 19:56:38 -05001613 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1614 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001615 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1616 BUG_ON(!piocb->vport);
1617 if (!(piocb->vport->load_flag & FC_UNLOADING))
1618 mod_timer(&piocb->vport->els_tmofunc,
1619 jiffies +
1620 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1621 }
dea31012005-04-17 16:05:31 -05001622
James Smart2e0fef82007-06-17 19:56:36 -05001623 return 0;
dea31012005-04-17 16:05:31 -05001624}
1625
James Smarte59058c2008-08-24 21:49:00 -04001626/**
James Smart3621a712009-04-06 18:47:14 -04001627 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001628 * @phba: Pointer to HBA context object.
1629 * @pring: Pointer to driver SLI ring object.
1630 *
1631 * This function is called with hbalock held to get next
1632 * iocb in txq of the given ring. If there is any iocb in
1633 * the txq, the function returns first iocb in the list after
1634 * removing the iocb from the list, else it returns NULL.
1635 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001636struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001637lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001638{
dea31012005-04-17 16:05:31 -05001639 struct lpfc_iocbq *cmd_iocb;
1640
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001641 lockdep_assert_held(&phba->hbalock);
1642
James Smart858c9f62007-06-17 19:56:39 -05001643 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001644 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001645}
1646
James Smarte59058c2008-08-24 21:49:00 -04001647/**
James Smart3621a712009-04-06 18:47:14 -04001648 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001649 * @phba: Pointer to HBA context object.
1650 * @pring: Pointer to driver SLI ring object.
1651 *
1652 * This function is called with hbalock held and the caller must post the
1653 * iocb without releasing the lock. If the caller releases the lock,
1654 * iocb slot returned by the function is not guaranteed to be available.
1655 * The function returns pointer to the next available iocb slot if there
1656 * is available slot in the ring, else it returns NULL.
1657 * If the get index of the ring is ahead of the put index, the function
1658 * will post an error attention event to the worker thread to take the
1659 * HBA to offline state.
1660 **/
dea31012005-04-17 16:05:31 -05001661static IOCB_t *
1662lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1663{
James Smart34b02dc2008-08-24 21:49:55 -04001664 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001665 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001666
1667 lockdep_assert_held(&phba->hbalock);
1668
James Smart7e56aa22012-08-03 12:35:34 -04001669 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1670 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1671 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001672
James Smart7e56aa22012-08-03 12:35:34 -04001673 if (unlikely(pring->sli.sli3.local_getidx ==
1674 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001675
James Smart7e56aa22012-08-03 12:35:34 -04001676 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001677
James Smart7e56aa22012-08-03 12:35:34 -04001678 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001680 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001681 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001682 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001683 pring->sli.sli3.local_getidx,
1684 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001685
James Smart2e0fef82007-06-17 19:56:36 -05001686 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001687 /*
1688 * All error attention handlers are posted to
1689 * worker thread
1690 */
1691 phba->work_ha |= HA_ERATT;
1692 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001693
James Smart5e9d9b82008-06-14 22:52:53 -04001694 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001695
1696 return NULL;
1697 }
1698
James Smart7e56aa22012-08-03 12:35:34 -04001699 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001700 return NULL;
1701 }
1702
James Smarted957682007-06-17 19:56:37 -05001703 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001704}
1705
James Smarte59058c2008-08-24 21:49:00 -04001706/**
James Smart3621a712009-04-06 18:47:14 -04001707 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001708 * @phba: Pointer to HBA context object.
1709 * @iocbq: Pointer to driver iocb object.
1710 *
1711 * This function gets an iotag for the iocb. If there is no unused iotag and
1712 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1713 * array and assigns a new iotag.
1714 * The function returns the allocated iotag if successful, else returns zero.
1715 * Zero is not a valid iotag.
1716 * The caller is not required to hold any lock.
1717 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001718uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001719lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001720{
James Smart2e0fef82007-06-17 19:56:36 -05001721 struct lpfc_iocbq **new_arr;
1722 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001723 size_t new_len;
1724 struct lpfc_sli *psli = &phba->sli;
1725 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001726
James Smart2e0fef82007-06-17 19:56:36 -05001727 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001728 iotag = psli->last_iotag;
1729 if(++iotag < psli->iocbq_lookup_len) {
1730 psli->last_iotag = iotag;
1731 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001732 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001733 iocbq->iotag = iotag;
1734 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001735 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001736 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1737 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001738 spin_unlock_irq(&phba->hbalock);
Kees Cook6396bb22018-06-12 14:03:40 -07001739 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001740 GFP_KERNEL);
1741 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001742 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001743 old_arr = psli->iocbq_lookup;
1744 if (new_len <= psli->iocbq_lookup_len) {
1745 /* highly unprobable case */
1746 kfree(new_arr);
1747 iotag = psli->last_iotag;
1748 if(++iotag < psli->iocbq_lookup_len) {
1749 psli->last_iotag = iotag;
1750 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001751 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001752 iocbq->iotag = iotag;
1753 return iotag;
1754 }
James Smart2e0fef82007-06-17 19:56:36 -05001755 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001756 return 0;
1757 }
1758 if (psli->iocbq_lookup)
1759 memcpy(new_arr, old_arr,
1760 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001761 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001762 psli->iocbq_lookup = new_arr;
1763 psli->iocbq_lookup_len = new_len;
1764 psli->last_iotag = iotag;
1765 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001766 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001767 iocbq->iotag = iotag;
1768 kfree(old_arr);
1769 return iotag;
1770 }
James Smart8f6d98d2006-08-01 07:34:00 -04001771 } else
James Smart2e0fef82007-06-17 19:56:36 -05001772 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001773
James Smartbc739052010-08-04 16:11:18 -04001774 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001775 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1776 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001777
James Bottomley604a3e32005-10-29 10:28:33 -05001778 return 0;
dea31012005-04-17 16:05:31 -05001779}
1780
James Smarte59058c2008-08-24 21:49:00 -04001781/**
James Smart3621a712009-04-06 18:47:14 -04001782 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001783 * @phba: Pointer to HBA context object.
1784 * @pring: Pointer to driver SLI ring object.
1785 * @iocb: Pointer to iocb slot in the ring.
1786 * @nextiocb: Pointer to driver iocb object which need to be
1787 * posted to firmware.
1788 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001789 * This function is called to post a new iocb to the firmware. This
1790 * function copies the new iocb to ring iocb slot and updates the
1791 * ring pointers. It adds the new iocb to txcmplq if there is
James Smarte59058c2008-08-24 21:49:00 -04001792 * a completion call back for this iocb else the function will free the
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001793 * iocb object. The hbalock is asserted held in the code path calling
1794 * this routine.
James Smarte59058c2008-08-24 21:49:00 -04001795 **/
dea31012005-04-17 16:05:31 -05001796static void
1797lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1798 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1799{
1800 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001801 * Set up an iotag
dea31012005-04-17 16:05:31 -05001802 */
James Bottomley604a3e32005-10-29 10:28:33 -05001803 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001804
James Smarte2a0a9d2008-12-04 22:40:02 -05001805
James Smarta58cbd52007-08-02 11:09:43 -04001806 if (pring->ringno == LPFC_ELS_RING) {
1807 lpfc_debugfs_slow_ring_trc(phba,
1808 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1809 *(((uint32_t *) &nextiocb->iocb) + 4),
1810 *(((uint32_t *) &nextiocb->iocb) + 6),
1811 *(((uint32_t *) &nextiocb->iocb) + 7));
1812 }
1813
dea31012005-04-17 16:05:31 -05001814 /*
1815 * Issue iocb command to adapter
1816 */
James Smart92d7f7b2007-06-17 19:56:38 -05001817 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001818 wmb();
1819 pring->stats.iocb_cmd++;
1820
1821 /*
1822 * If there is no completion routine to call, we can release the
1823 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1824 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1825 */
1826 if (nextiocb->iocb_cmpl)
1827 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001828 else
James Smart2e0fef82007-06-17 19:56:36 -05001829 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001830
1831 /*
1832 * Let the HBA know what IOCB slot will be the next one the
1833 * driver will put a command into.
1834 */
James Smart7e56aa22012-08-03 12:35:34 -04001835 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1836 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001837}
1838
James Smarte59058c2008-08-24 21:49:00 -04001839/**
James Smart3621a712009-04-06 18:47:14 -04001840 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001841 * @phba: Pointer to HBA context object.
1842 * @pring: Pointer to driver SLI ring object.
1843 *
1844 * The caller is not required to hold any lock for calling this function.
1845 * This function updates the chip attention bits for the ring to inform firmware
1846 * that there are pending work to be done for this ring and requests an
1847 * interrupt when there is space available in the ring. This function is
1848 * called when the driver is unable to post more iocbs to the ring due
1849 * to unavailability of space in the ring.
1850 **/
dea31012005-04-17 16:05:31 -05001851static void
James Smart2e0fef82007-06-17 19:56:36 -05001852lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001853{
1854 int ringno = pring->ringno;
1855
1856 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1857
1858 wmb();
1859
1860 /*
1861 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1862 * The HBA will tell us when an IOCB entry is available.
1863 */
1864 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1865 readl(phba->CAregaddr); /* flush */
1866
1867 pring->stats.iocb_cmd_full++;
1868}
1869
James Smarte59058c2008-08-24 21:49:00 -04001870/**
James Smart3621a712009-04-06 18:47:14 -04001871 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001872 * @phba: Pointer to HBA context object.
1873 * @pring: Pointer to driver SLI ring object.
1874 *
1875 * This function updates the chip attention register bit for the
1876 * given ring to inform HBA that there is more work to be done
1877 * in this ring. The caller is not required to hold any lock.
1878 **/
dea31012005-04-17 16:05:31 -05001879static void
James Smart2e0fef82007-06-17 19:56:36 -05001880lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001881{
1882 int ringno = pring->ringno;
1883
1884 /*
1885 * Tell the HBA that there is work to do in this ring.
1886 */
James Smart34b02dc2008-08-24 21:49:55 -04001887 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1888 wmb();
1889 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1890 readl(phba->CAregaddr); /* flush */
1891 }
dea31012005-04-17 16:05:31 -05001892}
1893
James Smarte59058c2008-08-24 21:49:00 -04001894/**
James Smart3621a712009-04-06 18:47:14 -04001895 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001896 * @phba: Pointer to HBA context object.
1897 * @pring: Pointer to driver SLI ring object.
1898 *
1899 * This function is called with hbalock held to post pending iocbs
1900 * in the txq to the firmware. This function is called when driver
1901 * detects space available in the ring.
1902 **/
dea31012005-04-17 16:05:31 -05001903static void
James Smart2e0fef82007-06-17 19:56:36 -05001904lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001905{
1906 IOCB_t *iocb;
1907 struct lpfc_iocbq *nextiocb;
1908
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001909 lockdep_assert_held(&phba->hbalock);
1910
dea31012005-04-17 16:05:31 -05001911 /*
1912 * Check to see if:
1913 * (a) there is anything on the txq to send
1914 * (b) link is up
1915 * (c) link attention events can be processed (fcp ring only)
1916 * (d) IOCB processing is not blocked by the outstanding mbox command.
1917 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001918
1919 if (lpfc_is_link_up(phba) &&
1920 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001921 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001922 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001923
1924 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1925 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1926 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1927
1928 if (iocb)
1929 lpfc_sli_update_ring(phba, pring);
1930 else
1931 lpfc_sli_update_full_ring(phba, pring);
1932 }
1933
1934 return;
1935}
1936
James Smarte59058c2008-08-24 21:49:00 -04001937/**
James Smart3621a712009-04-06 18:47:14 -04001938 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001939 * @phba: Pointer to HBA context object.
1940 * @hbqno: HBQ number.
1941 *
1942 * This function is called with hbalock held to get the next
1943 * available slot for the given HBQ. If there is free slot
1944 * available for the HBQ it will return pointer to the next available
1945 * HBQ entry else it will return NULL.
1946 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001947static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001948lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1949{
1950 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1951
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001952 lockdep_assert_held(&phba->hbalock);
1953
James Smarted957682007-06-17 19:56:37 -05001954 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1955 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1956 hbqp->next_hbqPutIdx = 0;
1957
1958 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001959 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001960 uint32_t getidx = le32_to_cpu(raw_index);
1961
1962 hbqp->local_hbqGetIdx = getidx;
1963
1964 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1965 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001966 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001967 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001968 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001969 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001970 hbqp->entry_count);
1971
1972 phba->link_state = LPFC_HBA_ERROR;
1973 return NULL;
1974 }
1975
1976 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1977 return NULL;
1978 }
1979
James Smart51ef4c22007-08-02 11:10:31 -04001980 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1981 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001982}
1983
James Smarte59058c2008-08-24 21:49:00 -04001984/**
James Smart3621a712009-04-06 18:47:14 -04001985 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001986 * @phba: Pointer to HBA context object.
1987 *
1988 * This function is called with no lock held to free all the
1989 * hbq buffers while uninitializing the SLI interface. It also
1990 * frees the HBQ buffers returned by the firmware but not yet
1991 * processed by the upper layers.
1992 **/
James Smarted957682007-06-17 19:56:37 -05001993void
1994lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1995{
James Smart92d7f7b2007-06-17 19:56:38 -05001996 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1997 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001998 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001999 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05002000
James Smart51ef4c22007-08-02 11:10:31 -04002001 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05002002 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05002003 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04002004 for (i = 0; i < hbq_count; ++i) {
2005 list_for_each_entry_safe(dmabuf, next_dmabuf,
2006 &phba->hbqs[i].hbq_buffer_list, list) {
2007 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
2008 list_del(&hbq_buf->dbuf.list);
2009 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2010 }
James Smarta8adb832007-10-27 13:37:53 -04002011 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05002012 }
James Smart3163f722008-02-08 18:50:25 -05002013
2014 /* Mark the HBQs not in use */
2015 phba->hbq_in_use = 0;
2016 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05002017}
2018
James Smarte59058c2008-08-24 21:49:00 -04002019/**
James Smart3621a712009-04-06 18:47:14 -04002020 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002021 * @phba: Pointer to HBA context object.
2022 * @hbqno: HBQ number.
2023 * @hbq_buf: Pointer to HBQ buffer.
2024 *
2025 * This function is called with the hbalock held to post a
2026 * hbq buffer to the firmware. If the function finds an empty
2027 * slot in the HBQ, it will post the buffer. The function will return
2028 * pointer to the hbq entry if it successfully post the buffer
2029 * else it will return NULL.
2030 **/
James Smart3772a992009-05-22 14:50:54 -04002031static int
James Smarted957682007-06-17 19:56:37 -05002032lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05002033 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05002034{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002035 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002036 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2037}
2038
2039/**
2040 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2041 * @phba: Pointer to HBA context object.
2042 * @hbqno: HBQ number.
2043 * @hbq_buf: Pointer to HBQ buffer.
2044 *
2045 * This function is called with the hbalock held to post a hbq buffer to the
2046 * firmware. If the function finds an empty slot in the HBQ, it will post the
2047 * buffer and place it on the hbq_buffer_list. The function will return zero if
2048 * it successfully post the buffer else it will return an error.
2049 **/
2050static int
2051lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2052 struct hbq_dmabuf *hbq_buf)
2053{
James Smarted957682007-06-17 19:56:37 -05002054 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002055 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002056
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002057 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002058 /* Get next HBQ entry slot to use */
2059 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2060 if (hbqe) {
2061 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2062
James Smart92d7f7b2007-06-17 19:56:38 -05002063 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2064 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002065 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002066 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002067 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2068 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2069 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002070 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2071 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002072 /* flush */
James Smarted957682007-06-17 19:56:37 -05002073 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002074 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002075 return 0;
2076 } else
2077 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002078}
2079
James Smart4f774512009-05-22 14:52:35 -04002080/**
2081 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2082 * @phba: Pointer to HBA context object.
2083 * @hbqno: HBQ number.
2084 * @hbq_buf: Pointer to HBQ buffer.
2085 *
2086 * This function is called with the hbalock held to post an RQE to the SLI4
2087 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2088 * the hbq_buffer_list and return zero, otherwise it will return an error.
2089 **/
2090static int
2091lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2092 struct hbq_dmabuf *hbq_buf)
2093{
2094 int rc;
2095 struct lpfc_rqe hrqe;
2096 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002097 struct lpfc_queue *hrq;
2098 struct lpfc_queue *drq;
2099
2100 if (hbqno != LPFC_ELS_HBQ)
2101 return 1;
2102 hrq = phba->sli4_hba.hdr_rq;
2103 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002104
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002105 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002106 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2107 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2108 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2109 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002110 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002111 if (rc < 0)
2112 return rc;
James Smart895427b2017-02-12 13:52:30 -08002113 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002114 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2115 return 0;
2116}
2117
James Smarte59058c2008-08-24 21:49:00 -04002118/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002119static struct lpfc_hbq_init lpfc_els_hbq = {
2120 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002121 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002122 .mask_count = 0,
2123 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002124 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002125 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002126 .init_count = 40,
2127 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002128};
James Smarted957682007-06-17 19:56:37 -05002129
James Smarte59058c2008-08-24 21:49:00 -04002130/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002131struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002132 &lpfc_els_hbq,
2133};
2134
James Smarte59058c2008-08-24 21:49:00 -04002135/**
James Smart3621a712009-04-06 18:47:14 -04002136 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002137 * @phba: Pointer to HBA context object.
2138 * @hbqno: HBQ number.
2139 * @count: Number of HBQ buffers to be posted.
2140 *
James Smartd7c255b2008-08-24 21:50:00 -04002141 * This function is called with no lock held to post more hbq buffers to the
2142 * given HBQ. The function returns the number of HBQ buffers successfully
2143 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002144 **/
James Smart311464e2007-08-02 11:10:37 -04002145static int
James Smart92d7f7b2007-06-17 19:56:38 -05002146lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2147{
James Smartd7c255b2008-08-24 21:50:00 -04002148 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002149 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002150 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002151 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002152 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002153 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002154
James Smartd7c255b2008-08-24 21:50:00 -04002155 if ((phba->hbqs[hbqno].buffer_count + count) >
2156 lpfc_hbq_defs[hbqno]->entry_count)
2157 count = lpfc_hbq_defs[hbqno]->entry_count -
2158 phba->hbqs[hbqno].buffer_count;
2159 if (!count)
2160 return 0;
2161 /* Allocate HBQ entries */
2162 for (i = 0; i < count; i++) {
2163 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2164 if (!hbq_buffer)
2165 break;
2166 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2167 }
James Smart3163f722008-02-08 18:50:25 -05002168 /* Check whether HBQ is still in use */
2169 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002170 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002171 goto err;
2172 while (!list_empty(&hbq_buf_list)) {
2173 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2174 dbuf.list);
2175 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2176 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002177 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002178 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002179 posted++;
2180 } else
James Smart51ef4c22007-08-02 11:10:31 -04002181 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002182 }
James Smart3163f722008-02-08 18:50:25 -05002183 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002184 return posted;
2185err:
2186 spin_unlock_irqrestore(&phba->hbalock, flags);
2187 while (!list_empty(&hbq_buf_list)) {
2188 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2189 dbuf.list);
2190 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2191 }
James Smart92d7f7b2007-06-17 19:56:38 -05002192 return 0;
James Smarted957682007-06-17 19:56:37 -05002193}
2194
James Smarte59058c2008-08-24 21:49:00 -04002195/**
James Smart3621a712009-04-06 18:47:14 -04002196 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002197 * @phba: Pointer to HBA context object.
2198 * @qno: HBQ number.
2199 *
2200 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002201 * is called with no lock held. The function returns the number of HBQ entries
2202 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002203 **/
James Smarted957682007-06-17 19:56:37 -05002204int
James Smart92d7f7b2007-06-17 19:56:38 -05002205lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002206{
James Smartdef9c7a2009-12-21 17:02:28 -05002207 if (phba->sli_rev == LPFC_SLI_REV4)
2208 return 0;
2209 else
2210 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2211 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002212}
2213
James Smarte59058c2008-08-24 21:49:00 -04002214/**
James Smart3621a712009-04-06 18:47:14 -04002215 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002216 * @phba: Pointer to HBA context object.
2217 * @qno: HBQ queue number.
2218 *
2219 * This function is called from SLI initialization code path with
2220 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002221 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002222 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002223static int
James Smart92d7f7b2007-06-17 19:56:38 -05002224lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002225{
James Smartdef9c7a2009-12-21 17:02:28 -05002226 if (phba->sli_rev == LPFC_SLI_REV4)
2227 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002228 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002229 else
2230 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2231 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002232}
2233
James Smarte59058c2008-08-24 21:49:00 -04002234/**
James Smart3772a992009-05-22 14:50:54 -04002235 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2236 * @phba: Pointer to HBA context object.
2237 * @hbqno: HBQ number.
2238 *
2239 * This function removes the first hbq buffer on an hbq list and returns a
2240 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2241 **/
2242static struct hbq_dmabuf *
2243lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2244{
2245 struct lpfc_dmabuf *d_buf;
2246
2247 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2248 if (!d_buf)
2249 return NULL;
2250 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2251}
2252
2253/**
James Smart2d7dbc42017-02-12 13:52:35 -08002254 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2255 * @phba: Pointer to HBA context object.
2256 * @hbqno: HBQ number.
2257 *
2258 * This function removes the first RQ buffer on an RQ buffer list and returns a
2259 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2260 **/
2261static struct rqb_dmabuf *
2262lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2263{
2264 struct lpfc_dmabuf *h_buf;
2265 struct lpfc_rqb *rqbp;
2266
2267 rqbp = hrq->rqbp;
2268 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2269 struct lpfc_dmabuf, list);
2270 if (!h_buf)
2271 return NULL;
2272 rqbp->buffer_count--;
2273 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2274}
2275
2276/**
James Smart3621a712009-04-06 18:47:14 -04002277 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002278 * @phba: Pointer to HBA context object.
2279 * @tag: Tag of the hbq buffer.
2280 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002281 * This function searches for the hbq buffer associated with the given tag in
2282 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2283 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002284 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002285static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002286lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2287{
James Smart92d7f7b2007-06-17 19:56:38 -05002288 struct lpfc_dmabuf *d_buf;
2289 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002290 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002291
James Smart51ef4c22007-08-02 11:10:31 -04002292 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002293 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002294 return NULL;
2295
James Smart3772a992009-05-22 14:50:54 -04002296 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002297 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002298 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002299 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002300 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002301 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002302 }
2303 }
James Smart3772a992009-05-22 14:50:54 -04002304 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002305 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002306 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002307 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002308 return NULL;
James Smarted957682007-06-17 19:56:37 -05002309}
2310
James Smarte59058c2008-08-24 21:49:00 -04002311/**
James Smart3621a712009-04-06 18:47:14 -04002312 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002313 * @phba: Pointer to HBA context object.
2314 * @hbq_buffer: Pointer to HBQ buffer.
2315 *
2316 * This function is called with hbalock. This function gives back
2317 * the hbq buffer to firmware. If the HBQ does not have space to
2318 * post the buffer, it will free the buffer.
2319 **/
James Smarted957682007-06-17 19:56:37 -05002320void
James Smart51ef4c22007-08-02 11:10:31 -04002321lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002322{
2323 uint32_t hbqno;
2324
James Smart51ef4c22007-08-02 11:10:31 -04002325 if (hbq_buffer) {
2326 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002327 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002328 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002329 }
2330}
2331
James Smarte59058c2008-08-24 21:49:00 -04002332/**
James Smart3621a712009-04-06 18:47:14 -04002333 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002334 * @mbxCommand: mailbox command code.
2335 *
2336 * This function is called by the mailbox event handler function to verify
2337 * that the completed mailbox command is a legitimate mailbox command. If the
2338 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2339 * and the mailbox event handler will take the HBA offline.
2340 **/
dea31012005-04-17 16:05:31 -05002341static int
2342lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2343{
2344 uint8_t ret;
2345
2346 switch (mbxCommand) {
2347 case MBX_LOAD_SM:
2348 case MBX_READ_NV:
2349 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002350 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002351 case MBX_RUN_BIU_DIAG:
2352 case MBX_INIT_LINK:
2353 case MBX_DOWN_LINK:
2354 case MBX_CONFIG_LINK:
2355 case MBX_CONFIG_RING:
2356 case MBX_RESET_RING:
2357 case MBX_READ_CONFIG:
2358 case MBX_READ_RCONFIG:
2359 case MBX_READ_SPARM:
2360 case MBX_READ_STATUS:
2361 case MBX_READ_RPI:
2362 case MBX_READ_XRI:
2363 case MBX_READ_REV:
2364 case MBX_READ_LNK_STAT:
2365 case MBX_REG_LOGIN:
2366 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002367 case MBX_CLEAR_LA:
2368 case MBX_DUMP_MEMORY:
2369 case MBX_DUMP_CONTEXT:
2370 case MBX_RUN_DIAGS:
2371 case MBX_RESTART:
2372 case MBX_UPDATE_CFG:
2373 case MBX_DOWN_LOAD:
2374 case MBX_DEL_LD_ENTRY:
2375 case MBX_RUN_PROGRAM:
2376 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002377 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002378 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002379 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002380 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002381 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002382 case MBX_LOAD_AREA:
2383 case MBX_RUN_BIU_DIAG64:
2384 case MBX_CONFIG_PORT:
2385 case MBX_READ_SPARM64:
2386 case MBX_READ_RPI64:
2387 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002388 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002389 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002390 case MBX_SET_DEBUG:
2391 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002392 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002393 case MBX_REG_VPI:
2394 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002395 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002396 case MBX_PORT_CAPABILITIES:
2397 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002398 case MBX_SLI4_CONFIG:
2399 case MBX_SLI4_REQ_FTRS:
2400 case MBX_REG_FCFI:
2401 case MBX_UNREG_FCFI:
2402 case MBX_REG_VFI:
2403 case MBX_UNREG_VFI:
2404 case MBX_INIT_VPI:
2405 case MBX_INIT_VFI:
2406 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002407 case MBX_READ_EVENT_LOG_STATUS:
2408 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002409 case MBX_SECURITY_MGMT:
2410 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002411 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002412 ret = mbxCommand;
2413 break;
2414 default:
2415 ret = MBX_SHUTDOWN;
2416 break;
2417 }
James Smart2e0fef82007-06-17 19:56:36 -05002418 return ret;
dea31012005-04-17 16:05:31 -05002419}
James Smarte59058c2008-08-24 21:49:00 -04002420
2421/**
James Smart3621a712009-04-06 18:47:14 -04002422 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002423 * @phba: Pointer to HBA context object.
2424 * @pmboxq: Pointer to mailbox command.
2425 *
2426 * This is completion handler function for mailbox commands issued from
2427 * lpfc_sli_issue_mbox_wait function. This function is called by the
2428 * mailbox event handler function with no lock held. This function
2429 * will wake up thread waiting on the wait queue pointed by context1
2430 * of the mailbox.
2431 **/
James Smart04c68492009-05-22 14:52:52 -04002432void
James Smart2e0fef82007-06-17 19:56:36 -05002433lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002434{
James Smart858c9f62007-06-17 19:56:39 -05002435 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002436 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002437
2438 /*
James Smarte29d74f2018-03-05 12:04:07 -08002439 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002440 * continued running.
2441 */
James Smart7054a602007-04-25 09:52:34 -04002442 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002443 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002444 pmbox_done = (struct completion *)pmboxq->context3;
2445 if (pmbox_done)
2446 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002447 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002448 return;
2449}
2450
James Smartb95b2112019-08-14 16:56:47 -07002451static void
2452__lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2453{
2454 unsigned long iflags;
2455
2456 if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
2457 lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
2458 spin_lock_irqsave(&vport->phba->ndlp_lock, iflags);
2459 ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
2460 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
2461 spin_unlock_irqrestore(&vport->phba->ndlp_lock, iflags);
2462 }
2463 ndlp->nlp_flag &= ~NLP_UNREG_INP;
2464}
James Smarte59058c2008-08-24 21:49:00 -04002465
2466/**
James Smart3621a712009-04-06 18:47:14 -04002467 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002468 * @phba: Pointer to HBA context object.
2469 * @pmb: Pointer to mailbox object.
2470 *
2471 * This function is the default mailbox completion handler. It
2472 * frees the memory resources associated with the completed mailbox
2473 * command. If the completed command is a REG_LOGIN mailbox command,
2474 * this function will issue a UREG_LOGIN to re-claim the RPI.
2475 **/
dea31012005-04-17 16:05:31 -05002476void
James Smart2e0fef82007-06-17 19:56:36 -05002477lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002478{
James Smartd439d282010-09-29 11:18:45 -04002479 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002480 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002481 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002482 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002483 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002484 int rc;
2485
James Smart3e1f0712018-11-29 16:09:29 -08002486 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smart7054a602007-04-25 09:52:34 -04002487
dea31012005-04-17 16:05:31 -05002488 if (mp) {
2489 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2490 kfree(mp);
2491 }
James Smart7054a602007-04-25 09:52:34 -04002492
2493 /*
2494 * If a REG_LOGIN succeeded after node is destroyed or node
2495 * is in re-discovery driver need to cleanup the RPI.
2496 */
James Smart2e0fef82007-06-17 19:56:36 -05002497 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002498 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2499 !pmb->u.mb.mbxStatus) {
2500 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002501 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart38503942020-03-22 11:12:53 -07002502 if (phba->sli_rev == LPFC_SLI_REV4)
2503 vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
James Smart04c68492009-05-22 14:52:52 -04002504 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002505 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002506 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002507 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2508 if (rc != MBX_NOT_FINISHED)
2509 return;
2510 }
2511
James Smart695a8142010-01-26 23:08:03 -05002512 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2513 !(phba->pport->load_flag & FC_UNLOADING) &&
2514 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002515 shost = lpfc_shost_from_vport(vport);
2516 spin_lock_irq(shost->host_lock);
2517 vport->vpi_state |= LPFC_VPI_REGISTERED;
2518 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2519 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002520 }
2521
James Smartd439d282010-09-29 11:18:45 -04002522 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -08002523 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -04002524 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002525 pmb->ctx_buf = NULL;
2526 pmb->ctx_ndlp = NULL;
2527 }
2528
2529 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2530 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2531
2532 /* Check to see if there are any deferred events to process */
2533 if (ndlp) {
2534 lpfc_printf_vlog(
2535 vport,
2536 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2537 "1438 UNREG cmpl deferred mbox x%x "
James Smart32350662019-08-14 16:57:06 -07002538 "on NPort x%x Data: x%x x%x %px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002539 ndlp->nlp_rpi, ndlp->nlp_DID,
2540 ndlp->nlp_flag, ndlp->nlp_defer_did, ndlp);
2541
2542 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2543 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
James Smart00292e02018-12-13 15:17:55 -08002544 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002545 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2546 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002547 } else {
James Smartb95b2112019-08-14 16:56:47 -07002548 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002549 }
James Smart97acd002019-09-21 20:58:51 -07002550 if (vport->load_flag & FC_UNLOADING)
2551 lpfc_nlp_put(ndlp);
James Smart9b164062019-03-12 16:30:06 -07002552 pmb->ctx_ndlp = NULL;
James Smartdea16bd2018-11-29 16:09:30 -08002553 }
James Smartd439d282010-09-29 11:18:45 -04002554 }
2555
James Smartdcf2a4e2010-09-29 11:18:53 -04002556 /* Check security permission status on INIT_LINK mailbox command */
2557 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2558 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2559 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2560 "2860 SLI authentication is required "
2561 "for INIT_LINK but has not done yet\n");
2562
James Smart04c68492009-05-22 14:52:52 -04002563 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2564 lpfc_sli4_mbox_cmd_free(phba, pmb);
2565 else
2566 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002567}
James Smartbe6bb942015-04-07 15:07:22 -04002568 /**
2569 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2570 * @phba: Pointer to HBA context object.
2571 * @pmb: Pointer to mailbox object.
2572 *
2573 * This function is the unreg rpi mailbox completion handler. It
2574 * frees the memory resources associated with the completed mailbox
2575 * command. An additional refrenece is put on the ndlp to prevent
2576 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2577 * the unreg mailbox command completes, this routine puts the
2578 * reference back.
2579 *
2580 **/
2581void
2582lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2583{
2584 struct lpfc_vport *vport = pmb->vport;
2585 struct lpfc_nodelist *ndlp;
2586
James Smart3e1f0712018-11-29 16:09:29 -08002587 ndlp = pmb->ctx_ndlp;
James Smartbe6bb942015-04-07 15:07:22 -04002588 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2589 if (phba->sli_rev == LPFC_SLI_REV4 &&
2590 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002591 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002592 LPFC_SLI_INTF_IF_TYPE_2)) {
2593 if (ndlp) {
James Smartdea16bd2018-11-29 16:09:30 -08002594 lpfc_printf_vlog(
2595 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2596 "0010 UNREG_LOGIN vpi:%x "
2597 "rpi:%x DID:%x defer x%x flg x%x "
James Smart32350662019-08-14 16:57:06 -07002598 "map:%x %px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002599 vport->vpi, ndlp->nlp_rpi,
2600 ndlp->nlp_DID, ndlp->nlp_defer_did,
2601 ndlp->nlp_flag,
2602 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002603 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002604 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002605
2606 /* Check to see if there are any deferred
2607 * events to process
2608 */
2609 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2610 (ndlp->nlp_defer_did !=
2611 NLP_EVT_NOTHING_PENDING)) {
2612 lpfc_printf_vlog(
2613 vport, KERN_INFO, LOG_DISCOVERY,
2614 "4111 UNREG cmpl deferred "
2615 "clr x%x on "
James Smart32350662019-08-14 16:57:06 -07002616 "NPort x%x Data: x%x x%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002617 ndlp->nlp_rpi, ndlp->nlp_DID,
2618 ndlp->nlp_defer_did, ndlp);
James Smart00292e02018-12-13 15:17:55 -08002619 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002620 ndlp->nlp_defer_did =
2621 NLP_EVT_NOTHING_PENDING;
2622 lpfc_issue_els_plogi(
2623 vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002624 } else {
James Smartb95b2112019-08-14 16:56:47 -07002625 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002626 }
James Smartbe6bb942015-04-07 15:07:22 -04002627 }
2628 }
2629 }
2630
2631 mempool_free(pmb, phba->mbox_mem_pool);
2632}
dea31012005-04-17 16:05:31 -05002633
James Smarte59058c2008-08-24 21:49:00 -04002634/**
James Smart3621a712009-04-06 18:47:14 -04002635 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002636 * @phba: Pointer to HBA context object.
2637 *
2638 * This function is called with no lock held. This function processes all
2639 * the completed mailbox commands and gives it to upper layers. The interrupt
2640 * service routine processes mailbox completion interrupt and adds completed
2641 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2642 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2643 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2644 * function returns the mailbox commands to the upper layer by calling the
2645 * completion handler function of each mailbox.
2646 **/
dea31012005-04-17 16:05:31 -05002647int
James Smart2e0fef82007-06-17 19:56:36 -05002648lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002649{
James Smart92d7f7b2007-06-17 19:56:38 -05002650 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002651 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002652 int rc;
2653 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002654
2655 phba->sli.slistat.mbox_event++;
2656
James Smart92d7f7b2007-06-17 19:56:38 -05002657 /* Get all completed mailboxe buffers into the cmplq */
2658 spin_lock_irq(&phba->hbalock);
2659 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2660 spin_unlock_irq(&phba->hbalock);
2661
dea31012005-04-17 16:05:31 -05002662 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002663 do {
2664 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2665 if (pmb == NULL)
2666 break;
2667
James Smart04c68492009-05-22 14:52:52 -04002668 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002669
James Smart858c9f62007-06-17 19:56:39 -05002670 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2671 if (pmb->vport) {
2672 lpfc_debugfs_disc_trc(pmb->vport,
2673 LPFC_DISC_TRC_MBOX_VPORT,
2674 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2675 (uint32_t)pmbox->mbxCommand,
2676 pmbox->un.varWords[0],
2677 pmbox->un.varWords[1]);
2678 }
2679 else {
2680 lpfc_debugfs_disc_trc(phba->pport,
2681 LPFC_DISC_TRC_MBOX,
2682 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2683 (uint32_t)pmbox->mbxCommand,
2684 pmbox->un.varWords[0],
2685 pmbox->un.varWords[1]);
2686 }
2687 }
2688
dea31012005-04-17 16:05:31 -05002689 /*
2690 * It is a fatal error if unknown mbox command completion.
2691 */
2692 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2693 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002694 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002695 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002696 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002697 "x%x (x%x/x%x) Cmpl\n",
James Smart43bfea12019-09-21 20:58:57 -07002698 pmb->vport ? pmb->vport->vpi :
2699 LPFC_VPORT_UNKNOWN,
James Smart04c68492009-05-22 14:52:52 -04002700 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002701 lpfc_sli_config_mbox_subsys_get(phba,
2702 pmb),
2703 lpfc_sli_config_mbox_opcode_get(phba,
2704 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002705 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002706 phba->work_hs = HS_FFER3;
2707 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002708 continue;
dea31012005-04-17 16:05:31 -05002709 }
2710
dea31012005-04-17 16:05:31 -05002711 if (pmbox->mbxStatus) {
2712 phba->sli.slistat.mbox_stat_err++;
2713 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2714 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002715 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002716 LOG_MBOX | LOG_SLI,
2717 "(%d):0305 Mbox cmd cmpl "
2718 "error - RETRYing Data: x%x "
2719 "(x%x/x%x) x%x x%x x%x\n",
James Smart43bfea12019-09-21 20:58:57 -07002720 pmb->vport ? pmb->vport->vpi :
2721 LPFC_VPORT_UNKNOWN,
James Smarta183a152011-10-10 21:32:43 -04002722 pmbox->mbxCommand,
2723 lpfc_sli_config_mbox_subsys_get(phba,
2724 pmb),
2725 lpfc_sli_config_mbox_opcode_get(phba,
2726 pmb),
2727 pmbox->mbxStatus,
2728 pmbox->un.varWords[0],
James Smart43bfea12019-09-21 20:58:57 -07002729 pmb->vport ? pmb->vport->port_state :
2730 LPFC_VPORT_UNKNOWN);
dea31012005-04-17 16:05:31 -05002731 pmbox->mbxStatus = 0;
2732 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002733 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002734 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002735 continue;
dea31012005-04-17 16:05:31 -05002736 }
2737 }
2738
2739 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002740 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
Sakari Ailus2d44d162019-09-04 19:04:23 +03002741 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
James Smarte74c03c2013-04-17 20:15:19 -04002742 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2743 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002744 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002745 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002746 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2747 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002748 pmb->mbox_cmpl,
2749 *((uint32_t *) pmbox),
2750 pmbox->un.varWords[0],
2751 pmbox->un.varWords[1],
2752 pmbox->un.varWords[2],
2753 pmbox->un.varWords[3],
2754 pmbox->un.varWords[4],
2755 pmbox->un.varWords[5],
2756 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002757 pmbox->un.varWords[7],
2758 pmbox->un.varWords[8],
2759 pmbox->un.varWords[9],
2760 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002761
James Smart92d7f7b2007-06-17 19:56:38 -05002762 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002763 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002764 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002765 return 0;
dea31012005-04-17 16:05:31 -05002766}
James Smart92d7f7b2007-06-17 19:56:38 -05002767
James Smarte59058c2008-08-24 21:49:00 -04002768/**
James Smart3621a712009-04-06 18:47:14 -04002769 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002770 * @phba: Pointer to HBA context object.
2771 * @pring: Pointer to driver SLI ring object.
2772 * @tag: buffer tag.
2773 *
2774 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2775 * is set in the tag the buffer is posted for a particular exchange,
2776 * the function will return the buffer without replacing the buffer.
2777 * If the buffer is for unsolicited ELS or CT traffic, this function
2778 * returns the buffer and also posts another buffer to the firmware.
2779 **/
James Smart76bb24e2007-10-27 13:38:00 -04002780static struct lpfc_dmabuf *
2781lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002782 struct lpfc_sli_ring *pring,
2783 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002784{
James Smart9f1e1b52008-12-04 22:39:40 -05002785 struct hbq_dmabuf *hbq_entry;
2786
James Smart76bb24e2007-10-27 13:38:00 -04002787 if (tag & QUE_BUFTAG_BIT)
2788 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002789 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2790 if (!hbq_entry)
2791 return NULL;
2792 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002793}
James Smart57127f12007-10-27 13:37:05 -04002794
James Smart3772a992009-05-22 14:50:54 -04002795/**
James Smart3a8070c2020-03-31 09:50:05 -07002796 * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
2797 * containing a NVME LS request.
2798 * @phba: pointer to lpfc hba data structure.
2799 * @piocb: pointer to the iocbq struct representing the sequence starting
2800 * frame.
2801 *
2802 * This routine initially validates the NVME LS, validates there is a login
2803 * with the port that sent the LS, and then calls the appropriate nvme host
2804 * or target LS request handler.
2805 **/
2806static void
2807lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
2808{
2809 struct lpfc_nodelist *ndlp;
2810 struct lpfc_dmabuf *d_buf;
2811 struct hbq_dmabuf *nvmebuf;
2812 struct fc_frame_header *fc_hdr;
2813 struct lpfc_async_xchg_ctx *axchg = NULL;
2814 char *failwhy = NULL;
2815 uint32_t oxid, sid, did, fctl, size;
James Smart4e57e0b2020-05-20 11:59:28 -07002816 int ret = 1;
James Smart3a8070c2020-03-31 09:50:05 -07002817
2818 d_buf = piocb->context2;
2819
2820 nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2821 fc_hdr = nvmebuf->hbuf.virt;
2822 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2823 sid = sli4_sid_from_fc_hdr(fc_hdr);
2824 did = sli4_did_from_fc_hdr(fc_hdr);
2825 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
2826 fc_hdr->fh_f_ctl[1] << 8 |
2827 fc_hdr->fh_f_ctl[2]);
2828 size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
2829
2830 lpfc_nvmeio_data(phba, "NVME LS RCV: xri x%x sz %d from %06x\n",
2831 oxid, size, sid);
2832
2833 if (phba->pport->load_flag & FC_UNLOADING) {
2834 failwhy = "Driver Unloading";
2835 } else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
2836 failwhy = "NVME FC4 Disabled";
2837 } else if (!phba->nvmet_support && !phba->pport->localport) {
2838 failwhy = "No Localport";
2839 } else if (phba->nvmet_support && !phba->targetport) {
2840 failwhy = "No Targetport";
2841 } else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
2842 failwhy = "Bad NVME LS R_CTL";
2843 } else if (unlikely((fctl & 0x00FF0000) !=
2844 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
2845 failwhy = "Bad NVME LS F_CTL";
2846 } else {
2847 axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);
2848 if (!axchg)
2849 failwhy = "No CTX memory";
2850 }
2851
2852 if (unlikely(failwhy)) {
2853 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC | LOG_NVME_IOERR,
2854 "6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
2855 sid, oxid, failwhy);
2856 goto out_fail;
2857 }
2858
2859 /* validate the source of the LS is logged in */
2860 ndlp = lpfc_findnode_did(phba->pport, sid);
2861 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2862 ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2863 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2864 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2865 "6216 NVME Unsol rcv: No ndlp: "
2866 "NPort_ID x%x oxid x%x\n",
2867 sid, oxid);
2868 goto out_fail;
2869 }
2870
2871 axchg->phba = phba;
2872 axchg->ndlp = ndlp;
2873 axchg->size = size;
2874 axchg->oxid = oxid;
2875 axchg->sid = sid;
2876 axchg->wqeq = NULL;
2877 axchg->state = LPFC_NVME_STE_LS_RCV;
2878 axchg->entry_cnt = 1;
2879 axchg->rqb_buffer = (void *)nvmebuf;
2880 axchg->hdwq = &phba->sli4_hba.hdwq[0];
2881 axchg->payload = nvmebuf->dbuf.virt;
2882 INIT_LIST_HEAD(&axchg->list);
2883
2884 if (phba->nvmet_support)
2885 ret = lpfc_nvmet_handle_lsreq(phba, axchg);
2886 else
2887 ret = lpfc_nvme_handle_lsreq(phba, axchg);
2888
2889 /* if zero, LS was successfully handled. If non-zero, LS not handled */
2890 if (!ret)
2891 return;
2892
2893 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC | LOG_NVME_IOERR,
2894 "6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
2895 "NVMe%s handler failed %d\n",
2896 did, sid, oxid,
2897 (phba->nvmet_support) ? "T" : "I", ret);
2898
2899out_fail:
James Smart3a8070c2020-03-31 09:50:05 -07002900
2901 /* recycle receive buffer */
2902 lpfc_in_buf_free(phba, &nvmebuf->dbuf);
2903
2904 /* If start of new exchange, abort it */
James Smart4e57e0b2020-05-20 11:59:28 -07002905 if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
2906 ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
2907
2908 if (ret)
2909 kfree(axchg);
James Smart3a8070c2020-03-31 09:50:05 -07002910}
2911
2912/**
James Smart3772a992009-05-22 14:50:54 -04002913 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2914 * @phba: Pointer to HBA context object.
2915 * @pring: Pointer to driver SLI ring object.
2916 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2917 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2918 * @fch_type: the type for the first frame of the sequence.
2919 *
2920 * This function is called with no lock held. This function uses the r_ctl and
2921 * type of the received sequence to find the correct callback function to call
2922 * to process the sequence.
2923 **/
2924static int
2925lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2926 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2927 uint32_t fch_type)
2928{
2929 int i;
2930
James Smartf358dd02017-02-12 13:52:34 -08002931 switch (fch_type) {
2932 case FC_TYPE_NVME:
James Smart3a8070c2020-03-31 09:50:05 -07002933 lpfc_nvme_unsol_ls_handler(phba, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002934 return 1;
2935 default:
2936 break;
2937 }
2938
James Smart3772a992009-05-22 14:50:54 -04002939 /* unSolicited Responses */
2940 if (pring->prt[0].profile) {
2941 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2942 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2943 saveq);
2944 return 1;
2945 }
2946 /* We must search, based on rctl / type
2947 for the right routine */
2948 for (i = 0; i < pring->num_mask; i++) {
2949 if ((pring->prt[i].rctl == fch_r_ctl) &&
2950 (pring->prt[i].type == fch_type)) {
2951 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2952 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2953 (phba, pring, saveq);
2954 return 1;
2955 }
2956 }
2957 return 0;
2958}
James Smarte59058c2008-08-24 21:49:00 -04002959
2960/**
James Smart3621a712009-04-06 18:47:14 -04002961 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002962 * @phba: Pointer to HBA context object.
2963 * @pring: Pointer to driver SLI ring object.
2964 * @saveq: Pointer to the unsolicited iocb.
2965 *
2966 * This function is called with no lock held by the ring event handler
2967 * when there is an unsolicited iocb posted to the response ring by the
2968 * firmware. This function gets the buffer associated with the iocbs
2969 * and calls the event handler for the ring. This function handles both
2970 * qring buffers and hbq buffers.
2971 * When the function returns 1 the caller can free the iocb object otherwise
2972 * upper layer functions will free the iocb objects.
2973 **/
dea31012005-04-17 16:05:31 -05002974static int
2975lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2976 struct lpfc_iocbq *saveq)
2977{
2978 IOCB_t * irsp;
2979 WORD5 * w5p;
2980 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002981 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002982 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002983
dea31012005-04-17 16:05:31 -05002984 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002985
2986 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2987 if (pring->lpfc_sli_rcv_async_status)
2988 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2989 else
2990 lpfc_printf_log(phba,
2991 KERN_WARNING,
2992 LOG_SLI,
2993 "0316 Ring %d handler: unexpected "
2994 "ASYNC_STATUS iocb received evt_code "
2995 "0x%x\n",
2996 pring->ringno,
2997 irsp->un.asyncstat.evt_code);
2998 return 1;
2999 }
3000
James Smart3163f722008-02-08 18:50:25 -05003001 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
3002 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
3003 if (irsp->ulpBdeCount > 0) {
3004 dmzbuf = lpfc_sli_get_buff(phba, pring,
3005 irsp->un.ulpWord[3]);
3006 lpfc_in_buf_free(phba, dmzbuf);
3007 }
3008
3009 if (irsp->ulpBdeCount > 1) {
3010 dmzbuf = lpfc_sli_get_buff(phba, pring,
3011 irsp->unsli3.sli3Words[3]);
3012 lpfc_in_buf_free(phba, dmzbuf);
3013 }
3014
3015 if (irsp->ulpBdeCount > 2) {
3016 dmzbuf = lpfc_sli_get_buff(phba, pring,
3017 irsp->unsli3.sli3Words[7]);
3018 lpfc_in_buf_free(phba, dmzbuf);
3019 }
3020
3021 return 1;
3022 }
3023
James Smart92d7f7b2007-06-17 19:56:38 -05003024 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04003025 if (irsp->ulpBdeCount != 0) {
3026 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05003027 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04003028 if (!saveq->context2)
3029 lpfc_printf_log(phba,
3030 KERN_ERR,
3031 LOG_SLI,
3032 "0341 Ring %d Cannot find buffer for "
3033 "an unsolicited iocb. tag 0x%x\n",
3034 pring->ringno,
3035 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04003036 }
3037 if (irsp->ulpBdeCount == 2) {
3038 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04003039 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04003040 if (!saveq->context3)
3041 lpfc_printf_log(phba,
3042 KERN_ERR,
3043 LOG_SLI,
3044 "0342 Ring %d Cannot find buffer for an"
3045 " unsolicited iocb. tag 0x%x\n",
3046 pring->ringno,
3047 irsp->unsli3.sli3Words[7]);
3048 }
3049 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04003050 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04003051 if (irsp->ulpBdeCount != 0) {
3052 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
3053 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05003054 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04003055 lpfc_printf_log(phba,
3056 KERN_ERR,
3057 LOG_SLI,
3058 "0343 Ring %d Cannot find "
3059 "buffer for an unsolicited iocb"
3060 ". tag 0x%x\n", pring->ringno,
3061 irsp->un.ulpWord[3]);
3062 }
3063 if (irsp->ulpBdeCount == 2) {
3064 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
3065 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05003066 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04003067 lpfc_printf_log(phba,
3068 KERN_ERR,
3069 LOG_SLI,
3070 "0344 Ring %d Cannot find "
3071 "buffer for an unsolicited "
3072 "iocb. tag 0x%x\n",
3073 pring->ringno,
3074 irsp->unsli3.sli3Words[7]);
3075 }
3076 }
James Smart92d7f7b2007-06-17 19:56:38 -05003077 }
James Smart9c2face2008-01-11 01:53:18 -05003078 if (irsp->ulpBdeCount != 0 &&
3079 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
3080 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
3081 int found = 0;
3082
3083 /* search continue save q for same XRI */
3084 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04003085 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
3086 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05003087 list_add_tail(&saveq->list, &iocbq->list);
3088 found = 1;
3089 break;
3090 }
3091 }
3092 if (!found)
3093 list_add_tail(&saveq->clist,
3094 &pring->iocb_continue_saveq);
3095 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
3096 list_del_init(&iocbq->clist);
3097 saveq = iocbq;
3098 irsp = &(saveq->iocb);
3099 } else
3100 return 0;
3101 }
3102 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
3103 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
3104 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04003105 Rctl = FC_RCTL_ELS_REQ;
3106 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05003107 } else {
3108 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
3109 Rctl = w5p->hcsw.Rctl;
3110 Type = w5p->hcsw.Type;
3111
3112 /* Firmware Workaround */
3113 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
3114 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
3115 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04003116 Rctl = FC_RCTL_ELS_REQ;
3117 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05003118 w5p->hcsw.Rctl = Rctl;
3119 w5p->hcsw.Type = Type;
3120 }
3121 }
James Smart92d7f7b2007-06-17 19:56:38 -05003122
James Smart3772a992009-05-22 14:50:54 -04003123 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05003124 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003125 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003126 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04003127 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04003128
James Smart92d7f7b2007-06-17 19:56:38 -05003129 return 1;
dea31012005-04-17 16:05:31 -05003130}
3131
James Smarte59058c2008-08-24 21:49:00 -04003132/**
James Smart3621a712009-04-06 18:47:14 -04003133 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04003134 * @phba: Pointer to HBA context object.
3135 * @pring: Pointer to driver SLI ring object.
3136 * @prspiocb: Pointer to response iocb object.
3137 *
3138 * This function looks up the iocb_lookup table to get the command iocb
3139 * corresponding to the given response iocb using the iotag of the
James Smarte2a8be52019-05-06 17:26:47 -07003140 * response iocb. The driver calls this function with the hbalock held
3141 * for SLI3 ports or the ring lock held for SLI4 ports.
James Smarte59058c2008-08-24 21:49:00 -04003142 * This function returns the command iocb object if it finds the command
3143 * iocb else returns NULL.
3144 **/
dea31012005-04-17 16:05:31 -05003145static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05003146lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3147 struct lpfc_sli_ring *pring,
3148 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05003149{
dea31012005-04-17 16:05:31 -05003150 struct lpfc_iocbq *cmd_iocb = NULL;
3151 uint16_t iotag;
James Smarte2a8be52019-05-06 17:26:47 -07003152 spinlock_t *temp_lock = NULL;
3153 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05003154
James Smarte2a8be52019-05-06 17:26:47 -07003155 if (phba->sli_rev == LPFC_SLI_REV4)
3156 temp_lock = &pring->ring_lock;
3157 else
3158 temp_lock = &phba->hbalock;
3159
3160 spin_lock_irqsave(temp_lock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003161 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05003162
James Bottomley604a3e32005-10-29 10:28:33 -05003163 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3164 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003165 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07003166 /* remove from txcmpl queue list */
3167 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04003168 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003169 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003170 spin_unlock_irqrestore(temp_lock, iflag);
James Smart89533e92016-10-13 15:06:15 -07003171 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003172 }
dea31012005-04-17 16:05:31 -05003173 }
3174
James Smarte2a8be52019-05-06 17:26:47 -07003175 spin_unlock_irqrestore(temp_lock, iflag);
dea31012005-04-17 16:05:31 -05003176 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07003177 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05003178 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003179 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05003180 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05003181 return NULL;
3182}
3183
James Smarte59058c2008-08-24 21:49:00 -04003184/**
James Smart3772a992009-05-22 14:50:54 -04003185 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3186 * @phba: Pointer to HBA context object.
3187 * @pring: Pointer to driver SLI ring object.
3188 * @iotag: IOCB tag.
3189 *
3190 * This function looks up the iocb_lookup table to get the command iocb
James Smarte2a8be52019-05-06 17:26:47 -07003191 * corresponding to the given iotag. The driver calls this function with
3192 * the ring lock held because this function is an SLI4 port only helper.
James Smart3772a992009-05-22 14:50:54 -04003193 * This function returns the command iocb object if it finds the command
3194 * iocb else returns NULL.
3195 **/
3196static struct lpfc_iocbq *
3197lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3198 struct lpfc_sli_ring *pring, uint16_t iotag)
3199{
James Smart895427b2017-02-12 13:52:30 -08003200 struct lpfc_iocbq *cmd_iocb = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07003201 spinlock_t *temp_lock = NULL;
3202 unsigned long iflag = 0;
James Smart3772a992009-05-22 14:50:54 -04003203
James Smarte2a8be52019-05-06 17:26:47 -07003204 if (phba->sli_rev == LPFC_SLI_REV4)
3205 temp_lock = &pring->ring_lock;
3206 else
3207 temp_lock = &phba->hbalock;
3208
3209 spin_lock_irqsave(temp_lock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003210 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3211 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003212 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3213 /* remove from txcmpl queue list */
3214 list_del_init(&cmd_iocb->list);
3215 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003216 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003217 spin_unlock_irqrestore(temp_lock, iflag);
James Smart4f2e66c2012-05-09 21:17:07 -04003218 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003219 }
James Smart3772a992009-05-22 14:50:54 -04003220 }
James Smart89533e92016-10-13 15:06:15 -07003221
James Smarte2a8be52019-05-06 17:26:47 -07003222 spin_unlock_irqrestore(temp_lock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003223 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08003224 "0372 iotag x%x lookup error: max iotag (x%x) "
3225 "iocb_flag x%x\n",
3226 iotag, phba->sli.last_iotag,
3227 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003228 return NULL;
3229}
3230
3231/**
James Smart3621a712009-04-06 18:47:14 -04003232 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003233 * @phba: Pointer to HBA context object.
3234 * @pring: Pointer to driver SLI ring object.
3235 * @saveq: Pointer to the response iocb to be processed.
3236 *
3237 * This function is called by the ring event handler for non-fcp
3238 * rings when there is a new response iocb in the response ring.
3239 * The caller is not required to hold any locks. This function
3240 * gets the command iocb associated with the response iocb and
3241 * calls the completion handler for the command iocb. If there
3242 * is no completion handler, the function will free the resources
3243 * associated with command iocb. If the response iocb is for
3244 * an already aborted command iocb, the status of the completion
3245 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3246 * This function always returns 1.
3247 **/
dea31012005-04-17 16:05:31 -05003248static int
James Smart2e0fef82007-06-17 19:56:36 -05003249lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003250 struct lpfc_iocbq *saveq)
3251{
James Smart2e0fef82007-06-17 19:56:36 -05003252 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003253 int rc = 1;
3254 unsigned long iflag;
3255
James Bottomley604a3e32005-10-29 10:28:33 -05003256 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
dea31012005-04-17 16:05:31 -05003257 if (cmdiocbp) {
3258 if (cmdiocbp->iocb_cmpl) {
3259 /*
James Smartea2151b2008-09-07 11:52:10 -04003260 * If an ELS command failed send an event to mgmt
3261 * application.
3262 */
3263 if (saveq->iocb.ulpStatus &&
3264 (pring->ringno == LPFC_ELS_RING) &&
3265 (cmdiocbp->iocb.ulpCommand ==
3266 CMD_ELS_REQUEST64_CR))
3267 lpfc_send_els_failure_event(phba,
3268 cmdiocbp, saveq);
3269
3270 /*
dea31012005-04-17 16:05:31 -05003271 * Post all ELS completions to the worker thread.
3272 * All other are passed to the completion callback.
3273 */
3274 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003275 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3276 (cmdiocbp->iocb_flag &
3277 LPFC_DRIVER_ABORTED)) {
3278 spin_lock_irqsave(&phba->hbalock,
3279 iflag);
James Smart07951072007-04-25 09:51:38 -04003280 cmdiocbp->iocb_flag &=
3281 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003282 spin_unlock_irqrestore(&phba->hbalock,
3283 iflag);
James Smart07951072007-04-25 09:51:38 -04003284 saveq->iocb.ulpStatus =
3285 IOSTAT_LOCAL_REJECT;
3286 saveq->iocb.un.ulpWord[4] =
3287 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003288
3289 /* Firmware could still be in progress
3290 * of DMAing payload, so don't free data
3291 * buffer till after a hbeat.
3292 */
James Smart341af102010-01-26 23:07:37 -05003293 spin_lock_irqsave(&phba->hbalock,
3294 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003295 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003296 spin_unlock_irqrestore(&phba->hbalock,
3297 iflag);
3298 }
James Smart0f65ff62010-02-26 14:14:23 -05003299 if (phba->sli_rev == LPFC_SLI_REV4) {
3300 if (saveq->iocb_flag &
3301 LPFC_EXCHANGE_BUSY) {
3302 /* Set cmdiocb flag for the
3303 * exchange busy so sgl (xri)
3304 * will not be released until
3305 * the abort xri is received
3306 * from hba.
3307 */
3308 spin_lock_irqsave(
3309 &phba->hbalock, iflag);
3310 cmdiocbp->iocb_flag |=
3311 LPFC_EXCHANGE_BUSY;
3312 spin_unlock_irqrestore(
3313 &phba->hbalock, iflag);
3314 }
3315 if (cmdiocbp->iocb_flag &
3316 LPFC_DRIVER_ABORTED) {
3317 /*
3318 * Clear LPFC_DRIVER_ABORTED
3319 * bit in case it was driver
3320 * initiated abort.
3321 */
3322 spin_lock_irqsave(
3323 &phba->hbalock, iflag);
3324 cmdiocbp->iocb_flag &=
3325 ~LPFC_DRIVER_ABORTED;
3326 spin_unlock_irqrestore(
3327 &phba->hbalock, iflag);
3328 cmdiocbp->iocb.ulpStatus =
3329 IOSTAT_LOCAL_REJECT;
3330 cmdiocbp->iocb.un.ulpWord[4] =
3331 IOERR_ABORT_REQUESTED;
3332 /*
3333 * For SLI4, irsiocb contains
3334 * NO_XRI in sli_xritag, it
3335 * shall not affect releasing
3336 * sgl (xri) process.
3337 */
3338 saveq->iocb.ulpStatus =
3339 IOSTAT_LOCAL_REJECT;
3340 saveq->iocb.un.ulpWord[4] =
3341 IOERR_SLI_ABORTED;
3342 spin_lock_irqsave(
3343 &phba->hbalock, iflag);
3344 saveq->iocb_flag |=
3345 LPFC_DELAY_MEM_FREE;
3346 spin_unlock_irqrestore(
3347 &phba->hbalock, iflag);
3348 }
James Smart07951072007-04-25 09:51:38 -04003349 }
dea31012005-04-17 16:05:31 -05003350 }
James Smart2e0fef82007-06-17 19:56:36 -05003351 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003352 } else
3353 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003354 } else {
3355 /*
3356 * Unknown initiating command based on the response iotag.
3357 * This could be the case on the ELS ring because of
3358 * lpfc_els_abort().
3359 */
3360 if (pring->ringno != LPFC_ELS_RING) {
3361 /*
3362 * Ring <ringno> handler: unexpected completion IoTag
3363 * <IoTag>
3364 */
James Smarta257bf92009-04-06 18:48:10 -04003365 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003366 "0322 Ring %d handler: "
3367 "unexpected completion IoTag x%x "
3368 "Data: x%x x%x x%x x%x\n",
3369 pring->ringno,
3370 saveq->iocb.ulpIoTag,
3371 saveq->iocb.ulpStatus,
3372 saveq->iocb.un.ulpWord[4],
3373 saveq->iocb.ulpCommand,
3374 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003375 }
3376 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003377
dea31012005-04-17 16:05:31 -05003378 return rc;
3379}
3380
James Smarte59058c2008-08-24 21:49:00 -04003381/**
James Smart3621a712009-04-06 18:47:14 -04003382 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003383 * @phba: Pointer to HBA context object.
3384 * @pring: Pointer to driver SLI ring object.
3385 *
3386 * This function is called from the iocb ring event handlers when
3387 * put pointer is ahead of the get pointer for a ring. This function signal
3388 * an error attention condition to the worker thread and the worker
3389 * thread will transition the HBA to offline state.
3390 **/
James Smart2e0fef82007-06-17 19:56:36 -05003391static void
3392lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003393{
James Smart34b02dc2008-08-24 21:49:55 -04003394 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003395 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003396 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003397 * rsp ring <portRspMax>
3398 */
3399 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003400 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003401 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003402 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003403 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003404
James Smart2e0fef82007-06-17 19:56:36 -05003405 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003406
3407 /*
3408 * All error attention handlers are posted to
3409 * worker thread
3410 */
3411 phba->work_ha |= HA_ERATT;
3412 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003413
James Smart5e9d9b82008-06-14 22:52:53 -04003414 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003415
3416 return;
3417}
3418
James Smarte59058c2008-08-24 21:49:00 -04003419/**
James Smart3621a712009-04-06 18:47:14 -04003420 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003421 * @ptr: Pointer to address of HBA context object.
3422 *
3423 * This function is invoked by the Error Attention polling timer when the
3424 * timer times out. It will check the SLI Error Attention register for
3425 * possible attention events. If so, it will post an Error Attention event
3426 * and wake up worker thread to process it. Otherwise, it will set up the
3427 * Error Attention polling timer for the next poll.
3428 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003429void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003430{
3431 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003432 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003433 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003434
Kees Cookf22eb4d2017-09-06 20:24:26 -07003435 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003436
James Smartaa6fbb72012-08-03 12:36:03 -04003437 /* Here we will also keep track of interrupts per sec of the hba */
3438 sli_intr = phba->sli.slistat.sli_intr;
3439
3440 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3441 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3442 sli_intr);
3443 else
3444 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3445
James Smart65791f12016-07-06 12:35:56 -07003446 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3447 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003448 phba->sli.slistat.sli_ips = cnt;
3449
3450 phba->sli.slistat.sli_prev_intr = sli_intr;
3451
James Smart93996272008-08-24 21:50:30 -04003452 /* Check chip HA register for error event */
3453 eratt = lpfc_sli_check_eratt(phba);
3454
3455 if (eratt)
3456 /* Tell the worker thread there is work to do */
3457 lpfc_worker_wake_up(phba);
3458 else
3459 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003460 mod_timer(&phba->eratt_poll,
3461 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003462 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003463 return;
3464}
3465
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003466
James Smarte59058c2008-08-24 21:49:00 -04003467/**
James Smart3621a712009-04-06 18:47:14 -04003468 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003469 * @phba: Pointer to HBA context object.
3470 * @pring: Pointer to driver SLI ring object.
3471 * @mask: Host attention register mask for this ring.
3472 *
3473 * This function is called from the interrupt context when there is a ring
3474 * event for the fcp ring. The caller does not hold any lock.
3475 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003476 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003477 * LE bit set. The function will call the completion handler of the command iocb
3478 * if the response iocb indicates a completion for a command iocb or it is
3479 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3480 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003481 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003482 * to check it explicitly.
3483 */
3484int
James Smart2e0fef82007-06-17 19:56:36 -05003485lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3486 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003487{
James Smart34b02dc2008-08-24 21:49:55 -04003488 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003489 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003490 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003491 struct lpfc_iocbq *cmdiocbq = NULL;
3492 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003493 uint32_t status;
3494 uint32_t portRspPut, portRspMax;
3495 int rc = 1;
3496 lpfc_iocb_type type;
3497 unsigned long iflag;
3498 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003499
James Smart2e0fef82007-06-17 19:56:36 -05003500 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003501 pring->stats.iocb_event++;
3502
dea31012005-04-17 16:05:31 -05003503 /*
3504 * The next available response entry should never exceed the maximum
3505 * entries. If it does, treat it as an adapter hardware error.
3506 */
James Smart7e56aa22012-08-03 12:35:34 -04003507 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003508 portRspPut = le32_to_cpu(pgp->rspPutInx);
3509 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003510 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003511 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003512 return 1;
3513 }
James Smart45ed1192009-10-02 15:17:02 -04003514 if (phba->fcp_ring_in_use) {
3515 spin_unlock_irqrestore(&phba->hbalock, iflag);
3516 return 1;
3517 } else
3518 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003519
3520 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003521 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003522 /*
3523 * Fetch an entry off the ring and copy it into a local data
3524 * structure. The copy involves a byte-swap since the
3525 * network byte order and pci byte orders are different.
3526 */
James Smarted957682007-06-17 19:56:37 -05003527 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003528 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003529
James Smart7e56aa22012-08-03 12:35:34 -04003530 if (++pring->sli.sli3.rspidx >= portRspMax)
3531 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003532
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003533 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3534 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003535 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003536 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003537 irsp = &rspiocbq.iocb;
3538
dea31012005-04-17 16:05:31 -05003539 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3540 pring->stats.iocb_rsp++;
3541 rsp_cmpl++;
3542
3543 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003544 /*
3545 * If resource errors reported from HBA, reduce
3546 * queuedepths of the SCSI device.
3547 */
3548 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003549 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3550 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003551 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003552 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003553 spin_lock_irqsave(&phba->hbalock, iflag);
3554 }
3555
dea31012005-04-17 16:05:31 -05003556 /* Rsp ring <ringno> error: IOCB */
3557 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003558 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003559 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003560 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003561 irsp->un.ulpWord[0],
3562 irsp->un.ulpWord[1],
3563 irsp->un.ulpWord[2],
3564 irsp->un.ulpWord[3],
3565 irsp->un.ulpWord[4],
3566 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003567 *(uint32_t *)&irsp->un1,
3568 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003569 }
3570
3571 switch (type) {
3572 case LPFC_ABORT_IOCB:
3573 case LPFC_SOL_IOCB:
3574 /*
3575 * Idle exchange closed via ABTS from port. No iocb
3576 * resources need to be recovered.
3577 */
3578 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003579 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003580 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003581 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003582 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003583 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003584 break;
3585 }
3586
James Smarte2a8be52019-05-06 17:26:47 -07003587 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003588 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3589 &rspiocbq);
James Smarte2a8be52019-05-06 17:26:47 -07003590 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05003591 if (unlikely(!cmdiocbq))
3592 break;
3593 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3594 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3595 if (cmdiocbq->iocb_cmpl) {
3596 spin_unlock_irqrestore(&phba->hbalock, iflag);
3597 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3598 &rspiocbq);
3599 spin_lock_irqsave(&phba->hbalock, iflag);
3600 }
dea31012005-04-17 16:05:31 -05003601 break;
James Smarta4bc3372006-12-02 13:34:16 -05003602 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003603 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003604 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003605 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003606 break;
dea31012005-04-17 16:05:31 -05003607 default:
3608 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3609 char adaptermsg[LPFC_MAX_ADPTMSG];
3610 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3611 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3612 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003613 dev_warn(&((phba->pcidev)->dev),
3614 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003615 phba->brd_no, adaptermsg);
3616 } else {
3617 /* Unknown IOCB command */
3618 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003619 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003620 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003621 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003622 irsp->ulpStatus,
3623 irsp->ulpIoTag,
3624 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003625 }
3626 break;
3627 }
3628
3629 /*
3630 * The response IOCB has been processed. Update the ring
3631 * pointer in SLIM. If the port response put pointer has not
3632 * been updated, sync the pgp->rspPutInx and fetch the new port
3633 * response put pointer.
3634 */
James Smart7e56aa22012-08-03 12:35:34 -04003635 writel(pring->sli.sli3.rspidx,
3636 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003637
James Smart7e56aa22012-08-03 12:35:34 -04003638 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003639 portRspPut = le32_to_cpu(pgp->rspPutInx);
3640 }
3641
3642 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3643 pring->stats.iocb_rsp_full++;
3644 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3645 writel(status, phba->CAregaddr);
3646 readl(phba->CAregaddr);
3647 }
3648 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3649 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3650 pring->stats.iocb_cmd_empty++;
3651
3652 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003653 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003654 lpfc_sli_resume_iocb(phba, pring);
3655
3656 if ((pring->lpfc_sli_cmd_available))
3657 (pring->lpfc_sli_cmd_available) (phba, pring);
3658
3659 }
3660
James Smart45ed1192009-10-02 15:17:02 -04003661 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003662 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003663 return rc;
3664}
3665
James Smarte59058c2008-08-24 21:49:00 -04003666/**
James Smart3772a992009-05-22 14:50:54 -04003667 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3668 * @phba: Pointer to HBA context object.
3669 * @pring: Pointer to driver SLI ring object.
3670 * @rspiocbp: Pointer to driver response IOCB object.
3671 *
3672 * This function is called from the worker thread when there is a slow-path
3673 * response IOCB to process. This function chains all the response iocbs until
3674 * seeing the iocb with the LE bit set. The function will call
3675 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3676 * completion of a command iocb. The function will call the
3677 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3678 * The function frees the resources or calls the completion handler if this
3679 * iocb is an abort completion. The function returns NULL when the response
3680 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3681 * this function shall chain the iocb on to the iocb_continueq and return the
3682 * response iocb passed in.
3683 **/
3684static struct lpfc_iocbq *
3685lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3686 struct lpfc_iocbq *rspiocbp)
3687{
3688 struct lpfc_iocbq *saveq;
3689 struct lpfc_iocbq *cmdiocbp;
3690 struct lpfc_iocbq *next_iocb;
3691 IOCB_t *irsp = NULL;
3692 uint32_t free_saveq;
3693 uint8_t iocb_cmd_type;
3694 lpfc_iocb_type type;
3695 unsigned long iflag;
3696 int rc;
3697
3698 spin_lock_irqsave(&phba->hbalock, iflag);
3699 /* First add the response iocb to the countinueq list */
3700 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3701 pring->iocb_continueq_cnt++;
3702
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003703 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003704 irsp = &rspiocbp->iocb;
3705 if (irsp->ulpLe) {
3706 /*
3707 * By default, the driver expects to free all resources
3708 * associated with this iocb completion.
3709 */
3710 free_saveq = 1;
3711 saveq = list_get_first(&pring->iocb_continueq,
3712 struct lpfc_iocbq, list);
3713 irsp = &(saveq->iocb);
3714 list_del_init(&pring->iocb_continueq);
3715 pring->iocb_continueq_cnt = 0;
3716
3717 pring->stats.iocb_rsp++;
3718
3719 /*
3720 * If resource errors reported from HBA, reduce
3721 * queuedepths of the SCSI device.
3722 */
3723 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003724 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3725 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003726 spin_unlock_irqrestore(&phba->hbalock, iflag);
3727 phba->lpfc_rampdown_queue_depth(phba);
3728 spin_lock_irqsave(&phba->hbalock, iflag);
3729 }
3730
3731 if (irsp->ulpStatus) {
3732 /* Rsp ring <ringno> error: IOCB */
3733 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3734 "0328 Rsp Ring %d error: "
3735 "IOCB Data: "
3736 "x%x x%x x%x x%x "
3737 "x%x x%x x%x x%x "
3738 "x%x x%x x%x x%x "
3739 "x%x x%x x%x x%x\n",
3740 pring->ringno,
3741 irsp->un.ulpWord[0],
3742 irsp->un.ulpWord[1],
3743 irsp->un.ulpWord[2],
3744 irsp->un.ulpWord[3],
3745 irsp->un.ulpWord[4],
3746 irsp->un.ulpWord[5],
3747 *(((uint32_t *) irsp) + 6),
3748 *(((uint32_t *) irsp) + 7),
3749 *(((uint32_t *) irsp) + 8),
3750 *(((uint32_t *) irsp) + 9),
3751 *(((uint32_t *) irsp) + 10),
3752 *(((uint32_t *) irsp) + 11),
3753 *(((uint32_t *) irsp) + 12),
3754 *(((uint32_t *) irsp) + 13),
3755 *(((uint32_t *) irsp) + 14),
3756 *(((uint32_t *) irsp) + 15));
3757 }
3758
3759 /*
3760 * Fetch the IOCB command type and call the correct completion
3761 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3762 * get freed back to the lpfc_iocb_list by the discovery
3763 * kernel thread.
3764 */
3765 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3766 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3767 switch (type) {
3768 case LPFC_SOL_IOCB:
3769 spin_unlock_irqrestore(&phba->hbalock, iflag);
3770 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3771 spin_lock_irqsave(&phba->hbalock, iflag);
3772 break;
3773
3774 case LPFC_UNSOL_IOCB:
3775 spin_unlock_irqrestore(&phba->hbalock, iflag);
3776 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3777 spin_lock_irqsave(&phba->hbalock, iflag);
3778 if (!rc)
3779 free_saveq = 0;
3780 break;
3781
3782 case LPFC_ABORT_IOCB:
3783 cmdiocbp = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07003784 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX) {
3785 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003786 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3787 saveq);
James Smarte2a8be52019-05-06 17:26:47 -07003788 spin_lock_irqsave(&phba->hbalock, iflag);
3789 }
James Smart3772a992009-05-22 14:50:54 -04003790 if (cmdiocbp) {
3791 /* Call the specified completion routine */
3792 if (cmdiocbp->iocb_cmpl) {
3793 spin_unlock_irqrestore(&phba->hbalock,
3794 iflag);
3795 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3796 saveq);
3797 spin_lock_irqsave(&phba->hbalock,
3798 iflag);
3799 } else
3800 __lpfc_sli_release_iocbq(phba,
3801 cmdiocbp);
3802 }
3803 break;
3804
3805 case LPFC_UNKNOWN_IOCB:
3806 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3807 char adaptermsg[LPFC_MAX_ADPTMSG];
3808 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3809 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3810 MAX_MSG_DATA);
3811 dev_warn(&((phba->pcidev)->dev),
3812 "lpfc%d: %s\n",
3813 phba->brd_no, adaptermsg);
3814 } else {
3815 /* Unknown IOCB command */
3816 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3817 "0335 Unknown IOCB "
3818 "command Data: x%x "
3819 "x%x x%x x%x\n",
3820 irsp->ulpCommand,
3821 irsp->ulpStatus,
3822 irsp->ulpIoTag,
3823 irsp->ulpContext);
3824 }
3825 break;
3826 }
3827
3828 if (free_saveq) {
3829 list_for_each_entry_safe(rspiocbp, next_iocb,
3830 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003831 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003832 __lpfc_sli_release_iocbq(phba, rspiocbp);
3833 }
3834 __lpfc_sli_release_iocbq(phba, saveq);
3835 }
3836 rspiocbp = NULL;
3837 }
3838 spin_unlock_irqrestore(&phba->hbalock, iflag);
3839 return rspiocbp;
3840}
3841
3842/**
3843 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003844 * @phba: Pointer to HBA context object.
3845 * @pring: Pointer to driver SLI ring object.
3846 * @mask: Host attention register mask for this ring.
3847 *
James Smart3772a992009-05-22 14:50:54 -04003848 * This routine wraps the actual slow_ring event process routine from the
3849 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003850 **/
James Smart3772a992009-05-22 14:50:54 -04003851void
James Smart2e0fef82007-06-17 19:56:36 -05003852lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3853 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003854{
James Smart3772a992009-05-22 14:50:54 -04003855 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3856}
3857
3858/**
3859 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3860 * @phba: Pointer to HBA context object.
3861 * @pring: Pointer to driver SLI ring object.
3862 * @mask: Host attention register mask for this ring.
3863 *
3864 * This function is called from the worker thread when there is a ring event
3865 * for non-fcp rings. The caller does not hold any lock. The function will
3866 * remove each response iocb in the response ring and calls the handle
3867 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3868 **/
3869static void
3870lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3871 struct lpfc_sli_ring *pring, uint32_t mask)
3872{
James Smart34b02dc2008-08-24 21:49:55 -04003873 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003874 IOCB_t *entry;
3875 IOCB_t *irsp = NULL;
3876 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003877 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003878 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003879 uint32_t status;
dea31012005-04-17 16:05:31 -05003880
James Smart34b02dc2008-08-24 21:49:55 -04003881 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003882 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003883 pring->stats.iocb_event++;
3884
dea31012005-04-17 16:05:31 -05003885 /*
3886 * The next available response entry should never exceed the maximum
3887 * entries. If it does, treat it as an adapter hardware error.
3888 */
James Smart7e56aa22012-08-03 12:35:34 -04003889 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003890 portRspPut = le32_to_cpu(pgp->rspPutInx);
3891 if (portRspPut >= portRspMax) {
3892 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003893 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003894 * rsp ring <portRspMax>
3895 */
James Smarted957682007-06-17 19:56:37 -05003896 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003897 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003898 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003899 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003900
James Smart2e0fef82007-06-17 19:56:36 -05003901 phba->link_state = LPFC_HBA_ERROR;
3902 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003903
3904 phba->work_hs = HS_FFER3;
3905 lpfc_handle_eratt(phba);
3906
James Smart3772a992009-05-22 14:50:54 -04003907 return;
dea31012005-04-17 16:05:31 -05003908 }
3909
3910 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003911 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003912 /*
3913 * Build a completion list and call the appropriate handler.
3914 * The process is to get the next available response iocb, get
3915 * a free iocb from the list, copy the response data into the
3916 * free iocb, insert to the continuation list, and update the
3917 * next response index to slim. This process makes response
3918 * iocb's in the ring available to DMA as fast as possible but
3919 * pays a penalty for a copy operation. Since the iocb is
3920 * only 32 bytes, this penalty is considered small relative to
3921 * the PCI reads for register values and a slim write. When
3922 * the ulpLe field is set, the entire Command has been
3923 * received.
3924 */
James Smarted957682007-06-17 19:56:37 -05003925 entry = lpfc_resp_iocb(phba, pring);
3926
James Smart858c9f62007-06-17 19:56:39 -05003927 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003928 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003929 if (rspiocbp == NULL) {
3930 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003931 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003932 break;
3933 }
3934
James Smarted957682007-06-17 19:56:37 -05003935 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3936 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003937 irsp = &rspiocbp->iocb;
3938
James Smart7e56aa22012-08-03 12:35:34 -04003939 if (++pring->sli.sli3.rspidx >= portRspMax)
3940 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003941
James Smarta58cbd52007-08-02 11:09:43 -04003942 if (pring->ringno == LPFC_ELS_RING) {
3943 lpfc_debugfs_slow_ring_trc(phba,
3944 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3945 *(((uint32_t *) irsp) + 4),
3946 *(((uint32_t *) irsp) + 6),
3947 *(((uint32_t *) irsp) + 7));
3948 }
3949
James Smart7e56aa22012-08-03 12:35:34 -04003950 writel(pring->sli.sli3.rspidx,
3951 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003952
James Smart3772a992009-05-22 14:50:54 -04003953 spin_unlock_irqrestore(&phba->hbalock, iflag);
3954 /* Handle the response IOCB */
3955 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3956 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003957
3958 /*
3959 * If the port response put pointer has not been updated, sync
3960 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3961 * response put pointer.
3962 */
James Smart7e56aa22012-08-03 12:35:34 -04003963 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003964 portRspPut = le32_to_cpu(pgp->rspPutInx);
3965 }
James Smart7e56aa22012-08-03 12:35:34 -04003966 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003967
James Smart92d7f7b2007-06-17 19:56:38 -05003968 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003969 /* At least one response entry has been freed */
3970 pring->stats.iocb_rsp_full++;
3971 /* SET RxRE_RSP in Chip Att register */
3972 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3973 writel(status, phba->CAregaddr);
3974 readl(phba->CAregaddr); /* flush */
3975 }
3976 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3977 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3978 pring->stats.iocb_cmd_empty++;
3979
3980 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003981 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003982 lpfc_sli_resume_iocb(phba, pring);
3983
3984 if ((pring->lpfc_sli_cmd_available))
3985 (pring->lpfc_sli_cmd_available) (phba, pring);
3986
3987 }
3988
James Smart2e0fef82007-06-17 19:56:36 -05003989 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003990 return;
dea31012005-04-17 16:05:31 -05003991}
3992
James Smarte59058c2008-08-24 21:49:00 -04003993/**
James Smart4f774512009-05-22 14:52:35 -04003994 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3995 * @phba: Pointer to HBA context object.
3996 * @pring: Pointer to driver SLI ring object.
3997 * @mask: Host attention register mask for this ring.
3998 *
3999 * This function is called from the worker thread when there is a pending
4000 * ELS response iocb on the driver internal slow-path response iocb worker
4001 * queue. The caller does not hold any lock. The function will remove each
4002 * response iocb from the response worker queue and calls the handle
4003 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4004 **/
4005static void
4006lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4007 struct lpfc_sli_ring *pring, uint32_t mask)
4008{
4009 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04004010 struct hbq_dmabuf *dmabuf;
4011 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04004012 unsigned long iflag;
James Smart0ef01a22018-09-10 10:30:45 -07004013 int count = 0;
James Smart4f774512009-05-22 14:52:35 -04004014
James Smart45ed1192009-10-02 15:17:02 -04004015 spin_lock_irqsave(&phba->hbalock, iflag);
4016 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
4017 spin_unlock_irqrestore(&phba->hbalock, iflag);
4018 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04004019 /* Get the response iocb from the head of work queue */
4020 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04004021 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04004022 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04004023 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04004024
4025 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4026 case CQE_CODE_COMPL_WQE:
4027 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4028 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04004029 /* Translate ELS WCQE to response IOCBQ */
4030 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
4031 irspiocbq);
4032 if (irspiocbq)
4033 lpfc_sli_sp_handle_rspiocb(phba, pring,
4034 irspiocbq);
James Smart0ef01a22018-09-10 10:30:45 -07004035 count++;
James Smart4d9ab992009-10-02 15:16:39 -04004036 break;
4037 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04004038 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04004039 dmabuf = container_of(cq_event, struct hbq_dmabuf,
4040 cq_event);
4041 lpfc_sli4_handle_received_buffer(phba, dmabuf);
James Smart0ef01a22018-09-10 10:30:45 -07004042 count++;
James Smart4d9ab992009-10-02 15:16:39 -04004043 break;
4044 default:
4045 break;
4046 }
James Smart0ef01a22018-09-10 10:30:45 -07004047
4048 /* Limit the number of events to 64 to avoid soft lockups */
4049 if (count == 64)
4050 break;
James Smart4f774512009-05-22 14:52:35 -04004051 }
4052}
4053
4054/**
James Smart3621a712009-04-06 18:47:14 -04004055 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04004056 * @phba: Pointer to HBA context object.
4057 * @pring: Pointer to driver SLI ring object.
4058 *
4059 * This function aborts all iocbs in the given ring and frees all the iocb
4060 * objects in txq. This function issues an abort iocb for all the iocb commands
4061 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4062 * the return of this function. The caller is not required to hold any locks.
4063 **/
James Smart2e0fef82007-06-17 19:56:36 -05004064void
dea31012005-04-17 16:05:31 -05004065lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4066{
James Smart2534ba72007-04-25 09:52:20 -04004067 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004068 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05004069
James Smart92d7f7b2007-06-17 19:56:38 -05004070 if (pring->ringno == LPFC_ELS_RING) {
4071 lpfc_fabric_abort_hba(phba);
4072 }
4073
dea31012005-04-17 16:05:31 -05004074 /* Error everything on txq and txcmplq
4075 * First do the txq.
4076 */
James Smartdb55fba2014-04-04 13:52:02 -04004077 if (phba->sli_rev >= LPFC_SLI_REV4) {
4078 spin_lock_irq(&pring->ring_lock);
4079 list_splice_init(&pring->txq, &completions);
4080 pring->txq_cnt = 0;
4081 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05004082
James Smartdb55fba2014-04-04 13:52:02 -04004083 spin_lock_irq(&phba->hbalock);
4084 /* Next issue ABTS for everything on the txcmplq */
4085 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
4086 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
4087 spin_unlock_irq(&phba->hbalock);
4088 } else {
4089 spin_lock_irq(&phba->hbalock);
4090 list_splice_init(&pring->txq, &completions);
4091 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04004092
James Smartdb55fba2014-04-04 13:52:02 -04004093 /* Next issue ABTS for everything on the txcmplq */
4094 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
4095 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
4096 spin_unlock_irq(&phba->hbalock);
4097 }
James Smart2534ba72007-04-25 09:52:20 -04004098
James Smarta257bf92009-04-06 18:48:10 -04004099 /* Cancel all the IOCBs from the completions list */
4100 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4101 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004102}
4103
James Smarte59058c2008-08-24 21:49:00 -04004104/**
James Smartdb55fba2014-04-04 13:52:02 -04004105 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4106 * @phba: Pointer to HBA context object.
4107 * @pring: Pointer to driver SLI ring object.
4108 *
4109 * This function aborts all iocbs in FCP rings and frees all the iocb
4110 * objects in txq. This function issues an abort iocb for all the iocb commands
4111 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4112 * the return of this function. The caller is not required to hold any locks.
4113 **/
4114void
4115lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4116{
4117 struct lpfc_sli *psli = &phba->sli;
4118 struct lpfc_sli_ring *pring;
4119 uint32_t i;
4120
4121 /* Look on all the FCP Rings for the iotag */
4122 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004123 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07004124 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smartdb55fba2014-04-04 13:52:02 -04004125 lpfc_sli_abort_iocb_ring(phba, pring);
4126 }
4127 } else {
James Smart895427b2017-02-12 13:52:30 -08004128 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004129 lpfc_sli_abort_iocb_ring(phba, pring);
4130 }
4131}
4132
James Smart895427b2017-02-12 13:52:30 -08004133/**
James Smartc00f62e2019-08-14 16:57:11 -07004134 * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
James Smarta8e497d2008-08-24 21:50:11 -04004135 * @phba: Pointer to HBA context object.
4136 *
James Smartc00f62e2019-08-14 16:57:11 -07004137 * This function flushes all iocbs in the IO ring and frees all the iocb
James Smarta8e497d2008-08-24 21:50:11 -04004138 * objects in txq and txcmplq. This function will not issue abort iocbs
4139 * for all the iocb commands in txcmplq, they will just be returned with
4140 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4141 * slot has been permanently disabled.
4142 **/
4143void
James Smartc00f62e2019-08-14 16:57:11 -07004144lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
James Smarta8e497d2008-08-24 21:50:11 -04004145{
4146 LIST_HEAD(txq);
4147 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04004148 struct lpfc_sli *psli = &phba->sli;
4149 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04004150 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004151 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04004152
4153 spin_lock_irq(&phba->hbalock);
James Smart4cd70892020-03-22 11:12:57 -07004154 if (phba->hba_flag & HBA_IOQ_FLUSH ||
4155 !phba->sli4_hba.hdwq) {
4156 spin_unlock_irq(&phba->hbalock);
4157 return;
4158 }
James Smart4f2e66c2012-05-09 21:17:07 -04004159 /* Indicate the I/O queues are flushed */
James Smartc00f62e2019-08-14 16:57:11 -07004160 phba->hba_flag |= HBA_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04004161 spin_unlock_irq(&phba->hbalock);
4162
James Smartdb55fba2014-04-04 13:52:02 -04004163 /* Look on all the FCP Rings for the iotag */
4164 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004165 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07004166 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smarta8e497d2008-08-24 21:50:11 -04004167
James Smartdb55fba2014-04-04 13:52:02 -04004168 spin_lock_irq(&pring->ring_lock);
4169 /* Retrieve everything on txq */
4170 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004171 list_for_each_entry_safe(piocb, next_iocb,
4172 &pring->txcmplq, list)
4173 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004174 /* Retrieve everything on the txcmplq */
4175 list_splice_init(&pring->txcmplq, &txcmplq);
4176 pring->txq_cnt = 0;
4177 pring->txcmplq_cnt = 0;
4178 spin_unlock_irq(&pring->ring_lock);
4179
4180 /* Flush the txq */
4181 lpfc_sli_cancel_iocbs(phba, &txq,
4182 IOSTAT_LOCAL_REJECT,
4183 IOERR_SLI_DOWN);
4184 /* Flush the txcmpq */
4185 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4186 IOSTAT_LOCAL_REJECT,
4187 IOERR_SLI_DOWN);
4188 }
4189 } else {
James Smart895427b2017-02-12 13:52:30 -08004190 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004191
4192 spin_lock_irq(&phba->hbalock);
4193 /* Retrieve everything on txq */
4194 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004195 list_for_each_entry_safe(piocb, next_iocb,
4196 &pring->txcmplq, list)
4197 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004198 /* Retrieve everything on the txcmplq */
4199 list_splice_init(&pring->txcmplq, &txcmplq);
4200 pring->txq_cnt = 0;
4201 pring->txcmplq_cnt = 0;
4202 spin_unlock_irq(&phba->hbalock);
4203
4204 /* Flush the txq */
4205 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4206 IOERR_SLI_DOWN);
4207 /* Flush the txcmpq */
4208 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4209 IOERR_SLI_DOWN);
4210 }
James Smarta8e497d2008-08-24 21:50:11 -04004211}
4212
4213/**
James Smart3772a992009-05-22 14:50:54 -04004214 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004215 * @phba: Pointer to HBA context object.
4216 * @mask: Bit mask to be checked.
4217 *
4218 * This function reads the host status register and compares
4219 * with the provided bit mask to check if HBA completed
4220 * the restart. This function will wait in a loop for the
4221 * HBA to complete restart. If the HBA does not restart within
4222 * 15 iterations, the function will reset the HBA again. The
4223 * function returns 1 when HBA fail to restart otherwise returns
4224 * zero.
4225 **/
James Smart3772a992009-05-22 14:50:54 -04004226static int
4227lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004228{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004229 uint32_t status;
4230 int i = 0;
4231 int retval = 0;
dea31012005-04-17 16:05:31 -05004232
Jamie Wellnitz41415862006-02-28 19:25:27 -05004233 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004234 if (lpfc_readl(phba->HSregaddr, &status))
4235 return 1;
dea31012005-04-17 16:05:31 -05004236
Jamie Wellnitz41415862006-02-28 19:25:27 -05004237 /*
4238 * Check status register every 100ms for 5 retries, then every
4239 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4240 * every 2.5 sec for 4.
4241 * Break our of the loop if errors occurred during init.
4242 */
4243 while (((status & mask) != mask) &&
4244 !(status & HS_FFERM) &&
4245 i++ < 20) {
dea31012005-04-17 16:05:31 -05004246
Jamie Wellnitz41415862006-02-28 19:25:27 -05004247 if (i <= 5)
4248 msleep(10);
4249 else if (i <= 10)
4250 msleep(500);
4251 else
4252 msleep(2500);
dea31012005-04-17 16:05:31 -05004253
Jamie Wellnitz41415862006-02-28 19:25:27 -05004254 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004255 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004256 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004257 lpfc_sli_brdrestart(phba);
4258 }
4259 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004260 if (lpfc_readl(phba->HSregaddr, &status)) {
4261 retval = 1;
4262 break;
4263 }
dea31012005-04-17 16:05:31 -05004264 }
dea31012005-04-17 16:05:31 -05004265
Jamie Wellnitz41415862006-02-28 19:25:27 -05004266 /* Check to see if any errors occurred during init */
4267 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4269 "2751 Adapter failed to restart, "
4270 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4271 status,
4272 readl(phba->MBslimaddr + 0xa8),
4273 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004274 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004275 retval = 1;
4276 }
dea31012005-04-17 16:05:31 -05004277
Jamie Wellnitz41415862006-02-28 19:25:27 -05004278 return retval;
dea31012005-04-17 16:05:31 -05004279}
4280
James Smartda0436e2009-05-22 14:51:39 -04004281/**
4282 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4283 * @phba: Pointer to HBA context object.
4284 * @mask: Bit mask to be checked.
4285 *
4286 * This function checks the host status register to check if HBA is
4287 * ready. This function will wait in a loop for the HBA to be ready
4288 * If the HBA is not ready , the function will will reset the HBA PCI
4289 * function again. The function returns 1 when HBA fail to be ready
4290 * otherwise returns zero.
4291 **/
4292static int
4293lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4294{
4295 uint32_t status;
4296 int retval = 0;
4297
4298 /* Read the HBA Host Status Register */
4299 status = lpfc_sli4_post_status_check(phba);
4300
4301 if (status) {
4302 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4303 lpfc_sli_brdrestart(phba);
4304 status = lpfc_sli4_post_status_check(phba);
4305 }
4306
4307 /* Check to see if any errors occurred during init */
4308 if (status) {
4309 phba->link_state = LPFC_HBA_ERROR;
4310 retval = 1;
4311 } else
4312 phba->sli4_hba.intr_enable = 0;
4313
4314 return retval;
4315}
4316
4317/**
4318 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4319 * @phba: Pointer to HBA context object.
4320 * @mask: Bit mask to be checked.
4321 *
4322 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4323 * from the API jump table function pointer from the lpfc_hba struct.
4324 **/
4325int
4326lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4327{
4328 return phba->lpfc_sli_brdready(phba, mask);
4329}
4330
James Smart92908312006-03-07 15:04:13 -05004331#define BARRIER_TEST_PATTERN (0xdeadbeef)
4332
James Smarte59058c2008-08-24 21:49:00 -04004333/**
James Smart3621a712009-04-06 18:47:14 -04004334 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004335 * @phba: Pointer to HBA context object.
4336 *
James Smart1b511972011-12-13 13:23:09 -05004337 * This function is called before resetting an HBA. This function is called
4338 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004339 **/
James Smart2e0fef82007-06-17 19:56:36 -05004340void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004341{
James Smart65a29c12006-07-06 15:50:50 -04004342 uint32_t __iomem *resp_buf;
4343 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004344 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004345 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004346 int i;
4347 uint8_t hdrtype;
4348
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004349 lockdep_assert_held(&phba->hbalock);
4350
James Smart92908312006-03-07 15:04:13 -05004351 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4352 if (hdrtype != 0x80 ||
4353 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4354 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4355 return;
4356
4357 /*
4358 * Tell the other part of the chip to suspend temporarily all
4359 * its DMA activity.
4360 */
James Smart65a29c12006-07-06 15:50:50 -04004361 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004362
4363 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004364 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4365 return;
James Smart92908312006-03-07 15:04:13 -05004366 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4367 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004368 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004369
James Smart9940b972011-03-11 16:06:12 -05004370 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4371 return;
4372 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004373 /* Clear Chip error bit */
4374 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004375 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004376 }
4377
4378 mbox = 0;
4379 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4380 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4381
4382 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004383 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004384 writel(mbox, mbox_buf);
4385
James Smart9940b972011-03-11 16:06:12 -05004386 for (i = 0; i < 50; i++) {
4387 if (lpfc_readl((resp_buf + 1), &resp_data))
4388 return;
4389 if (resp_data != ~(BARRIER_TEST_PATTERN))
4390 mdelay(1);
4391 else
4392 break;
4393 }
4394 resp_data = 0;
4395 if (lpfc_readl((resp_buf + 1), &resp_data))
4396 return;
4397 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004398 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004399 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004400 goto restore_hc;
4401 else
4402 goto clear_errat;
4403 }
4404
4405 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004406 resp_data = 0;
4407 for (i = 0; i < 500; i++) {
4408 if (lpfc_readl(resp_buf, &resp_data))
4409 return;
4410 if (resp_data != mbox)
4411 mdelay(1);
4412 else
4413 break;
4414 }
James Smart92908312006-03-07 15:04:13 -05004415
4416clear_errat:
4417
James Smart9940b972011-03-11 16:06:12 -05004418 while (++i < 500) {
4419 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4420 return;
4421 if (!(ha_copy & HA_ERATT))
4422 mdelay(1);
4423 else
4424 break;
4425 }
James Smart92908312006-03-07 15:04:13 -05004426
4427 if (readl(phba->HAregaddr) & HA_ERATT) {
4428 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004429 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004430 }
4431
4432restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004433 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004434 writel(hc_copy, phba->HCregaddr);
4435 readl(phba->HCregaddr); /* flush */
4436}
4437
James Smarte59058c2008-08-24 21:49:00 -04004438/**
James Smart3621a712009-04-06 18:47:14 -04004439 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004440 * @phba: Pointer to HBA context object.
4441 *
4442 * This function issues a kill_board mailbox command and waits for
4443 * the error attention interrupt. This function is called for stopping
4444 * the firmware processing. The caller is not required to hold any
4445 * locks. This function calls lpfc_hba_down_post function to free
4446 * any pending commands after the kill. The function will return 1 when it
4447 * fails to kill the board else will return 0.
4448 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004449int
James Smart2e0fef82007-06-17 19:56:36 -05004450lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004451{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004452 struct lpfc_sli *psli;
4453 LPFC_MBOXQ_t *pmb;
4454 uint32_t status;
4455 uint32_t ha_copy;
4456 int retval;
4457 int i = 0;
4458
4459 psli = &phba->sli;
4460
4461 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004462 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004463 "0329 Kill HBA Data: x%x x%x\n",
4464 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004465
James Smart98c9ea52007-10-27 13:37:33 -04004466 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4467 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004468 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004469
4470 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004471 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004472 if (lpfc_readl(phba->HCregaddr, &status)) {
4473 spin_unlock_irq(&phba->hbalock);
4474 mempool_free(pmb, phba->mbox_mem_pool);
4475 return 1;
4476 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004477 status &= ~HC_ERINT_ENA;
4478 writel(status, phba->HCregaddr);
4479 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004480 phba->link_flag |= LS_IGNORE_ERATT;
4481 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004482
4483 lpfc_kill_board(phba, pmb);
4484 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4485 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4486
4487 if (retval != MBX_SUCCESS) {
4488 if (retval != MBX_BUSY)
4489 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004490 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4491 "2752 KILL_BOARD command failed retval %d\n",
4492 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004493 spin_lock_irq(&phba->hbalock);
4494 phba->link_flag &= ~LS_IGNORE_ERATT;
4495 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004496 return 1;
4497 }
4498
James Smartf4b4c682009-05-22 14:53:12 -04004499 spin_lock_irq(&phba->hbalock);
4500 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4501 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004502
Jamie Wellnitz41415862006-02-28 19:25:27 -05004503 mempool_free(pmb, phba->mbox_mem_pool);
4504
4505 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4506 * attention every 100ms for 3 seconds. If we don't get ERATT after
4507 * 3 seconds we still set HBA_ERROR state because the status of the
4508 * board is now undefined.
4509 */
James Smart9940b972011-03-11 16:06:12 -05004510 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4511 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004512 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4513 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004514 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4515 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004516 }
4517
4518 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004519 if (ha_copy & HA_ERATT) {
4520 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004521 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004522 }
James Smart2e0fef82007-06-17 19:56:36 -05004523 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004524 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004525 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004526 phba->link_flag &= ~LS_IGNORE_ERATT;
4527 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004528
Jamie Wellnitz41415862006-02-28 19:25:27 -05004529 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004530 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004531
James Smart2e0fef82007-06-17 19:56:36 -05004532 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004533}
4534
James Smarte59058c2008-08-24 21:49:00 -04004535/**
James Smart3772a992009-05-22 14:50:54 -04004536 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004537 * @phba: Pointer to HBA context object.
4538 *
4539 * This function resets the HBA by writing HC_INITFF to the control
4540 * register. After the HBA resets, this function resets all the iocb ring
4541 * indices. This function disables PCI layer parity checking during
4542 * the reset.
4543 * This function returns 0 always.
4544 * The caller is not required to hold any locks.
4545 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004546int
James Smart2e0fef82007-06-17 19:56:36 -05004547lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004548{
4549 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004550 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004551 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004552 int i;
dea31012005-04-17 16:05:31 -05004553
Jamie Wellnitz41415862006-02-28 19:25:27 -05004554 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004555
Jamie Wellnitz41415862006-02-28 19:25:27 -05004556 /* Reset HBA */
4557 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004558 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004559 (phba->pport) ? phba->pport->port_state : 0,
4560 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004561
4562 /* perform board reset */
4563 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004564 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004565 if (phba->pport) {
4566 phba->pport->fc_myDID = 0;
4567 phba->pport->fc_prevDID = 0;
4568 }
dea31012005-04-17 16:05:31 -05004569
Jamie Wellnitz41415862006-02-28 19:25:27 -05004570 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004571 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
4572 return -EIO;
4573
Jamie Wellnitz41415862006-02-28 19:25:27 -05004574 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4575 (cfg_value &
4576 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4577
James Smart3772a992009-05-22 14:50:54 -04004578 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4579
Jamie Wellnitz41415862006-02-28 19:25:27 -05004580 /* Now toggle INITFF bit in the Host Control Register */
4581 writel(HC_INITFF, phba->HCregaddr);
4582 mdelay(1);
4583 readl(phba->HCregaddr); /* flush */
4584 writel(0, phba->HCregaddr);
4585 readl(phba->HCregaddr); /* flush */
4586
4587 /* Restore PCI cmd register */
4588 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004589
4590 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004591 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004592 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004593 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004594 pring->sli.sli3.rspidx = 0;
4595 pring->sli.sli3.next_cmdidx = 0;
4596 pring->sli.sli3.local_getidx = 0;
4597 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004598 pring->missbufcnt = 0;
4599 }
dea31012005-04-17 16:05:31 -05004600
James Smart2e0fef82007-06-17 19:56:36 -05004601 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004602 return 0;
4603}
4604
James Smarte59058c2008-08-24 21:49:00 -04004605/**
James Smartda0436e2009-05-22 14:51:39 -04004606 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4607 * @phba: Pointer to HBA context object.
4608 *
4609 * This function resets a SLI4 HBA. This function disables PCI layer parity
4610 * checking during resets the device. The caller is not required to hold
4611 * any locks.
4612 *
James Smart8c24a4f2019-08-14 16:56:53 -07004613 * This function returns 0 on success else returns negative error code.
James Smartda0436e2009-05-22 14:51:39 -04004614 **/
4615int
4616lpfc_sli4_brdreset(struct lpfc_hba *phba)
4617{
4618 struct lpfc_sli *psli = &phba->sli;
4619 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004620 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004621
4622 /* Reset HBA */
4623 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004624 "0295 Reset HBA Data: x%x x%x x%x\n",
4625 phba->pport->port_state, psli->sli_flag,
4626 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004627
4628 /* perform board reset */
4629 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004630 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004631 phba->pport->fc_myDID = 0;
4632 phba->pport->fc_prevDID = 0;
4633
James Smartda0436e2009-05-22 14:51:39 -04004634 spin_lock_irq(&phba->hbalock);
4635 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4636 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004637 spin_unlock_irq(&phba->hbalock);
4638
James Smart02936352014-04-04 13:52:12 -04004639 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4640 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4641 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4642 return rc;
4643 }
4644
James Smartda0436e2009-05-22 14:51:39 -04004645 /* Now physically reset the device */
4646 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4647 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004648
4649 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004650 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
4651 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4652 "3205 PCI read Config failed\n");
4653 return -EIO;
4654 }
4655
James Smartbe858b62010-12-15 17:57:20 -05004656 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4657 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4658
James Smart88318812012-09-29 11:29:29 -04004659 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004660 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004661
James Smartbe858b62010-12-15 17:57:20 -05004662 /* Restore PCI cmd register */
4663 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4664
James Smart27b01b82012-05-09 21:19:44 -04004665 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004666}
4667
4668/**
4669 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004670 * @phba: Pointer to HBA context object.
4671 *
4672 * This function is called in the SLI initialization code path to
4673 * restart the HBA. The caller is not required to hold any lock.
4674 * This function writes MBX_RESTART mailbox command to the SLIM and
4675 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4676 * function to free any pending commands. The function enables
4677 * POST only during the first initialization. The function returns zero.
4678 * The function does not guarantee completion of MBX_RESTART mailbox
4679 * command before the return of this function.
4680 **/
James Smartda0436e2009-05-22 14:51:39 -04004681static int
4682lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004683{
4684 MAILBOX_t *mb;
4685 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004686 volatile uint32_t word0;
4687 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004688 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004689
James Smart2e0fef82007-06-17 19:56:36 -05004690 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004691
James Smart0d878412009-10-02 15:16:56 -04004692 /* Take PCIe device Advanced Error Reporting (AER) state */
4693 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4694
Jamie Wellnitz41415862006-02-28 19:25:27 -05004695 psli = &phba->sli;
4696
4697 /* Restart HBA */
4698 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004699 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004700 (phba->pport) ? phba->pport->port_state : 0,
4701 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004702
4703 word0 = 0;
4704 mb = (MAILBOX_t *) &word0;
4705 mb->mbxCommand = MBX_RESTART;
4706 mb->mbxHc = 1;
4707
James Smart92908312006-03-07 15:04:13 -05004708 lpfc_reset_barrier(phba);
4709
Jamie Wellnitz41415862006-02-28 19:25:27 -05004710 to_slim = phba->MBslimaddr;
4711 writel(*(uint32_t *) mb, to_slim);
4712 readl(to_slim); /* flush */
4713
4714 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004715 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004716 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004717 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004718 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004719 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004720 writel(*(uint32_t *) mb, to_slim);
4721 readl(to_slim); /* flush */
4722
4723 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004724 if (phba->pport)
4725 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004726 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004727 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004728 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004729
James Smart64ba8812006-08-02 15:24:34 -04004730 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004731 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04004732
James Smarteaf15d52008-12-04 22:39:29 -05004733 /* Give the INITFF and Post time to settle. */
4734 mdelay(100);
dea31012005-04-17 16:05:31 -05004735
James Smart0d878412009-10-02 15:16:56 -04004736 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4737 if (hba_aer_enabled)
4738 pci_disable_pcie_error_reporting(phba->pcidev);
4739
Jamie Wellnitz41415862006-02-28 19:25:27 -05004740 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004741
4742 return 0;
4743}
4744
James Smarte59058c2008-08-24 21:49:00 -04004745/**
James Smartda0436e2009-05-22 14:51:39 -04004746 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4747 * @phba: Pointer to HBA context object.
4748 *
4749 * This function is called in the SLI initialization code path to restart
4750 * a SLI4 HBA. The caller is not required to hold any lock.
4751 * At the end of the function, it calls lpfc_hba_down_post function to
4752 * free any pending commands.
4753 **/
4754static int
4755lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4756{
4757 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004758 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004759 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004760
4761 /* Restart HBA */
4762 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4763 "0296 Restart HBA Data: x%x x%x\n",
4764 phba->pport->port_state, psli->sli_flag);
4765
James Smart75baf692010-06-08 18:31:21 -04004766 /* Take PCIe device Advanced Error Reporting (AER) state */
4767 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4768
James Smart27b01b82012-05-09 21:19:44 -04004769 rc = lpfc_sli4_brdreset(phba);
James Smart4fb86a62019-09-03 14:54:41 -07004770 if (rc) {
4771 phba->link_state = LPFC_HBA_ERROR;
4772 goto hba_down_queue;
4773 }
James Smartda0436e2009-05-22 14:51:39 -04004774
4775 spin_lock_irq(&phba->hbalock);
4776 phba->pport->stopped = 0;
4777 phba->link_state = LPFC_INIT_START;
4778 phba->hba_flag = 0;
4779 spin_unlock_irq(&phba->hbalock);
4780
4781 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004782 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04004783
James Smart75baf692010-06-08 18:31:21 -04004784 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4785 if (hba_aer_enabled)
4786 pci_disable_pcie_error_reporting(phba->pcidev);
4787
James Smart4fb86a62019-09-03 14:54:41 -07004788hba_down_queue:
James Smartda0436e2009-05-22 14:51:39 -04004789 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004790 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004791
James Smart27b01b82012-05-09 21:19:44 -04004792 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004793}
4794
4795/**
4796 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4797 * @phba: Pointer to HBA context object.
4798 *
4799 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4800 * API jump table function pointer from the lpfc_hba struct.
4801**/
4802int
4803lpfc_sli_brdrestart(struct lpfc_hba *phba)
4804{
4805 return phba->lpfc_sli_brdrestart(phba);
4806}
4807
4808/**
James Smart3621a712009-04-06 18:47:14 -04004809 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004810 * @phba: Pointer to HBA context object.
4811 *
4812 * This function is called after a HBA restart to wait for successful
4813 * restart of the HBA. Successful restart of the HBA is indicated by
4814 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4815 * iteration, the function will restart the HBA again. The function returns
4816 * zero if HBA successfully restarted else returns negative error code.
4817 **/
James Smart4492b732017-04-27 15:08:26 -07004818int
dea31012005-04-17 16:05:31 -05004819lpfc_sli_chipset_init(struct lpfc_hba *phba)
4820{
4821 uint32_t status, i = 0;
4822
4823 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004824 if (lpfc_readl(phba->HSregaddr, &status))
4825 return -EIO;
dea31012005-04-17 16:05:31 -05004826
4827 /* Check status register to see what current state is */
4828 i = 0;
4829 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4830
James Smartdcf2a4e2010-09-29 11:18:53 -04004831 /* Check every 10ms for 10 retries, then every 100ms for 90
4832 * retries, then every 1 sec for 50 retires for a total of
4833 * ~60 seconds before reset the board again and check every
4834 * 1 sec for 50 retries. The up to 60 seconds before the
4835 * board ready is required by the Falcon FIPS zeroization
4836 * complete, and any reset the board in between shall cause
4837 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004838 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004839 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004840 /* Adapter failed to init, timeout, status reg
4841 <status> */
James Smarted957682007-06-17 19:56:37 -05004842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004843 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004844 "timeout, status reg x%x, "
4845 "FW Data: A8 x%x AC x%x\n", status,
4846 readl(phba->MBslimaddr + 0xa8),
4847 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004848 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004849 return -ETIMEDOUT;
4850 }
4851
4852 /* Check to see if any errors occurred during init */
4853 if (status & HS_FFERM) {
4854 /* ERROR: During chipset initialization */
4855 /* Adapter failed to init, chipset, status reg
4856 <status> */
James Smarted957682007-06-17 19:56:37 -05004857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004858 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004859 "chipset, status reg x%x, "
4860 "FW Data: A8 x%x AC x%x\n", status,
4861 readl(phba->MBslimaddr + 0xa8),
4862 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004863 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004864 return -EIO;
4865 }
4866
James Smartdcf2a4e2010-09-29 11:18:53 -04004867 if (i <= 10)
dea31012005-04-17 16:05:31 -05004868 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004869 else if (i <= 100)
4870 msleep(100);
4871 else
4872 msleep(1000);
dea31012005-04-17 16:05:31 -05004873
James Smartdcf2a4e2010-09-29 11:18:53 -04004874 if (i == 150) {
4875 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004876 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004877 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004878 }
4879 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004880 if (lpfc_readl(phba->HSregaddr, &status))
4881 return -EIO;
dea31012005-04-17 16:05:31 -05004882 }
4883
4884 /* Check to see if any errors occurred during init */
4885 if (status & HS_FFERM) {
4886 /* ERROR: During chipset initialization */
4887 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004888 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004889 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004890 "status reg x%x, "
4891 "FW Data: A8 x%x AC x%x\n", status,
4892 readl(phba->MBslimaddr + 0xa8),
4893 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004894 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004895 return -EIO;
4896 }
4897
4898 /* Clear all interrupt enable conditions */
4899 writel(0, phba->HCregaddr);
4900 readl(phba->HCregaddr); /* flush */
4901
4902 /* setup host attn register */
4903 writel(0xffffffff, phba->HAregaddr);
4904 readl(phba->HAregaddr); /* flush */
4905 return 0;
4906}
4907
James Smarte59058c2008-08-24 21:49:00 -04004908/**
James Smart3621a712009-04-06 18:47:14 -04004909 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004910 *
4911 * This function calculates and returns the number of HBQs required to be
4912 * configured.
4913 **/
James Smart78b2d852007-08-02 11:10:21 -04004914int
James Smarted957682007-06-17 19:56:37 -05004915lpfc_sli_hbq_count(void)
4916{
James Smart92d7f7b2007-06-17 19:56:38 -05004917 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004918}
4919
James Smarte59058c2008-08-24 21:49:00 -04004920/**
James Smart3621a712009-04-06 18:47:14 -04004921 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004922 *
4923 * This function adds the number of hbq entries in every HBQ to get
4924 * the total number of hbq entries required for the HBA and returns
4925 * the total count.
4926 **/
James Smarted957682007-06-17 19:56:37 -05004927static int
4928lpfc_sli_hbq_entry_count(void)
4929{
4930 int hbq_count = lpfc_sli_hbq_count();
4931 int count = 0;
4932 int i;
4933
4934 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004935 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004936 return count;
4937}
4938
James Smarte59058c2008-08-24 21:49:00 -04004939/**
James Smart3621a712009-04-06 18:47:14 -04004940 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004941 *
4942 * This function calculates amount of memory required for all hbq entries
4943 * to be configured and returns the total memory required.
4944 **/
dea31012005-04-17 16:05:31 -05004945int
James Smarted957682007-06-17 19:56:37 -05004946lpfc_sli_hbq_size(void)
4947{
4948 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4949}
4950
James Smarte59058c2008-08-24 21:49:00 -04004951/**
James Smart3621a712009-04-06 18:47:14 -04004952 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004953 * @phba: Pointer to HBA context object.
4954 *
4955 * This function is called during the SLI initialization to configure
4956 * all the HBQs and post buffers to the HBQ. The caller is not
4957 * required to hold any locks. This function will return zero if successful
4958 * else it will return negative error code.
4959 **/
James Smarted957682007-06-17 19:56:37 -05004960static int
4961lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4962{
4963 int hbq_count = lpfc_sli_hbq_count();
4964 LPFC_MBOXQ_t *pmb;
4965 MAILBOX_t *pmbox;
4966 uint32_t hbqno;
4967 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004968
James Smart92d7f7b2007-06-17 19:56:38 -05004969 /* Get a Mailbox buffer to setup mailbox
4970 * commands for HBA initialization
4971 */
James Smarted957682007-06-17 19:56:37 -05004972 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4973
4974 if (!pmb)
4975 return -ENOMEM;
4976
James Smart04c68492009-05-22 14:52:52 -04004977 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004978
4979 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4980 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004981 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004982
4983 hbq_entry_index = 0;
4984 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4985 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4986 phba->hbqs[hbqno].hbqPutIdx = 0;
4987 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4988 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004989 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004990 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4991 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004992 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4993
4994 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4995 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4996 mbxStatus <status>, ring <num> */
4997
4998 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004999 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005000 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05005001 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005002 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05005003 pmbox->mbxStatus, hbqno);
5004
5005 phba->link_state = LPFC_HBA_ERROR;
5006 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04005007 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05005008 }
5009 }
5010 phba->hbq_count = hbq_count;
5011
James Smarted957682007-06-17 19:56:37 -05005012 mempool_free(pmb, phba->mbox_mem_pool);
5013
James Smart92d7f7b2007-06-17 19:56:38 -05005014 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04005015 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5016 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05005017 return 0;
5018}
5019
James Smarte59058c2008-08-24 21:49:00 -04005020/**
James Smart4f774512009-05-22 14:52:35 -04005021 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5022 * @phba: Pointer to HBA context object.
5023 *
5024 * This function is called during the SLI initialization to configure
5025 * all the HBQs and post buffers to the HBQ. The caller is not
5026 * required to hold any locks. This function will return zero if successful
5027 * else it will return negative error code.
5028 **/
5029static int
5030lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5031{
5032 phba->hbq_in_use = 1;
James Smart999fbbc2019-12-18 15:58:06 -08005033 /**
5034 * Specific case when the MDS diagnostics is enabled and supported.
5035 * The receive buffer count is truncated to manage the incoming
5036 * traffic.
5037 **/
5038 if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5039 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5040 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5041 else
5042 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5043 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04005044 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08005045 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04005046 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04005047 return 0;
5048}
5049
5050/**
James Smart3621a712009-04-06 18:47:14 -04005051 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04005052 * @phba: Pointer to HBA context object.
5053 * @sli_mode: sli mode - 2/3
5054 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005055 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04005056 * to issue config_port mailbox command. This function restarts the
5057 * HBA firmware and issues a config_port mailbox command to configure
5058 * the SLI interface in the sli mode specified by sli_mode
5059 * variable. The caller is not required to hold any locks.
5060 * The function returns 0 if successful, else returns negative error
5061 * code.
5062 **/
James Smart93996272008-08-24 21:50:30 -04005063int
5064lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05005065{
5066 LPFC_MBOXQ_t *pmb;
5067 uint32_t resetcount = 0, rc = 0, done = 0;
5068
5069 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5070 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05005071 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005072 return -ENOMEM;
5073 }
5074
James Smarted957682007-06-17 19:56:37 -05005075 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05005076 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05005077 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04005078 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005079 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05005080 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005081 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05005082 rc = lpfc_sli_chipset_init(phba);
5083 if (rc)
5084 break;
5085
James Smart2e0fef82007-06-17 19:56:36 -05005086 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04005087 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005088 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005089 resetcount++;
5090
James Smarted957682007-06-17 19:56:37 -05005091 /* Call pre CONFIG_PORT mailbox command initialization. A
5092 * value of 0 means the call was successful. Any other
5093 * nonzero value is a failure, but if ERESTART is returned,
5094 * the driver may reset the HBA and try again.
5095 */
dea31012005-04-17 16:05:31 -05005096 rc = lpfc_config_port_prep(phba);
5097 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05005098 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05005099 continue;
James Smart34b02dc2008-08-24 21:49:55 -04005100 } else if (rc)
dea31012005-04-17 16:05:31 -05005101 break;
James Smart6d368e52011-05-24 11:44:12 -04005102
James Smart2e0fef82007-06-17 19:56:36 -05005103 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05005104 lpfc_config_port(phba, pmb);
5105 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04005106 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5107 LPFC_SLI3_HBQ_ENABLED |
5108 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04005109 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05005110 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05005111 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005112 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05005113 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04005114 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005115 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04005116 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005117 spin_unlock_irq(&phba->hbalock);
5118 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04005119 } else {
5120 /* Allow asynchronous mailbox command to go through */
5121 spin_lock_irq(&phba->hbalock);
5122 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5123 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05005124 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05005125
5126 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5127 (pmb->u.mb.un.varCfgPort.gasabt == 0))
5128 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5129 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04005130 }
dea31012005-04-17 16:05:31 -05005131 }
James Smarted957682007-06-17 19:56:37 -05005132 if (!done) {
5133 rc = -EINVAL;
5134 goto do_prep_failed;
5135 }
James Smart04c68492009-05-22 14:52:52 -04005136 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5137 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04005138 rc = -ENXIO;
5139 goto do_prep_failed;
5140 }
James Smart04c68492009-05-22 14:52:52 -04005141 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04005142 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005143 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5144 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5145 phba->max_vpi : phba->max_vports;
5146
James Smart34b02dc2008-08-24 21:49:55 -04005147 } else
5148 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04005149 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005150 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005151 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005152 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005153
5154 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5155 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005156
James Smartf44ac122018-03-05 12:04:08 -08005157 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5158 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5159 phba->cfg_enable_bg = 0;
5160 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smarte2a0a9d2008-12-04 22:40:02 -05005161 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5162 "0443 Adapter did not grant "
5163 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005164 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005165 }
James Smart34b02dc2008-08-24 21:49:55 -04005166 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005167 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005168 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005169 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005170 }
James Smart92d7f7b2007-06-17 19:56:38 -05005171do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005172 mempool_free(pmb, phba->mbox_mem_pool);
5173 return rc;
5174}
5175
James Smarte59058c2008-08-24 21:49:00 -04005176
5177/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005178 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005179 * @phba: Pointer to HBA context object.
5180 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005181 * This function is the main SLI initialization function. This function
5182 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005183 * error attention handler code. Caller is not required to hold any
5184 * locks. This function issues config_port mailbox command to configure
5185 * the SLI, setup iocb rings and HBQ rings. In the end the function
5186 * calls the config_port_post function to issue init_link mailbox
5187 * command and to start the discovery. The function will return zero
5188 * if successful, else it will return negative error code.
5189 **/
James Smarted957682007-06-17 19:56:37 -05005190int
5191lpfc_sli_hba_setup(struct lpfc_hba *phba)
5192{
5193 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005194 int mode = 3, i;
5195 int longs;
James Smarted957682007-06-17 19:56:37 -05005196
James Smart12247e82016-07-06 12:36:09 -07005197 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005198 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005199 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005201 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005202 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005203 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005204 break;
5205 }
James Smarted957682007-06-17 19:56:37 -05005206 mode = 2;
5207 break;
5208 case 0:
5209 case 3:
5210 break;
5211 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005213 "1819 Unrecognized sli_mode parameter: %d.\n",
5214 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005215
5216 break;
5217 }
James Smartb5c53952016-03-31 14:12:30 -07005218 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005219
James Smart93996272008-08-24 21:50:30 -04005220 rc = lpfc_sli_config_port(phba, mode);
5221
James Smart12247e82016-07-06 12:36:09 -07005222 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005223 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005224 "1820 Unable to select SLI-3. "
5225 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005226 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005227 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005228 else if (rc && mode == 2)
5229 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005230 if (rc)
dea31012005-04-17 16:05:31 -05005231 goto lpfc_sli_hba_setup_error;
5232
James Smart0d878412009-10-02 15:16:56 -04005233 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5234 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5235 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5236 if (!rc) {
5237 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5238 "2709 This device supports "
5239 "Advanced Error Reporting (AER)\n");
5240 spin_lock_irq(&phba->hbalock);
5241 phba->hba_flag |= HBA_AER_ENABLED;
5242 spin_unlock_irq(&phba->hbalock);
5243 } else {
5244 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5245 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005246 "Advanced Error Reporting (AER): %d\n",
5247 rc);
James Smart0d878412009-10-02 15:16:56 -04005248 phba->cfg_aer_support = 0;
5249 }
5250 }
5251
James Smarted957682007-06-17 19:56:37 -05005252 if (phba->sli_rev == 3) {
5253 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5254 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005255 } else {
5256 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5257 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005258 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005259 }
5260
5261 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005262 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5263 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005264 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005265
5266 if (rc)
5267 goto lpfc_sli_hba_setup_error;
5268
James Smart6d368e52011-05-24 11:44:12 -04005269 /* Initialize VPIs. */
5270 if (phba->sli_rev == LPFC_SLI_REV3) {
5271 /*
5272 * The VPI bitmask and physical ID array are allocated
5273 * and initialized once only - at driver load. A port
5274 * reset doesn't need to reinitialize this memory.
5275 */
5276 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5277 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005278 phba->vpi_bmask = kcalloc(longs,
5279 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005280 GFP_KERNEL);
5281 if (!phba->vpi_bmask) {
5282 rc = -ENOMEM;
5283 goto lpfc_sli_hba_setup_error;
5284 }
5285
Kees Cook6396bb22018-06-12 14:03:40 -07005286 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5287 sizeof(uint16_t),
5288 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005289 if (!phba->vpi_ids) {
5290 kfree(phba->vpi_bmask);
5291 rc = -ENOMEM;
5292 goto lpfc_sli_hba_setup_error;
5293 }
5294 for (i = 0; i < phba->max_vpi; i++)
5295 phba->vpi_ids[i] = i;
5296 }
5297 }
5298
James Smart93996272008-08-24 21:50:30 -04005299 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005300 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5301 rc = lpfc_sli_hbq_setup(phba);
5302 if (rc)
5303 goto lpfc_sli_hba_setup_error;
5304 }
James Smart04c68492009-05-22 14:52:52 -04005305 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005306 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005307 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005308
5309 rc = lpfc_config_port_post(phba);
5310 if (rc)
5311 goto lpfc_sli_hba_setup_error;
5312
James Smarted957682007-06-17 19:56:37 -05005313 return rc;
5314
James Smart92d7f7b2007-06-17 19:56:38 -05005315lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005316 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005318 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005319 return rc;
5320}
5321
James Smartda0436e2009-05-22 14:51:39 -04005322/**
5323 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5324 * @phba: Pointer to HBA context object.
5325 * @mboxq: mailbox pointer.
5326 * This function issue a dump mailbox command to read config region
5327 * 23 and parse the records in the region and populate driver
5328 * data structure.
5329 **/
5330static int
James Smartff78d8f2011-12-13 13:21:35 -05005331lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005332{
James Smartff78d8f2011-12-13 13:21:35 -05005333 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005334 struct lpfc_dmabuf *mp;
5335 struct lpfc_mqe *mqe;
5336 uint32_t data_length;
5337 int rc;
5338
5339 /* Program the default value of vlan_id and fc_map */
5340 phba->valid_vlan = 0;
5341 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5342 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5343 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5344
James Smartff78d8f2011-12-13 13:21:35 -05005345 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5346 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005347 return -ENOMEM;
5348
James Smartff78d8f2011-12-13 13:21:35 -05005349 mqe = &mboxq->u.mqe;
5350 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5351 rc = -ENOMEM;
5352 goto out_free_mboxq;
5353 }
5354
James Smart3e1f0712018-11-29 16:09:29 -08005355 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04005356 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5357
5358 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5359 "(%d):2571 Mailbox cmd x%x Status x%x "
5360 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5361 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5362 "CQ: x%x x%x x%x x%x\n",
5363 mboxq->vport ? mboxq->vport->vpi : 0,
5364 bf_get(lpfc_mqe_command, mqe),
5365 bf_get(lpfc_mqe_status, mqe),
5366 mqe->un.mb_words[0], mqe->un.mb_words[1],
5367 mqe->un.mb_words[2], mqe->un.mb_words[3],
5368 mqe->un.mb_words[4], mqe->un.mb_words[5],
5369 mqe->un.mb_words[6], mqe->un.mb_words[7],
5370 mqe->un.mb_words[8], mqe->un.mb_words[9],
5371 mqe->un.mb_words[10], mqe->un.mb_words[11],
5372 mqe->un.mb_words[12], mqe->un.mb_words[13],
5373 mqe->un.mb_words[14], mqe->un.mb_words[15],
5374 mqe->un.mb_words[16], mqe->un.mb_words[50],
5375 mboxq->mcqe.word0,
5376 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5377 mboxq->mcqe.trailer);
5378
5379 if (rc) {
5380 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5381 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005382 rc = -EIO;
5383 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005384 }
5385 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005386 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005387 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5388 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005389 rc = -EIO;
5390 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005391 }
James Smartda0436e2009-05-22 14:51:39 -04005392
5393 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5394 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5395 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005396 rc = 0;
5397
5398out_free_mboxq:
5399 mempool_free(mboxq, phba->mbox_mem_pool);
5400 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005401}
5402
5403/**
5404 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5405 * @phba: pointer to lpfc hba data structure.
5406 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5407 * @vpd: pointer to the memory to hold resulting port vpd data.
5408 * @vpd_size: On input, the number of bytes allocated to @vpd.
5409 * On output, the number of data bytes in @vpd.
5410 *
5411 * This routine executes a READ_REV SLI4 mailbox command. In
5412 * addition, this routine gets the port vpd data.
5413 *
5414 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005415 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005416 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005417 **/
5418static int
5419lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5420 uint8_t *vpd, uint32_t *vpd_size)
5421{
5422 int rc = 0;
5423 uint32_t dma_size;
5424 struct lpfc_dmabuf *dmabuf;
5425 struct lpfc_mqe *mqe;
5426
5427 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5428 if (!dmabuf)
5429 return -ENOMEM;
5430
5431 /*
5432 * Get a DMA buffer for the vpd data resulting from the READ_REV
5433 * mailbox command.
5434 */
5435 dma_size = *vpd_size;
Luis Chamberlain750afb02019-01-04 09:23:09 +01005436 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5437 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005438 if (!dmabuf->virt) {
5439 kfree(dmabuf);
5440 return -ENOMEM;
5441 }
James Smartda0436e2009-05-22 14:51:39 -04005442
5443 /*
5444 * The SLI4 implementation of READ_REV conflicts at word1,
5445 * bits 31:16 and SLI4 adds vpd functionality not present
5446 * in SLI3. This code corrects the conflicts.
5447 */
5448 lpfc_read_rev(phba, mboxq);
5449 mqe = &mboxq->u.mqe;
5450 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5451 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5452 mqe->un.read_rev.word1 &= 0x0000FFFF;
5453 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5454 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5455
5456 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5457 if (rc) {
5458 dma_free_coherent(&phba->pcidev->dev, dma_size,
5459 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005460 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005461 return -EIO;
5462 }
5463
James Smartda0436e2009-05-22 14:51:39 -04005464 /*
5465 * The available vpd length cannot be bigger than the
5466 * DMA buffer passed to the port. Catch the less than
5467 * case and update the caller's size.
5468 */
5469 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5470 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5471
James Smartd7c47992010-06-08 18:31:54 -04005472 memcpy(vpd, dmabuf->virt, *vpd_size);
5473
James Smartda0436e2009-05-22 14:51:39 -04005474 dma_free_coherent(&phba->pcidev->dev, dma_size,
5475 dmabuf->virt, dmabuf->phys);
5476 kfree(dmabuf);
5477 return 0;
5478}
5479
5480/**
James Smartb3b4f3e2019-03-12 16:30:23 -07005481 * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005482 * @phba: pointer to lpfc hba data structure.
5483 *
5484 * This routine retrieves SLI4 device physical port name this PCI function
5485 * is attached to.
5486 *
5487 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005488 * 0 - successful
James Smartb3b4f3e2019-03-12 16:30:23 -07005489 * otherwise - failed to retrieve controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005490 **/
5491static int
James Smartb3b4f3e2019-03-12 16:30:23 -07005492lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
James Smartcd1c8302011-10-10 21:33:25 -04005493{
5494 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005495 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5496 struct lpfc_controller_attribute *cntl_attr;
James Smartcd1c8302011-10-10 21:33:25 -04005497 void *virtaddr = NULL;
5498 uint32_t alloclen, reqlen;
5499 uint32_t shdr_status, shdr_add_status;
5500 union lpfc_sli4_cfg_shdr *shdr;
James Smartcd1c8302011-10-10 21:33:25 -04005501 int rc;
5502
James Smartcd1c8302011-10-10 21:33:25 -04005503 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5504 if (!mboxq)
5505 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005506
James Smartb3b4f3e2019-03-12 16:30:23 -07005507 /* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
James Smartcd1c8302011-10-10 21:33:25 -04005508 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5509 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5510 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5511 LPFC_SLI4_MBX_NEMBED);
James Smartb3b4f3e2019-03-12 16:30:23 -07005512
James Smartcd1c8302011-10-10 21:33:25 -04005513 if (alloclen < reqlen) {
5514 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5515 "3084 Allocated DMA memory size (%d) is "
5516 "less than the requested DMA memory size "
5517 "(%d)\n", alloclen, reqlen);
5518 rc = -ENOMEM;
5519 goto out_free_mboxq;
5520 }
5521 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5522 virtaddr = mboxq->sge_array->addr[0];
5523 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5524 shdr = &mbx_cntl_attr->cfg_shdr;
5525 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5526 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5527 if (shdr_status || shdr_add_status || rc) {
5528 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5529 "3085 Mailbox x%x (x%x/x%x) failed, "
5530 "rc:x%x, status:x%x, add_status:x%x\n",
5531 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5532 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5533 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5534 rc, shdr_status, shdr_add_status);
5535 rc = -ENXIO;
5536 goto out_free_mboxq;
5537 }
James Smartb3b4f3e2019-03-12 16:30:23 -07005538
James Smartcd1c8302011-10-10 21:33:25 -04005539 cntl_attr = &mbx_cntl_attr->cntl_attr;
5540 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5541 phba->sli4_hba.lnk_info.lnk_tp =
5542 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5543 phba->sli4_hba.lnk_info.lnk_no =
5544 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
James Smartb3b4f3e2019-03-12 16:30:23 -07005545
5546 memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
5547 strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
5548 sizeof(phba->BIOSVersion));
5549
James Smartcd1c8302011-10-10 21:33:25 -04005550 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartb3b4f3e2019-03-12 16:30:23 -07005551 "3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s\n",
James Smartcd1c8302011-10-10 21:33:25 -04005552 phba->sli4_hba.lnk_info.lnk_tp,
James Smartb3b4f3e2019-03-12 16:30:23 -07005553 phba->sli4_hba.lnk_info.lnk_no,
5554 phba->BIOSVersion);
5555out_free_mboxq:
5556 if (rc != MBX_TIMEOUT) {
5557 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5558 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5559 else
5560 mempool_free(mboxq, phba->mbox_mem_pool);
5561 }
5562 return rc;
5563}
5564
5565/**
5566 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5567 * @phba: pointer to lpfc hba data structure.
5568 *
5569 * This routine retrieves SLI4 device physical port name this PCI function
5570 * is attached to.
5571 *
5572 * Return codes
5573 * 0 - successful
5574 * otherwise - failed to retrieve physical port name
5575 **/
5576static int
5577lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5578{
5579 LPFC_MBOXQ_t *mboxq;
5580 struct lpfc_mbx_get_port_name *get_port_name;
5581 uint32_t shdr_status, shdr_add_status;
5582 union lpfc_sli4_cfg_shdr *shdr;
5583 char cport_name = 0;
5584 int rc;
5585
5586 /* We assume nothing at this point */
5587 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5588 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5589
5590 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5591 if (!mboxq)
5592 return -ENOMEM;
5593 /* obtain link type and link number via READ_CONFIG */
5594 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5595 lpfc_sli4_read_config(phba);
5596 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5597 goto retrieve_ppname;
5598
5599 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5600 rc = lpfc_sli4_get_ctl_attr(phba);
5601 if (rc)
5602 goto out_free_mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005603
5604retrieve_ppname:
5605 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5606 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5607 sizeof(struct lpfc_mbx_get_port_name) -
5608 sizeof(struct lpfc_sli4_cfg_mhdr),
5609 LPFC_SLI4_MBX_EMBED);
5610 get_port_name = &mboxq->u.mqe.un.get_port_name;
5611 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5612 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5613 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5614 phba->sli4_hba.lnk_info.lnk_tp);
5615 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5616 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5617 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5618 if (shdr_status || shdr_add_status || rc) {
5619 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5620 "3087 Mailbox x%x (x%x/x%x) failed: "
5621 "rc:x%x, status:x%x, add_status:x%x\n",
5622 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5623 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5624 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5625 rc, shdr_status, shdr_add_status);
5626 rc = -ENXIO;
5627 goto out_free_mboxq;
5628 }
5629 switch (phba->sli4_hba.lnk_info.lnk_no) {
5630 case LPFC_LINK_NUMBER_0:
5631 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5632 &get_port_name->u.response);
5633 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5634 break;
5635 case LPFC_LINK_NUMBER_1:
5636 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5637 &get_port_name->u.response);
5638 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5639 break;
5640 case LPFC_LINK_NUMBER_2:
5641 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5642 &get_port_name->u.response);
5643 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5644 break;
5645 case LPFC_LINK_NUMBER_3:
5646 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5647 &get_port_name->u.response);
5648 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5649 break;
5650 default:
5651 break;
5652 }
5653
5654 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5655 phba->Port[0] = cport_name;
5656 phba->Port[1] = '\0';
5657 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5658 "3091 SLI get port name: %s\n", phba->Port);
5659 }
5660
5661out_free_mboxq:
5662 if (rc != MBX_TIMEOUT) {
5663 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5664 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5665 else
5666 mempool_free(mboxq, phba->mbox_mem_pool);
5667 }
5668 return rc;
5669}
5670
5671/**
James Smartda0436e2009-05-22 14:51:39 -04005672 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5673 * @phba: pointer to lpfc hba data structure.
5674 *
5675 * This routine is called to explicitly arm the SLI4 device's completion and
5676 * event queues
5677 **/
5678static void
5679lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5680{
James Smart895427b2017-02-12 13:52:30 -08005681 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005682 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartcdb42be2019-01-28 11:14:21 -08005683 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07005684 struct lpfc_queue *eq;
James Smartda0436e2009-05-22 14:51:39 -04005685
James Smart32517fc2019-01-28 11:14:33 -08005686 sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
5687 sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
James Smartb71413d2018-02-22 08:18:40 -08005688 if (sli4_hba->nvmels_cq)
James Smart32517fc2019-01-28 11:14:33 -08005689 sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
5690 LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005691
James Smartcdb42be2019-01-28 11:14:21 -08005692 if (sli4_hba->hdwq) {
James Smart657add42019-05-21 17:49:06 -07005693 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08005694 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07005695 qp = &sli4_hba->hdwq[qidx];
5696 /* ARM the corresponding CQ */
James Smart01f2ef62019-08-28 16:19:11 -07005697 sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
James Smartc00f62e2019-08-14 16:57:11 -07005698 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08005699 }
James Smart895427b2017-02-12 13:52:30 -08005700
James Smart657add42019-05-21 17:49:06 -07005701 /* Loop thru all IRQ vectors */
5702 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
5703 eq = sli4_hba->hba_eq_hdl[qidx].eq;
5704 /* ARM the corresponding EQ */
5705 sli4_hba->sli4_write_eq_db(phba, eq,
5706 0, LPFC_QUEUE_REARM);
5707 }
James Smartcdb42be2019-01-28 11:14:21 -08005708 }
James Smart1ba981f2014-02-20 09:56:45 -05005709
James Smart2d7dbc42017-02-12 13:52:35 -08005710 if (phba->nvmet_support) {
5711 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smart32517fc2019-01-28 11:14:33 -08005712 sli4_hba->sli4_write_cq_db(phba,
5713 sli4_hba->nvmet_cqset[qidx], 0,
James Smart2d7dbc42017-02-12 13:52:35 -08005714 LPFC_QUEUE_REARM);
5715 }
James Smart2e90f4b2011-12-13 13:22:37 -05005716 }
James Smartda0436e2009-05-22 14:51:39 -04005717}
5718
5719/**
James Smart6d368e52011-05-24 11:44:12 -04005720 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5721 * @phba: Pointer to HBA context object.
5722 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005723 * @extnt_count: buffer to hold port available extent count.
5724 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005725 *
James Smartb76f2dc2011-07-22 18:37:42 -04005726 * This function calls the port and retrievs the number of available
5727 * extents and their size for a particular extent type.
5728 *
5729 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005730 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005731int
James Smart6d368e52011-05-24 11:44:12 -04005732lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5733 uint16_t *extnt_count, uint16_t *extnt_size)
5734{
5735 int rc = 0;
5736 uint32_t length;
5737 uint32_t mbox_tmo;
5738 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5739 LPFC_MBOXQ_t *mbox;
5740
5741 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5742 if (!mbox)
5743 return -ENOMEM;
5744
5745 /* Find out how many extents are available for this resource type */
5746 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5747 sizeof(struct lpfc_sli4_cfg_mhdr));
5748 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5749 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5750 length, LPFC_SLI4_MBX_EMBED);
5751
5752 /* Send an extents count of 0 - the GET doesn't use it. */
5753 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5754 LPFC_SLI4_MBX_EMBED);
5755 if (unlikely(rc)) {
5756 rc = -EIO;
5757 goto err_exit;
5758 }
5759
5760 if (!phba->sli4_hba.intr_enable)
5761 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5762 else {
James Smarta183a152011-10-10 21:32:43 -04005763 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005764 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5765 }
5766 if (unlikely(rc)) {
5767 rc = -EIO;
5768 goto err_exit;
5769 }
5770
5771 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5772 if (bf_get(lpfc_mbox_hdr_status,
5773 &rsrc_info->header.cfg_shdr.response)) {
5774 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5775 "2930 Failed to get resource extents "
5776 "Status 0x%x Add'l Status 0x%x\n",
5777 bf_get(lpfc_mbox_hdr_status,
5778 &rsrc_info->header.cfg_shdr.response),
5779 bf_get(lpfc_mbox_hdr_add_status,
5780 &rsrc_info->header.cfg_shdr.response));
5781 rc = -EIO;
5782 goto err_exit;
5783 }
5784
5785 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5786 &rsrc_info->u.rsp);
5787 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5788 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005789
5790 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5791 "3162 Retrieved extents type-%d from port: count:%d, "
5792 "size:%d\n", type, *extnt_count, *extnt_size);
5793
5794err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005795 mempool_free(mbox, phba->mbox_mem_pool);
5796 return rc;
5797}
5798
5799/**
5800 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5801 * @phba: Pointer to HBA context object.
5802 * @type: The extent type to check.
5803 *
5804 * This function reads the current available extents from the port and checks
5805 * if the extent count or extent size has changed since the last access.
5806 * Callers use this routine post port reset to understand if there is a
5807 * extent reprovisioning requirement.
5808 *
5809 * Returns:
5810 * -Error: error indicates problem.
5811 * 1: Extent count or size has changed.
5812 * 0: No changes.
5813 **/
5814static int
5815lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5816{
5817 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5818 uint16_t size_diff, rsrc_ext_size;
5819 int rc = 0;
5820 struct lpfc_rsrc_blks *rsrc_entry;
5821 struct list_head *rsrc_blk_list = NULL;
5822
5823 size_diff = 0;
5824 curr_ext_cnt = 0;
5825 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5826 &rsrc_ext_cnt,
5827 &rsrc_ext_size);
5828 if (unlikely(rc))
5829 return -EIO;
5830
5831 switch (type) {
5832 case LPFC_RSC_TYPE_FCOE_RPI:
5833 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5834 break;
5835 case LPFC_RSC_TYPE_FCOE_VPI:
5836 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5837 break;
5838 case LPFC_RSC_TYPE_FCOE_XRI:
5839 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5840 break;
5841 case LPFC_RSC_TYPE_FCOE_VFI:
5842 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5843 break;
5844 default:
5845 break;
5846 }
5847
5848 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5849 curr_ext_cnt++;
5850 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5851 size_diff++;
5852 }
5853
5854 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5855 rc = 1;
5856
5857 return rc;
5858}
5859
5860/**
5861 * lpfc_sli4_cfg_post_extnts -
5862 * @phba: Pointer to HBA context object.
5863 * @extnt_cnt - number of available extents.
5864 * @type - the extent type (rpi, xri, vfi, vpi).
5865 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5866 * @mbox - pointer to the caller's allocated mailbox structure.
5867 *
5868 * This function executes the extents allocation request. It also
5869 * takes care of the amount of memory needed to allocate or get the
5870 * allocated extents. It is the caller's responsibility to evaluate
5871 * the response.
5872 *
5873 * Returns:
5874 * -Error: Error value describes the condition found.
5875 * 0: if successful
5876 **/
5877static int
James Smart8a9d2e82012-05-09 21:16:12 -04005878lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005879 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5880{
5881 int rc = 0;
5882 uint32_t req_len;
5883 uint32_t emb_len;
5884 uint32_t alloc_len, mbox_tmo;
5885
5886 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005887 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005888
5889 /*
5890 * Calculate the size of an embedded mailbox. The uint32_t
5891 * accounts for extents-specific word.
5892 */
5893 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5894 sizeof(uint32_t);
5895
5896 /*
5897 * Presume the allocation and response will fit into an embedded
5898 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5899 */
5900 *emb = LPFC_SLI4_MBX_EMBED;
5901 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005902 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005903 sizeof(union lpfc_sli4_cfg_shdr) +
5904 sizeof(uint32_t);
5905 *emb = LPFC_SLI4_MBX_NEMBED;
5906 }
5907
5908 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5909 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5910 req_len, *emb);
5911 if (alloc_len < req_len) {
5912 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005913 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005914 "less than the requested DMA memory "
5915 "size (x%x)\n", alloc_len, req_len);
5916 return -ENOMEM;
5917 }
James Smart8a9d2e82012-05-09 21:16:12 -04005918 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005919 if (unlikely(rc))
5920 return -EIO;
5921
5922 if (!phba->sli4_hba.intr_enable)
5923 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5924 else {
James Smarta183a152011-10-10 21:32:43 -04005925 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005926 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5927 }
5928
5929 if (unlikely(rc))
5930 rc = -EIO;
5931 return rc;
5932}
5933
5934/**
5935 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5936 * @phba: Pointer to HBA context object.
5937 * @type: The resource extent type to allocate.
5938 *
5939 * This function allocates the number of elements for the specified
5940 * resource type.
5941 **/
5942static int
5943lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5944{
5945 bool emb = false;
5946 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5947 uint16_t rsrc_id, rsrc_start, j, k;
5948 uint16_t *ids;
5949 int i, rc;
5950 unsigned long longs;
5951 unsigned long *bmask;
5952 struct lpfc_rsrc_blks *rsrc_blks;
5953 LPFC_MBOXQ_t *mbox;
5954 uint32_t length;
5955 struct lpfc_id_range *id_array = NULL;
5956 void *virtaddr = NULL;
5957 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5958 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5959 struct list_head *ext_blk_list;
5960
5961 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5962 &rsrc_cnt,
5963 &rsrc_size);
5964 if (unlikely(rc))
5965 return -EIO;
5966
5967 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5968 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5969 "3009 No available Resource Extents "
5970 "for resource type 0x%x: Count: 0x%x, "
5971 "Size 0x%x\n", type, rsrc_cnt,
5972 rsrc_size);
5973 return -ENOMEM;
5974 }
5975
James Smart8a9d2e82012-05-09 21:16:12 -04005976 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5977 "2903 Post resource extents type-0x%x: "
5978 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005979
5980 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5981 if (!mbox)
5982 return -ENOMEM;
5983
James Smart8a9d2e82012-05-09 21:16:12 -04005984 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005985 if (unlikely(rc)) {
5986 rc = -EIO;
5987 goto err_exit;
5988 }
5989
5990 /*
5991 * Figure out where the response is located. Then get local pointers
5992 * to the response data. The port does not guarantee to respond to
5993 * all extents counts request so update the local variable with the
5994 * allocated count from the port.
5995 */
5996 if (emb == LPFC_SLI4_MBX_EMBED) {
5997 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5998 id_array = &rsrc_ext->u.rsp.id[0];
5999 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6000 } else {
6001 virtaddr = mbox->sge_array->addr[0];
6002 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6003 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6004 id_array = &n_rsrc->id;
6005 }
6006
6007 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6008 rsrc_id_cnt = rsrc_cnt * rsrc_size;
6009
6010 /*
6011 * Based on the resource size and count, correct the base and max
6012 * resource values.
6013 */
6014 length = sizeof(struct lpfc_rsrc_blks);
6015 switch (type) {
6016 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07006017 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006018 sizeof(unsigned long),
6019 GFP_KERNEL);
6020 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6021 rc = -ENOMEM;
6022 goto err_exit;
6023 }
Kees Cook6396bb22018-06-12 14:03:40 -07006024 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006025 sizeof(uint16_t),
6026 GFP_KERNEL);
6027 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6028 kfree(phba->sli4_hba.rpi_bmask);
6029 rc = -ENOMEM;
6030 goto err_exit;
6031 }
6032
6033 /*
6034 * The next_rpi was initialized with the maximum available
6035 * count but the port may allocate a smaller number. Catch
6036 * that case and update the next_rpi.
6037 */
6038 phba->sli4_hba.next_rpi = rsrc_id_cnt;
6039
6040 /* Initialize local ptrs for common extent processing later. */
6041 bmask = phba->sli4_hba.rpi_bmask;
6042 ids = phba->sli4_hba.rpi_ids;
6043 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6044 break;
6045 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07006046 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006047 GFP_KERNEL);
6048 if (unlikely(!phba->vpi_bmask)) {
6049 rc = -ENOMEM;
6050 goto err_exit;
6051 }
Kees Cook6396bb22018-06-12 14:03:40 -07006052 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006053 GFP_KERNEL);
6054 if (unlikely(!phba->vpi_ids)) {
6055 kfree(phba->vpi_bmask);
6056 rc = -ENOMEM;
6057 goto err_exit;
6058 }
6059
6060 /* Initialize local ptrs for common extent processing later. */
6061 bmask = phba->vpi_bmask;
6062 ids = phba->vpi_ids;
6063 ext_blk_list = &phba->lpfc_vpi_blk_list;
6064 break;
6065 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07006066 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006067 sizeof(unsigned long),
6068 GFP_KERNEL);
6069 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6070 rc = -ENOMEM;
6071 goto err_exit;
6072 }
James Smart8a9d2e82012-05-09 21:16:12 -04006073 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006074 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006075 sizeof(uint16_t),
6076 GFP_KERNEL);
6077 if (unlikely(!phba->sli4_hba.xri_ids)) {
6078 kfree(phba->sli4_hba.xri_bmask);
6079 rc = -ENOMEM;
6080 goto err_exit;
6081 }
6082
6083 /* Initialize local ptrs for common extent processing later. */
6084 bmask = phba->sli4_hba.xri_bmask;
6085 ids = phba->sli4_hba.xri_ids;
6086 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6087 break;
6088 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07006089 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006090 sizeof(unsigned long),
6091 GFP_KERNEL);
6092 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6093 rc = -ENOMEM;
6094 goto err_exit;
6095 }
Kees Cook6396bb22018-06-12 14:03:40 -07006096 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006097 sizeof(uint16_t),
6098 GFP_KERNEL);
6099 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6100 kfree(phba->sli4_hba.vfi_bmask);
6101 rc = -ENOMEM;
6102 goto err_exit;
6103 }
6104
6105 /* Initialize local ptrs for common extent processing later. */
6106 bmask = phba->sli4_hba.vfi_bmask;
6107 ids = phba->sli4_hba.vfi_ids;
6108 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6109 break;
6110 default:
6111 /* Unsupported Opcode. Fail call. */
6112 id_array = NULL;
6113 bmask = NULL;
6114 ids = NULL;
6115 ext_blk_list = NULL;
6116 goto err_exit;
6117 }
6118
6119 /*
6120 * Complete initializing the extent configuration with the
6121 * allocated ids assigned to this function. The bitmask serves
6122 * as an index into the array and manages the available ids. The
6123 * array just stores the ids communicated to the port via the wqes.
6124 */
6125 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6126 if ((i % 2) == 0)
6127 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6128 &id_array[k]);
6129 else
6130 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6131 &id_array[k]);
6132
6133 rsrc_blks = kzalloc(length, GFP_KERNEL);
6134 if (unlikely(!rsrc_blks)) {
6135 rc = -ENOMEM;
6136 kfree(bmask);
6137 kfree(ids);
6138 goto err_exit;
6139 }
6140 rsrc_blks->rsrc_start = rsrc_id;
6141 rsrc_blks->rsrc_size = rsrc_size;
6142 list_add_tail(&rsrc_blks->list, ext_blk_list);
6143 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08006144 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart5e5b5112019-01-28 11:14:22 -08006145 phba->sli4_hba.io_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08006146 lpfc_sli4_get_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08006147 }
James Smart6d368e52011-05-24 11:44:12 -04006148
6149 while (rsrc_id < (rsrc_start + rsrc_size)) {
6150 ids[j] = rsrc_id;
6151 rsrc_id++;
6152 j++;
6153 }
6154 /* Entire word processed. Get next word.*/
6155 if ((i % 2) == 1)
6156 k++;
6157 }
6158 err_exit:
6159 lpfc_sli4_mbox_cmd_free(phba, mbox);
6160 return rc;
6161}
6162
James Smart895427b2017-02-12 13:52:30 -08006163
6164
James Smart6d368e52011-05-24 11:44:12 -04006165/**
6166 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6167 * @phba: Pointer to HBA context object.
6168 * @type: the extent's type.
6169 *
6170 * This function deallocates all extents of a particular resource type.
6171 * SLI4 does not allow for deallocating a particular extent range. It
6172 * is the caller's responsibility to release all kernel memory resources.
6173 **/
6174static int
6175lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6176{
6177 int rc;
6178 uint32_t length, mbox_tmo = 0;
6179 LPFC_MBOXQ_t *mbox;
6180 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6181 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6182
6183 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6184 if (!mbox)
6185 return -ENOMEM;
6186
6187 /*
6188 * This function sends an embedded mailbox because it only sends the
6189 * the resource type. All extents of this type are released by the
6190 * port.
6191 */
6192 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6193 sizeof(struct lpfc_sli4_cfg_mhdr));
6194 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6195 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6196 length, LPFC_SLI4_MBX_EMBED);
6197
6198 /* Send an extents count of 0 - the dealloc doesn't use it. */
6199 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6200 LPFC_SLI4_MBX_EMBED);
6201 if (unlikely(rc)) {
6202 rc = -EIO;
6203 goto out_free_mbox;
6204 }
6205 if (!phba->sli4_hba.intr_enable)
6206 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6207 else {
James Smarta183a152011-10-10 21:32:43 -04006208 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006209 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6210 }
6211 if (unlikely(rc)) {
6212 rc = -EIO;
6213 goto out_free_mbox;
6214 }
6215
6216 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6217 if (bf_get(lpfc_mbox_hdr_status,
6218 &dealloc_rsrc->header.cfg_shdr.response)) {
6219 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6220 "2919 Failed to release resource extents "
6221 "for type %d - Status 0x%x Add'l Status 0x%x. "
6222 "Resource memory not released.\n",
6223 type,
6224 bf_get(lpfc_mbox_hdr_status,
6225 &dealloc_rsrc->header.cfg_shdr.response),
6226 bf_get(lpfc_mbox_hdr_add_status,
6227 &dealloc_rsrc->header.cfg_shdr.response));
6228 rc = -EIO;
6229 goto out_free_mbox;
6230 }
6231
6232 /* Release kernel memory resources for the specific type. */
6233 switch (type) {
6234 case LPFC_RSC_TYPE_FCOE_VPI:
6235 kfree(phba->vpi_bmask);
6236 kfree(phba->vpi_ids);
6237 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6238 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6239 &phba->lpfc_vpi_blk_list, list) {
6240 list_del_init(&rsrc_blk->list);
6241 kfree(rsrc_blk);
6242 }
James Smart16a3a202013-04-17 20:14:38 -04006243 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006244 break;
6245 case LPFC_RSC_TYPE_FCOE_XRI:
6246 kfree(phba->sli4_hba.xri_bmask);
6247 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006248 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6249 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6250 list_del_init(&rsrc_blk->list);
6251 kfree(rsrc_blk);
6252 }
6253 break;
6254 case LPFC_RSC_TYPE_FCOE_VFI:
6255 kfree(phba->sli4_hba.vfi_bmask);
6256 kfree(phba->sli4_hba.vfi_ids);
6257 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6258 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6259 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6260 list_del_init(&rsrc_blk->list);
6261 kfree(rsrc_blk);
6262 }
6263 break;
6264 case LPFC_RSC_TYPE_FCOE_RPI:
6265 /* RPI bitmask and physical id array are cleaned up earlier. */
6266 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6267 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6268 list_del_init(&rsrc_blk->list);
6269 kfree(rsrc_blk);
6270 }
6271 break;
6272 default:
6273 break;
6274 }
6275
6276 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6277
6278 out_free_mbox:
6279 mempool_free(mbox, phba->mbox_mem_pool);
6280 return rc;
6281}
6282
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006283static void
James Smart7bdedb32016-07-06 12:36:00 -07006284lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6285 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006286{
James Smart65791f12016-07-06 12:35:56 -07006287 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006288
James Smart65791f12016-07-06 12:35:56 -07006289 len = sizeof(struct lpfc_mbx_set_feature) -
6290 sizeof(struct lpfc_sli4_cfg_mhdr);
6291 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6292 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6293 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006294
James Smart7bdedb32016-07-06 12:36:00 -07006295 switch (feature) {
6296 case LPFC_SET_UE_RECOVERY:
6297 bf_set(lpfc_mbx_set_feature_UER,
6298 &mbox->u.mqe.un.set_feature, 1);
6299 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6300 mbox->u.mqe.un.set_feature.param_len = 8;
6301 break;
6302 case LPFC_SET_MDS_DIAGS:
6303 bf_set(lpfc_mbx_set_feature_mds,
6304 &mbox->u.mqe.un.set_feature, 1);
6305 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006306 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006307 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6308 mbox->u.mqe.un.set_feature.param_len = 8;
6309 break;
James Smart171f6c42019-11-04 16:57:07 -08006310 case LPFC_SET_DUAL_DUMP:
6311 bf_set(lpfc_mbx_set_feature_dd,
6312 &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6313 bf_set(lpfc_mbx_set_feature_ddquery,
6314 &mbox->u.mqe.un.set_feature, 0);
6315 mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6316 mbox->u.mqe.un.set_feature.param_len = 4;
6317 break;
James Smart65791f12016-07-06 12:35:56 -07006318 }
James Smart7bdedb32016-07-06 12:36:00 -07006319
6320 return;
James Smart65791f12016-07-06 12:35:56 -07006321}
6322
James Smart6d368e52011-05-24 11:44:12 -04006323/**
James Smart1165a5c2018-11-29 16:09:39 -08006324 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6325 * @phba: Pointer to HBA context object.
6326 *
6327 * Disable FW logging into host memory on the adapter. To
6328 * be done before reading logs from the host memory.
6329 **/
6330void
6331lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6332{
6333 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6334
James Smart95bfc6d2019-10-18 14:18:27 -07006335 spin_lock_irq(&phba->hbalock);
6336 ras_fwlog->state = INACTIVE;
6337 spin_unlock_irq(&phba->hbalock);
James Smart1165a5c2018-11-29 16:09:39 -08006338
6339 /* Disable FW logging to host memory */
6340 writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6341 phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
James Smart95bfc6d2019-10-18 14:18:27 -07006342
6343 /* Wait 10ms for firmware to stop using DMA buffer */
6344 usleep_range(10 * 1000, 20 * 1000);
James Smart1165a5c2018-11-29 16:09:39 -08006345}
6346
6347/**
James Smartd2cc9bc2018-09-10 10:30:50 -07006348 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6349 * @phba: Pointer to HBA context object.
6350 *
6351 * This function is called to free memory allocated for RAS FW logging
6352 * support in the driver.
6353 **/
6354void
6355lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6356{
6357 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6358 struct lpfc_dmabuf *dmabuf, *next;
6359
6360 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6361 list_for_each_entry_safe(dmabuf, next,
6362 &ras_fwlog->fwlog_buff_list,
6363 list) {
6364 list_del(&dmabuf->list);
6365 dma_free_coherent(&phba->pcidev->dev,
6366 LPFC_RAS_MAX_ENTRY_SIZE,
6367 dmabuf->virt, dmabuf->phys);
6368 kfree(dmabuf);
6369 }
6370 }
6371
6372 if (ras_fwlog->lwpd.virt) {
6373 dma_free_coherent(&phba->pcidev->dev,
6374 sizeof(uint32_t) * 2,
6375 ras_fwlog->lwpd.virt,
6376 ras_fwlog->lwpd.phys);
6377 ras_fwlog->lwpd.virt = NULL;
6378 }
6379
James Smart95bfc6d2019-10-18 14:18:27 -07006380 spin_lock_irq(&phba->hbalock);
6381 ras_fwlog->state = INACTIVE;
6382 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006383}
6384
6385/**
6386 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6387 * @phba: Pointer to HBA context object.
6388 * @fwlog_buff_count: Count of buffers to be created.
6389 *
6390 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6391 * to update FW log is posted to the adapter.
6392 * Buffer count is calculated based on module param ras_fwlog_buffsize
6393 * Size of each buffer posted to FW is 64K.
6394 **/
6395
6396static int
6397lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6398 uint32_t fwlog_buff_count)
6399{
6400 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6401 struct lpfc_dmabuf *dmabuf;
6402 int rc = 0, i = 0;
6403
6404 /* Initialize List */
6405 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6406
6407 /* Allocate memory for the LWPD */
6408 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6409 sizeof(uint32_t) * 2,
6410 &ras_fwlog->lwpd.phys,
6411 GFP_KERNEL);
6412 if (!ras_fwlog->lwpd.virt) {
James Smartcb349902018-11-29 16:09:27 -08006413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006414 "6185 LWPD Memory Alloc Failed\n");
6415
6416 return -ENOMEM;
6417 }
6418
6419 ras_fwlog->fw_buffcount = fwlog_buff_count;
6420 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6421 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6422 GFP_KERNEL);
6423 if (!dmabuf) {
6424 rc = -ENOMEM;
6425 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6426 "6186 Memory Alloc failed FW logging");
6427 goto free_mem;
6428 }
6429
Luis Chamberlain750afb02019-01-04 09:23:09 +01006430 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartd2cc9bc2018-09-10 10:30:50 -07006431 LPFC_RAS_MAX_ENTRY_SIZE,
Luis Chamberlain750afb02019-01-04 09:23:09 +01006432 &dmabuf->phys, GFP_KERNEL);
James Smartd2cc9bc2018-09-10 10:30:50 -07006433 if (!dmabuf->virt) {
6434 kfree(dmabuf);
6435 rc = -ENOMEM;
6436 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6437 "6187 DMA Alloc Failed FW logging");
6438 goto free_mem;
6439 }
James Smartd2cc9bc2018-09-10 10:30:50 -07006440 dmabuf->buffer_tag = i;
6441 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6442 }
6443
6444free_mem:
6445 if (rc)
6446 lpfc_sli4_ras_dma_free(phba);
6447
6448 return rc;
6449}
6450
6451/**
6452 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6453 * @phba: pointer to lpfc hba data structure.
6454 * @pmboxq: pointer to the driver internal queue element for mailbox command.
6455 *
6456 * Completion handler for driver's RAS MBX command to the device.
6457 **/
6458static void
6459lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6460{
6461 MAILBOX_t *mb;
6462 union lpfc_sli4_cfg_shdr *shdr;
6463 uint32_t shdr_status, shdr_add_status;
6464 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6465
6466 mb = &pmb->u.mb;
6467
6468 shdr = (union lpfc_sli4_cfg_shdr *)
6469 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6470 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6471 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6472
6473 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
James Smartcb349902018-11-29 16:09:27 -08006474 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smartd2cc9bc2018-09-10 10:30:50 -07006475 "6188 FW LOG mailbox "
6476 "completed with status x%x add_status x%x,"
6477 " mbx status x%x\n",
6478 shdr_status, shdr_add_status, mb->mbxStatus);
James Smartcb349902018-11-29 16:09:27 -08006479
6480 ras_fwlog->ras_hwsupport = false;
James Smartd2cc9bc2018-09-10 10:30:50 -07006481 goto disable_ras;
6482 }
6483
James Smart95bfc6d2019-10-18 14:18:27 -07006484 spin_lock_irq(&phba->hbalock);
6485 ras_fwlog->state = ACTIVE;
6486 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006487 mempool_free(pmb, phba->mbox_mem_pool);
6488
6489 return;
6490
6491disable_ras:
6492 /* Free RAS DMA memory */
6493 lpfc_sli4_ras_dma_free(phba);
6494 mempool_free(pmb, phba->mbox_mem_pool);
6495}
6496
6497/**
6498 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6499 * @phba: pointer to lpfc hba data structure.
6500 * @fwlog_level: Logging verbosity level.
6501 * @fwlog_enable: Enable/Disable logging.
6502 *
6503 * Initialize memory and post mailbox command to enable FW logging in host
6504 * memory.
6505 **/
6506int
6507lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6508 uint32_t fwlog_level,
6509 uint32_t fwlog_enable)
6510{
6511 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6512 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6513 struct lpfc_dmabuf *dmabuf;
6514 LPFC_MBOXQ_t *mbox;
6515 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6516 int rc = 0;
6517
James Smart95bfc6d2019-10-18 14:18:27 -07006518 spin_lock_irq(&phba->hbalock);
6519 ras_fwlog->state = INACTIVE;
6520 spin_unlock_irq(&phba->hbalock);
6521
James Smartd2cc9bc2018-09-10 10:30:50 -07006522 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6523 phba->cfg_ras_fwlog_buffsize);
6524 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6525
6526 /*
6527 * If re-enabling FW logging support use earlier allocated
6528 * DMA buffers while posting MBX command.
6529 **/
6530 if (!ras_fwlog->lwpd.virt) {
6531 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6532 if (rc) {
6533 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smartcb349902018-11-29 16:09:27 -08006534 "6189 FW Log Memory Allocation Failed");
James Smartd2cc9bc2018-09-10 10:30:50 -07006535 return rc;
6536 }
6537 }
6538
6539 /* Setup Mailbox command */
6540 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6541 if (!mbox) {
James Smartcb349902018-11-29 16:09:27 -08006542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006543 "6190 RAS MBX Alloc Failed");
6544 rc = -ENOMEM;
6545 goto mem_free;
6546 }
6547
6548 ras_fwlog->fw_loglevel = fwlog_level;
6549 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6550 sizeof(struct lpfc_sli4_cfg_mhdr));
6551
6552 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6553 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6554 len, LPFC_SLI4_MBX_EMBED);
6555
6556 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6557 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6558 fwlog_enable);
6559 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6560 ras_fwlog->fw_loglevel);
6561 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6562 ras_fwlog->fw_buffcount);
6563 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6564 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6565
6566 /* Update DMA buffer address */
6567 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6568 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6569
6570 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6571 putPaddrLow(dmabuf->phys);
6572
6573 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6574 putPaddrHigh(dmabuf->phys);
6575 }
6576
6577 /* Update LPWD address */
6578 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6579 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6580
James Smart95bfc6d2019-10-18 14:18:27 -07006581 spin_lock_irq(&phba->hbalock);
6582 ras_fwlog->state = REG_INPROGRESS;
6583 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006584 mbox->vport = phba->pport;
6585 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6586
6587 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6588
6589 if (rc == MBX_NOT_FINISHED) {
James Smartcb349902018-11-29 16:09:27 -08006590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6591 "6191 FW-Log Mailbox failed. "
James Smartd2cc9bc2018-09-10 10:30:50 -07006592 "status %d mbxStatus : x%x", rc,
6593 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6594 mempool_free(mbox, phba->mbox_mem_pool);
6595 rc = -EIO;
6596 goto mem_free;
6597 } else
6598 rc = 0;
6599mem_free:
6600 if (rc)
6601 lpfc_sli4_ras_dma_free(phba);
6602
6603 return rc;
6604}
6605
6606/**
6607 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6608 * @phba: Pointer to HBA context object.
6609 *
6610 * Check if RAS is supported on the adapter and initialize it.
6611 **/
6612void
6613lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6614{
6615 /* Check RAS FW Log needs to be enabled or not */
6616 if (lpfc_check_fwlog_support(phba))
6617 return;
6618
6619 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6620 LPFC_RAS_ENABLE_LOGGING);
6621}
6622
6623/**
James Smart6d368e52011-05-24 11:44:12 -04006624 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6625 * @phba: Pointer to HBA context object.
6626 *
6627 * This function allocates all SLI4 resource identifiers.
6628 **/
6629int
6630lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6631{
6632 int i, rc, error = 0;
6633 uint16_t count, base;
6634 unsigned long longs;
6635
James Smartff78d8f2011-12-13 13:21:35 -05006636 if (!phba->sli4_hba.rpi_hdrs_in_use)
6637 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006638 if (phba->sli4_hba.extents_in_use) {
6639 /*
6640 * The port supports resource extents. The XRI, VPI, VFI, RPI
6641 * resource extent count must be read and allocated before
6642 * provisioning the resource id arrays.
6643 */
6644 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6645 LPFC_IDX_RSRC_RDY) {
6646 /*
6647 * Extent-based resources are set - the driver could
6648 * be in a port reset. Figure out if any corrective
6649 * actions need to be taken.
6650 */
6651 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6652 LPFC_RSC_TYPE_FCOE_VFI);
6653 if (rc != 0)
6654 error++;
6655 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6656 LPFC_RSC_TYPE_FCOE_VPI);
6657 if (rc != 0)
6658 error++;
6659 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6660 LPFC_RSC_TYPE_FCOE_XRI);
6661 if (rc != 0)
6662 error++;
6663 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6664 LPFC_RSC_TYPE_FCOE_RPI);
6665 if (rc != 0)
6666 error++;
6667
6668 /*
6669 * It's possible that the number of resources
6670 * provided to this port instance changed between
6671 * resets. Detect this condition and reallocate
6672 * resources. Otherwise, there is no action.
6673 */
6674 if (error) {
6675 lpfc_printf_log(phba, KERN_INFO,
6676 LOG_MBOX | LOG_INIT,
6677 "2931 Detected extent resource "
6678 "change. Reallocating all "
6679 "extents.\n");
6680 rc = lpfc_sli4_dealloc_extent(phba,
6681 LPFC_RSC_TYPE_FCOE_VFI);
6682 rc = lpfc_sli4_dealloc_extent(phba,
6683 LPFC_RSC_TYPE_FCOE_VPI);
6684 rc = lpfc_sli4_dealloc_extent(phba,
6685 LPFC_RSC_TYPE_FCOE_XRI);
6686 rc = lpfc_sli4_dealloc_extent(phba,
6687 LPFC_RSC_TYPE_FCOE_RPI);
6688 } else
6689 return 0;
6690 }
6691
6692 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6693 if (unlikely(rc))
6694 goto err_exit;
6695
6696 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6697 if (unlikely(rc))
6698 goto err_exit;
6699
6700 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6701 if (unlikely(rc))
6702 goto err_exit;
6703
6704 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6705 if (unlikely(rc))
6706 goto err_exit;
6707 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6708 LPFC_IDX_RSRC_RDY);
6709 return rc;
6710 } else {
6711 /*
6712 * The port does not support resource extents. The XRI, VPI,
6713 * VFI, RPI resource ids were determined from READ_CONFIG.
6714 * Just allocate the bitmasks and provision the resource id
6715 * arrays. If a port reset is active, the resources don't
6716 * need any action - just exit.
6717 */
6718 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006719 LPFC_IDX_RSRC_RDY) {
6720 lpfc_sli4_dealloc_resource_identifiers(phba);
6721 lpfc_sli4_remove_rpis(phba);
6722 }
James Smart6d368e52011-05-24 11:44:12 -04006723 /* RPIs. */
6724 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006725 if (count <= 0) {
6726 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6727 "3279 Invalid provisioning of "
6728 "rpi:%d\n", count);
6729 rc = -EINVAL;
6730 goto err_exit;
6731 }
James Smart6d368e52011-05-24 11:44:12 -04006732 base = phba->sli4_hba.max_cfg_param.rpi_base;
6733 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006734 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006735 sizeof(unsigned long),
6736 GFP_KERNEL);
6737 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6738 rc = -ENOMEM;
6739 goto err_exit;
6740 }
Kees Cook6396bb22018-06-12 14:03:40 -07006741 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006742 GFP_KERNEL);
6743 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6744 rc = -ENOMEM;
6745 goto free_rpi_bmask;
6746 }
6747
6748 for (i = 0; i < count; i++)
6749 phba->sli4_hba.rpi_ids[i] = base + i;
6750
6751 /* VPIs. */
6752 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006753 if (count <= 0) {
6754 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6755 "3280 Invalid provisioning of "
6756 "vpi:%d\n", count);
6757 rc = -EINVAL;
6758 goto free_rpi_ids;
6759 }
James Smart6d368e52011-05-24 11:44:12 -04006760 base = phba->sli4_hba.max_cfg_param.vpi_base;
6761 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006762 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006763 GFP_KERNEL);
6764 if (unlikely(!phba->vpi_bmask)) {
6765 rc = -ENOMEM;
6766 goto free_rpi_ids;
6767 }
Kees Cook6396bb22018-06-12 14:03:40 -07006768 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006769 GFP_KERNEL);
6770 if (unlikely(!phba->vpi_ids)) {
6771 rc = -ENOMEM;
6772 goto free_vpi_bmask;
6773 }
6774
6775 for (i = 0; i < count; i++)
6776 phba->vpi_ids[i] = base + i;
6777
6778 /* XRIs. */
6779 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006780 if (count <= 0) {
6781 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6782 "3281 Invalid provisioning of "
6783 "xri:%d\n", count);
6784 rc = -EINVAL;
6785 goto free_vpi_ids;
6786 }
James Smart6d368e52011-05-24 11:44:12 -04006787 base = phba->sli4_hba.max_cfg_param.xri_base;
6788 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006789 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006790 sizeof(unsigned long),
6791 GFP_KERNEL);
6792 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6793 rc = -ENOMEM;
6794 goto free_vpi_ids;
6795 }
James Smart41899be2012-03-01 22:34:19 -05006796 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006797 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006798 GFP_KERNEL);
6799 if (unlikely(!phba->sli4_hba.xri_ids)) {
6800 rc = -ENOMEM;
6801 goto free_xri_bmask;
6802 }
6803
6804 for (i = 0; i < count; i++)
6805 phba->sli4_hba.xri_ids[i] = base + i;
6806
6807 /* VFIs. */
6808 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006809 if (count <= 0) {
6810 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6811 "3282 Invalid provisioning of "
6812 "vfi:%d\n", count);
6813 rc = -EINVAL;
6814 goto free_xri_ids;
6815 }
James Smart6d368e52011-05-24 11:44:12 -04006816 base = phba->sli4_hba.max_cfg_param.vfi_base;
6817 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006818 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006819 sizeof(unsigned long),
6820 GFP_KERNEL);
6821 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6822 rc = -ENOMEM;
6823 goto free_xri_ids;
6824 }
Kees Cook6396bb22018-06-12 14:03:40 -07006825 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006826 GFP_KERNEL);
6827 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6828 rc = -ENOMEM;
6829 goto free_vfi_bmask;
6830 }
6831
6832 for (i = 0; i < count; i++)
6833 phba->sli4_hba.vfi_ids[i] = base + i;
6834
6835 /*
6836 * Mark all resources ready. An HBA reset doesn't need
6837 * to reset the initialization.
6838 */
6839 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6840 LPFC_IDX_RSRC_RDY);
6841 return 0;
6842 }
6843
6844 free_vfi_bmask:
6845 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006846 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006847 free_xri_ids:
6848 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006849 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006850 free_xri_bmask:
6851 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006852 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006853 free_vpi_ids:
6854 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006855 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006856 free_vpi_bmask:
6857 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006858 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006859 free_rpi_ids:
6860 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006861 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006862 free_rpi_bmask:
6863 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006864 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006865 err_exit:
6866 return rc;
6867}
6868
6869/**
6870 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6871 * @phba: Pointer to HBA context object.
6872 *
6873 * This function allocates the number of elements for the specified
6874 * resource type.
6875 **/
6876int
6877lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6878{
6879 if (phba->sli4_hba.extents_in_use) {
6880 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6881 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6882 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6883 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6884 } else {
6885 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006886 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006887 kfree(phba->vpi_ids);
6888 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6889 kfree(phba->sli4_hba.xri_bmask);
6890 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006891 kfree(phba->sli4_hba.vfi_bmask);
6892 kfree(phba->sli4_hba.vfi_ids);
6893 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6894 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6895 }
6896
6897 return 0;
6898}
6899
6900/**
James Smartb76f2dc2011-07-22 18:37:42 -04006901 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6902 * @phba: Pointer to HBA context object.
6903 * @type: The resource extent type.
6904 * @extnt_count: buffer to hold port extent count response
6905 * @extnt_size: buffer to hold port extent size response.
6906 *
6907 * This function calls the port to read the host allocated extents
6908 * for a particular type.
6909 **/
6910int
6911lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6912 uint16_t *extnt_cnt, uint16_t *extnt_size)
6913{
6914 bool emb;
6915 int rc = 0;
6916 uint16_t curr_blks = 0;
6917 uint32_t req_len, emb_len;
6918 uint32_t alloc_len, mbox_tmo;
6919 struct list_head *blk_list_head;
6920 struct lpfc_rsrc_blks *rsrc_blk;
6921 LPFC_MBOXQ_t *mbox;
6922 void *virtaddr = NULL;
6923 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6924 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6925 union lpfc_sli4_cfg_shdr *shdr;
6926
6927 switch (type) {
6928 case LPFC_RSC_TYPE_FCOE_VPI:
6929 blk_list_head = &phba->lpfc_vpi_blk_list;
6930 break;
6931 case LPFC_RSC_TYPE_FCOE_XRI:
6932 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6933 break;
6934 case LPFC_RSC_TYPE_FCOE_VFI:
6935 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6936 break;
6937 case LPFC_RSC_TYPE_FCOE_RPI:
6938 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6939 break;
6940 default:
6941 return -EIO;
6942 }
6943
6944 /* Count the number of extents currently allocatd for this type. */
6945 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6946 if (curr_blks == 0) {
6947 /*
6948 * The GET_ALLOCATED mailbox does not return the size,
6949 * just the count. The size should be just the size
6950 * stored in the current allocated block and all sizes
6951 * for an extent type are the same so set the return
6952 * value now.
6953 */
6954 *extnt_size = rsrc_blk->rsrc_size;
6955 }
6956 curr_blks++;
6957 }
6958
James Smartb76f2dc2011-07-22 18:37:42 -04006959 /*
6960 * Calculate the size of an embedded mailbox. The uint32_t
6961 * accounts for extents-specific word.
6962 */
6963 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6964 sizeof(uint32_t);
6965
6966 /*
6967 * Presume the allocation and response will fit into an embedded
6968 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6969 */
6970 emb = LPFC_SLI4_MBX_EMBED;
6971 req_len = emb_len;
6972 if (req_len > emb_len) {
6973 req_len = curr_blks * sizeof(uint16_t) +
6974 sizeof(union lpfc_sli4_cfg_shdr) +
6975 sizeof(uint32_t);
6976 emb = LPFC_SLI4_MBX_NEMBED;
6977 }
6978
6979 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6980 if (!mbox)
6981 return -ENOMEM;
6982 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6983
6984 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6985 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6986 req_len, emb);
6987 if (alloc_len < req_len) {
6988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6989 "2983 Allocated DMA memory size (x%x) is "
6990 "less than the requested DMA memory "
6991 "size (x%x)\n", alloc_len, req_len);
6992 rc = -ENOMEM;
6993 goto err_exit;
6994 }
6995 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6996 if (unlikely(rc)) {
6997 rc = -EIO;
6998 goto err_exit;
6999 }
7000
7001 if (!phba->sli4_hba.intr_enable)
7002 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7003 else {
James Smarta183a152011-10-10 21:32:43 -04007004 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04007005 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7006 }
7007
7008 if (unlikely(rc)) {
7009 rc = -EIO;
7010 goto err_exit;
7011 }
7012
7013 /*
7014 * Figure out where the response is located. Then get local pointers
7015 * to the response data. The port does not guarantee to respond to
7016 * all extents counts request so update the local variable with the
7017 * allocated count from the port.
7018 */
7019 if (emb == LPFC_SLI4_MBX_EMBED) {
7020 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7021 shdr = &rsrc_ext->header.cfg_shdr;
7022 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7023 } else {
7024 virtaddr = mbox->sge_array->addr[0];
7025 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7026 shdr = &n_rsrc->cfg_shdr;
7027 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7028 }
7029
7030 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
7031 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
7032 "2984 Failed to read allocated resources "
7033 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
7034 type,
7035 bf_get(lpfc_mbox_hdr_status, &shdr->response),
7036 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7037 rc = -EIO;
7038 goto err_exit;
7039 }
7040 err_exit:
7041 lpfc_sli4_mbox_cmd_free(phba, mbox);
7042 return rc;
7043}
7044
7045/**
James Smart0ef69962017-04-21 16:04:50 -07007046 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04007047 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08007048 * @pring: Pointer to driver SLI ring object.
7049 * @sgl_list: linked link of sgl buffers to post
7050 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04007051 *
James Smart895427b2017-02-12 13:52:30 -08007052 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04007053 * repost them to the port by using SGL block post. This is needed after a
7054 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08007055 * of buffer sgls which contains contiguous xris and uses the non-embedded
7056 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04007057 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7058 * mailbox command for posting.
7059 *
7060 * Returns: 0 = success, non-zero failure.
7061 **/
7062static int
James Smart895427b2017-02-12 13:52:30 -08007063lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7064 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04007065{
7066 struct lpfc_sglq *sglq_entry = NULL;
7067 struct lpfc_sglq *sglq_entry_next = NULL;
7068 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08007069 int status, total_cnt;
7070 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04007071 int last_xritag = NO_XRI;
7072 LIST_HEAD(prep_sgl_list);
7073 LIST_HEAD(blck_sgl_list);
7074 LIST_HEAD(allc_sgl_list);
7075 LIST_HEAD(post_sgl_list);
7076 LIST_HEAD(free_sgl_list);
7077
James Smart38c20672013-03-01 16:37:44 -05007078 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007079 spin_lock(&phba->sli4_hba.sgl_list_lock);
7080 list_splice_init(sgl_list, &allc_sgl_list);
7081 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007082 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007083
James Smart895427b2017-02-12 13:52:30 -08007084 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007085 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7086 &allc_sgl_list, list) {
7087 list_del_init(&sglq_entry->list);
7088 block_cnt++;
7089 if ((last_xritag != NO_XRI) &&
7090 (sglq_entry->sli4_xritag != last_xritag + 1)) {
7091 /* a hole in xri block, form a sgl posting block */
7092 list_splice_init(&prep_sgl_list, &blck_sgl_list);
7093 post_cnt = block_cnt - 1;
7094 /* prepare list for next posting block */
7095 list_add_tail(&sglq_entry->list, &prep_sgl_list);
7096 block_cnt = 1;
7097 } else {
7098 /* prepare list for next posting block */
7099 list_add_tail(&sglq_entry->list, &prep_sgl_list);
7100 /* enough sgls for non-embed sgl mbox command */
7101 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7102 list_splice_init(&prep_sgl_list,
7103 &blck_sgl_list);
7104 post_cnt = block_cnt;
7105 block_cnt = 0;
7106 }
7107 }
7108 num_posted++;
7109
7110 /* keep track of last sgl's xritag */
7111 last_xritag = sglq_entry->sli4_xritag;
7112
James Smart895427b2017-02-12 13:52:30 -08007113 /* end of repost sgl list condition for buffers */
7114 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04007115 if (post_cnt == 0) {
7116 list_splice_init(&prep_sgl_list,
7117 &blck_sgl_list);
7118 post_cnt = block_cnt;
7119 } else if (block_cnt == 1) {
7120 status = lpfc_sli4_post_sgl(phba,
7121 sglq_entry->phys, 0,
7122 sglq_entry->sli4_xritag);
7123 if (!status) {
7124 /* successful, put sgl to posted list */
7125 list_add_tail(&sglq_entry->list,
7126 &post_sgl_list);
7127 } else {
7128 /* Failure, put sgl to free list */
7129 lpfc_printf_log(phba, KERN_WARNING,
7130 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007131 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04007132 "sgl, xritag:x%x\n",
7133 sglq_entry->sli4_xritag);
7134 list_add_tail(&sglq_entry->list,
7135 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007136 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04007137 }
7138 }
7139 }
7140
7141 /* continue until a nembed page worth of sgls */
7142 if (post_cnt == 0)
7143 continue;
7144
James Smart895427b2017-02-12 13:52:30 -08007145 /* post the buffer list sgls as a block */
7146 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7147 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04007148
7149 if (!status) {
7150 /* success, put sgl list to posted sgl list */
7151 list_splice_init(&blck_sgl_list, &post_sgl_list);
7152 } else {
7153 /* Failure, put sgl list to free sgl list */
7154 sglq_entry_first = list_first_entry(&blck_sgl_list,
7155 struct lpfc_sglq,
7156 list);
7157 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007158 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04007159 "xritag:x%x-x%x\n",
7160 sglq_entry_first->sli4_xritag,
7161 (sglq_entry_first->sli4_xritag +
7162 post_cnt - 1));
7163 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007164 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007165 }
7166
7167 /* don't reset xirtag due to hole in xri block */
7168 if (block_cnt == 0)
7169 last_xritag = NO_XRI;
7170
James Smart895427b2017-02-12 13:52:30 -08007171 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04007172 post_cnt = 0;
7173 }
7174
James Smart895427b2017-02-12 13:52:30 -08007175 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04007176 lpfc_free_sgl_list(phba, &free_sgl_list);
7177
James Smart895427b2017-02-12 13:52:30 -08007178 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04007179 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05007180 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007181 spin_lock(&phba->sli4_hba.sgl_list_lock);
7182 list_splice_init(&post_sgl_list, sgl_list);
7183 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007184 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007185 } else {
7186 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007187 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04007188 return -EIO;
7189 }
James Smart895427b2017-02-12 13:52:30 -08007190
7191 /* return the number of XRIs actually posted */
7192 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007193}
7194
James Smart0794d602019-01-28 11:14:19 -08007195/**
James Smart5e5b5112019-01-28 11:14:22 -08007196 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
James Smart0794d602019-01-28 11:14:19 -08007197 * @phba: pointer to lpfc hba data structure.
7198 *
7199 * This routine walks the list of nvme buffers that have been allocated and
7200 * repost them to the port by using SGL block post. This is needed after a
7201 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7202 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
James Smart5e5b5112019-01-28 11:14:22 -08007203 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
James Smart0794d602019-01-28 11:14:19 -08007204 *
7205 * Returns: 0 = success, non-zero failure.
7206 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07007207static int
James Smart5e5b5112019-01-28 11:14:22 -08007208lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
James Smart0794d602019-01-28 11:14:19 -08007209{
7210 LIST_HEAD(post_nblist);
7211 int num_posted, rc = 0;
7212
7213 /* get all NVME buffers need to repost to a local list */
James Smart5e5b5112019-01-28 11:14:22 -08007214 lpfc_io_buf_flush(phba, &post_nblist);
James Smart0794d602019-01-28 11:14:19 -08007215
7216 /* post the list of nvme buffer sgls to port if available */
7217 if (!list_empty(&post_nblist)) {
James Smart5e5b5112019-01-28 11:14:22 -08007218 num_posted = lpfc_sli4_post_io_sgl_list(
7219 phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
James Smart0794d602019-01-28 11:14:19 -08007220 /* failed to post any nvme buffer, return error */
7221 if (num_posted == 0)
7222 rc = -EIO;
7223 }
7224 return rc;
7225}
7226
Bart Van Assche3999df72019-03-28 11:06:16 -07007227static void
James Smart61bda8f2016-10-13 15:06:05 -07007228lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7229{
7230 uint32_t len;
7231
7232 len = sizeof(struct lpfc_mbx_set_host_data) -
7233 sizeof(struct lpfc_sli4_cfg_mhdr);
7234 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7235 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7236 LPFC_SLI4_MBX_EMBED);
7237
7238 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08007239 mbox->u.mqe.un.set_host_data.param_len =
7240 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07007241 snprintf(mbox->u.mqe.un.set_host_data.data,
7242 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7243 "Linux %s v"LPFC_DRIVER_VERSION,
7244 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7245}
7246
James Smarta8cf5df2017-05-15 15:20:46 -07007247int
James Smart6c621a22017-05-15 15:20:45 -07007248lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07007249 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07007250{
7251 int rc, i;
7252 struct lpfc_rqe hrqe;
7253 struct lpfc_rqe drqe;
7254 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08007255 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07007256 struct rqb_dmabuf *rqb_buffer;
7257 LIST_HEAD(rqb_buf_list);
7258
James Smart411de512018-01-30 15:58:52 -08007259 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007260 rqbp = hrq->rqbp;
7261 for (i = 0; i < count; i++) {
7262 /* IF RQ is already full, don't bother */
7263 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
7264 break;
7265 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7266 if (!rqb_buffer)
7267 break;
7268 rqb_buffer->hrq = hrq;
7269 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07007270 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07007271 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7272 }
7273 while (!list_empty(&rqb_buf_list)) {
7274 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7275 hbuf.list);
7276
7277 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7278 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7279 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7280 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7281 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7282 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08007283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7284 "6421 Cannot post to HRQ %d: %x %x %x "
7285 "DRQ %x %x\n",
7286 hrq->queue_id,
7287 hrq->host_index,
7288 hrq->hba_index,
7289 hrq->entry_count,
7290 drq->host_index,
7291 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07007292 rqbp->rqb_free_buffer(phba, rqb_buffer);
7293 } else {
7294 list_add_tail(&rqb_buffer->hbuf.list,
7295 &rqbp->rqb_buffer_list);
7296 rqbp->buffer_count++;
7297 }
7298 }
James Smart411de512018-01-30 15:58:52 -08007299 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007300 return 1;
7301}
7302
Dick Kennedyf0020e42020-06-30 14:49:58 -07007303static void lpfc_sli4_dip(struct lpfc_hba *phba)
7304{
7305 uint32_t if_type;
7306
7307 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7308 if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
7309 if_type == LPFC_SLI_INTF_IF_TYPE_6) {
7310 struct lpfc_register reg_data;
7311
7312 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7313 &reg_data.word0))
7314 return;
7315
7316 if (bf_get(lpfc_sliport_status_dip, &reg_data))
7317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7318 "2904 Firmware Dump Image Present"
7319 " on Adapter");
7320 }
7321}
7322
James Smart8a9d2e82012-05-09 21:16:12 -04007323/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08007324 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04007325 * @phba: Pointer to HBA context object.
7326 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08007327 * This function is the main SLI4 device initialization PCI function. This
7328 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04007329 * HBA error attention handler code. Caller is not required to hold any
7330 * locks.
7331 **/
7332int
7333lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7334{
James Smart171f6c42019-11-04 16:57:07 -08007335 int rc, i, cnt, len, dd;
James Smartda0436e2009-05-22 14:51:39 -04007336 LPFC_MBOXQ_t *mboxq;
7337 struct lpfc_mqe *mqe;
7338 uint8_t *vpd;
7339 uint32_t vpd_size;
7340 uint32_t ftr_rsp = 0;
7341 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7342 struct lpfc_vport *vport = phba->pport;
7343 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08007344 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04007345
7346 /* Perform a PCI function reset to start from clean */
7347 rc = lpfc_pci_function_reset(phba);
7348 if (unlikely(rc))
7349 return -ENODEV;
7350
7351 /* Check the HBA Host Status Register for readyness */
7352 rc = lpfc_sli4_post_status_check(phba);
7353 if (unlikely(rc))
7354 return -ENODEV;
7355 else {
7356 spin_lock_irq(&phba->hbalock);
7357 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7358 spin_unlock_irq(&phba->hbalock);
7359 }
7360
Dick Kennedyf0020e42020-06-30 14:49:58 -07007361 lpfc_sli4_dip(phba);
7362
James Smartda0436e2009-05-22 14:51:39 -04007363 /*
7364 * Allocate a single mailbox container for initializing the
7365 * port.
7366 */
7367 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7368 if (!mboxq)
7369 return -ENOMEM;
7370
James Smartda0436e2009-05-22 14:51:39 -04007371 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04007372 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04007373 vpd = kzalloc(vpd_size, GFP_KERNEL);
7374 if (!vpd) {
7375 rc = -ENOMEM;
7376 goto out_free_mbox;
7377 }
7378
7379 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05007380 if (unlikely(rc)) {
7381 kfree(vpd);
7382 goto out_free_mbox;
7383 }
James Smart572709e2013-07-15 18:32:43 -04007384
James Smartda0436e2009-05-22 14:51:39 -04007385 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04007386 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07007387 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05007388 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007389 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7390 } else {
James Smart76a95d72010-11-20 23:11:48 -05007391 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007392 }
James Smart45ed1192009-10-02 15:17:02 -04007393
7394 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7395 LPFC_DCBX_CEE_MODE)
7396 phba->hba_flag |= HBA_FIP_SUPPORT;
7397 else
7398 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7399
James Smartc00f62e2019-08-14 16:57:11 -07007400 phba->hba_flag &= ~HBA_IOQ_FLUSH;
James Smart4f2e66c2012-05-09 21:17:07 -04007401
James Smartc31098c2011-04-16 11:03:33 -04007402 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007403 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7404 "0376 READ_REV Error. SLI Level %d "
7405 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05007406 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04007407 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007408 kfree(vpd);
7409 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007410 }
James Smartcd1c8302011-10-10 21:33:25 -04007411
7412 /*
James Smartff78d8f2011-12-13 13:21:35 -05007413 * Continue initialization with default values even if driver failed
7414 * to read FCoE param config regions, only read parameters if the
7415 * board is FCoE
7416 */
7417 if (phba->hba_flag & HBA_FCOE_MODE &&
7418 lpfc_sli4_read_fcoe_params(phba))
7419 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7420 "2570 Failed to read FCoE parameters\n");
7421
7422 /*
James Smartcd1c8302011-10-10 21:33:25 -04007423 * Retrieve sli4 device physical port name, failure of doing it
7424 * is considered as non-fatal.
7425 */
7426 rc = lpfc_sli4_retrieve_pport_name(phba);
7427 if (!rc)
7428 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7429 "3080 Successful retrieving SLI4 device "
7430 "physical port name: %s.\n", phba->Port);
7431
James Smartb3b4f3e2019-03-12 16:30:23 -07007432 rc = lpfc_sli4_get_ctl_attr(phba);
7433 if (!rc)
7434 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7435 "8351 Successful retrieving SLI4 device "
7436 "CTL ATTR\n");
7437
James Smartda0436e2009-05-22 14:51:39 -04007438 /*
7439 * Evaluate the read rev and vpd data. Populate the driver
7440 * state with the results. If this routine fails, the failure
7441 * is not fatal as the driver will use generic values.
7442 */
7443 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7444 if (unlikely(!rc)) {
7445 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7446 "0377 Error %d parsing vpd. "
7447 "Using defaults.\n", rc);
7448 rc = 0;
7449 }
James Smart76a95d72010-11-20 23:11:48 -05007450 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04007451
James Smartf1126682009-06-10 17:22:44 -04007452 /* Save information as VPD data */
7453 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7454 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08007455
7456 /*
7457 * This is because first G7 ASIC doesn't support the standard
7458 * 0x5a NVME cmd descriptor type/subtype
7459 */
7460 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7461 LPFC_SLI_INTF_IF_TYPE_6) &&
7462 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7463 (phba->vpd.rev.smRev == 0) &&
7464 (phba->cfg_nvme_embed_cmd == 1))
7465 phba->cfg_nvme_embed_cmd = 0;
7466
James Smartf1126682009-06-10 17:22:44 -04007467 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7468 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7469 &mqe->un.read_rev);
7470 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7471 &mqe->un.read_rev);
7472 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7473 &mqe->un.read_rev);
7474 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7475 &mqe->un.read_rev);
7476 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7477 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7478 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7479 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7480 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7481 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7482 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7483 "(%d):0380 READ_REV Status x%x "
7484 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7485 mboxq->vport ? mboxq->vport->vpi : 0,
7486 bf_get(lpfc_mqe_status, mqe),
7487 phba->vpd.rev.opFwName,
7488 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7489 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04007490
James Smart65791f12016-07-06 12:35:56 -07007491 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07007492 LPFC_SLI_INTF_IF_TYPE_0) {
7493 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7494 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7495 if (rc == MBX_SUCCESS) {
7496 phba->hba_flag |= HBA_RECOVERABLE_UE;
7497 /* Set 1Sec interval to detect UE */
7498 phba->eratt_poll_interval = 1;
7499 phba->sli4_hba.ue_to_sr = bf_get(
7500 lpfc_mbx_set_feature_UESR,
7501 &mboxq->u.mqe.un.set_feature);
7502 phba->sli4_hba.ue_to_rp = bf_get(
7503 lpfc_mbx_set_feature_UERP,
7504 &mboxq->u.mqe.un.set_feature);
7505 }
7506 }
7507
7508 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7509 /* Enable MDS Diagnostics only if the SLI Port supports it */
7510 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7511 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7512 if (rc != MBX_SUCCESS)
7513 phba->mds_diags_support = 0;
7514 }
James Smart572709e2013-07-15 18:32:43 -04007515
James Smartda0436e2009-05-22 14:51:39 -04007516 /*
7517 * Discover the port's supported feature set and match it against the
7518 * hosts requests.
7519 */
7520 lpfc_request_features(phba, mboxq);
7521 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7522 if (unlikely(rc)) {
7523 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007524 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007525 }
7526
7527 /*
7528 * The port must support FCP initiator mode as this is the
7529 * only mode running in the host.
7530 */
7531 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7532 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7533 "0378 No support for fcpi mode.\n");
7534 ftr_rsp++;
7535 }
James Smart0bc2b7c2018-02-22 08:18:48 -08007536
7537 /* Performance Hints are ONLY for FCoE */
7538 if (phba->hba_flag & HBA_FCOE_MODE) {
7539 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7540 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7541 else
7542 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7543 }
7544
James Smartda0436e2009-05-22 14:51:39 -04007545 /*
7546 * If the port cannot support the host's requested features
7547 * then turn off the global config parameters to disable the
7548 * feature in the driver. This is not a fatal error.
7549 */
James Smartf44ac122018-03-05 12:04:08 -08007550 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7551 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7552 phba->cfg_enable_bg = 0;
7553 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04007554 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08007555 }
James Smartbf086112011-08-21 21:48:13 -04007556 }
James Smartda0436e2009-05-22 14:51:39 -04007557
7558 if (phba->max_vpi && phba->cfg_enable_npiv &&
7559 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7560 ftr_rsp++;
7561
7562 if (ftr_rsp) {
7563 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7564 "0379 Feature Mismatch Data: x%08x %08x "
7565 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7566 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7567 phba->cfg_enable_npiv, phba->max_vpi);
7568 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7569 phba->cfg_enable_bg = 0;
7570 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7571 phba->cfg_enable_npiv = 0;
7572 }
7573
7574 /* These SLI3 features are assumed in SLI4 */
7575 spin_lock_irq(&phba->hbalock);
7576 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7577 spin_unlock_irq(&phba->hbalock);
7578
James Smart171f6c42019-11-04 16:57:07 -08007579 /* Always try to enable dual dump feature if we can */
7580 lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
7581 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7582 dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
7583 if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
7584 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_INIT,
7585 "6448 Dual Dump is enabled\n");
7586 else
7587 lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
7588 "6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
7589 "rc:x%x dd:x%x\n",
7590 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7591 lpfc_sli_config_mbox_subsys_get(
7592 phba, mboxq),
7593 lpfc_sli_config_mbox_opcode_get(
7594 phba, mboxq),
7595 rc, dd);
James Smart6d368e52011-05-24 11:44:12 -04007596 /*
7597 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7598 * calls depends on these resources to complete port setup.
7599 */
7600 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7601 if (rc) {
7602 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7603 "2920 Failed to alloc Resource IDs "
7604 "rc = x%x\n", rc);
7605 goto out_free_mbox;
7606 }
7607
James Smart61bda8f2016-10-13 15:06:05 -07007608 lpfc_set_host_data(phba, mboxq);
7609
7610 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7611 if (rc) {
7612 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7613 "2134 Failed to set host os driver version %x",
7614 rc);
7615 }
7616
James Smartda0436e2009-05-22 14:51:39 -04007617 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007618 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7619 if (rc) {
7620 phba->link_state = LPFC_HBA_ERROR;
7621 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007622 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007623 }
7624
James Smartda0436e2009-05-22 14:51:39 -04007625 mboxq->vport = vport;
7626 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart3e1f0712018-11-29 16:09:29 -08007627 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04007628 if (rc == MBX_SUCCESS) {
7629 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7630 rc = 0;
7631 }
7632
7633 /*
7634 * This memory was allocated by the lpfc_read_sparam routine. Release
7635 * it to the mbuf pool.
7636 */
7637 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7638 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -08007639 mboxq->ctx_buf = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007640 if (unlikely(rc)) {
7641 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7642 "0382 READ_SPARAM command failed "
7643 "status %d, mbxStatus x%x\n",
7644 rc, bf_get(lpfc_mqe_status, mqe));
7645 phba->link_state = LPFC_HBA_ERROR;
7646 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007647 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007648 }
7649
James Smart05580562011-05-24 11:40:48 -04007650 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007651
7652 /* Update the fc_host data structures with new wwn. */
7653 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7654 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7655
James Smart895427b2017-02-12 13:52:30 -08007656 /* Create all the SLI4 queues */
7657 rc = lpfc_sli4_queue_create(phba);
7658 if (rc) {
7659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7660 "3089 Failed to allocate queues\n");
7661 rc = -ENODEV;
7662 goto out_free_mbox;
7663 }
7664 /* Set up all the queues to the device */
7665 rc = lpfc_sli4_queue_setup(phba);
7666 if (unlikely(rc)) {
7667 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7668 "0381 Error %d during queue setup.\n ", rc);
7669 goto out_stop_timers;
7670 }
7671 /* Initialize the driver internal SLI layer lists. */
7672 lpfc_sli4_setup(phba);
7673 lpfc_sli4_queue_init(phba);
7674
7675 /* update host els xri-sgl sizes and mappings */
7676 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007677 if (unlikely(rc)) {
7678 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7679 "1400 Failed to update xri-sgl size and "
7680 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007681 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007682 }
7683
James Smart8a9d2e82012-05-09 21:16:12 -04007684 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007685 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7686 phba->sli4_hba.els_xri_cnt);
7687 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007688 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7689 "0582 Error %d during els sgl post "
7690 "operation\n", rc);
7691 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007692 goto out_destroy_queue;
7693 }
7694 phba->sli4_hba.els_xri_cnt = rc;
7695
James Smartf358dd02017-02-12 13:52:34 -08007696 if (phba->nvmet_support) {
7697 /* update host nvmet xri-sgl sizes and mappings */
7698 rc = lpfc_sli4_nvmet_sgl_update(phba);
7699 if (unlikely(rc)) {
7700 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7701 "6308 Failed to update nvmet-sgl size "
7702 "and mapping: %d\n", rc);
7703 goto out_destroy_queue;
7704 }
7705
7706 /* register the nvmet sgl pool to the port */
7707 rc = lpfc_sli4_repost_sgl_list(
7708 phba,
7709 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7710 phba->sli4_hba.nvmet_xri_cnt);
7711 if (unlikely(rc < 0)) {
7712 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7713 "3117 Error %d during nvmet "
7714 "sgl post\n", rc);
7715 rc = -ENODEV;
7716 goto out_destroy_queue;
7717 }
7718 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007719
James Smarta5f73372019-09-21 20:58:50 -07007720 /* We allocate an iocbq for every receive context SGL.
7721 * The additional allocation is for abort and ls handling.
7722 */
7723 cnt = phba->sli4_hba.nvmet_xri_cnt +
7724 phba->sli4_hba.max_cfg_param.max_xri;
James Smartf358dd02017-02-12 13:52:34 -08007725 } else {
James Smart0794d602019-01-28 11:14:19 -08007726 /* update host common xri-sgl sizes and mappings */
James Smart5e5b5112019-01-28 11:14:22 -08007727 rc = lpfc_sli4_io_sgl_update(phba);
James Smart895427b2017-02-12 13:52:30 -08007728 if (unlikely(rc)) {
7729 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7730 "6082 Failed to update nvme-sgl size "
7731 "and mapping: %d\n", rc);
7732 goto out_destroy_queue;
7733 }
James Smart6c621a22017-05-15 15:20:45 -07007734
James Smart0794d602019-01-28 11:14:19 -08007735 /* register the allocated common sgl pool to the port */
James Smart5e5b5112019-01-28 11:14:22 -08007736 rc = lpfc_sli4_repost_io_sgl_list(phba);
James Smart0794d602019-01-28 11:14:19 -08007737 if (unlikely(rc)) {
7738 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7739 "6116 Error %d during nvme sgl post "
7740 "operation\n", rc);
7741 /* Some NVME buffers were moved to abort nvme list */
7742 /* A pci function reset will repost them */
7743 rc = -ENODEV;
7744 goto out_destroy_queue;
7745 }
James Smarta5f73372019-09-21 20:58:50 -07007746 /* Each lpfc_io_buf job structure has an iocbq element.
7747 * This cnt provides for abort, els, ct and ls requests.
7748 */
7749 cnt = phba->sli4_hba.max_cfg_param.max_xri;
James Smart11e644e2017-06-15 22:56:48 -07007750 }
7751
7752 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007753 /* Initialize and populate the iocb list per host */
7754 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta5f73372019-09-21 20:58:50 -07007755 "2821 initialize iocb list with %d entries\n",
7756 cnt);
James Smart6c621a22017-05-15 15:20:45 -07007757 rc = lpfc_init_iocb_list(phba, cnt);
7758 if (rc) {
7759 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007760 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007761 goto out_destroy_queue;
7762 }
James Smart8a9d2e82012-05-09 21:16:12 -04007763 }
7764
James Smart11e644e2017-06-15 22:56:48 -07007765 if (phba->nvmet_support)
7766 lpfc_nvmet_create_targetport(phba);
7767
James Smart2d7dbc42017-02-12 13:52:35 -08007768 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007769 /* Post initial buffers to all RQs created */
7770 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7771 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7772 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7773 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7774 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007775 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007776 rqbp->buffer_count = 0;
7777
James Smart2d7dbc42017-02-12 13:52:35 -08007778 lpfc_post_rq_buffer(
7779 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7780 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007781 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007782 }
7783 }
7784
James Smartda0436e2009-05-22 14:51:39 -04007785 /* Post the rpi header region to the device. */
7786 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7787 if (unlikely(rc)) {
7788 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7789 "0393 Error %d during rpi post operation\n",
7790 rc);
7791 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007792 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007793 }
James Smart97f2ecf2012-03-01 22:35:23 -05007794 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007795
James Smart895427b2017-02-12 13:52:30 -08007796 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007797 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007798 /*
7799 * The FC Port needs to register FCFI (index 0)
7800 */
7801 lpfc_reg_fcfi(phba, mboxq);
7802 mboxq->vport = phba->pport;
7803 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7804 if (rc != MBX_SUCCESS)
7805 goto out_unset_queue;
7806 rc = 0;
7807 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7808 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007809 } else {
7810 /* We are a NVME Target mode with MRQ > 1 */
7811
7812 /* First register the FCFI */
7813 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7814 mboxq->vport = phba->pport;
7815 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7816 if (rc != MBX_SUCCESS)
7817 goto out_unset_queue;
7818 rc = 0;
7819 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7820 &mboxq->u.mqe.un.reg_fcfi_mrq);
7821
7822 /* Next register the MRQs */
7823 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7824 mboxq->vport = phba->pport;
7825 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7826 if (rc != MBX_SUCCESS)
7827 goto out_unset_queue;
7828 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007829 }
7830 /* Check if the port is configured to be disabled */
7831 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007832 }
7833
James Smartc4908502019-01-28 11:14:28 -08007834 /* Don't post more new bufs if repost already recovered
7835 * the nvme sgls.
7836 */
7837 if (phba->nvmet_support == 0) {
7838 if (phba->sli4_hba.io_xri_cnt == 0) {
7839 len = lpfc_new_io_buf(
7840 phba, phba->sli4_hba.io_xri_max);
7841 if (len == 0) {
7842 rc = -ENOMEM;
7843 goto out_unset_queue;
7844 }
7845
7846 if (phba->cfg_xri_rebalancing)
7847 lpfc_create_multixri_pools(phba);
7848 }
7849 } else {
7850 phba->cfg_xri_rebalancing = 0;
7851 }
7852
James Smartda0436e2009-05-22 14:51:39 -04007853 /* Allow asynchronous mailbox command to go through */
7854 spin_lock_irq(&phba->hbalock);
7855 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7856 spin_unlock_irq(&phba->hbalock);
7857
7858 /* Post receive buffers to the device */
7859 lpfc_sli4_rb_setup(phba);
7860
James Smartfc2b9892010-02-26 14:15:29 -05007861 /* Reset HBA FCF states after HBA reset */
7862 phba->fcf.fcf_flag = 0;
7863 phba->fcf.current_rec.flag = 0;
7864
James Smartda0436e2009-05-22 14:51:39 -04007865 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007866 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007867 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007868
7869 /* Start heart beat timer */
7870 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007871 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007872 phba->hb_outstanding = 0;
7873 phba->last_completion_time = jiffies;
7874
James Smart32517fc2019-01-28 11:14:33 -08007875 /* start eq_delay heartbeat */
7876 if (phba->cfg_auto_imax)
7877 queue_delayed_work(phba->wq, &phba->eq_delay_work,
7878 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
7879
James Smartda0436e2009-05-22 14:51:39 -04007880 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007881 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007882 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007883
James Smart75baf692010-06-08 18:31:21 -04007884 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7885 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7886 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7887 if (!rc) {
7888 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7889 "2829 This device supports "
7890 "Advanced Error Reporting (AER)\n");
7891 spin_lock_irq(&phba->hbalock);
7892 phba->hba_flag |= HBA_AER_ENABLED;
7893 spin_unlock_irq(&phba->hbalock);
7894 } else {
7895 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7896 "2830 This device does not support "
7897 "Advanced Error Reporting (AER)\n");
7898 phba->cfg_aer_support = 0;
7899 }
James Smart0a96e972011-07-22 18:37:28 -04007900 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007901 }
7902
James Smartda0436e2009-05-22 14:51:39 -04007903 /*
7904 * The port is ready, set the host's link state to LINK_DOWN
7905 * in preparation for link interrupts.
7906 */
James Smartda0436e2009-05-22 14:51:39 -04007907 spin_lock_irq(&phba->hbalock);
7908 phba->link_state = LPFC_LINK_DOWN;
James Smart1dc5ec22018-10-23 13:41:11 -07007909
7910 /* Check if physical ports are trunked */
7911 if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
7912 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
7913 if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
7914 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
7915 if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
7916 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
7917 if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
7918 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04007919 spin_unlock_irq(&phba->hbalock);
James Smart1dc5ec22018-10-23 13:41:11 -07007920
James Smarte8869f52019-03-12 16:30:18 -07007921 /* Arm the CQs and then EQs on device */
7922 lpfc_sli4_arm_cqeq_intr(phba);
7923
7924 /* Indicate device interrupt mode */
7925 phba->sli4_hba.intr_enable = 1;
7926
James Smart026abb82011-12-13 13:20:45 -05007927 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7928 (phba->hba_flag & LINK_DISABLED)) {
7929 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7930 "3103 Adapter Link is disabled.\n");
7931 lpfc_down_link(phba, mboxq);
7932 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7933 if (rc != MBX_SUCCESS) {
7934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7935 "3104 Adapter failed to issue "
7936 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
James Smartc4908502019-01-28 11:14:28 -08007937 goto out_io_buff_free;
James Smart026abb82011-12-13 13:20:45 -05007938 }
7939 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007940 /* don't perform init_link on SLI4 FC port loopback test */
7941 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7942 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7943 if (rc)
James Smartc4908502019-01-28 11:14:28 -08007944 goto out_io_buff_free;
James Smart1b511972011-12-13 13:23:09 -05007945 }
James Smart5350d872011-10-10 21:33:49 -04007946 }
7947 mempool_free(mboxq, phba->mbox_mem_pool);
7948 return rc;
James Smartc4908502019-01-28 11:14:28 -08007949out_io_buff_free:
7950 /* Free allocated IO Buffers */
7951 lpfc_io_free(phba);
James Smart76a95d72010-11-20 23:11:48 -05007952out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007953 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007954 lpfc_sli4_queue_unset(phba);
7955out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007956 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007957 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007958out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007959 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007960out_free_mbox:
7961 mempool_free(mboxq, phba->mbox_mem_pool);
7962 return rc;
7963}
James Smarte59058c2008-08-24 21:49:00 -04007964
7965/**
James Smart3621a712009-04-06 18:47:14 -04007966 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007967 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007968 *
James Smarte59058c2008-08-24 21:49:00 -04007969 * This is the callback function for mailbox timer. The mailbox
7970 * timer is armed when a new mailbox command is issued and the timer
7971 * is deleted when the mailbox complete. The function is called by
7972 * the kernel timer code when a mailbox does not complete within
7973 * expected time. This function wakes up the worker thread to
7974 * process the mailbox timeout and returns. All the processing is
7975 * done by the worker thread function lpfc_mbox_timeout_handler.
7976 **/
dea31012005-04-17 16:05:31 -05007977void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007978lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007979{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007980 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007981 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007982 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007983
James Smart2e0fef82007-06-17 19:56:36 -05007984 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007985 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007986 if (!tmo_posted)
7987 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7988 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7989
James Smart5e9d9b82008-06-14 22:52:53 -04007990 if (!tmo_posted)
7991 lpfc_worker_wake_up(phba);
7992 return;
dea31012005-04-17 16:05:31 -05007993}
7994
James Smarte8d3c3b2013-10-10 12:21:30 -04007995/**
7996 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7997 * are pending
7998 * @phba: Pointer to HBA context object.
7999 *
8000 * This function checks if any mailbox completions are present on the mailbox
8001 * completion queue.
8002 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04008003static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04008004lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
8005{
8006
8007 uint32_t idx;
8008 struct lpfc_queue *mcq;
8009 struct lpfc_mcqe *mcqe;
8010 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08008011 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04008012
8013 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8014 return false;
8015
8016 /* Check for completions on mailbox completion queue */
8017
8018 mcq = phba->sli4_hba.mbx_cq;
8019 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08008020 qe_valid = mcq->qe_valid;
James Smart9afbee32019-03-12 16:30:28 -07008021 while (bf_get_le32(lpfc_cqe_valid,
8022 (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
8023 mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
James Smarte8d3c3b2013-10-10 12:21:30 -04008024 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
8025 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
8026 pending_completions = true;
8027 break;
8028 }
8029 idx = (idx + 1) % mcq->entry_count;
8030 if (mcq->hba_index == idx)
8031 break;
James Smart7365f6f2018-02-22 08:18:46 -08008032
8033 /* if the index wrapped around, toggle the valid bit */
8034 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
8035 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04008036 }
8037 return pending_completions;
8038
8039}
8040
8041/**
8042 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
8043 * that were missed.
8044 * @phba: Pointer to HBA context object.
8045 *
8046 * For sli4, it is possible to miss an interrupt. As such mbox completions
8047 * maybe missed causing erroneous mailbox timeouts to occur. This function
8048 * checks to see if mbox completions are on the mailbox completion queue
8049 * and will process all the completions associated with the eq for the
8050 * mailbox completion queue.
8051 **/
YueHaibingd7b761b2019-05-31 23:28:41 +08008052static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04008053lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
8054{
James Smartb71413d2018-02-22 08:18:40 -08008055 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04008056 uint32_t eqidx;
8057 struct lpfc_queue *fpeq = NULL;
James Smart657add42019-05-21 17:49:06 -07008058 struct lpfc_queue *eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04008059 bool mbox_pending;
8060
8061 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8062 return false;
8063
James Smart657add42019-05-21 17:49:06 -07008064 /* Find the EQ associated with the mbox CQ */
8065 if (sli4_hba->hdwq) {
8066 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
8067 eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
Daniel Wagner535fb492019-10-18 18:21:11 +02008068 if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
James Smart657add42019-05-21 17:49:06 -07008069 fpeq = eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04008070 break;
8071 }
James Smart657add42019-05-21 17:49:06 -07008072 }
8073 }
James Smarte8d3c3b2013-10-10 12:21:30 -04008074 if (!fpeq)
8075 return false;
8076
8077 /* Turn off interrupts from this EQ */
8078
James Smartb71413d2018-02-22 08:18:40 -08008079 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04008080
8081 /* Check to see if a mbox completion is pending */
8082
8083 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
8084
8085 /*
8086 * If a mbox completion is pending, process all the events on EQ
8087 * associated with the mbox completion queue (this could include
8088 * mailbox commands, async events, els commands, receive queue data
8089 * and fcp commands)
8090 */
8091
8092 if (mbox_pending)
James Smart32517fc2019-01-28 11:14:33 -08008093 /* process and rearm the EQ */
James Smart93a4d6f2019-11-04 16:57:05 -08008094 lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
James Smart32517fc2019-01-28 11:14:33 -08008095 else
8096 /* Always clear and re-arm the EQ */
8097 sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04008098
8099 return mbox_pending;
8100
8101}
James Smarte59058c2008-08-24 21:49:00 -04008102
8103/**
James Smart3621a712009-04-06 18:47:14 -04008104 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04008105 * @phba: Pointer to HBA context object.
8106 *
8107 * This function is called from worker thread when a mailbox command times out.
8108 * The caller is not required to hold any locks. This function will reset the
8109 * HBA and recover all the pending commands.
8110 **/
dea31012005-04-17 16:05:31 -05008111void
8112lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
8113{
James Smart2e0fef82007-06-17 19:56:36 -05008114 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04008115 MAILBOX_t *mb = NULL;
8116
James Smart1dcb58e2007-04-25 09:51:30 -04008117 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008118
James Smarte8d3c3b2013-10-10 12:21:30 -04008119 /* If the mailbox completed, process the completion and return */
8120 if (lpfc_sli4_process_missed_mbox_completions(phba))
8121 return;
8122
James Smarteb016562014-09-03 12:58:06 -04008123 if (pmbox != NULL)
8124 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04008125 /* Check the pmbox pointer first. There is a race condition
8126 * between the mbox timeout handler getting executed in the
8127 * worklist and the mailbox actually completing. When this
8128 * race condition occurs, the mbox_active will be NULL.
8129 */
8130 spin_lock_irq(&phba->hbalock);
8131 if (pmbox == NULL) {
8132 lpfc_printf_log(phba, KERN_WARNING,
8133 LOG_MBOX | LOG_SLI,
8134 "0353 Active Mailbox cleared - mailbox timeout "
8135 "exiting\n");
8136 spin_unlock_irq(&phba->hbalock);
8137 return;
8138 }
8139
dea31012005-04-17 16:05:31 -05008140 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05008141 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart32350662019-08-14 16:57:06 -07008142 "0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008143 mb->mbxCommand,
8144 phba->pport->port_state,
8145 phba->sli.sli_flag,
8146 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04008147 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008148
James Smart1dcb58e2007-04-25 09:51:30 -04008149 /* Setting state unknown so lpfc_sli_abort_iocb_ring
8150 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008151 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04008152 */
James Smart2e0fef82007-06-17 19:56:36 -05008153 spin_lock_irq(&phba->pport->work_port_lock);
8154 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8155 spin_unlock_irq(&phba->pport->work_port_lock);
8156 spin_lock_irq(&phba->hbalock);
8157 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04008158 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008159 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04008160
James Smartdb55fba2014-04-04 13:52:02 -04008161 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04008162
8163 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008164 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04008165
8166 /* Reset the HBA device */
8167 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05008168}
8169
James Smarte59058c2008-08-24 21:49:00 -04008170/**
James Smart3772a992009-05-22 14:50:54 -04008171 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04008172 * @phba: Pointer to HBA context object.
8173 * @pmbox: Pointer to mailbox object.
8174 * @flag: Flag indicating how the mailbox need to be processed.
8175 *
8176 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04008177 * to submit a mailbox command to firmware with SLI-3 interface spec. This
8178 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04008179 * The mailbox command can be submitted in polling mode, in which case
8180 * this function will wait in a polling loop for the completion of the
8181 * mailbox.
8182 * If the mailbox is submitted in no_wait mode (not polling) the
8183 * function will submit the command and returns immediately without waiting
8184 * for the mailbox completion. The no_wait is supported only when HBA
8185 * is in SLI2/SLI3 mode - interrupts are enabled.
8186 * The SLI interface allows only one mailbox pending at a time. If the
8187 * mailbox is issued in polling mode and there is already a mailbox
8188 * pending, then the function will return an error. If the mailbox is issued
8189 * in NO_WAIT mode and there is a mailbox pending already, the function
8190 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
8191 * The sli layer owns the mailbox object until the completion of mailbox
8192 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
8193 * return codes the caller owns the mailbox command after the return of
8194 * the function.
8195 **/
James Smart3772a992009-05-22 14:50:54 -04008196static int
8197lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
8198 uint32_t flag)
dea31012005-04-17 16:05:31 -05008199{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008200 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05008201 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008202 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05008203 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008204 int i;
James Smart09372822008-01-11 01:52:54 -05008205 unsigned long timeout;
dea31012005-04-17 16:05:31 -05008206 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04008207 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05008208 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04008209 int processing_queue = 0;
8210
8211 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8212 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04008213 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04008214 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04008215 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8216 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8217 return MBX_SUCCESS;
8218 }
James Smart58da1ff2008-04-07 10:15:56 -04008219 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04008220 pmbox = lpfc_mbox_get(phba);
8221 if (!pmbox) {
8222 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8223 return MBX_SUCCESS;
8224 }
8225 }
dea31012005-04-17 16:05:31 -05008226
James Smarted957682007-06-17 19:56:37 -05008227 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05008228 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05008229 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04008230 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05008231 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05008232 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008233 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04008234 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05008235 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04008236 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05008237 }
8238 }
8239
Linas Vepstas8d63f372007-02-14 14:28:36 -06008240 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04008241 if (unlikely(pci_channel_offline(phba->pcidev))) {
8242 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8243 goto out_not_finished;
8244 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008245
James Smarta257bf92009-04-06 18:48:10 -04008246 /* If HBA has a deferred error attention, fail the iocb. */
8247 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8248 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8249 goto out_not_finished;
8250 }
8251
dea31012005-04-17 16:05:31 -05008252 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05008253
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008254 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05008255 status = MBX_SUCCESS;
8256
James Smart2e0fef82007-06-17 19:56:36 -05008257 if (phba->link_state == LPFC_HBA_ERROR) {
8258 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05008259
8260 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008261 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8262 "(%d):0311 Mailbox command x%x cannot "
8263 "issue Data: x%x x%x\n",
8264 pmbox->vport ? pmbox->vport->vpi : 0,
8265 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008266 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05008267 }
8268
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008269 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05008270 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8271 !(hc_copy & HC_MBINT_ENA)) {
8272 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8273 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04008274 "(%d):2528 Mailbox command x%x cannot "
8275 "issue Data: x%x x%x\n",
8276 pmbox->vport ? pmbox->vport->vpi : 0,
8277 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05008278 goto out_not_finished;
8279 }
James Smart92908312006-03-07 15:04:13 -05008280 }
8281
dea31012005-04-17 16:05:31 -05008282 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8283 /* Polling for a mbox command when another one is already active
8284 * is not allowed in SLI. Also, the driver must have established
8285 * SLI2 mode to queue and process multiple mbox commands.
8286 */
8287
8288 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05008289 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008290
8291 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008292 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8293 "(%d):2529 Mailbox command x%x "
8294 "cannot issue Data: x%x x%x\n",
8295 pmbox->vport ? pmbox->vport->vpi : 0,
8296 pmbox->u.mb.mbxCommand,
8297 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008298 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008299 }
8300
James Smart3772a992009-05-22 14:50:54 -04008301 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05008302 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008303 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008304 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8305 "(%d):2530 Mailbox command x%x "
8306 "cannot issue Data: x%x x%x\n",
8307 pmbox->vport ? pmbox->vport->vpi : 0,
8308 pmbox->u.mb.mbxCommand,
8309 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008310 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008311 }
8312
dea31012005-04-17 16:05:31 -05008313 /* Another mailbox command is still being processed, queue this
8314 * command to be processed later.
8315 */
8316 lpfc_mbox_put(phba, pmbox);
8317
8318 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05008319 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008320 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008321 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008322 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07008323 mbx->mbxCommand,
8324 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008325 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008326
8327 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05008328 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008329
James Smart858c9f62007-06-17 19:56:39 -05008330 if (pmbox->vport) {
8331 lpfc_debugfs_disc_trc(pmbox->vport,
8332 LPFC_DISC_TRC_MBOX_VPORT,
8333 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008334 (uint32_t)mbx->mbxCommand,
8335 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008336 }
8337 else {
8338 lpfc_debugfs_disc_trc(phba->pport,
8339 LPFC_DISC_TRC_MBOX,
8340 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008341 (uint32_t)mbx->mbxCommand,
8342 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008343 }
8344
James Smart2e0fef82007-06-17 19:56:36 -05008345 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05008346 }
8347
dea31012005-04-17 16:05:31 -05008348 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8349
8350 /* If we are not polling, we MUST be in SLI2 mode */
8351 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04008352 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008353 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05008354 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008355 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008356 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008357 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8358 "(%d):2531 Mailbox command x%x "
8359 "cannot issue Data: x%x x%x\n",
8360 pmbox->vport ? pmbox->vport->vpi : 0,
8361 pmbox->u.mb.mbxCommand,
8362 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008363 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008364 }
8365 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04008366 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8367 1000);
8368 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05008369 }
8370
8371 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05008372 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008373 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05008374 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008375 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07008376 mbx->mbxCommand,
8377 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008378 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008379
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008380 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05008381 if (pmbox->vport) {
8382 lpfc_debugfs_disc_trc(pmbox->vport,
8383 LPFC_DISC_TRC_MBOX_VPORT,
8384 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008385 (uint32_t)mbx->mbxCommand,
8386 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008387 }
8388 else {
8389 lpfc_debugfs_disc_trc(phba->pport,
8390 LPFC_DISC_TRC_MBOX,
8391 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008392 (uint32_t)mbx->mbxCommand,
8393 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008394 }
8395 }
8396
dea31012005-04-17 16:05:31 -05008397 psli->slistat.mbox_cmd++;
8398 evtctr = psli->slistat.mbox_event;
8399
8400 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008401 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05008402
James Smart3772a992009-05-22 14:50:54 -04008403 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04008404 /* Populate mbox extension offset word. */
8405 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008406 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008407 = (uint8_t *)phba->mbox_ext
8408 - (uint8_t *)phba->mbox;
8409 }
8410
8411 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008412 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8413 lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8414 (uint8_t *)phba->mbox_ext,
8415 pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008416 }
8417 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008418 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008419 } else {
James Smart7a470272010-03-15 11:25:20 -04008420 /* Populate mbox extension offset word. */
8421 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008422 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008423 = MAILBOX_HBA_EXT_OFFSET;
8424
8425 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008426 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -04008427 lpfc_memcpy_to_slim(phba->MBslimaddr +
8428 MAILBOX_HBA_EXT_OFFSET,
James Smart3e1f0712018-11-29 16:09:29 -08008429 pmbox->ctx_buf, pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008430
James Smart895427b2017-02-12 13:52:30 -08008431 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008432 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08008433 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8434 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008435
8436 /* First copy mbox command data to HBA SLIM, skip past first
8437 word */
8438 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008439 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05008440 MAILBOX_CMD_SIZE - sizeof (uint32_t));
8441
8442 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008443 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05008444 to_slim = phba->MBslimaddr;
8445 writel(ldata, to_slim);
8446 readl(to_slim); /* flush */
8447
James Smart895427b2017-02-12 13:52:30 -08008448 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008449 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04008450 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008451 }
8452
8453 wmb();
dea31012005-04-17 16:05:31 -05008454
8455 switch (flag) {
8456 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05008457 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05008458 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05008459 /* Interrupt board to do it */
8460 writel(CA_MBATT, phba->CAregaddr);
8461 readl(phba->CAregaddr); /* flush */
8462 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05008463 break;
8464
8465 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05008466 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05008467 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05008468 /* Interrupt board to do it */
8469 writel(CA_MBATT, phba->CAregaddr);
8470 readl(phba->CAregaddr); /* flush */
8471
James Smart3772a992009-05-22 14:50:54 -04008472 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008473 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04008474 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008475 word0 = le32_to_cpu(word0);
8476 } else {
8477 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05008478 if (lpfc_readl(phba->MBslimaddr, &word0)) {
8479 spin_unlock_irqrestore(&phba->hbalock,
8480 drvr_flag);
8481 goto out_not_finished;
8482 }
dea31012005-04-17 16:05:31 -05008483 }
8484
8485 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008486 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8487 spin_unlock_irqrestore(&phba->hbalock,
8488 drvr_flag);
8489 goto out_not_finished;
8490 }
James Smarta183a152011-10-10 21:32:43 -04008491 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8492 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05008493 i = 0;
dea31012005-04-17 16:05:31 -05008494 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05008495 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8496 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05008497 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05008498 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05008499 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008500 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05008501 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04008502 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008503 }
8504
8505 /* Check if we took a mbox interrupt while we were
8506 polling */
8507 if (((word0 & OWN_CHIP) != OWN_CHIP)
8508 && (evtctr != psli->slistat.mbox_event))
8509 break;
8510
James Smart09372822008-01-11 01:52:54 -05008511 if (i++ > 10) {
8512 spin_unlock_irqrestore(&phba->hbalock,
8513 drvr_flag);
8514 msleep(1);
8515 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8516 }
dea31012005-04-17 16:05:31 -05008517
James Smart3772a992009-05-22 14:50:54 -04008518 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008519 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04008520 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008521 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008522 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05008523 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04008524 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05008525 /* Check real SLIM for any errors */
8526 slimword0 = readl(phba->MBslimaddr);
8527 slimmb = (MAILBOX_t *) & slimword0;
8528 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8529 && slimmb->mbxStatus) {
8530 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04008531 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008532 word0 = slimword0;
8533 }
8534 }
8535 } else {
8536 /* First copy command data */
8537 word0 = readl(phba->MBslimaddr);
8538 }
8539 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008540 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8541 spin_unlock_irqrestore(&phba->hbalock,
8542 drvr_flag);
8543 goto out_not_finished;
8544 }
dea31012005-04-17 16:05:31 -05008545 }
8546
James Smart3772a992009-05-22 14:50:54 -04008547 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008548 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08008549 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8550 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008551 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008552 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
James Smart7a470272010-03-15 11:25:20 -04008553 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -08008554 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008555 pmbox->out_ext_byte_len);
8556 }
dea31012005-04-17 16:05:31 -05008557 } else {
8558 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008559 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08008560 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008561 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008562 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8563 lpfc_memcpy_from_slim(
8564 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008565 phba->MBslimaddr +
8566 MAILBOX_HBA_EXT_OFFSET,
8567 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05008568 }
8569 }
8570
8571 writel(HA_MBATT, phba->HAregaddr);
8572 readl(phba->HAregaddr); /* flush */
8573
8574 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008575 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05008576 }
8577
James Smart2e0fef82007-06-17 19:56:36 -05008578 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8579 return status;
James Smart58da1ff2008-04-07 10:15:56 -04008580
8581out_not_finished:
8582 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04008583 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04008584 lpfc_mbox_cmpl_put(phba, pmbox);
8585 }
8586 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008587}
8588
James Smarte59058c2008-08-24 21:49:00 -04008589/**
James Smartf1126682009-06-10 17:22:44 -04008590 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8591 * @phba: Pointer to HBA context object.
8592 *
8593 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8594 * the driver internal pending mailbox queue. It will then try to wait out the
8595 * possible outstanding mailbox command before return.
8596 *
8597 * Returns:
8598 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8599 * the outstanding mailbox command timed out.
8600 **/
8601static int
8602lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8603{
8604 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008605 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008606 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008607
8608 /* Mark the asynchronous mailbox command posting as blocked */
8609 spin_lock_irq(&phba->hbalock);
8610 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008611 /* Determine how long we might wait for the active mailbox
8612 * command to be gracefully completed by firmware.
8613 */
James Smarta183a152011-10-10 21:32:43 -04008614 if (phba->sli.mbox_active)
8615 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8616 phba->sli.mbox_active) *
8617 1000) + jiffies;
8618 spin_unlock_irq(&phba->hbalock);
8619
James Smarte8d3c3b2013-10-10 12:21:30 -04008620 /* Make sure the mailbox is really active */
8621 if (timeout)
8622 lpfc_sli4_process_missed_mbox_completions(phba);
8623
James Smartf1126682009-06-10 17:22:44 -04008624 /* Wait for the outstnading mailbox command to complete */
8625 while (phba->sli.mbox_active) {
8626 /* Check active mailbox complete status every 2ms */
8627 msleep(2);
8628 if (time_after(jiffies, timeout)) {
8629 /* Timeout, marked the outstanding cmd not complete */
8630 rc = 1;
8631 break;
8632 }
8633 }
8634
8635 /* Can not cleanly block async mailbox command, fails it */
8636 if (rc) {
8637 spin_lock_irq(&phba->hbalock);
8638 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8639 spin_unlock_irq(&phba->hbalock);
8640 }
8641 return rc;
8642}
8643
8644/**
8645 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8646 * @phba: Pointer to HBA context object.
8647 *
8648 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8649 * commands from the driver internal pending mailbox queue. It makes sure
8650 * that there is no outstanding mailbox command before resuming posting
8651 * asynchronous mailbox commands. If, for any reason, there is outstanding
8652 * mailbox command, it will try to wait it out before resuming asynchronous
8653 * mailbox command posting.
8654 **/
8655static void
8656lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8657{
8658 struct lpfc_sli *psli = &phba->sli;
8659
8660 spin_lock_irq(&phba->hbalock);
8661 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8662 /* Asynchronous mailbox posting is not blocked, do nothing */
8663 spin_unlock_irq(&phba->hbalock);
8664 return;
8665 }
8666
8667 /* Outstanding synchronous mailbox command is guaranteed to be done,
8668 * successful or timeout, after timing-out the outstanding mailbox
8669 * command shall always be removed, so just unblock posting async
8670 * mailbox command and resume
8671 */
8672 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8673 spin_unlock_irq(&phba->hbalock);
8674
Colin Ian King291c2542019-12-18 08:43:01 +00008675 /* wake up worker thread to post asynchronous mailbox command */
James Smartf1126682009-06-10 17:22:44 -04008676 lpfc_worker_wake_up(phba);
8677}
8678
8679/**
James Smart2d843ed2012-09-29 11:29:06 -04008680 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8681 * @phba: Pointer to HBA context object.
8682 * @mboxq: Pointer to mailbox object.
8683 *
8684 * The function waits for the bootstrap mailbox register ready bit from
8685 * port for twice the regular mailbox command timeout value.
8686 *
8687 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8688 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8689 **/
8690static int
8691lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8692{
8693 uint32_t db_ready;
8694 unsigned long timeout;
8695 struct lpfc_register bmbx_reg;
8696
8697 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8698 * 1000) + jiffies;
8699
8700 do {
8701 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8702 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8703 if (!db_ready)
James Smarte2ffe4d2019-03-12 16:30:15 -07008704 mdelay(2);
James Smart2d843ed2012-09-29 11:29:06 -04008705
8706 if (time_after(jiffies, timeout))
8707 return MBXERR_ERROR;
8708 } while (!db_ready);
8709
8710 return 0;
8711}
8712
8713/**
James Smartda0436e2009-05-22 14:51:39 -04008714 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8715 * @phba: Pointer to HBA context object.
8716 * @mboxq: Pointer to mailbox object.
8717 *
8718 * The function posts a mailbox to the port. The mailbox is expected
8719 * to be comletely filled in and ready for the port to operate on it.
8720 * This routine executes a synchronous completion operation on the
8721 * mailbox by polling for its completion.
8722 *
8723 * The caller must not be holding any locks when calling this routine.
8724 *
8725 * Returns:
8726 * MBX_SUCCESS - mailbox posted successfully
8727 * Any of the MBX error values.
8728 **/
8729static int
8730lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8731{
8732 int rc = MBX_SUCCESS;
8733 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008734 uint32_t mcqe_status;
8735 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008736 struct lpfc_sli *psli = &phba->sli;
8737 struct lpfc_mqe *mb = &mboxq->u.mqe;
8738 struct lpfc_bmbx_create *mbox_rgn;
8739 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008740
8741 /*
8742 * Only one mailbox can be active to the bootstrap mailbox region
8743 * at a time and there is no queueing provided.
8744 */
8745 spin_lock_irqsave(&phba->hbalock, iflag);
8746 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8747 spin_unlock_irqrestore(&phba->hbalock, iflag);
8748 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008749 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008750 "cannot issue Data: x%x x%x\n",
8751 mboxq->vport ? mboxq->vport->vpi : 0,
8752 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008753 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8754 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008755 psli->sli_flag, MBX_POLL);
8756 return MBXERR_ERROR;
8757 }
8758 /* The server grabs the token and owns it until release */
8759 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8760 phba->sli.mbox_active = mboxq;
8761 spin_unlock_irqrestore(&phba->hbalock, iflag);
8762
James Smart2d843ed2012-09-29 11:29:06 -04008763 /* wait for bootstrap mbox register for readyness */
8764 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8765 if (rc)
8766 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008767 /*
8768 * Initialize the bootstrap memory region to avoid stale data areas
8769 * in the mailbox post. Then copy the caller's mailbox contents to
8770 * the bmbx mailbox region.
8771 */
8772 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8773 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07008774 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8775 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008776
8777 /* Post the high mailbox dma address to the port and wait for ready. */
8778 dma_address = &phba->sli4_hba.bmbx.dma_address;
8779 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8780
James Smart2d843ed2012-09-29 11:29:06 -04008781 /* wait for bootstrap mbox register for hi-address write done */
8782 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8783 if (rc)
8784 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008785
8786 /* Post the low mailbox dma address to the port. */
8787 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008788
James Smart2d843ed2012-09-29 11:29:06 -04008789 /* wait for bootstrap mbox register for low address write done */
8790 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8791 if (rc)
8792 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008793
8794 /*
8795 * Read the CQ to ensure the mailbox has completed.
8796 * If so, update the mailbox status so that the upper layers
8797 * can complete the request normally.
8798 */
James Smart48f8fdb2018-05-04 20:37:51 -07008799 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8800 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008801 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07008802 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8803 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04008804 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008805 /*
8806 * When the CQE status indicates a failure and the mailbox status
8807 * indicates success then copy the CQE status into the mailbox status
8808 * (and prefix it with x4000).
8809 */
James Smartda0436e2009-05-22 14:51:39 -04008810 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008811 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8812 bf_set(lpfc_mqe_status, mb,
8813 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008814 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008815 } else
8816 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008817
8818 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008819 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008820 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8821 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008822 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8823 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8824 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008825 bf_get(lpfc_mqe_status, mb),
8826 mb->un.mb_words[0], mb->un.mb_words[1],
8827 mb->un.mb_words[2], mb->un.mb_words[3],
8828 mb->un.mb_words[4], mb->un.mb_words[5],
8829 mb->un.mb_words[6], mb->un.mb_words[7],
8830 mb->un.mb_words[8], mb->un.mb_words[9],
8831 mb->un.mb_words[10], mb->un.mb_words[11],
8832 mb->un.mb_words[12], mboxq->mcqe.word0,
8833 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8834 mboxq->mcqe.trailer);
8835exit:
8836 /* We are holding the token, no needed for lock when release */
8837 spin_lock_irqsave(&phba->hbalock, iflag);
8838 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8839 phba->sli.mbox_active = NULL;
8840 spin_unlock_irqrestore(&phba->hbalock, iflag);
8841 return rc;
8842}
8843
8844/**
8845 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8846 * @phba: Pointer to HBA context object.
8847 * @pmbox: Pointer to mailbox object.
8848 * @flag: Flag indicating how the mailbox need to be processed.
8849 *
8850 * This function is called by discovery code and HBA management code to submit
8851 * a mailbox command to firmware with SLI-4 interface spec.
8852 *
8853 * Return codes the caller owns the mailbox command after the return of the
8854 * function.
8855 **/
8856static int
8857lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8858 uint32_t flag)
8859{
8860 struct lpfc_sli *psli = &phba->sli;
8861 unsigned long iflags;
8862 int rc;
8863
James Smartb76f2dc2011-07-22 18:37:42 -04008864 /* dump from issue mailbox command if setup */
8865 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8866
James Smart8fa38512009-07-19 10:01:03 -04008867 rc = lpfc_mbox_dev_check(phba);
8868 if (unlikely(rc)) {
8869 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008870 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008871 "cannot issue Data: x%x x%x\n",
8872 mboxq->vport ? mboxq->vport->vpi : 0,
8873 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008874 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8875 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008876 psli->sli_flag, flag);
8877 goto out_not_finished;
8878 }
8879
James Smartda0436e2009-05-22 14:51:39 -04008880 /* Detect polling mode and jump to a handler */
8881 if (!phba->sli4_hba.intr_enable) {
8882 if (flag == MBX_POLL)
8883 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8884 else
8885 rc = -EIO;
8886 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008887 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008888 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008889 "(x%x/x%x) failure: "
8890 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8891 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008892 mboxq->vport ? mboxq->vport->vpi : 0,
8893 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008894 lpfc_sli_config_mbox_subsys_get(phba,
8895 mboxq),
8896 lpfc_sli_config_mbox_opcode_get(phba,
8897 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008898 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8899 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8900 bf_get(lpfc_mcqe_ext_status,
8901 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008902 psli->sli_flag, flag);
8903 return rc;
8904 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008905 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8906 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008907 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008908 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008909 mboxq->vport ? mboxq->vport->vpi : 0,
8910 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008911 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8912 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008913 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008914 /* Try to block the asynchronous mailbox posting */
8915 rc = lpfc_sli4_async_mbox_block(phba);
8916 if (!rc) {
8917 /* Successfully blocked, now issue sync mbox cmd */
8918 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8919 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008920 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008921 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008922 "(%d):2597 Sync Mailbox command "
8923 "x%x (x%x/x%x) failure: "
8924 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8925 "Data: x%x x%x\n,",
8926 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008927 mboxq->u.mb.mbxCommand,
8928 lpfc_sli_config_mbox_subsys_get(phba,
8929 mboxq),
8930 lpfc_sli_config_mbox_opcode_get(phba,
8931 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008932 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8933 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8934 bf_get(lpfc_mcqe_ext_status,
8935 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008936 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008937 /* Unblock the async mailbox posting afterward */
8938 lpfc_sli4_async_mbox_unblock(phba);
8939 }
8940 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008941 }
8942
Colin Ian King291c2542019-12-18 08:43:01 +00008943 /* Now, interrupt mode asynchronous mailbox command */
James Smartda0436e2009-05-22 14:51:39 -04008944 rc = lpfc_mbox_cmd_check(phba, mboxq);
8945 if (rc) {
8946 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008947 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008948 "cannot issue Data: x%x x%x\n",
8949 mboxq->vport ? mboxq->vport->vpi : 0,
8950 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008951 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8952 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008953 psli->sli_flag, flag);
8954 goto out_not_finished;
8955 }
James Smartda0436e2009-05-22 14:51:39 -04008956
8957 /* Put the mailbox command to the driver internal FIFO */
8958 psli->slistat.mbox_busy++;
8959 spin_lock_irqsave(&phba->hbalock, iflags);
8960 lpfc_mbox_put(phba, mboxq);
8961 spin_unlock_irqrestore(&phba->hbalock, iflags);
8962 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8963 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008964 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008965 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8966 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008967 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8968 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008969 phba->pport->port_state,
8970 psli->sli_flag, MBX_NOWAIT);
8971 /* Wake up worker thread to transport mailbox command from head */
8972 lpfc_worker_wake_up(phba);
8973
8974 return MBX_BUSY;
8975
8976out_not_finished:
8977 return MBX_NOT_FINISHED;
8978}
8979
8980/**
8981 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8982 * @phba: Pointer to HBA context object.
8983 *
8984 * This function is called by worker thread to send a mailbox command to
8985 * SLI4 HBA firmware.
8986 *
8987 **/
8988int
8989lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8990{
8991 struct lpfc_sli *psli = &phba->sli;
8992 LPFC_MBOXQ_t *mboxq;
8993 int rc = MBX_SUCCESS;
8994 unsigned long iflags;
8995 struct lpfc_mqe *mqe;
8996 uint32_t mbx_cmnd;
8997
8998 /* Check interrupt mode before post async mailbox command */
8999 if (unlikely(!phba->sli4_hba.intr_enable))
9000 return MBX_NOT_FINISHED;
9001
9002 /* Check for mailbox command service token */
9003 spin_lock_irqsave(&phba->hbalock, iflags);
9004 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9005 spin_unlock_irqrestore(&phba->hbalock, iflags);
9006 return MBX_NOT_FINISHED;
9007 }
9008 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9009 spin_unlock_irqrestore(&phba->hbalock, iflags);
9010 return MBX_NOT_FINISHED;
9011 }
9012 if (unlikely(phba->sli.mbox_active)) {
9013 spin_unlock_irqrestore(&phba->hbalock, iflags);
9014 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
9015 "0384 There is pending active mailbox cmd\n");
9016 return MBX_NOT_FINISHED;
9017 }
9018 /* Take the mailbox command service token */
9019 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9020
9021 /* Get the next mailbox command from head of queue */
9022 mboxq = lpfc_mbox_get(phba);
9023
9024 /* If no more mailbox command waiting for post, we're done */
9025 if (!mboxq) {
9026 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9027 spin_unlock_irqrestore(&phba->hbalock, iflags);
9028 return MBX_SUCCESS;
9029 }
9030 phba->sli.mbox_active = mboxq;
9031 spin_unlock_irqrestore(&phba->hbalock, iflags);
9032
9033 /* Check device readiness for posting mailbox command */
9034 rc = lpfc_mbox_dev_check(phba);
9035 if (unlikely(rc))
9036 /* Driver clean routine will clean up pending mailbox */
9037 goto out_not_finished;
9038
9039 /* Prepare the mbox command to be posted */
9040 mqe = &mboxq->u.mqe;
9041 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
9042
9043 /* Start timer for the mbox_tmo and log some mailbox post messages */
9044 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04009045 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04009046
9047 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04009048 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04009049 "x%x x%x\n",
9050 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
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 Smartda0436e2009-05-22 14:51:39 -04009053 phba->pport->port_state, psli->sli_flag);
9054
9055 if (mbx_cmnd != MBX_HEARTBEAT) {
9056 if (mboxq->vport) {
9057 lpfc_debugfs_disc_trc(mboxq->vport,
9058 LPFC_DISC_TRC_MBOX_VPORT,
9059 "MBOX Send vport: cmd:x%x mb:x%x x%x",
9060 mbx_cmnd, mqe->un.mb_words[0],
9061 mqe->un.mb_words[1]);
9062 } else {
9063 lpfc_debugfs_disc_trc(phba->pport,
9064 LPFC_DISC_TRC_MBOX,
9065 "MBOX Send: cmd:x%x mb:x%x x%x",
9066 mbx_cmnd, mqe->un.mb_words[0],
9067 mqe->un.mb_words[1]);
9068 }
9069 }
9070 psli->slistat.mbox_cmd++;
9071
9072 /* Post the mailbox command to the port */
9073 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
9074 if (rc != MBX_SUCCESS) {
9075 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04009076 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04009077 "cannot issue Data: x%x x%x\n",
9078 mboxq->vport ? mboxq->vport->vpi : 0,
9079 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009080 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9081 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009082 psli->sli_flag, MBX_NOWAIT);
9083 goto out_not_finished;
9084 }
9085
9086 return rc;
9087
9088out_not_finished:
9089 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05009090 if (phba->sli.mbox_active) {
9091 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9092 __lpfc_mbox_cmpl_put(phba, mboxq);
9093 /* Release the token */
9094 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9095 phba->sli.mbox_active = NULL;
9096 }
James Smartda0436e2009-05-22 14:51:39 -04009097 spin_unlock_irqrestore(&phba->hbalock, iflags);
9098
9099 return MBX_NOT_FINISHED;
9100}
9101
9102/**
9103 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
9104 * @phba: Pointer to HBA context object.
9105 * @pmbox: Pointer to mailbox object.
9106 * @flag: Flag indicating how the mailbox need to be processed.
9107 *
9108 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
9109 * the API jump table function pointer from the lpfc_hba struct.
9110 *
9111 * Return codes the caller owns the mailbox command after the return of the
9112 * function.
9113 **/
9114int
9115lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
9116{
9117 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
9118}
9119
9120/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009121 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04009122 * @phba: The hba struct for which this call is being executed.
9123 * @dev_grp: The HBA PCI-Device group number.
9124 *
9125 * This routine sets up the mbox interface API function jump table in @phba
9126 * struct.
9127 * Returns: 0 - success, -ENODEV - failure.
9128 **/
9129int
9130lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9131{
9132
9133 switch (dev_grp) {
9134 case LPFC_PCI_DEV_LP:
9135 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
9136 phba->lpfc_sli_handle_slow_ring_event =
9137 lpfc_sli_handle_slow_ring_event_s3;
9138 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
9139 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
9140 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
9141 break;
9142 case LPFC_PCI_DEV_OC:
9143 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
9144 phba->lpfc_sli_handle_slow_ring_event =
9145 lpfc_sli_handle_slow_ring_event_s4;
9146 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
9147 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
9148 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
9149 break;
9150 default:
9151 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9152 "1420 Invalid HBA PCI-device group: 0x%x\n",
9153 dev_grp);
9154 return -ENODEV;
9155 break;
9156 }
9157 return 0;
9158}
9159
9160/**
James Smart3621a712009-04-06 18:47:14 -04009161 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04009162 * @phba: Pointer to HBA context object.
9163 * @pring: Pointer to driver SLI ring object.
9164 * @piocb: Pointer to address of newly added command iocb.
9165 *
James Smart27f3efd2019-10-18 14:18:19 -07009166 * This function is called with hbalock held for SLI3 ports or
9167 * the ring lock held for SLI4 ports to add a command
James Smarte59058c2008-08-24 21:49:00 -04009168 * iocb to the txq when SLI layer cannot submit the command iocb
9169 * to the ring.
9170 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009171void
James Smart92d7f7b2007-06-17 19:56:38 -05009172__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009173 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05009174{
James Smart27f3efd2019-10-18 14:18:19 -07009175 if (phba->sli_rev == LPFC_SLI_REV4)
9176 lockdep_assert_held(&pring->ring_lock);
9177 else
9178 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009179 /* Insert the caller's iocb in the txq tail for later processing. */
9180 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05009181}
9182
James Smarte59058c2008-08-24 21:49:00 -04009183/**
James Smart3621a712009-04-06 18:47:14 -04009184 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04009185 * @phba: Pointer to HBA context object.
9186 * @pring: Pointer to driver SLI ring object.
9187 * @piocb: Pointer to address of newly added command iocb.
9188 *
9189 * This function is called with hbalock held before a new
9190 * iocb is submitted to the firmware. This function checks
9191 * txq to flush the iocbs in txq to Firmware before
9192 * submitting new iocbs to the Firmware.
9193 * If there are iocbs in the txq which need to be submitted
9194 * to firmware, lpfc_sli_next_iocb returns the first element
9195 * of the txq after dequeuing it from txq.
9196 * If there is no iocb in the txq then the function will return
9197 * *piocb and *piocb is set to NULL. Caller needs to check
9198 * *piocb to find if there are more commands in the txq.
9199 **/
dea31012005-04-17 16:05:31 -05009200static struct lpfc_iocbq *
9201lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009202 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05009203{
9204 struct lpfc_iocbq * nextiocb;
9205
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009206 lockdep_assert_held(&phba->hbalock);
9207
dea31012005-04-17 16:05:31 -05009208 nextiocb = lpfc_sli_ringtx_get(phba, pring);
9209 if (!nextiocb) {
9210 nextiocb = *piocb;
9211 *piocb = NULL;
9212 }
9213
9214 return nextiocb;
9215}
9216
James Smarte59058c2008-08-24 21:49:00 -04009217/**
James Smart3772a992009-05-22 14:50:54 -04009218 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009219 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04009220 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04009221 * @piocb: Pointer to command iocb.
9222 * @flag: Flag indicating if this command can be put into txq.
9223 *
James Smart3772a992009-05-22 14:50:54 -04009224 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
9225 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
9226 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
9227 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
9228 * this function allows only iocbs for posting buffers. This function finds
9229 * next available slot in the command ring and posts the command to the
9230 * available slot and writes the port attention register to request HBA start
9231 * processing new iocb. If there is no slot available in the ring and
9232 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9233 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04009234 *
James Smart3772a992009-05-22 14:50:54 -04009235 * This function is called with hbalock held. The function will return success
9236 * after it successfully submit the iocb to firmware or after adding to the
9237 * txq.
James Smarte59058c2008-08-24 21:49:00 -04009238 **/
James Smart98c9ea52007-10-27 13:37:33 -04009239static int
James Smart3772a992009-05-22 14:50:54 -04009240__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05009241 struct lpfc_iocbq *piocb, uint32_t flag)
9242{
9243 struct lpfc_iocbq *nextiocb;
9244 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08009245 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05009246
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009247 lockdep_assert_held(&phba->hbalock);
9248
James Smart92d7f7b2007-06-17 19:56:38 -05009249 if (piocb->iocb_cmpl && (!piocb->vport) &&
9250 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9251 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
9252 lpfc_printf_log(phba, KERN_ERR,
9253 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04009254 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05009255 piocb->iocb.ulpCommand);
9256 dump_stack();
9257 return IOCB_ERROR;
9258 }
9259
9260
Linas Vepstas8d63f372007-02-14 14:28:36 -06009261 /* If the PCI channel is in offline state, do not post iocbs. */
9262 if (unlikely(pci_channel_offline(phba->pcidev)))
9263 return IOCB_ERROR;
9264
James Smarta257bf92009-04-06 18:48:10 -04009265 /* If HBA has a deferred error attention, fail the iocb. */
9266 if (unlikely(phba->hba_flag & DEFER_ERATT))
9267 return IOCB_ERROR;
9268
dea31012005-04-17 16:05:31 -05009269 /*
9270 * We should never get an IOCB if we are in a < LINK_DOWN state
9271 */
James Smart2e0fef82007-06-17 19:56:36 -05009272 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05009273 return IOCB_ERROR;
9274
9275 /*
9276 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04009277 * outstanding event.
dea31012005-04-17 16:05:31 -05009278 */
James Smart0b727fe2007-10-27 13:37:25 -04009279 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05009280 goto iocb_busy;
9281
James Smart2e0fef82007-06-17 19:56:36 -05009282 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05009283 /*
James Smart2680eea2007-04-25 09:52:55 -04009284 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05009285 * can be issued if the link is not up.
9286 */
9287 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04009288 case CMD_GEN_REQUEST64_CR:
9289 case CMD_GEN_REQUEST64_CX:
9290 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9291 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04009292 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04009293 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9294 MENLO_TRANSPORT_TYPE))
9295
9296 goto iocb_busy;
9297 break;
dea31012005-04-17 16:05:31 -05009298 case CMD_QUE_RING_BUF_CN:
9299 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05009300 /*
9301 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9302 * completion, iocb_cmpl MUST be 0.
9303 */
9304 if (piocb->iocb_cmpl)
9305 piocb->iocb_cmpl = NULL;
9306 /*FALLTHROUGH*/
9307 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04009308 case CMD_CLOSE_XRI_CN:
9309 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05009310 break;
9311 default:
9312 goto iocb_busy;
9313 }
9314
9315 /*
9316 * For FCP commands, we must be in a state where we can process link
9317 * attention events.
9318 */
James Smart895427b2017-02-12 13:52:30 -08009319 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05009320 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05009321 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05009322 }
dea31012005-04-17 16:05:31 -05009323
dea31012005-04-17 16:05:31 -05009324 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9325 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9326 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9327
9328 if (iocb)
9329 lpfc_sli_update_ring(phba, pring);
9330 else
9331 lpfc_sli_update_full_ring(phba, pring);
9332
9333 if (!piocb)
9334 return IOCB_SUCCESS;
9335
9336 goto out_busy;
9337
9338 iocb_busy:
9339 pring->stats.iocb_cmd_delay++;
9340
9341 out_busy:
9342
9343 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009344 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05009345 return IOCB_SUCCESS;
9346 }
9347
9348 return IOCB_BUSY;
9349}
9350
James Smart3772a992009-05-22 14:50:54 -04009351/**
James Smart4f774512009-05-22 14:52:35 -04009352 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9353 * @phba: Pointer to HBA context object.
9354 * @piocb: Pointer to command iocb.
9355 * @sglq: Pointer to the scatter gather queue object.
9356 *
9357 * This routine converts the bpl or bde that is in the IOCB
9358 * to a sgl list for the sli4 hardware. The physical address
9359 * of the bpl/bde is converted back to a virtual address.
9360 * If the IOCB contains a BPL then the list of BDE's is
9361 * converted to sli4_sge's. If the IOCB contains a single
9362 * BDE then it is converted to a single sli_sge.
9363 * The IOCB is still in cpu endianess so the contents of
9364 * the bpl can be used without byte swapping.
9365 *
9366 * Returns valid XRI = Success, NO_XRI = Failure.
9367**/
9368static uint16_t
9369lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9370 struct lpfc_sglq *sglq)
9371{
9372 uint16_t xritag = NO_XRI;
9373 struct ulp_bde64 *bpl = NULL;
9374 struct ulp_bde64 bde;
9375 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05009376 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04009377 IOCB_t *icmd;
9378 int numBdes = 0;
9379 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05009380 uint32_t offset = 0; /* accumulated offset in the sg request list */
9381 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04009382
9383 if (!piocbq || !sglq)
9384 return xritag;
9385
9386 sgl = (struct sli4_sge *)sglq->sgl;
9387 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05009388 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9389 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009390 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9391 numBdes = icmd->un.genreq64.bdl.bdeSize /
9392 sizeof(struct ulp_bde64);
9393 /* The addrHigh and addrLow fields within the IOCB
9394 * have not been byteswapped yet so there is no
9395 * need to swap them back.
9396 */
James Smart1b511972011-12-13 13:23:09 -05009397 if (piocbq->context3)
9398 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9399 else
9400 return xritag;
James Smart4f774512009-05-22 14:52:35 -04009401
James Smart1b511972011-12-13 13:23:09 -05009402 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04009403 if (!bpl)
9404 return xritag;
9405
9406 for (i = 0; i < numBdes; i++) {
9407 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05009408 sgl->addr_hi = bpl->addrHigh;
9409 sgl->addr_lo = bpl->addrLow;
9410
James Smart05580562011-05-24 11:40:48 -04009411 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009412 if ((i+1) == numBdes)
9413 bf_set(lpfc_sli4_sge_last, sgl, 1);
9414 else
9415 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05009416 /* swap the size field back to the cpu so we
9417 * can assign it to the sgl.
9418 */
9419 bde.tus.w = le32_to_cpu(bpl->tus.w);
9420 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05009421 /* The offsets in the sgl need to be accumulated
9422 * separately for the request and reply lists.
9423 * The request is always first, the reply follows.
9424 */
9425 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9426 /* add up the reply sg entries */
9427 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9428 inbound++;
9429 /* first inbound? reset the offset */
9430 if (inbound == 1)
9431 offset = 0;
9432 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04009433 bf_set(lpfc_sli4_sge_type, sgl,
9434 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05009435 offset += bde.tus.f.bdeSize;
9436 }
James Smart546fc852011-03-11 16:06:29 -05009437 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009438 bpl++;
9439 sgl++;
9440 }
9441 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9442 /* The addrHigh and addrLow fields of the BDE have not
9443 * been byteswapped yet so they need to be swapped
9444 * before putting them in the sgl.
9445 */
9446 sgl->addr_hi =
9447 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9448 sgl->addr_lo =
9449 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04009450 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009451 bf_set(lpfc_sli4_sge_last, sgl, 1);
9452 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05009453 sgl->sge_len =
9454 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04009455 }
9456 return sglq->sli4_xritag;
9457}
9458
9459/**
James Smart4f774512009-05-22 14:52:35 -04009460 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9461 * @phba: Pointer to HBA context object.
9462 * @piocb: Pointer to command iocb.
9463 * @wqe: Pointer to the work queue entry.
9464 *
9465 * This routine converts the iocb command to its Work Queue Entry
9466 * equivalent. The wqe pointer should not have any fields set when
9467 * this routine is called because it will memcpy over them.
9468 * This routine does not set the CQ_ID or the WQEC bits in the
9469 * wqe.
9470 *
9471 * Returns: 0 = Success, IOCB_ERROR = Failure.
9472 **/
9473static int
9474lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08009475 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04009476{
James Smart5ffc2662009-11-18 15:39:44 -05009477 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04009478 uint8_t ct = 0;
9479 uint32_t fip;
9480 uint32_t abort_tag;
9481 uint8_t command_type = ELS_COMMAND_NON_FIP;
9482 uint8_t cmnd;
9483 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04009484 uint16_t abrt_iotag;
9485 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04009486 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04009487 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05009488 int numBdes, i;
9489 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04009490 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05009491 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05009492 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04009493
James Smart45ed1192009-10-02 15:17:02 -04009494 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04009495 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04009496 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04009497 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05009498 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04009499 command_type = ELS_COMMAND_FIP;
9500 else
9501 command_type = ELS_COMMAND_NON_FIP;
9502
James Smartb5c53952016-03-31 14:12:30 -07009503 if (phba->fcp_embed_io)
9504 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04009505 /* Some of the fields are in the right position already */
9506 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smarte62245d2019-08-14 16:57:08 -07009507 /* The ct field has moved so reset */
9508 wqe->generic.wqe_com.word7 = 0;
9509 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07009510
9511 abort_tag = (uint32_t) iocbq->iotag;
9512 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009513 /* words0-2 bpl convert bde */
9514 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05009515 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9516 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04009517 bpl = (struct ulp_bde64 *)
9518 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9519 if (!bpl)
9520 return IOCB_ERROR;
9521
9522 /* Should already be byte swapped. */
9523 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
9524 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
9525 /* swap the size field back to the cpu so we
9526 * can assign it to the sgl.
9527 */
9528 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05009529 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9530 total_len = 0;
9531 for (i = 0; i < numBdes; i++) {
9532 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9533 total_len += bde.tus.f.bdeSize;
9534 }
James Smart4f774512009-05-22 14:52:35 -04009535 } else
James Smart5ffc2662009-11-18 15:39:44 -05009536 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04009537
9538 iocbq->iocb.ulpIoTag = iocbq->iotag;
9539 cmnd = iocbq->iocb.ulpCommand;
9540
9541 switch (iocbq->iocb.ulpCommand) {
9542 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04009543 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9544 ndlp = iocbq->context_un.ndlp;
9545 else
9546 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009547 if (!iocbq->iocb.ulpLe) {
9548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9549 "2007 Only Limited Edition cmd Format"
9550 " supported 0x%x\n",
9551 iocbq->iocb.ulpCommand);
9552 return IOCB_ERROR;
9553 }
James Smartff78d8f2011-12-13 13:21:35 -05009554
James Smart5ffc2662009-11-18 15:39:44 -05009555 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009556 /* Els_reguest64 has a TMO */
9557 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9558 iocbq->iocb.ulpTimeout);
9559 /* Need a VF for word 4 set the vf bit*/
9560 bf_set(els_req64_vf, &wqe->els_req, 0);
9561 /* And a VFID for word 12 */
9562 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04009563 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04009564 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9565 iocbq->iocb.ulpContext);
9566 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9567 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04009568 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05009569 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05009570 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9571 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05009572 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9573 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05009574 if_type = bf_get(lpfc_sli_intf_if_type,
9575 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009576 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05009577 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05009578 *pcmd == ELS_CMD_SCR ||
James Smartdf3fe762020-02-10 09:31:55 -08009579 *pcmd == ELS_CMD_RDF ||
James Smartf60cb932019-05-14 14:58:05 -07009580 *pcmd == ELS_CMD_RSCN_XMT ||
James Smart6b5151f2012-01-18 16:24:06 -05009581 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05009582 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05009583 *pcmd == ELS_CMD_PLOGI)) {
9584 bf_set(els_req64_sp, &wqe->els_req, 1);
9585 bf_set(els_req64_sid, &wqe->els_req,
9586 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009587 if ((*pcmd == ELS_CMD_FLOGI) &&
9588 !(phba->fc_topology ==
9589 LPFC_TOPOLOGY_LOOP))
9590 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009591 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9592 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009593 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009594 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009595 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9596 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9597 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9598 }
James Smartc8685952009-11-18 15:39:16 -05009599 }
James Smart6d368e52011-05-24 11:44:12 -04009600 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9601 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009602 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9603 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9604 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9605 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9606 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9607 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009608 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009609 break;
James Smart5ffc2662009-11-18 15:39:44 -05009610 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009611 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9612 iocbq->iocb.un.ulpWord[3]);
9613 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009614 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009615 /* The entire sequence is transmitted for this IOCB */
9616 xmit_len = total_len;
9617 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009618 if (phba->link_flag & LS_LOOPBACK_MODE)
9619 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -06009620 /* fall through */
James Smart4f774512009-05-22 14:52:35 -04009621 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009622 /* word3 iocb=io_tag32 wqe=reserved */
9623 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009624 /* word4 relative_offset memcpy */
9625 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009626 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9627 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9628 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9629 LPFC_WQE_IOD_WRITE);
9630 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9631 LPFC_WQE_LENLOC_WORD12);
9632 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009633 wqe->xmit_sequence.xmit_len = xmit_len;
9634 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009635 break;
James Smart4f774512009-05-22 14:52:35 -04009636 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009637 /* word3 iocb=iotag32 wqe=seq_payload_len */
9638 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009639 /* word4 iocb=rsvd wqe=rsvd */
9640 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9641 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009642 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009643 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009644 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9645 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9646 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9647 LPFC_WQE_LENLOC_WORD3);
9648 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009649 break;
James Smart4f774512009-05-22 14:52:35 -04009650 case CMD_FCP_IWRITE64_CR:
9651 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009652 /* word3 iocb=iotag wqe=payload_offset_len */
9653 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009654 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9655 xmit_len + sizeof(struct fcp_rsp));
9656 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9657 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009658 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9659 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9660 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9661 iocbq->iocb.ulpFCP2Rcvy);
9662 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9663 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009664 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9665 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9666 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009667 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009668 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009669 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9670 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009671 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9672 if (iocbq->priority) {
9673 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9674 (iocbq->priority << 1));
9675 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009676 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9677 (phba->cfg_XLanePriority << 1));
9678 }
9679 }
James Smartb5c53952016-03-31 14:12:30 -07009680 /* Note, word 10 is already initialized to 0 */
9681
James Smart414abe02018-06-26 08:24:26 -07009682 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9683 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009684 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9685 else
9686 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9687
James Smartb5c53952016-03-31 14:12:30 -07009688 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009689 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009690 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009691 struct fcp_cmnd *fcp_cmnd;
9692 uint32_t *ptr;
9693
9694 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009695
9696 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009697 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009698 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9699
9700 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009701 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009702 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009703 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9704 wqe->generic.bde.addrHigh = 0;
9705 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009706
James Smart205e8242018-03-05 12:04:03 -08009707 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9708 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009709
9710 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009711 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009712 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9713 }
James Smart7851fe22011-07-22 18:36:52 -04009714 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009715 case CMD_FCP_IREAD64_CR:
9716 /* word3 iocb=iotag wqe=payload_offset_len */
9717 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009718 bf_set(payload_offset_len, &wqe->fcp_iread,
9719 xmit_len + sizeof(struct fcp_rsp));
9720 bf_set(cmd_buff_len, &wqe->fcp_iread,
9721 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009722 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9723 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9724 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9725 iocbq->iocb.ulpFCP2Rcvy);
9726 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009727 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009728 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9729 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9730 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009731 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009732 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009733 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9734 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009735 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9736 if (iocbq->priority) {
9737 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9738 (iocbq->priority << 1));
9739 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009740 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9741 (phba->cfg_XLanePriority << 1));
9742 }
9743 }
James Smartb5c53952016-03-31 14:12:30 -07009744 /* Note, word 10 is already initialized to 0 */
9745
James Smart414abe02018-06-26 08:24:26 -07009746 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9747 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009748 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9749 else
9750 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9751
James Smartb5c53952016-03-31 14:12:30 -07009752 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009753 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009754 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009755 struct fcp_cmnd *fcp_cmnd;
9756 uint32_t *ptr;
9757
9758 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009759
9760 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009761 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009762 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9763
9764 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009765 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009766 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009767 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9768 wqe->generic.bde.addrHigh = 0;
9769 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009770
James Smart205e8242018-03-05 12:04:03 -08009771 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9772 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009773
9774 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009775 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009776 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9777 }
James Smart7851fe22011-07-22 18:36:52 -04009778 break;
James Smartf1126682009-06-10 17:22:44 -04009779 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009780 /* word3 iocb=iotag wqe=payload_offset_len */
9781 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9782 bf_set(payload_offset_len, &wqe->fcp_icmd,
9783 xmit_len + sizeof(struct fcp_rsp));
9784 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9785 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009786 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009787 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009788 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009789 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9790 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9791 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9792 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9793 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009794 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9795 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009796 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9797 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009798 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9799 if (iocbq->priority) {
9800 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9801 (iocbq->priority << 1));
9802 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009803 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9804 (phba->cfg_XLanePriority << 1));
9805 }
9806 }
James Smartb5c53952016-03-31 14:12:30 -07009807 /* Note, word 10 is already initialized to 0 */
9808
9809 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009810 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009811 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009812 struct fcp_cmnd *fcp_cmnd;
9813 uint32_t *ptr;
9814
9815 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009816
9817 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009818 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009819 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9820
9821 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009822 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009823 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009824 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9825 wqe->generic.bde.addrHigh = 0;
9826 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009827
James Smart205e8242018-03-05 12:04:03 -08009828 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9829 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009830
9831 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009832 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009833 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9834 }
James Smart7851fe22011-07-22 18:36:52 -04009835 break;
James Smart4f774512009-05-22 14:52:35 -04009836 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009837 /* For this command calculate the xmit length of the
9838 * request bde.
9839 */
9840 xmit_len = 0;
9841 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9842 sizeof(struct ulp_bde64);
9843 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009844 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009845 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9846 break;
James Smart63e801c2010-11-20 23:14:19 -05009847 xmit_len += bde.tus.f.bdeSize;
9848 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009849 /* word3 iocb=IO_TAG wqe=request_payload_len */
9850 wqe->gen_req.request_payload_len = xmit_len;
9851 /* word4 iocb=parameter wqe=relative_offset memcpy */
9852 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009853 /* word6 context tag copied in memcpy */
9854 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9855 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9856 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9857 "2015 Invalid CT %x command 0x%x\n",
9858 ct, iocbq->iocb.ulpCommand);
9859 return IOCB_ERROR;
9860 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009861 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9862 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9863 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9864 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9865 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9866 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9867 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9868 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009869 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009870 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009871 break;
James Smart4f774512009-05-22 14:52:35 -04009872 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009873 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009874 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009875 /* word3 iocb=iotag32 wqe=response_payload_len */
9876 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009877 /* word4 */
9878 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009879 /* word5 iocb=rsvd wge=did */
9880 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009881 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9882
9883 if_type = bf_get(lpfc_sli_intf_if_type,
9884 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009885 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009886 if (iocbq->vport->fc_flag & FC_PT2PT) {
9887 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9888 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9889 iocbq->vport->fc_myDID);
9890 if (iocbq->vport->fc_myDID == Fabric_DID) {
9891 bf_set(wqe_els_did,
9892 &wqe->xmit_els_rsp.wqe_dest, 0);
9893 }
9894 }
9895 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009896 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9897 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9898 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9899 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009900 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009901 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009902 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009903 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009904 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9905 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9906 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9907 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9908 LPFC_WQE_LENLOC_WORD3);
9909 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009910 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9911 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009912 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9913 iocbq->context2)->virt);
9914 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009915 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9916 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009917 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009918 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9919 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009920 phba->vpi_ids[phba->pport->vpi]);
9921 }
James Smart4f774512009-05-22 14:52:35 -04009922 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009923 break;
James Smart4f774512009-05-22 14:52:35 -04009924 case CMD_CLOSE_XRI_CN:
9925 case CMD_ABORT_XRI_CN:
9926 case CMD_ABORT_XRI_CX:
9927 /* words 0-2 memcpy should be 0 rserved */
9928 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009929 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9930 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9931 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9932 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9933 } else
9934 fip = 0;
9935
9936 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009937 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009938 * The link is down, or the command was ELS_FIP
9939 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009940 * on the wire.
9941 */
9942 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9943 else
9944 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9945 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009946 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9947 wqe->abort_cmd.rsrvd5 = 0;
9948 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009949 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9950 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009951 /*
9952 * The abort handler will send us CMD_ABORT_XRI_CN or
9953 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9954 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009955 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9956 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9957 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9958 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009959 cmnd = CMD_ABORT_XRI_CX;
9960 command_type = OTHER_COMMAND;
9961 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009962 break;
James Smart6669f9b2009-10-02 15:16:45 -04009963 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009964 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009965 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009966 * we re-construct this WQE here based on information in
9967 * iocbq from scratch.
9968 */
James Smartd9f492a2019-08-14 16:57:04 -07009969 memset(wqe, 0, sizeof(*wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009970 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009971 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009972 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9973 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009974 LPFC_ABTS_UNSOL_INT) {
9975 /* ABTS sent by initiator to CT exchange, the
9976 * RX_ID field will be filled with the newly
9977 * allocated responder XRI.
9978 */
9979 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9980 iocbq->sli4_xritag);
9981 } else {
9982 /* ABTS sent by responder to CT exchange, the
9983 * RX_ID field will be filled with the responder
9984 * RX_ID from ABTS.
9985 */
9986 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009987 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009988 }
James Smart6669f9b2009-10-02 15:16:45 -04009989 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9990 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009991
9992 /* Use CT=VPI */
9993 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9994 ndlp->nlp_DID);
9995 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9996 iocbq->iocb.ulpContext);
9997 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009998 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009999 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -040010000 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
10001 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
10002 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -040010003 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
10004 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -050010005 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
10006 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
10007 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
10008 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
10009 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
10010 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
10011 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
10012 }
10013
James Smart7851fe22011-07-22 18:36:52 -040010014 break;
James Smartae9e28f2017-05-15 15:20:51 -070010015 case CMD_SEND_FRAME:
James Smarte62245d2019-08-14 16:57:08 -070010016 bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_SEND_FRAME);
10017 bf_set(wqe_sof, &wqe->generic.wqe_com, 0x2E); /* SOF byte */
10018 bf_set(wqe_eof, &wqe->generic.wqe_com, 0x41); /* EOF byte */
10019 bf_set(wqe_lenloc, &wqe->generic.wqe_com, 1);
10020 bf_set(wqe_xbl, &wqe->generic.wqe_com, 1);
10021 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10022 bf_set(wqe_xc, &wqe->generic.wqe_com, 1);
10023 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, 0xA);
10024 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smartae9e28f2017-05-15 15:20:51 -070010025 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10026 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10027 return 0;
James Smart4f774512009-05-22 14:52:35 -040010028 case CMD_XRI_ABORTED_CX:
10029 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -040010030 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
10031 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
10032 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
10033 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
10034 default:
10035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10036 "2014 Invalid command 0x%x\n",
10037 iocbq->iocb.ulpCommand);
10038 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -040010039 break;
James Smart4f774512009-05-22 14:52:35 -040010040 }
James Smart6d368e52011-05-24 11:44:12 -040010041
James Smart8012cc32012-10-31 14:44:49 -040010042 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
10043 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
10044 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
10045 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
10046 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
10047 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
10048 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
10049 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -040010050 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10051 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10052 wqe->generic.wqe_com.abort_tag = abort_tag;
10053 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
10054 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
10055 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
10056 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -040010057 return 0;
10058}
10059
10060/**
10061 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
10062 * @phba: Pointer to HBA context object.
10063 * @ring_number: SLI ring number to issue iocb on.
10064 * @piocb: Pointer to command iocb.
10065 * @flag: Flag indicating if this command can be put into txq.
10066 *
10067 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
10068 * an iocb command to an HBA with SLI-4 interface spec.
10069 *
James Smart27f3efd2019-10-18 14:18:19 -070010070 * This function is called with ringlock held. The function will return success
James Smart4f774512009-05-22 14:52:35 -040010071 * after it successfully submit the iocb to firmware or after adding to the
10072 * txq.
10073 **/
10074static int
10075__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
10076 struct lpfc_iocbq *piocb, uint32_t flag)
10077{
10078 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080010079 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -050010080 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -080010081 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -040010082
James Smart895427b2017-02-12 13:52:30 -080010083 /* Get the WQ */
10084 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
10085 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc00f62e2019-08-14 16:57:11 -070010086 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
James Smart895427b2017-02-12 13:52:30 -080010087 } else {
10088 wq = phba->sli4_hba.els_wq;
10089 }
10090
10091 /* Get corresponding ring */
10092 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010093
James Smartb5c53952016-03-31 14:12:30 -070010094 /*
10095 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -070010096 */
James Smartb5c53952016-03-31 14:12:30 -070010097
James Smartcda7fa182019-03-04 15:15:43 -080010098 lockdep_assert_held(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010099
James Smart4f774512009-05-22 14:52:35 -040010100 if (piocb->sli4_xritag == NO_XRI) {
10101 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -050010102 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -040010103 sglq = NULL;
10104 else {
James Smart0e9bb8d2013-03-01 16:35:12 -050010105 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -040010106 if (!(flag & SLI_IOCB_RET_IOCB)) {
10107 __lpfc_sli_ringtx_put(phba,
10108 pring, piocb);
10109 return IOCB_SUCCESS;
10110 } else {
10111 return IOCB_BUSY;
10112 }
10113 } else {
James Smart895427b2017-02-12 13:52:30 -080010114 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -040010115 if (!sglq) {
10116 if (!(flag & SLI_IOCB_RET_IOCB)) {
10117 __lpfc_sli_ringtx_put(phba,
10118 pring,
10119 piocb);
10120 return IOCB_SUCCESS;
10121 } else
10122 return IOCB_BUSY;
10123 }
10124 }
James Smart4f774512009-05-22 14:52:35 -040010125 }
James Smart2ea259e2017-02-12 13:52:27 -080010126 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -040010127 /* These IO's already have an XRI and a mapped sgl. */
10128 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -080010129 else {
James Smart6d368e52011-05-24 11:44:12 -040010130 /*
10131 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -040010132 * sglq is on the active list
10133 */
James Smartedccdc12013-01-03 15:43:45 -050010134 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -040010135 if (!sglq)
10136 return IOCB_ERROR;
10137 }
10138
10139 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -040010140 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040010141 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040010142 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -040010143 return IOCB_ERROR;
10144 }
10145
James Smart205e8242018-03-05 12:04:03 -080010146 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -040010147 return IOCB_ERROR;
10148
James Smart205e8242018-03-05 12:04:03 -080010149 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -080010150 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -040010151 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
10152
10153 return 0;
10154}
10155
10156/**
James Smart3772a992009-05-22 14:50:54 -040010157 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
10158 *
10159 * This routine wraps the actual lockless version for issusing IOCB function
10160 * pointer from the lpfc_hba struct.
10161 *
10162 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -070010163 * IOCB_ERROR - Error
10164 * IOCB_SUCCESS - Success
10165 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -040010166 **/
James Smart2a9bf3d2010-06-07 15:24:45 -040010167int
James Smart3772a992009-05-22 14:50:54 -040010168__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10169 struct lpfc_iocbq *piocb, uint32_t flag)
10170{
10171 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10172}
10173
10174/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010175 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -040010176 * @phba: The hba struct for which this call is being executed.
10177 * @dev_grp: The HBA PCI-Device group number.
10178 *
10179 * This routine sets up the SLI interface API function jump table in @phba
10180 * struct.
10181 * Returns: 0 - success, -ENODEV - failure.
10182 **/
10183int
10184lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
10185{
10186
10187 switch (dev_grp) {
10188 case LPFC_PCI_DEV_LP:
10189 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
10190 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
10191 break;
James Smart4f774512009-05-22 14:52:35 -040010192 case LPFC_PCI_DEV_OC:
10193 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
10194 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
10195 break;
James Smart3772a992009-05-22 14:50:54 -040010196 default:
10197 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10198 "1419 Invalid HBA PCI-device group: 0x%x\n",
10199 dev_grp);
10200 return -ENODEV;
10201 break;
10202 }
10203 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
10204 return 0;
10205}
James Smart92d7f7b2007-06-17 19:56:38 -050010206
James Smarta1efe162015-05-21 13:55:20 -040010207/**
James Smart895427b2017-02-12 13:52:30 -080010208 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -040010209 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -040010210 * @piocb: Pointer to command iocb.
10211 *
James Smart895427b2017-02-12 13:52:30 -080010212 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
10213 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -040010214 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -080010215 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -040010216 */
James Smart895427b2017-02-12 13:52:30 -080010217struct lpfc_sli_ring *
10218lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -040010219{
James Smartc4908502019-01-28 11:14:28 -080010220 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -080010221
James Smart895427b2017-02-12 13:52:30 -080010222 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -080010223 if (unlikely(!phba->sli4_hba.hdwq))
James Smart7370d102019-01-28 11:14:20 -080010224 return NULL;
10225 /*
10226 * for abort iocb hba_wqidx should already
10227 * be setup based on what work queue we used.
10228 */
10229 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc4908502019-01-28 11:14:28 -080010230 lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
James Smart1fbf9742019-01-28 11:14:26 -080010231 piocb->hba_wqidx = lpfc_cmd->hdwq_no;
James Smart9bd2bff52014-09-03 12:57:30 -040010232 }
James Smartc00f62e2019-08-14 16:57:11 -070010233 return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010234 } else {
10235 if (unlikely(!phba->sli4_hba.els_wq))
10236 return NULL;
10237 piocb->hba_wqidx = 0;
10238 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -040010239 }
James Smart9bd2bff52014-09-03 12:57:30 -040010240}
10241
James Smarte59058c2008-08-24 21:49:00 -040010242/**
James Smart3621a712009-04-06 18:47:14 -040010243 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040010244 * @phba: Pointer to HBA context object.
10245 * @pring: Pointer to driver SLI ring object.
10246 * @piocb: Pointer to command iocb.
10247 * @flag: Flag indicating if this command can be put into txq.
10248 *
10249 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10250 * function. This function gets the hbalock and calls
10251 * __lpfc_sli_issue_iocb function and will return the error returned
10252 * by __lpfc_sli_issue_iocb function. This wrapper is used by
10253 * functions which do not hold hbalock.
10254 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010255int
James Smart3772a992009-05-22 14:50:54 -040010256lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -050010257 struct lpfc_iocbq *piocb, uint32_t flag)
10258{
James Smart2a76a282012-08-03 12:35:54 -040010259 struct lpfc_sli_ring *pring;
James Smart93a4d6f2019-11-04 16:57:05 -080010260 struct lpfc_queue *eq;
James Smart92d7f7b2007-06-17 19:56:38 -050010261 unsigned long iflags;
James Smart6a828b02019-01-28 11:14:31 -080010262 int rc;
James Smart92d7f7b2007-06-17 19:56:38 -050010263
James Smart7e56aa22012-08-03 12:35:34 -040010264 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart93a4d6f2019-11-04 16:57:05 -080010265 eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
10266
James Smart895427b2017-02-12 13:52:30 -080010267 pring = lpfc_sli4_calc_ring(phba, piocb);
10268 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010269 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -040010270
James Smart9bd2bff52014-09-03 12:57:30 -040010271 spin_lock_irqsave(&pring->ring_lock, iflags);
10272 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10273 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080010274
10275 lpfc_sli4_poll_eq(eq, LPFC_POLL_FASTPATH);
James Smart7e56aa22012-08-03 12:35:34 -040010276 } else {
10277 /* For now, SLI2/3 will still use hbalock */
10278 spin_lock_irqsave(&phba->hbalock, iflags);
10279 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10280 spin_unlock_irqrestore(&phba->hbalock, iflags);
10281 }
James Smart92d7f7b2007-06-17 19:56:38 -050010282 return rc;
10283}
10284
James Smarte59058c2008-08-24 21:49:00 -040010285/**
James Smart3621a712009-04-06 18:47:14 -040010286 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010287 * @phba: Pointer to HBA context object.
10288 *
10289 * This function is called while driver attaches with the
10290 * HBA to setup the extra ring. The extra ring is used
10291 * only when driver needs to support target mode functionality
10292 * or IP over FC functionalities.
10293 *
James Smart895427b2017-02-12 13:52:30 -080010294 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -040010295 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010296static int
10297lpfc_extra_ring_setup( struct lpfc_hba *phba)
10298{
10299 struct lpfc_sli *psli;
10300 struct lpfc_sli_ring *pring;
10301
10302 psli = &phba->sli;
10303
10304 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -050010305
10306 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -080010307 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -040010308 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10309 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10310 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10311 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010312
James Smarta4bc3372006-12-02 13:34:16 -050010313 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -080010314 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -050010315
James Smart7e56aa22012-08-03 12:35:34 -040010316 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10317 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10318 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10319 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010320
10321 /* Setup default profile for this ring */
10322 pring->iotag_max = 4096;
10323 pring->num_mask = 1;
10324 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -050010325 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10326 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010327 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10328 return 0;
10329}
10330
James Smartcb69f7d2011-12-13 13:21:57 -050010331/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10332 * @phba: Pointer to HBA context object.
10333 * @iocbq: Pointer to iocb object.
10334 *
10335 * The async_event handler calls this routine when it receives
10336 * an ASYNC_STATUS_CN event from the port. The port generates
10337 * this event when an Abort Sequence request to an rport fails
10338 * twice in succession. The abort could be originated by the
10339 * driver or by the port. The ABTS could have been for an ELS
10340 * or FCP IO. The port only generates this event when an ABTS
10341 * fails to complete after one retry.
10342 */
10343static void
10344lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10345 struct lpfc_iocbq *iocbq)
10346{
10347 struct lpfc_nodelist *ndlp = NULL;
10348 uint16_t rpi = 0, vpi = 0;
10349 struct lpfc_vport *vport = NULL;
10350
10351 /* The rpi in the ulpContext is vport-sensitive. */
10352 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10353 rpi = iocbq->iocb.ulpContext;
10354
10355 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10356 "3092 Port generated ABTS async event "
10357 "on vpi %d rpi %d status 0x%x\n",
10358 vpi, rpi, iocbq->iocb.ulpStatus);
10359
10360 vport = lpfc_find_vport_by_vpid(phba, vpi);
10361 if (!vport)
10362 goto err_exit;
10363 ndlp = lpfc_findnode_rpi(vport, rpi);
10364 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
10365 goto err_exit;
10366
10367 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10368 lpfc_sli_abts_recover_port(vport, ndlp);
10369 return;
10370
10371 err_exit:
10372 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10373 "3095 Event Context not found, no "
10374 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10375 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10376 vpi, rpi);
10377}
10378
10379/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10380 * @phba: pointer to HBA context object.
10381 * @ndlp: nodelist pointer for the impacted rport.
10382 * @axri: pointer to the wcqe containing the failed exchange.
10383 *
10384 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10385 * port. The port generates this event when an abort exchange request to an
10386 * rport fails twice in succession with no reply. The abort could be originated
10387 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
10388 */
10389void
10390lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10391 struct lpfc_nodelist *ndlp,
10392 struct sli4_wcqe_xri_aborted *axri)
10393{
10394 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -050010395 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -050010396
James Smart6b5151f2012-01-18 16:24:06 -050010397 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -050010398 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10399 "3115 Node Context not found, driver "
10400 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -050010401 return;
10402 }
10403
James Smartcb69f7d2011-12-13 13:21:57 -050010404 vport = ndlp->vport;
10405 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10406 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -050010407 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -040010408 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -050010409 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -050010410 bf_get(lpfc_wcqe_xa_status, axri),
10411 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -050010412
James Smart5c1db2a2012-03-01 22:34:36 -050010413 /*
10414 * Catch the ABTS protocol failure case. Older OCe FW releases returned
10415 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10416 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10417 */
James Smarte3d2b802012-08-14 14:25:43 -040010418 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -050010419 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10420 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -050010421 lpfc_sli_abts_recover_port(vport, ndlp);
10422}
10423
James Smarte59058c2008-08-24 21:49:00 -040010424/**
James Smart3621a712009-04-06 18:47:14 -040010425 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -040010426 * @phba: Pointer to HBA context object.
10427 * @pring: Pointer to driver SLI ring object.
10428 * @iocbq: Pointer to iocb object.
10429 *
10430 * This function is called by the slow ring event handler
10431 * function when there is an ASYNC event iocb in the ring.
10432 * This function is called with no lock held.
10433 * Currently this function handles only temperature related
10434 * ASYNC events. The function decodes the temperature sensor
10435 * event message and posts events for the management applications.
10436 **/
James Smart98c9ea52007-10-27 13:37:33 -040010437static void
James Smart57127f12007-10-27 13:37:05 -040010438lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10439 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10440{
10441 IOCB_t *icmd;
10442 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -040010443 struct temp_event temp_event_data;
10444 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -040010445 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -040010446
10447 icmd = &iocbq->iocb;
10448 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -040010449
James Smartcb69f7d2011-12-13 13:21:57 -050010450 switch (evt_code) {
10451 case ASYNC_TEMP_WARN:
10452 case ASYNC_TEMP_SAFE:
10453 temp_event_data.data = (uint32_t) icmd->ulpContext;
10454 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10455 if (evt_code == ASYNC_TEMP_WARN) {
10456 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10457 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10458 "0347 Adapter is very hot, please take "
10459 "corrective action. temperature : %d Celsius\n",
10460 (uint32_t) icmd->ulpContext);
10461 } else {
10462 temp_event_data.event_code = LPFC_NORMAL_TEMP;
10463 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10464 "0340 Adapter temperature is OK now. "
10465 "temperature : %d Celsius\n",
10466 (uint32_t) icmd->ulpContext);
10467 }
10468
10469 /* Send temperature change event to applications */
10470 shost = lpfc_shost_from_vport(phba->pport);
10471 fc_host_post_vendor_event(shost, fc_get_event_number(),
10472 sizeof(temp_event_data), (char *) &temp_event_data,
10473 LPFC_NL_VENDOR_ID);
10474 break;
10475 case ASYNC_STATUS_CN:
10476 lpfc_sli_abts_err_handler(phba, iocbq);
10477 break;
10478 default:
James Smarta257bf92009-04-06 18:48:10 -040010479 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -050010480 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -040010481 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -040010482 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -040010483 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
10484 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
10485 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
10486 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -050010487 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -040010488 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10489 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10490 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10491 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10492
James Smartcb69f7d2011-12-13 13:21:57 -050010493 break;
James Smart57127f12007-10-27 13:37:05 -040010494 }
James Smart57127f12007-10-27 13:37:05 -040010495}
10496
10497
James Smarte59058c2008-08-24 21:49:00 -040010498/**
James Smart895427b2017-02-12 13:52:30 -080010499 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010500 * @phba: Pointer to HBA context object.
10501 *
10502 * lpfc_sli_setup sets up rings of the SLI interface with
10503 * number of iocbs per ring and iotags. This function is
10504 * called while driver attach to the HBA and before the
10505 * interrupts are enabled. So there is no need for locking.
10506 *
10507 * This function always returns 0.
10508 **/
dea31012005-04-17 16:05:31 -050010509int
James Smart895427b2017-02-12 13:52:30 -080010510lpfc_sli4_setup(struct lpfc_hba *phba)
10511{
10512 struct lpfc_sli_ring *pring;
10513
10514 pring = phba->sli4_hba.els_wq->pring;
10515 pring->num_mask = LPFC_MAX_RING_MASK;
10516 pring->prt[0].profile = 0; /* Mask 0 */
10517 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10518 pring->prt[0].type = FC_TYPE_ELS;
10519 pring->prt[0].lpfc_sli_rcv_unsol_event =
10520 lpfc_els_unsol_event;
10521 pring->prt[1].profile = 0; /* Mask 1 */
10522 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10523 pring->prt[1].type = FC_TYPE_ELS;
10524 pring->prt[1].lpfc_sli_rcv_unsol_event =
10525 lpfc_els_unsol_event;
10526 pring->prt[2].profile = 0; /* Mask 2 */
10527 /* NameServer Inquiry */
10528 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10529 /* NameServer */
10530 pring->prt[2].type = FC_TYPE_CT;
10531 pring->prt[2].lpfc_sli_rcv_unsol_event =
10532 lpfc_ct_unsol_event;
10533 pring->prt[3].profile = 0; /* Mask 3 */
10534 /* NameServer response */
10535 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10536 /* NameServer */
10537 pring->prt[3].type = FC_TYPE_CT;
10538 pring->prt[3].lpfc_sli_rcv_unsol_event =
10539 lpfc_ct_unsol_event;
10540 return 0;
10541}
10542
10543/**
10544 * lpfc_sli_setup - SLI ring setup function
10545 * @phba: Pointer to HBA context object.
10546 *
10547 * lpfc_sli_setup sets up rings of the SLI interface with
10548 * number of iocbs per ring and iotags. This function is
10549 * called while driver attach to the HBA and before the
10550 * interrupts are enabled. So there is no need for locking.
10551 *
10552 * This function always returns 0. SLI3 only.
10553 **/
10554int
dea31012005-04-17 16:05:31 -050010555lpfc_sli_setup(struct lpfc_hba *phba)
10556{
James Smarted957682007-06-17 19:56:37 -050010557 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050010558 struct lpfc_sli *psli = &phba->sli;
10559 struct lpfc_sli_ring *pring;
10560
James Smart2a76a282012-08-03 12:35:54 -040010561 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010562 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010563
James Bottomley604a3e32005-10-29 10:28:33 -050010564 psli->iocbq_lookup = NULL;
10565 psli->iocbq_lookup_len = 0;
10566 psli->last_iotag = 0;
10567
dea31012005-04-17 16:05:31 -050010568 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010569 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010570 switch (i) {
10571 case LPFC_FCP_RING: /* ring 0 - FCP */
10572 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010573 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10574 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10575 pring->sli.sli3.numCiocb +=
10576 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10577 pring->sli.sli3.numRiocb +=
10578 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10579 pring->sli.sli3.numCiocb +=
10580 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10581 pring->sli.sli3.numRiocb +=
10582 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10583 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010584 SLI3_IOCB_CMD_SIZE :
10585 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010586 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010587 SLI3_IOCB_RSP_SIZE :
10588 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010589 pring->iotag_ctr = 0;
10590 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010591 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010592 pring->fast_iotag = pring->iotag_max;
10593 pring->num_mask = 0;
10594 break;
James Smarta4bc3372006-12-02 13:34:16 -050010595 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010596 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010597 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10598 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10599 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010600 SLI3_IOCB_CMD_SIZE :
10601 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010602 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010603 SLI3_IOCB_RSP_SIZE :
10604 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010605 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010606 pring->num_mask = 0;
10607 break;
10608 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10609 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010610 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10611 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10612 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010613 SLI3_IOCB_CMD_SIZE :
10614 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010615 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010616 SLI3_IOCB_RSP_SIZE :
10617 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010618 pring->fast_iotag = 0;
10619 pring->iotag_ctr = 0;
10620 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010621 pring->lpfc_sli_rcv_async_status =
10622 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010623 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010624 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010625 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10626 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010627 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010628 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010629 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010630 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10631 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010632 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010633 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010634 pring->prt[2].profile = 0; /* Mask 2 */
10635 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010636 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010637 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010638 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010639 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010640 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010641 pring->prt[3].profile = 0; /* Mask 3 */
10642 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010643 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010644 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010645 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010646 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010647 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010648 break;
10649 }
James Smart7e56aa22012-08-03 12:35:34 -040010650 totiocbsize += (pring->sli.sli3.numCiocb *
10651 pring->sli.sli3.sizeCiocb) +
10652 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010653 }
James Smarted957682007-06-17 19:56:37 -050010654 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010655 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010656 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10657 "SLI2 SLIM Data: x%x x%lx\n",
10658 phba->brd_no, totiocbsize,
10659 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010660 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010661 if (phba->cfg_multi_ring_support == 2)
10662 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010663
10664 return 0;
10665}
10666
James Smarte59058c2008-08-24 21:49:00 -040010667/**
James Smart895427b2017-02-12 13:52:30 -080010668 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010669 * @phba: Pointer to HBA context object.
10670 *
James Smart895427b2017-02-12 13:52:30 -080010671 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010672 * ring. This function also initializes ring indices of each ring.
10673 * This function is called during the initialization of the SLI
10674 * interface of an HBA.
10675 * This function is called with no lock held and always returns
10676 * 1.
10677 **/
James Smart895427b2017-02-12 13:52:30 -080010678void
10679lpfc_sli4_queue_init(struct lpfc_hba *phba)
10680{
10681 struct lpfc_sli *psli;
10682 struct lpfc_sli_ring *pring;
10683 int i;
10684
10685 psli = &phba->sli;
10686 spin_lock_irq(&phba->hbalock);
10687 INIT_LIST_HEAD(&psli->mboxq);
10688 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10689 /* Initialize list headers for txq and txcmplq as double linked lists */
James Smartcdb42be2019-01-28 11:14:21 -080010690 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -070010691 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010692 pring->flag = 0;
10693 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080010694 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010695 INIT_LIST_HEAD(&pring->txq);
10696 INIT_LIST_HEAD(&pring->txcmplq);
10697 INIT_LIST_HEAD(&pring->iocb_continueq);
10698 spin_lock_init(&pring->ring_lock);
10699 }
10700 pring = phba->sli4_hba.els_wq->pring;
10701 pring->flag = 0;
10702 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010703 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010704 INIT_LIST_HEAD(&pring->txq);
10705 INIT_LIST_HEAD(&pring->txcmplq);
10706 INIT_LIST_HEAD(&pring->iocb_continueq);
10707 spin_lock_init(&pring->ring_lock);
10708
James Smartcdb42be2019-01-28 11:14:21 -080010709 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080010710 pring = phba->sli4_hba.nvmels_wq->pring;
10711 pring->flag = 0;
10712 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010713 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010714 INIT_LIST_HEAD(&pring->txq);
10715 INIT_LIST_HEAD(&pring->txcmplq);
10716 INIT_LIST_HEAD(&pring->iocb_continueq);
10717 spin_lock_init(&pring->ring_lock);
10718 }
10719
10720 spin_unlock_irq(&phba->hbalock);
10721}
10722
10723/**
10724 * lpfc_sli_queue_init - Queue initialization function
10725 * @phba: Pointer to HBA context object.
10726 *
10727 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10728 * ring. This function also initializes ring indices of each ring.
10729 * This function is called during the initialization of the SLI
10730 * interface of an HBA.
10731 * This function is called with no lock held and always returns
10732 * 1.
10733 **/
10734void
10735lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010736{
10737 struct lpfc_sli *psli;
10738 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010739 int i;
dea31012005-04-17 16:05:31 -050010740
10741 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010742 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010743 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010744 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010745 /* Initialize list headers for txq and txcmplq as double linked lists */
10746 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010747 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010748 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010749 pring->sli.sli3.next_cmdidx = 0;
10750 pring->sli.sli3.local_getidx = 0;
10751 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010752 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010753 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010754 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010755 pring->flag = 0;
10756 INIT_LIST_HEAD(&pring->txq);
10757 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010758 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010759 }
James Smart2e0fef82007-06-17 19:56:36 -050010760 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010761}
10762
James Smarte59058c2008-08-24 21:49:00 -040010763/**
James Smart04c68492009-05-22 14:52:52 -040010764 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10765 * @phba: Pointer to HBA context object.
10766 *
10767 * This routine flushes the mailbox command subsystem. It will unconditionally
10768 * flush all the mailbox commands in the three possible stages in the mailbox
10769 * command sub-system: pending mailbox command queue; the outstanding mailbox
10770 * command; and completed mailbox command queue. It is caller's responsibility
10771 * to make sure that the driver is in the proper state to flush the mailbox
10772 * command sub-system. Namely, the posting of mailbox commands into the
10773 * pending mailbox command queue from the various clients must be stopped;
10774 * either the HBA is in a state that it will never works on the outstanding
10775 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10776 * mailbox command has been completed.
10777 **/
10778static void
10779lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10780{
10781 LIST_HEAD(completions);
10782 struct lpfc_sli *psli = &phba->sli;
10783 LPFC_MBOXQ_t *pmb;
10784 unsigned long iflag;
10785
James Smart523128e2018-09-10 10:30:46 -070010786 /* Disable softirqs, including timers from obtaining phba->hbalock */
10787 local_bh_disable();
10788
James Smart04c68492009-05-22 14:52:52 -040010789 /* Flush all the mailbox commands in the mbox system */
10790 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart523128e2018-09-10 10:30:46 -070010791
James Smart04c68492009-05-22 14:52:52 -040010792 /* The pending mailbox command queue */
10793 list_splice_init(&phba->sli.mboxq, &completions);
10794 /* The outstanding active mailbox command */
10795 if (psli->mbox_active) {
10796 list_add_tail(&psli->mbox_active->list, &completions);
10797 psli->mbox_active = NULL;
10798 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10799 }
10800 /* The completed mailbox command queue */
10801 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10802 spin_unlock_irqrestore(&phba->hbalock, iflag);
10803
James Smart523128e2018-09-10 10:30:46 -070010804 /* Enable softirqs again, done with phba->hbalock */
10805 local_bh_enable();
10806
James Smart04c68492009-05-22 14:52:52 -040010807 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10808 while (!list_empty(&completions)) {
10809 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10810 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10811 if (pmb->mbox_cmpl)
10812 pmb->mbox_cmpl(phba, pmb);
10813 }
10814}
10815
10816/**
James Smart3621a712009-04-06 18:47:14 -040010817 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010818 * @vport: Pointer to virtual port object.
10819 *
10820 * lpfc_sli_host_down is called to clean up the resources
10821 * associated with a vport before destroying virtual
10822 * port data structures.
10823 * This function does following operations:
10824 * - Free discovery resources associated with this virtual
10825 * port.
10826 * - Free iocbs associated with this virtual port in
10827 * the txq.
10828 * - Send abort for all iocb commands associated with this
10829 * vport in txcmplq.
10830 *
10831 * This function is called with no lock held and always returns 1.
10832 **/
dea31012005-04-17 16:05:31 -050010833int
James Smart92d7f7b2007-06-17 19:56:38 -050010834lpfc_sli_host_down(struct lpfc_vport *vport)
10835{
James Smart858c9f62007-06-17 19:56:39 -050010836 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010837 struct lpfc_hba *phba = vport->phba;
10838 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010839 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010840 struct lpfc_sli_ring *pring;
10841 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010842 int i;
10843 unsigned long flags = 0;
10844 uint16_t prev_pring_flag;
10845
10846 lpfc_cleanup_discovery_resources(vport);
10847
10848 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010849
James Smart895427b2017-02-12 13:52:30 -080010850 /*
10851 * Error everything on the txq since these iocbs
10852 * have not been given to the FW yet.
10853 * Also issue ABTS for everything on the txcmplq
10854 */
10855 if (phba->sli_rev != LPFC_SLI_REV4) {
10856 for (i = 0; i < psli->num_rings; i++) {
10857 pring = &psli->sli3_ring[i];
10858 prev_pring_flag = pring->flag;
10859 /* Only slow rings */
10860 if (pring->ringno == LPFC_ELS_RING) {
10861 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10862 /* Set the lpfc data pending flag */
10863 set_bit(LPFC_DATA_READY, &phba->data_flags);
10864 }
10865 list_for_each_entry_safe(iocb, next_iocb,
10866 &pring->txq, list) {
10867 if (iocb->vport != vport)
10868 continue;
10869 list_move_tail(&iocb->list, &completions);
10870 }
10871 list_for_each_entry_safe(iocb, next_iocb,
10872 &pring->txcmplq, list) {
10873 if (iocb->vport != vport)
10874 continue;
10875 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10876 }
10877 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010878 }
James Smart895427b2017-02-12 13:52:30 -080010879 } else {
10880 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10881 pring = qp->pring;
10882 if (!pring)
10883 continue;
10884 if (pring == phba->sli4_hba.els_wq->pring) {
10885 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10886 /* Set the lpfc data pending flag */
10887 set_bit(LPFC_DATA_READY, &phba->data_flags);
10888 }
10889 prev_pring_flag = pring->flag;
James Smart65a3df62019-09-21 20:58:48 -070010890 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010891 list_for_each_entry_safe(iocb, next_iocb,
10892 &pring->txq, list) {
10893 if (iocb->vport != vport)
10894 continue;
10895 list_move_tail(&iocb->list, &completions);
10896 }
James Smart65a3df62019-09-21 20:58:48 -070010897 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010898 list_for_each_entry_safe(iocb, next_iocb,
10899 &pring->txcmplq, list) {
10900 if (iocb->vport != vport)
10901 continue;
10902 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10903 }
10904 pring->flag = prev_pring_flag;
10905 }
James Smart92d7f7b2007-06-17 19:56:38 -050010906 }
James Smart92d7f7b2007-06-17 19:56:38 -050010907 spin_unlock_irqrestore(&phba->hbalock, flags);
10908
James Smarta257bf92009-04-06 18:48:10 -040010909 /* Cancel all the IOCBs from the completions list */
10910 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10911 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010912 return 1;
10913}
10914
James Smarte59058c2008-08-24 21:49:00 -040010915/**
James Smart3621a712009-04-06 18:47:14 -040010916 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010917 * @phba: Pointer to HBA context object.
10918 *
10919 * This function cleans up all iocb, buffers, mailbox commands
10920 * while shutting down the HBA. This function is called with no
10921 * lock held and always returns 1.
10922 * This function does the following to cleanup driver resources:
10923 * - Free discovery resources for each virtual port
10924 * - Cleanup any pending fabric iocbs
10925 * - Iterate through the iocb txq and free each entry
10926 * in the list.
10927 * - Free up any buffer posted to the HBA
10928 * - Free mailbox commands in the mailbox queue.
10929 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010930int
James Smart2e0fef82007-06-17 19:56:36 -050010931lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010932{
James Smart2534ba72007-04-25 09:52:20 -040010933 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010934 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010935 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010936 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010937 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010938 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010939 int i;
10940
10941 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010942 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010943
dea31012005-04-17 16:05:31 -050010944 lpfc_hba_down_prep(phba);
10945
James Smart523128e2018-09-10 10:30:46 -070010946 /* Disable softirqs, including timers from obtaining phba->hbalock */
10947 local_bh_disable();
10948
James Smart92d7f7b2007-06-17 19:56:38 -050010949 lpfc_fabric_abort_hba(phba);
10950
James Smart2e0fef82007-06-17 19:56:36 -050010951 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010952
James Smart895427b2017-02-12 13:52:30 -080010953 /*
10954 * Error everything on the txq since these iocbs
10955 * have not been given to the FW yet.
10956 */
10957 if (phba->sli_rev != LPFC_SLI_REV4) {
10958 for (i = 0; i < psli->num_rings; i++) {
10959 pring = &psli->sli3_ring[i];
10960 /* Only slow rings */
10961 if (pring->ringno == LPFC_ELS_RING) {
10962 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10963 /* Set the lpfc data pending flag */
10964 set_bit(LPFC_DATA_READY, &phba->data_flags);
10965 }
10966 list_splice_init(&pring->txq, &completions);
10967 }
10968 } else {
10969 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10970 pring = qp->pring;
10971 if (!pring)
10972 continue;
James Smart4b0a42b2019-08-14 16:56:42 -070010973 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010974 list_splice_init(&pring->txq, &completions);
James Smart4b0a42b2019-08-14 16:56:42 -070010975 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010976 if (pring == phba->sli4_hba.els_wq->pring) {
10977 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10978 /* Set the lpfc data pending flag */
10979 set_bit(LPFC_DATA_READY, &phba->data_flags);
10980 }
10981 }
dea31012005-04-17 16:05:31 -050010982 }
James Smart2e0fef82007-06-17 19:56:36 -050010983 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010984
James Smarta257bf92009-04-06 18:48:10 -040010985 /* Cancel all the IOCBs from the completions list */
10986 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10987 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010988
James Smart0ff10d42008-01-11 01:52:36 -050010989 spin_lock_irqsave(&phba->hbalock, flags);
10990 list_splice_init(&phba->elsbuf, &completions);
10991 phba->elsbuf_cnt = 0;
10992 phba->elsbuf_prev_cnt = 0;
10993 spin_unlock_irqrestore(&phba->hbalock, flags);
10994
10995 while (!list_empty(&completions)) {
10996 list_remove_head(&completions, buf_ptr,
10997 struct lpfc_dmabuf, list);
10998 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10999 kfree(buf_ptr);
11000 }
11001
James Smart523128e2018-09-10 10:30:46 -070011002 /* Enable softirqs again, done with phba->hbalock */
11003 local_bh_enable();
11004
dea31012005-04-17 16:05:31 -050011005 /* Return any active mbox cmds */
11006 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050011007
James Smartda0436e2009-05-22 14:51:39 -040011008 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050011009 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040011010 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050011011
James Smartda0436e2009-05-22 14:51:39 -040011012 return 1;
11013}
James Smart92d7f7b2007-06-17 19:56:38 -050011014
James Smartda0436e2009-05-22 14:51:39 -040011015/**
James Smart3621a712009-04-06 18:47:14 -040011016 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040011017 * @srcp: Source memory pointer.
11018 * @destp: Destination memory pointer.
11019 * @cnt: Number of words required to be copied.
11020 *
11021 * This function is used for copying data between driver memory
11022 * and the SLI memory. This function also changes the endianness
11023 * of each word if native endianness is different from SLI
11024 * endianness. This function can be called with or without
11025 * lock.
11026 **/
dea31012005-04-17 16:05:31 -050011027void
11028lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
11029{
11030 uint32_t *src = srcp;
11031 uint32_t *dest = destp;
11032 uint32_t ldata;
11033 int i;
11034
11035 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
11036 ldata = *src;
11037 ldata = le32_to_cpu(ldata);
11038 *dest = ldata;
11039 src++;
11040 dest++;
11041 }
11042}
11043
James Smarte59058c2008-08-24 21:49:00 -040011044
11045/**
James Smarta0c87cb2009-07-19 10:01:10 -040011046 * lpfc_sli_bemem_bcopy - SLI memory copy function
11047 * @srcp: Source memory pointer.
11048 * @destp: Destination memory pointer.
11049 * @cnt: Number of words required to be copied.
11050 *
11051 * This function is used for copying data between a data structure
11052 * with big endian representation to local endianness.
11053 * This function can be called with or without lock.
11054 **/
11055void
11056lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
11057{
11058 uint32_t *src = srcp;
11059 uint32_t *dest = destp;
11060 uint32_t ldata;
11061 int i;
11062
11063 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
11064 ldata = *src;
11065 ldata = be32_to_cpu(ldata);
11066 *dest = ldata;
11067 src++;
11068 dest++;
11069 }
11070}
11071
11072/**
James Smart3621a712009-04-06 18:47:14 -040011073 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040011074 * @phba: Pointer to HBA context object.
11075 * @pring: Pointer to driver SLI ring object.
11076 * @mp: Pointer to driver buffer object.
11077 *
11078 * This function is called with no lock held.
11079 * It always return zero after adding the buffer to the postbufq
11080 * buffer list.
11081 **/
dea31012005-04-17 16:05:31 -050011082int
James Smart2e0fef82007-06-17 19:56:36 -050011083lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11084 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050011085{
11086 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
11087 later */
James Smart2e0fef82007-06-17 19:56:36 -050011088 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011089 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050011090 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050011091 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011092 return 0;
11093}
11094
James Smarte59058c2008-08-24 21:49:00 -040011095/**
James Smart3621a712009-04-06 18:47:14 -040011096 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040011097 * @phba: Pointer to HBA context object.
11098 *
11099 * When HBQ is enabled, buffers are searched based on tags. This function
11100 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
11101 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
11102 * does not conflict with tags of buffer posted for unsolicited events.
11103 * The function returns the allocated tag. The function is called with
11104 * no locks held.
11105 **/
James Smart76bb24e2007-10-27 13:38:00 -040011106uint32_t
11107lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
11108{
11109 spin_lock_irq(&phba->hbalock);
11110 phba->buffer_tag_count++;
11111 /*
11112 * Always set the QUE_BUFTAG_BIT to distiguish between
11113 * a tag assigned by HBQ.
11114 */
11115 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
11116 spin_unlock_irq(&phba->hbalock);
11117 return phba->buffer_tag_count;
11118}
11119
James Smarte59058c2008-08-24 21:49:00 -040011120/**
James Smart3621a712009-04-06 18:47:14 -040011121 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040011122 * @phba: Pointer to HBA context object.
11123 * @pring: Pointer to driver SLI ring object.
11124 * @tag: Buffer tag.
11125 *
11126 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
11127 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
11128 * iocb is posted to the response ring with the tag of the buffer.
11129 * This function searches the pring->postbufq list using the tag
11130 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
11131 * iocb. If the buffer is found then lpfc_dmabuf object of the
11132 * buffer is returned to the caller else NULL is returned.
11133 * This function is called with no lock held.
11134 **/
James Smart76bb24e2007-10-27 13:38:00 -040011135struct lpfc_dmabuf *
11136lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11137 uint32_t tag)
11138{
11139 struct lpfc_dmabuf *mp, *next_mp;
11140 struct list_head *slp = &pring->postbufq;
11141
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011142 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040011143 spin_lock_irq(&phba->hbalock);
11144 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
11145 if (mp->buffer_tag == tag) {
11146 list_del_init(&mp->list);
11147 pring->postbufq_cnt--;
11148 spin_unlock_irq(&phba->hbalock);
11149 return mp;
11150 }
11151 }
11152
11153 spin_unlock_irq(&phba->hbalock);
11154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040011155 "0402 Cannot find virtual addr for buffer tag on "
James Smart32350662019-08-14 16:57:06 -070011156 "ring %d Data x%lx x%px x%px x%x\n",
James Smart76bb24e2007-10-27 13:38:00 -040011157 pring->ringno, (unsigned long) tag,
11158 slp->next, slp->prev, pring->postbufq_cnt);
11159
11160 return NULL;
11161}
dea31012005-04-17 16:05:31 -050011162
James Smarte59058c2008-08-24 21:49:00 -040011163/**
James Smart3621a712009-04-06 18:47:14 -040011164 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040011165 * @phba: Pointer to HBA context object.
11166 * @pring: Pointer to driver SLI ring object.
11167 * @phys: DMA address of the buffer.
11168 *
11169 * This function searches the buffer list using the dma_address
11170 * of unsolicited event to find the driver's lpfc_dmabuf object
11171 * corresponding to the dma_address. The function returns the
11172 * lpfc_dmabuf object if a buffer is found else it returns NULL.
11173 * This function is called by the ct and els unsolicited event
11174 * handlers to get the buffer associated with the unsolicited
11175 * event.
11176 *
11177 * This function is called with no lock held.
11178 **/
dea31012005-04-17 16:05:31 -050011179struct lpfc_dmabuf *
11180lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11181 dma_addr_t phys)
11182{
11183 struct lpfc_dmabuf *mp, *next_mp;
11184 struct list_head *slp = &pring->postbufq;
11185
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011186 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050011187 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011188 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
11189 if (mp->phys == phys) {
11190 list_del_init(&mp->list);
11191 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050011192 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011193 return mp;
11194 }
11195 }
11196
James Smart2e0fef82007-06-17 19:56:36 -050011197 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040011199 "0410 Cannot find virtual addr for mapped buf on "
James Smart32350662019-08-14 16:57:06 -070011200 "ring %d Data x%llx x%px x%px x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011201 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050011202 slp->next, slp->prev, pring->postbufq_cnt);
11203 return NULL;
11204}
11205
James Smarte59058c2008-08-24 21:49:00 -040011206/**
James Smart3621a712009-04-06 18:47:14 -040011207 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040011208 * @phba: Pointer to HBA context object.
11209 * @cmdiocb: Pointer to driver command iocb object.
11210 * @rspiocb: Pointer to driver response iocb object.
11211 *
11212 * This function is the completion handler for the abort iocbs for
11213 * ELS commands. This function is called from the ELS ring event
11214 * handler with no lock held. This function frees memory resources
11215 * associated with the abort iocb.
11216 **/
dea31012005-04-17 16:05:31 -050011217static void
James Smart2e0fef82007-06-17 19:56:36 -050011218lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11219 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050011220{
James Smart2e0fef82007-06-17 19:56:36 -050011221 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040011222 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050011223 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040011224
11225 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050011226
11227 /*
11228 * Assume that the port already completed and returned, or
11229 * will return the iocb. Just Log the message.
11230 */
James Smart2680eea2007-04-25 09:52:55 -040011231 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11232 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11233
James Smart2e0fef82007-06-17 19:56:36 -050011234 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040011235 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smartfaa832e2018-07-31 17:23:18 -070011236 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11237 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11238 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11239 spin_unlock_irq(&phba->hbalock);
11240 goto release_iocb;
11241 }
James Smart45ed1192009-10-02 15:17:02 -040011242 if (abort_iotag != 0 &&
11243 abort_iotag <= phba->sli.last_iotag)
11244 abort_iocb =
11245 phba->sli.iocbq_lookup[abort_iotag];
11246 } else
11247 /* For sli4 the abort_tag is the XRI,
11248 * so the abort routine puts the iotag of the iocb
11249 * being aborted in the context field of the abort
11250 * IOCB.
11251 */
11252 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040011253
James Smart2a9bf3d2010-06-07 15:24:45 -040011254 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
James Smart32350662019-08-14 16:57:06 -070011255 "0327 Cannot abort els iocb x%px "
James Smart2a9bf3d2010-06-07 15:24:45 -040011256 "with tag %x context %x, abort status %x, "
11257 "abort code %x\n",
11258 abort_iocb, abort_iotag, abort_context,
11259 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040011260
James Smartff78d8f2011-12-13 13:21:35 -050011261 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040011262 }
James Smartfaa832e2018-07-31 17:23:18 -070011263release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050011264 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050011265 return;
11266}
11267
James Smarte59058c2008-08-24 21:49:00 -040011268/**
James Smart3621a712009-04-06 18:47:14 -040011269 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040011270 * @phba: Pointer to HBA context object.
11271 * @cmdiocb: Pointer to driver command iocb object.
11272 * @rspiocb: Pointer to driver response iocb object.
11273 *
11274 * The function is called from SLI ring event handler with no
11275 * lock held. This function is the completion handler for ELS commands
11276 * which are aborted. The function frees memory resources used for
11277 * the aborted ELS commands.
11278 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011279static void
11280lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11281 struct lpfc_iocbq *rspiocb)
11282{
11283 IOCB_t *irsp = &rspiocb->iocb;
11284
11285 /* ELS cmd tag <ulpIoTag> completes */
11286 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040011287 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050011288 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011289 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050011290 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050011291 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11292 lpfc_ct_free_iocb(phba, cmdiocb);
11293 else
11294 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050011295 return;
11296}
11297
James Smarte59058c2008-08-24 21:49:00 -040011298/**
James Smart5af5eee2010-10-22 11:06:38 -040011299 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040011300 * @phba: Pointer to HBA context object.
11301 * @pring: Pointer to driver SLI ring object.
11302 * @cmdiocb: Pointer to driver command iocb object.
11303 *
James Smart5af5eee2010-10-22 11:06:38 -040011304 * This function issues an abort iocb for the provided command iocb down to
11305 * the port. Other than the case the outstanding command iocb is an abort
11306 * request, this function issues abort out unconditionally. This function is
11307 * called with hbalock held. The function returns 0 when it fails due to
11308 * memory allocation failure or when the command iocb is an abort request.
Dick Kennedy88acb4d2020-05-01 14:43:07 -070011309 * The hbalock is asserted held in the code path calling this routine.
James Smarte59058c2008-08-24 21:49:00 -040011310 **/
James Smart5af5eee2010-10-22 11:06:38 -040011311static int
11312lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050011313 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050011314{
James Smart2e0fef82007-06-17 19:56:36 -050011315 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011316 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050011317 IOCB_t *icmd = NULL;
11318 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011319 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040011320 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070011321 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040011322
James Smart92d7f7b2007-06-17 19:56:38 -050011323 /*
11324 * There are certain command types we don't want to abort. And we
11325 * don't want to abort commands that are already in the process of
11326 * being aborted.
James Smart07951072007-04-25 09:51:38 -040011327 */
11328 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050011329 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050011330 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11331 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040011332 return 0;
11333
dea31012005-04-17 16:05:31 -050011334 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050011335 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011336 if (abtsiocbp == NULL)
11337 return 0;
dea31012005-04-17 16:05:31 -050011338
James Smart07951072007-04-25 09:51:38 -040011339 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050011340 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040011341 */
11342 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11343
dea31012005-04-17 16:05:31 -050011344 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040011345 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11346 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040011347 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040011348 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040011349 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070011350 } else {
James Smartda0436e2009-05-22 14:51:39 -040011351 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070011352 if (pring->ringno == LPFC_ELS_RING) {
11353 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11354 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11355 }
11356 }
dea31012005-04-17 16:05:31 -050011357 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040011358 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050011359
James Smart5ffc2662009-11-18 15:39:44 -050011360 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011361 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011362 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
11363 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011364 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11365 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011366
James Smart2e0fef82007-06-17 19:56:36 -050011367 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040011368 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11369 else
11370 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11371
11372 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080011373 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040011374
James Smarte8b62012007-08-02 11:10:09 -040011375 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11376 "0339 Abort xri x%x, original iotag x%x, "
11377 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040011378 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040011379 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040011380 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040011381
11382 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011383 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11384 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040011385 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040011386 /* Note: both hbalock and ring_lock need to be set here */
11387 spin_lock_irqsave(&pring->ring_lock, iflags);
11388 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11389 abtsiocbp, 0);
11390 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11391 } else {
11392 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11393 abtsiocbp, 0);
11394 }
James Smart07951072007-04-25 09:51:38 -040011395
James Smartd7c255b2008-08-24 21:50:00 -040011396 if (retval)
11397 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040011398
11399 /*
11400 * Caller to this routine should check for IOCB_ERROR
11401 * and handle it properly. This routine no longer removes
11402 * iocb off txcmplq and call compl in case of IOCB_ERROR.
11403 */
11404 return retval;
11405}
11406
11407/**
11408 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11409 * @phba: Pointer to HBA context object.
11410 * @pring: Pointer to driver SLI ring object.
11411 * @cmdiocb: Pointer to driver command iocb object.
11412 *
11413 * This function issues an abort iocb for the provided command iocb. In case
11414 * of unloading, the abort iocb will not be issued to commands on the ELS
11415 * ring. Instead, the callback function shall be changed to those commands
11416 * so that nothing happens when them finishes. This function is called with
11417 * hbalock held. The function returns 0 when the command iocb is an abort
11418 * request.
11419 **/
11420int
11421lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11422 struct lpfc_iocbq *cmdiocb)
11423{
11424 struct lpfc_vport *vport = cmdiocb->vport;
11425 int retval = IOCB_ERROR;
11426 IOCB_t *icmd = NULL;
11427
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011428 lockdep_assert_held(&phba->hbalock);
11429
James Smart5af5eee2010-10-22 11:06:38 -040011430 /*
11431 * There are certain command types we don't want to abort. And we
11432 * don't want to abort commands that are already in the process of
11433 * being aborted.
11434 */
11435 icmd = &cmdiocb->iocb;
11436 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11437 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11438 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11439 return 0;
11440
Dick Kennedy1234a6d2017-09-29 17:34:29 -070011441 if (!pring) {
11442 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11443 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11444 else
11445 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11446 goto abort_iotag_exit;
11447 }
11448
James Smart5af5eee2010-10-22 11:06:38 -040011449 /*
11450 * If we're unloading, don't abort iocb on the ELS ring, but change
11451 * the callback so that nothing happens when it finishes.
11452 */
11453 if ((vport->load_flag & FC_UNLOADING) &&
11454 (pring->ringno == LPFC_ELS_RING)) {
11455 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11456 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11457 else
11458 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11459 goto abort_iotag_exit;
11460 }
11461
11462 /* Now, we try to issue the abort to the cmdiocb out */
11463 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
11464
James Smart07951072007-04-25 09:51:38 -040011465abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050011466 /*
11467 * Caller to this routine should check for IOCB_ERROR
11468 * and handle it properly. This routine no longer removes
11469 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040011470 */
James Smart2e0fef82007-06-17 19:56:36 -050011471 return retval;
dea31012005-04-17 16:05:31 -050011472}
11473
James Smarte59058c2008-08-24 21:49:00 -040011474/**
James Smart5af5eee2010-10-22 11:06:38 -040011475 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11476 * @phba: pointer to lpfc HBA data structure.
11477 *
11478 * This routine will abort all pending and outstanding iocbs to an HBA.
11479 **/
11480void
11481lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11482{
11483 struct lpfc_sli *psli = &phba->sli;
11484 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011485 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011486 int i;
11487
James Smart895427b2017-02-12 13:52:30 -080011488 if (phba->sli_rev != LPFC_SLI_REV4) {
11489 for (i = 0; i < psli->num_rings; i++) {
11490 pring = &psli->sli3_ring[i];
11491 lpfc_sli_abort_iocb_ring(phba, pring);
11492 }
11493 return;
11494 }
11495 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11496 pring = qp->pring;
11497 if (!pring)
11498 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011499 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011500 }
11501}
11502
11503/**
James Smart3621a712009-04-06 18:47:14 -040011504 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011505 * @iocbq: Pointer to driver iocb object.
11506 * @vport: Pointer to driver virtual port object.
11507 * @tgt_id: SCSI ID of the target.
11508 * @lun_id: LUN ID of the scsi device.
11509 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11510 *
James Smart3621a712009-04-06 18:47:14 -040011511 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011512 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11513 * 0 if the filtering criteria is met for the given iocb and will return
11514 * 1 if the filtering criteria is not met.
11515 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11516 * given iocb is for the SCSI device specified by vport, tgt_id and
11517 * lun_id parameter.
11518 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11519 * given iocb is for the SCSI target specified by vport and tgt_id
11520 * parameters.
11521 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11522 * given iocb is for the SCSI host associated with the given vport.
11523 * This function is called with no locks held.
11524 **/
dea31012005-04-17 16:05:31 -050011525static int
James Smart51ef4c22007-08-02 11:10:31 -040011526lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11527 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011528 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011529{
James Smartc4908502019-01-28 11:14:28 -080011530 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011531 int rc = 1;
11532
James Smartb0e83012018-06-26 08:24:29 -070011533 if (iocbq->vport != vport)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011534 return rc;
11535
James Smartb0e83012018-06-26 08:24:29 -070011536 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
11537 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
James Smart51ef4c22007-08-02 11:10:31 -040011538 return rc;
11539
James Smartc4908502019-01-28 11:14:28 -080011540 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011541
James Smart495a7142008-06-14 22:52:59 -040011542 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011543 return rc;
11544
11545 switch (ctx_cmd) {
11546 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070011547 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011548 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11549 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011550 rc = 0;
11551 break;
11552 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070011553 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011554 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011555 rc = 0;
11556 break;
dea31012005-04-17 16:05:31 -050011557 case LPFC_CTX_HOST:
11558 rc = 0;
11559 break;
11560 default:
11561 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011562 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011563 break;
11564 }
11565
11566 return rc;
11567}
11568
James Smarte59058c2008-08-24 21:49:00 -040011569/**
James Smart3621a712009-04-06 18:47:14 -040011570 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011571 * @vport: Pointer to virtual port.
11572 * @tgt_id: SCSI ID of the target.
11573 * @lun_id: LUN ID of the scsi device.
11574 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11575 *
11576 * This function returns number of FCP commands pending for the vport.
11577 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11578 * commands pending on the vport associated with SCSI device specified
11579 * by tgt_id and lun_id parameters.
11580 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11581 * commands pending on the vport associated with SCSI target specified
11582 * by tgt_id parameter.
11583 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11584 * commands pending on the vport.
11585 * This function returns the number of iocbs which satisfy the filter.
11586 * This function is called without any lock held.
11587 **/
dea31012005-04-17 16:05:31 -050011588int
James Smart51ef4c22007-08-02 11:10:31 -040011589lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11590 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011591{
James Smart51ef4c22007-08-02 11:10:31 -040011592 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011593 struct lpfc_iocbq *iocbq;
11594 int sum, i;
dea31012005-04-17 16:05:31 -050011595
Johannes Thumshirn31979002016-07-18 16:06:03 +020011596 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011597 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11598 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011599
James Smart51ef4c22007-08-02 11:10:31 -040011600 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11601 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011602 sum++;
dea31012005-04-17 16:05:31 -050011603 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011604 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011605
dea31012005-04-17 16:05:31 -050011606 return sum;
11607}
11608
James Smarte59058c2008-08-24 21:49:00 -040011609/**
James Smart3621a712009-04-06 18:47:14 -040011610 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011611 * @phba: Pointer to HBA context object
11612 * @cmdiocb: Pointer to command iocb object.
11613 * @rspiocb: Pointer to response iocb object.
11614 *
11615 * This function is called when an aborted FCP iocb completes. This
11616 * function is called by the ring event handler with no lock held.
11617 * This function frees the iocb.
11618 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011619void
James Smart2e0fef82007-06-17 19:56:36 -050011620lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11621 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011622{
James Smartcb69f7d2011-12-13 13:21:57 -050011623 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011624 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011625 "original iotag x%x, abort cmd iotag x%x "
11626 "status 0x%x, reason 0x%x\n",
11627 cmdiocb->iocb.un.acxri.abortContextTag,
11628 cmdiocb->iocb.un.acxri.abortIoTag,
11629 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11630 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011631 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011632 return;
11633}
11634
James Smarte59058c2008-08-24 21:49:00 -040011635/**
James Smart3621a712009-04-06 18:47:14 -040011636 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011637 * @vport: Pointer to virtual port.
11638 * @pring: Pointer to driver SLI ring object.
11639 * @tgt_id: SCSI ID of the target.
11640 * @lun_id: LUN ID of the scsi device.
11641 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11642 *
11643 * This function sends an abort command for every SCSI command
11644 * associated with the given virtual port pending on the ring
11645 * filtered by lpfc_sli_validate_fcp_iocb function.
11646 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11647 * FCP iocbs associated with lun specified by tgt_id and lun_id
11648 * parameters
11649 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11650 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11651 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11652 * FCP iocbs associated with virtual port.
11653 * This function returns number of iocbs it failed to abort.
11654 * This function is called with no locks held.
11655 **/
dea31012005-04-17 16:05:31 -050011656int
James Smart51ef4c22007-08-02 11:10:31 -040011657lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11658 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011659{
James Smart51ef4c22007-08-02 11:10:31 -040011660 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011661 struct lpfc_iocbq *iocbq;
11662 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011663 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011664 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011665 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011666 int i;
dea31012005-04-17 16:05:31 -050011667
James Smartb0e83012018-06-26 08:24:29 -070011668 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070011669 if (phba->hba_flag & HBA_IOQ_FLUSH)
James Smartb0e83012018-06-26 08:24:29 -070011670 return errcnt;
11671
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011672 for (i = 1; i <= phba->sli.last_iotag; i++) {
11673 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011674
James Smart51ef4c22007-08-02 11:10:31 -040011675 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011676 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011677 continue;
11678
James Smartafbd8d82013-09-06 12:22:13 -040011679 /*
11680 * If the iocbq is already being aborted, don't take a second
11681 * action, but do count it.
11682 */
11683 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11684 continue;
11685
dea31012005-04-17 16:05:31 -050011686 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011687 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011688 if (abtsiocb == NULL) {
11689 errcnt++;
11690 continue;
11691 }
dea31012005-04-17 16:05:31 -050011692
James Smartafbd8d82013-09-06 12:22:13 -040011693 /* indicate the IO is being aborted by the driver. */
11694 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11695
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011696 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011697 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11698 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011699 if (phba->sli_rev == LPFC_SLI_REV4)
11700 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11701 else
11702 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011703 abtsiocb->iocb.ulpLe = 1;
11704 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011705 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011706
James Smart5ffc2662009-11-18 15:39:44 -050011707 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011708 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011709 if (iocbq->iocb_flag & LPFC_IO_FCP)
11710 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011711 if (iocbq->iocb_flag & LPFC_IO_FOF)
11712 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011713
James Smart2e0fef82007-06-17 19:56:36 -050011714 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011715 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11716 else
11717 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11718
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011719 /* Setup callback routine and issue the command. */
11720 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011721 if (phba->sli_rev == LPFC_SLI_REV4) {
11722 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11723 if (!pring_s4)
11724 continue;
11725 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11726 abtsiocb, 0);
11727 } else
11728 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11729 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011730 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011731 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011732 errcnt++;
11733 continue;
11734 }
11735 }
11736
11737 return errcnt;
11738}
11739
James Smarte59058c2008-08-24 21:49:00 -040011740/**
James Smart98912dda2014-04-04 13:52:31 -040011741 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11742 * @vport: Pointer to virtual port.
11743 * @pring: Pointer to driver SLI ring object.
11744 * @tgt_id: SCSI ID of the target.
11745 * @lun_id: LUN ID of the scsi device.
11746 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11747 *
11748 * This function sends an abort command for every SCSI command
11749 * associated with the given virtual port pending on the ring
11750 * filtered by lpfc_sli_validate_fcp_iocb function.
11751 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11752 * FCP iocbs associated with lun specified by tgt_id and lun_id
11753 * parameters
11754 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11755 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11756 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11757 * FCP iocbs associated with virtual port.
11758 * This function returns number of iocbs it aborted .
11759 * This function is called with no locks held right after a taskmgmt
11760 * command is sent.
11761 **/
11762int
11763lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11764 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11765{
11766 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -080011767 struct lpfc_io_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011768 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011769 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011770 struct lpfc_iocbq *iocbq;
11771 IOCB_t *icmd;
11772 int sum, i, ret_val;
11773 unsigned long iflags;
James Smartc2017262019-01-28 11:14:37 -080011774 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart98912dda2014-04-04 13:52:31 -040011775
James Smart59c68ea2018-04-09 14:24:25 -070011776 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011777
11778 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070011779 if (phba->hba_flag & HBA_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070011780 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011781 return 0;
11782 }
11783 sum = 0;
11784
11785 for (i = 1; i <= phba->sli.last_iotag; i++) {
11786 iocbq = phba->sli.iocbq_lookup[i];
11787
11788 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11789 cmd) != 0)
11790 continue;
11791
James Smartc2017262019-01-28 11:14:37 -080011792 /* Guard against IO completion being called at same time */
11793 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
11794 spin_lock(&lpfc_cmd->buf_lock);
11795
11796 if (!lpfc_cmd->pCmd) {
11797 spin_unlock(&lpfc_cmd->buf_lock);
11798 continue;
11799 }
11800
11801 if (phba->sli_rev == LPFC_SLI_REV4) {
11802 pring_s4 =
James Smartc00f62e2019-08-14 16:57:11 -070011803 phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
James Smartc2017262019-01-28 11:14:37 -080011804 if (!pring_s4) {
11805 spin_unlock(&lpfc_cmd->buf_lock);
11806 continue;
11807 }
11808 /* Note: both hbalock and ring_lock must be set here */
11809 spin_lock(&pring_s4->ring_lock);
11810 }
11811
James Smart98912dda2014-04-04 13:52:31 -040011812 /*
11813 * If the iocbq is already being aborted, don't take a second
11814 * action, but do count it.
11815 */
James Smartc2017262019-01-28 11:14:37 -080011816 if ((iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
11817 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
11818 if (phba->sli_rev == LPFC_SLI_REV4)
11819 spin_unlock(&pring_s4->ring_lock);
11820 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011821 continue;
James Smartc2017262019-01-28 11:14:37 -080011822 }
James Smart98912dda2014-04-04 13:52:31 -040011823
11824 /* issue ABTS for this IOCB based on iotag */
11825 abtsiocbq = __lpfc_sli_get_iocbq(phba);
James Smartc2017262019-01-28 11:14:37 -080011826 if (!abtsiocbq) {
11827 if (phba->sli_rev == LPFC_SLI_REV4)
11828 spin_unlock(&pring_s4->ring_lock);
11829 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011830 continue;
James Smartc2017262019-01-28 11:14:37 -080011831 }
James Smart98912dda2014-04-04 13:52:31 -040011832
11833 icmd = &iocbq->iocb;
11834 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11835 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11836 if (phba->sli_rev == LPFC_SLI_REV4)
11837 abtsiocbq->iocb.un.acxri.abortIoTag =
11838 iocbq->sli4_xritag;
11839 else
11840 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11841 abtsiocbq->iocb.ulpLe = 1;
11842 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11843 abtsiocbq->vport = vport;
11844
11845 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011846 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011847 if (iocbq->iocb_flag & LPFC_IO_FCP)
11848 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011849 if (iocbq->iocb_flag & LPFC_IO_FOF)
11850 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011851
James Smart8c50d252014-09-03 12:58:16 -040011852 ndlp = lpfc_cmd->rdata->pnode;
11853
11854 if (lpfc_is_link_up(phba) &&
11855 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011856 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11857 else
11858 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11859
11860 /* Setup callback routine and issue the command. */
11861 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11862
11863 /*
11864 * Indicate the IO is being aborted by the driver and set
11865 * the caller's flag into the aborted IO.
11866 */
11867 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11868
11869 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -040011870 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11871 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070011872 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011873 } else {
11874 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11875 abtsiocbq, 0);
11876 }
11877
James Smartc2017262019-01-28 11:14:37 -080011878 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011879
11880 if (ret_val == IOCB_ERROR)
11881 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11882 else
11883 sum++;
11884 }
James Smart59c68ea2018-04-09 14:24:25 -070011885 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011886 return sum;
11887}
11888
11889/**
James Smart3621a712009-04-06 18:47:14 -040011890 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011891 * @phba: Pointer to HBA context object.
11892 * @cmdiocbq: Pointer to command iocb.
11893 * @rspiocbq: Pointer to response iocb.
11894 *
11895 * This function is the completion handler for iocbs issued using
11896 * lpfc_sli_issue_iocb_wait function. This function is called by the
11897 * ring event handler function without any lock held. This function
11898 * can be called from both worker thread context and interrupt
11899 * context. This function also can be called from other thread which
11900 * cleans up the SLI layer objects.
11901 * This function copy the contents of the response iocb to the
11902 * response iocb memory object provided by the caller of
11903 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11904 * sleeps for the iocb completion.
11905 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011906static void
11907lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11908 struct lpfc_iocbq *cmdiocbq,
11909 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011910{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011911 wait_queue_head_t *pdone_q;
11912 unsigned long iflags;
James Smartc4908502019-01-28 11:14:28 -080011913 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011914
James Smart2e0fef82007-06-17 19:56:36 -050011915 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011916 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11917
11918 /*
11919 * A time out has occurred for the iocb. If a time out
11920 * completion handler has been supplied, call it. Otherwise,
11921 * just free the iocbq.
11922 */
11923
11924 spin_unlock_irqrestore(&phba->hbalock, iflags);
11925 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11926 cmdiocbq->wait_iocb_cmpl = NULL;
11927 if (cmdiocbq->iocb_cmpl)
11928 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11929 else
11930 lpfc_sli_release_iocbq(phba, cmdiocbq);
11931 return;
11932 }
11933
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011934 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11935 if (cmdiocbq->context2 && rspiocbq)
11936 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11937 &rspiocbq->iocb, sizeof(IOCB_t));
11938
James Smart0f65ff62010-02-26 14:14:23 -050011939 /* Set the exchange busy flag for task management commands */
11940 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11941 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smartc4908502019-01-28 11:14:28 -080011942 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
James Smart0f65ff62010-02-26 14:14:23 -050011943 cur_iocbq);
James Smart324e1c42019-10-18 14:18:21 -070011944 if (rspiocbq && (rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY))
11945 lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
11946 else
11947 lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
James Smart0f65ff62010-02-26 14:14:23 -050011948 }
11949
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011950 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011951 if (pdone_q)
11952 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011953 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011954 return;
11955}
11956
James Smarte59058c2008-08-24 21:49:00 -040011957/**
James Smartd11e31d2009-06-10 17:23:06 -040011958 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11959 * @phba: Pointer to HBA context object..
11960 * @piocbq: Pointer to command iocb.
11961 * @flag: Flag to test.
11962 *
11963 * This routine grabs the hbalock and then test the iocb_flag to
11964 * see if the passed in flag is set.
11965 * Returns:
11966 * 1 if flag is set.
11967 * 0 if flag is not set.
11968 **/
11969static int
11970lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11971 struct lpfc_iocbq *piocbq, uint32_t flag)
11972{
11973 unsigned long iflags;
11974 int ret;
11975
11976 spin_lock_irqsave(&phba->hbalock, iflags);
11977 ret = piocbq->iocb_flag & flag;
11978 spin_unlock_irqrestore(&phba->hbalock, iflags);
11979 return ret;
11980
11981}
11982
11983/**
James Smart3621a712009-04-06 18:47:14 -040011984 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011985 * @phba: Pointer to HBA context object..
11986 * @pring: Pointer to sli ring.
11987 * @piocb: Pointer to command iocb.
11988 * @prspiocbq: Pointer to response iocb.
11989 * @timeout: Timeout in number of seconds.
11990 *
11991 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011992 * iocb to complete. The iocb_cmpl field of the shall be used
11993 * to handle iocbs which time out. If the field is NULL, the
11994 * function shall free the iocbq structure. If more clean up is
11995 * needed, the caller is expected to provide a completion function
11996 * that will provide the needed clean up. If the iocb command is
11997 * not completed within timeout seconds, the function will either
11998 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11999 * completion function set in the iocb_cmpl field and then return
12000 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
12001 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040012002 * The function waits for the iocb completion using an
12003 * non-interruptible wait.
12004 * This function will sleep while waiting for iocb completion.
12005 * So, this function should not be called from any context which
12006 * does not allow sleeping. Due to the same reason, this function
12007 * cannot be called with interrupt disabled.
12008 * This function assumes that the iocb completions occur while
12009 * this function sleep. So, this function cannot be called from
12010 * the thread which process iocb completion for this ring.
12011 * This function clears the iocb_flag of the iocb object before
12012 * issuing the iocb and the iocb completion handler sets this
12013 * flag and wakes this thread when the iocb completes.
12014 * The contents of the response iocb will be copied to prspiocbq
12015 * by the completion handler when the command completes.
12016 * This function returns IOCB_SUCCESS when success.
12017 * This function is called with no lock held.
12018 **/
dea31012005-04-17 16:05:31 -050012019int
James Smart2e0fef82007-06-17 19:56:36 -050012020lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040012021 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050012022 struct lpfc_iocbq *piocb,
12023 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012024 uint32_t timeout)
dea31012005-04-17 16:05:31 -050012025{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080012026 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012027 long timeleft, timeout_req = 0;
12028 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012029 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050012030 struct lpfc_iocbq *iocb;
12031 int txq_cnt = 0;
12032 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080012033 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040012034 unsigned long iflags;
12035 bool iocb_completed = true;
12036
James Smart895427b2017-02-12 13:52:30 -080012037 if (phba->sli_rev >= LPFC_SLI_REV4)
12038 pring = lpfc_sli4_calc_ring(phba, piocb);
12039 else
12040 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050012041 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012042 * If the caller has provided a response iocbq buffer, then context2
12043 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050012044 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012045 if (prspiocbq) {
12046 if (piocb->context2)
12047 return IOCB_ERROR;
12048 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050012049 }
12050
James Smart5a0916b2013-07-15 18:31:42 -040012051 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012052 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
12053 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040012054 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050012055
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012056 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050012057 if (lpfc_readl(phba->HCregaddr, &creg_val))
12058 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012059 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
12060 writel(creg_val, phba->HCregaddr);
12061 readl(phba->HCregaddr); /* flush */
12062 }
12063
James Smart2a9bf3d2010-06-07 15:24:45 -040012064 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
12065 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012066 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040012067 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012068 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040012069 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012070 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040012071 spin_lock_irqsave(&phba->hbalock, iflags);
12072 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050012073
James Smart5a0916b2013-07-15 18:31:42 -040012074 /*
12075 * IOCB timed out. Inform the wake iocb wait
12076 * completion function and set local status
12077 */
12078
12079 iocb_completed = false;
12080 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
12081 }
12082 spin_unlock_irqrestore(&phba->hbalock, iflags);
12083 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040012084 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012085 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040012086 /* Note: we are not indicating if the IOCB has a success
12087 * status or not - that's for the caller to check.
12088 * IOCB_SUCCESS means just that the command was sent and
12089 * completed. Not that it completed successfully.
12090 * */
James Smart7054a602007-04-25 09:52:34 -040012091 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012092 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012093 "0338 IOCB wait timeout error - no "
12094 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012095 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040012096 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012097 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012098 "0330 IOCB wake NOT set, "
12099 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012100 timeout, (timeleft / jiffies));
12101 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050012102 }
James Smart2a9bf3d2010-06-07 15:24:45 -040012103 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012104 if (phba->cfg_log_verbose & LOG_SLI) {
12105 list_for_each_entry(iocb, &pring->txq, list) {
12106 txq_cnt++;
12107 }
12108 list_for_each_entry(iocb, &pring->txcmplq, list) {
12109 txcmplq_cnt++;
12110 }
12111 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12112 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
12113 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
12114 }
James Smart2a9bf3d2010-06-07 15:24:45 -040012115 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012116 } else {
12117 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012118 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012119 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012120 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050012121 }
12122
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012123 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050012124 if (lpfc_readl(phba->HCregaddr, &creg_val))
12125 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012126 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
12127 writel(creg_val, phba->HCregaddr);
12128 readl(phba->HCregaddr); /* flush */
12129 }
12130
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012131 if (prspiocbq)
12132 piocb->context2 = NULL;
12133
12134 piocb->context_un.wait_queue = NULL;
12135 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050012136 return retval;
12137}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012138
James Smarte59058c2008-08-24 21:49:00 -040012139/**
James Smart3621a712009-04-06 18:47:14 -040012140 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040012141 * @phba: Pointer to HBA context object.
12142 * @pmboxq: Pointer to driver mailbox object.
12143 * @timeout: Timeout in number of seconds.
12144 *
12145 * This function issues the mailbox to firmware and waits for the
12146 * mailbox command to complete. If the mailbox command is not
12147 * completed within timeout seconds, it returns MBX_TIMEOUT.
12148 * The function waits for the mailbox completion using an
12149 * interruptible wait. If the thread is woken up due to a
12150 * signal, MBX_TIMEOUT error is returned to the caller. Caller
12151 * should not free the mailbox resources, if this function returns
12152 * MBX_TIMEOUT.
12153 * This function will sleep while waiting for mailbox completion.
12154 * So, this function should not be called from any context which
12155 * does not allow sleeping. Due to the same reason, this function
12156 * cannot be called with interrupt disabled.
12157 * This function assumes that the mailbox completion occurs while
12158 * this function sleep. So, this function cannot be called from
12159 * the worker thread which processes mailbox completion.
12160 * This function is called in the context of HBA management
12161 * applications.
12162 * This function returns MBX_SUCCESS when successful.
12163 * This function is called with no lock held.
12164 **/
dea31012005-04-17 16:05:31 -050012165int
James Smart2e0fef82007-06-17 19:56:36 -050012166lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050012167 uint32_t timeout)
12168{
James Smarte29d74f2018-03-05 12:04:07 -080012169 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050012170 int retval;
James Smart858c9f62007-06-17 19:56:39 -050012171 unsigned long flag;
dea31012005-04-17 16:05:31 -050012172
James Smart495a7142008-06-14 22:52:59 -040012173 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050012174 /* setup wake call as IOCB callback */
12175 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050012176
James Smarte29d74f2018-03-05 12:04:07 -080012177 /* setup context3 field to pass wait_queue pointer to wake function */
12178 init_completion(&mbox_done);
12179 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050012180 /* now issue the command */
12181 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050012182 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080012183 wait_for_completion_timeout(&mbox_done,
12184 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040012185
James Smart858c9f62007-06-17 19:56:39 -050012186 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080012187 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040012188 /*
12189 * if LPFC_MBX_WAKE flag is set the mailbox is completed
12190 * else do not free the resources.
12191 */
James Smartd7c47992010-06-08 18:31:54 -040012192 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050012193 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040012194 } else {
James Smart7054a602007-04-25 09:52:34 -040012195 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050012196 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12197 }
12198 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050012199 }
dea31012005-04-17 16:05:31 -050012200 return retval;
12201}
12202
James Smarte59058c2008-08-24 21:49:00 -040012203/**
James Smart3772a992009-05-22 14:50:54 -040012204 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040012205 * @phba: Pointer to HBA context.
12206 *
James Smart3772a992009-05-22 14:50:54 -040012207 * This function is called to shutdown the driver's mailbox sub-system.
12208 * It first marks the mailbox sub-system is in a block state to prevent
12209 * the asynchronous mailbox command from issued off the pending mailbox
12210 * command queue. If the mailbox command sub-system shutdown is due to
12211 * HBA error conditions such as EEH or ERATT, this routine shall invoke
12212 * the mailbox sub-system flush routine to forcefully bring down the
12213 * mailbox sub-system. Otherwise, if it is due to normal condition (such
12214 * as with offline or HBA function reset), this routine will wait for the
12215 * outstanding mailbox command to complete before invoking the mailbox
12216 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040012217 **/
James Smart3772a992009-05-22 14:50:54 -040012218void
James Smart618a5232012-06-12 13:54:36 -040012219lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040012220{
James Smart3772a992009-05-22 14:50:54 -040012221 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040012222 unsigned long timeout;
12223
James Smart618a5232012-06-12 13:54:36 -040012224 if (mbx_action == LPFC_MBX_NO_WAIT) {
12225 /* delay 100ms for port state */
12226 msleep(100);
12227 lpfc_sli_mbox_sys_flush(phba);
12228 return;
12229 }
James Smarta183a152011-10-10 21:32:43 -040012230 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050012231
James Smart523128e2018-09-10 10:30:46 -070012232 /* Disable softirqs, including timers from obtaining phba->hbalock */
12233 local_bh_disable();
12234
James Smart3772a992009-05-22 14:50:54 -040012235 spin_lock_irq(&phba->hbalock);
12236 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040012237
12238 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040012239 /* Determine how long we might wait for the active mailbox
12240 * command to be gracefully completed by firmware.
12241 */
James Smarta183a152011-10-10 21:32:43 -040012242 if (phba->sli.mbox_active)
12243 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12244 phba->sli.mbox_active) *
12245 1000) + jiffies;
12246 spin_unlock_irq(&phba->hbalock);
12247
James Smart523128e2018-09-10 10:30:46 -070012248 /* Enable softirqs again, done with phba->hbalock */
12249 local_bh_enable();
12250
James Smart3772a992009-05-22 14:50:54 -040012251 while (phba->sli.mbox_active) {
12252 /* Check active mailbox complete status every 2ms */
12253 msleep(2);
12254 if (time_after(jiffies, timeout))
12255 /* Timeout, let the mailbox flush routine to
12256 * forcefully release active mailbox command
12257 */
12258 break;
12259 }
James Smart523128e2018-09-10 10:30:46 -070012260 } else {
James Smartd7069f02012-03-01 22:36:29 -050012261 spin_unlock_irq(&phba->hbalock);
12262
James Smart523128e2018-09-10 10:30:46 -070012263 /* Enable softirqs again, done with phba->hbalock */
12264 local_bh_enable();
12265 }
12266
James Smart3772a992009-05-22 14:50:54 -040012267 lpfc_sli_mbox_sys_flush(phba);
12268}
12269
12270/**
12271 * lpfc_sli_eratt_read - read sli-3 error attention events
12272 * @phba: Pointer to HBA context.
12273 *
12274 * This function is called to read the SLI3 device error attention registers
12275 * for possible error attention events. The caller must hold the hostlock
12276 * with spin_lock_irq().
12277 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012278 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040012279 * Register and returns 0 otherwise.
12280 **/
12281static int
12282lpfc_sli_eratt_read(struct lpfc_hba *phba)
12283{
James Smarted957682007-06-17 19:56:37 -050012284 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040012285
James Smart3772a992009-05-22 14:50:54 -040012286 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050012287 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12288 goto unplug_err;
12289
James Smart3772a992009-05-22 14:50:54 -040012290 if (ha_copy & HA_ERATT) {
12291 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050012292 if (lpfc_sli_read_hs(phba))
12293 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040012294
James Smart3772a992009-05-22 14:50:54 -040012295 /* Check if there is a deferred error condition is active */
12296 if ((HS_FFER1 & phba->work_hs) &&
12297 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012298 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040012299 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040012300 /* Clear all interrupt enable conditions */
12301 writel(0, phba->HCregaddr);
12302 readl(phba->HCregaddr);
12303 }
12304
12305 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040012306 phba->work_ha |= HA_ERATT;
12307 /* Indicate polling handles this ERATT */
12308 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012309 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012310 }
James Smart3772a992009-05-22 14:50:54 -040012311 return 0;
James Smart9940b972011-03-11 16:06:12 -050012312
12313unplug_err:
12314 /* Set the driver HS work bitmap */
12315 phba->work_hs |= UNPLUG_ERR;
12316 /* Set the driver HA work bitmap */
12317 phba->work_ha |= HA_ERATT;
12318 /* Indicate polling handles this ERATT */
12319 phba->hba_flag |= HBA_ERATT_HANDLED;
12320 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012321}
12322
James Smarte59058c2008-08-24 21:49:00 -040012323/**
James Smartda0436e2009-05-22 14:51:39 -040012324 * lpfc_sli4_eratt_read - read sli-4 error attention events
12325 * @phba: Pointer to HBA context.
12326 *
12327 * This function is called to read the SLI4 device error attention registers
12328 * for possible error attention events. The caller must hold the hostlock
12329 * with spin_lock_irq().
12330 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012331 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040012332 * Register and returns 0 otherwise.
12333 **/
12334static int
12335lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12336{
12337 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050012338 uint32_t if_type, portsmphr;
12339 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040012340
James Smart2fcee4b2010-12-15 17:57:46 -050012341 /*
12342 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040012343 * registers for error attention. This can be changed later.
12344 */
James Smart2fcee4b2010-12-15 17:57:46 -050012345 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12346 switch (if_type) {
12347 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050012348 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12349 &uerr_sta_lo) ||
12350 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12351 &uerr_sta_hi)) {
12352 phba->work_hs |= UNPLUG_ERR;
12353 phba->work_ha |= HA_ERATT;
12354 phba->hba_flag |= HBA_ERATT_HANDLED;
12355 return 1;
12356 }
James Smart2fcee4b2010-12-15 17:57:46 -050012357 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12358 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12360 "1423 HBA Unrecoverable error: "
12361 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12362 "ue_mask_lo_reg=0x%x, "
12363 "ue_mask_hi_reg=0x%x\n",
12364 uerr_sta_lo, uerr_sta_hi,
12365 phba->sli4_hba.ue_mask_lo,
12366 phba->sli4_hba.ue_mask_hi);
12367 phba->work_status[0] = uerr_sta_lo;
12368 phba->work_status[1] = uerr_sta_hi;
12369 phba->work_ha |= HA_ERATT;
12370 phba->hba_flag |= HBA_ERATT_HANDLED;
12371 return 1;
12372 }
12373 break;
12374 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080012375 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050012376 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12377 &portstat_reg.word0) ||
12378 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12379 &portsmphr)){
12380 phba->work_hs |= UNPLUG_ERR;
12381 phba->work_ha |= HA_ERATT;
12382 phba->hba_flag |= HBA_ERATT_HANDLED;
12383 return 1;
12384 }
James Smart2fcee4b2010-12-15 17:57:46 -050012385 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12386 phba->work_status[0] =
12387 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12388 phba->work_status[1] =
12389 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050012391 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050012392 "port status reg 0x%x, "
12393 "port smphr reg 0x%x, "
12394 "error 1=0x%x, error 2=0x%x\n",
12395 portstat_reg.word0,
12396 portsmphr,
12397 phba->work_status[0],
12398 phba->work_status[1]);
12399 phba->work_ha |= HA_ERATT;
12400 phba->hba_flag |= HBA_ERATT_HANDLED;
12401 return 1;
12402 }
12403 break;
12404 case LPFC_SLI_INTF_IF_TYPE_1:
12405 default:
James Smarta747c9c2009-11-18 15:41:10 -050012406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050012407 "2886 HBA Error Attention on unsupported "
12408 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050012409 return 1;
James Smartda0436e2009-05-22 14:51:39 -040012410 }
James Smart2fcee4b2010-12-15 17:57:46 -050012411
James Smartda0436e2009-05-22 14:51:39 -040012412 return 0;
12413}
12414
12415/**
James Smart3621a712009-04-06 18:47:14 -040012416 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040012417 * @phba: Pointer to HBA context.
12418 *
James Smart3772a992009-05-22 14:50:54 -040012419 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040012420 * error attention register bit for error attention events.
12421 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012422 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040012423 * Register and returns 0 otherwise.
12424 **/
12425int
12426lpfc_sli_check_eratt(struct lpfc_hba *phba)
12427{
12428 uint32_t ha_copy;
12429
12430 /* If somebody is waiting to handle an eratt, don't process it
12431 * here. The brdkill function will do this.
12432 */
12433 if (phba->link_flag & LS_IGNORE_ERATT)
12434 return 0;
12435
12436 /* Check if interrupt handler handles this ERATT */
12437 spin_lock_irq(&phba->hbalock);
12438 if (phba->hba_flag & HBA_ERATT_HANDLED) {
12439 /* Interrupt handler has handled ERATT */
12440 spin_unlock_irq(&phba->hbalock);
12441 return 0;
12442 }
12443
James Smarta257bf92009-04-06 18:48:10 -040012444 /*
12445 * If there is deferred error attention, do not check for error
12446 * attention
12447 */
12448 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12449 spin_unlock_irq(&phba->hbalock);
12450 return 0;
12451 }
12452
James Smart3772a992009-05-22 14:50:54 -040012453 /* If PCI channel is offline, don't process it */
12454 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040012455 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012456 return 0;
12457 }
12458
12459 switch (phba->sli_rev) {
12460 case LPFC_SLI_REV2:
12461 case LPFC_SLI_REV3:
12462 /* Read chip Host Attention (HA) register */
12463 ha_copy = lpfc_sli_eratt_read(phba);
12464 break;
James Smartda0436e2009-05-22 14:51:39 -040012465 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050012466 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040012467 ha_copy = lpfc_sli4_eratt_read(phba);
12468 break;
James Smart3772a992009-05-22 14:50:54 -040012469 default:
12470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12471 "0299 Invalid SLI revision (%d)\n",
12472 phba->sli_rev);
12473 ha_copy = 0;
12474 break;
James Smart93996272008-08-24 21:50:30 -040012475 }
12476 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012477
12478 return ha_copy;
12479}
12480
12481/**
12482 * lpfc_intr_state_check - Check device state for interrupt handling
12483 * @phba: Pointer to HBA context.
12484 *
12485 * This inline routine checks whether a device or its PCI slot is in a state
12486 * that the interrupt should be handled.
12487 *
12488 * This function returns 0 if the device or the PCI slot is in a state that
12489 * interrupt should be handled, otherwise -EIO.
12490 */
12491static inline int
12492lpfc_intr_state_check(struct lpfc_hba *phba)
12493{
12494 /* If the pci channel is offline, ignore all the interrupts */
12495 if (unlikely(pci_channel_offline(phba->pcidev)))
12496 return -EIO;
12497
12498 /* Update device level interrupt statistics */
12499 phba->sli.slistat.sli_intr++;
12500
12501 /* Ignore all interrupts during initialization. */
12502 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12503 return -EIO;
12504
James Smart93996272008-08-24 21:50:30 -040012505 return 0;
12506}
12507
12508/**
James Smart3772a992009-05-22 14:50:54 -040012509 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012510 * @irq: Interrupt number.
12511 * @dev_id: The device context pointer.
12512 *
James Smart93996272008-08-24 21:50:30 -040012513 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012514 * service routine when device with SLI-3 interface spec is enabled with
12515 * MSI-X multi-message interrupt mode and there are slow-path events in
12516 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12517 * interrupt mode, this function is called as part of the device-level
12518 * interrupt handler. When the PCI slot is in error recovery or the HBA
12519 * is undergoing initialization, the interrupt handler will not process
12520 * the interrupt. The link attention and ELS ring attention events are
12521 * handled by the worker thread. The interrupt handler signals the worker
12522 * thread and returns for these events. This function is called without
12523 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012524 * structures.
12525 *
12526 * This function returns IRQ_HANDLED when interrupt is handled else it
12527 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012528 **/
dea31012005-04-17 16:05:31 -050012529irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012530lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012531{
James Smart2e0fef82007-06-17 19:56:36 -050012532 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012533 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012534 uint32_t work_ha_copy;
12535 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012536 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012537 uint32_t control;
12538
James Smart92d7f7b2007-06-17 19:56:38 -050012539 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012540 struct lpfc_vport *vport;
12541 struct lpfc_nodelist *ndlp;
12542 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012543 LPFC_MBOXQ_t *pmb;
12544 int rc;
12545
dea31012005-04-17 16:05:31 -050012546 /*
12547 * Get the driver's phba structure from the dev_id and
12548 * assume the HBA is not interrupting.
12549 */
James Smart93996272008-08-24 21:50:30 -040012550 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012551
12552 if (unlikely(!phba))
12553 return IRQ_NONE;
12554
dea31012005-04-17 16:05:31 -050012555 /*
James Smart93996272008-08-24 21:50:30 -040012556 * Stuff needs to be attented to when this function is invoked as an
12557 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012558 */
James Smart93996272008-08-24 21:50:30 -040012559 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012560 /* Check device state for handling interrupt */
12561 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012562 return IRQ_NONE;
12563 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012564 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012565 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12566 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012567 /* If somebody is waiting to handle an eratt don't process it
12568 * here. The brdkill function will do this.
12569 */
12570 if (phba->link_flag & LS_IGNORE_ERATT)
12571 ha_copy &= ~HA_ERATT;
12572 /* Check the need for handling ERATT in interrupt handler */
12573 if (ha_copy & HA_ERATT) {
12574 if (phba->hba_flag & HBA_ERATT_HANDLED)
12575 /* ERATT polling has handled ERATT */
12576 ha_copy &= ~HA_ERATT;
12577 else
12578 /* Indicate interrupt handler handles ERATT */
12579 phba->hba_flag |= HBA_ERATT_HANDLED;
12580 }
James Smarta257bf92009-04-06 18:48:10 -040012581
12582 /*
12583 * If there is deferred error attention, do not check for any
12584 * interrupt.
12585 */
12586 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012587 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012588 return IRQ_NONE;
12589 }
12590
James Smart93996272008-08-24 21:50:30 -040012591 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012592 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12593 goto unplug_error;
12594
James Smarta747c9c2009-11-18 15:41:10 -050012595 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12596 HC_LAINT_ENA | HC_ERINT_ENA),
12597 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012598 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12599 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012600 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012601 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012602 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012603 } else
12604 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012605
dea31012005-04-17 16:05:31 -050012606 work_ha_copy = ha_copy & phba->work_ha_mask;
12607
James Smart93996272008-08-24 21:50:30 -040012608 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012609 if (work_ha_copy & HA_LATT) {
12610 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12611 /*
12612 * Turn off Link Attention interrupts
12613 * until CLEAR_LA done
12614 */
James Smart5b75da22008-12-04 22:39:35 -050012615 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012616 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012617 if (lpfc_readl(phba->HCregaddr, &control))
12618 goto unplug_error;
dea31012005-04-17 16:05:31 -050012619 control &= ~HC_LAINT_ENA;
12620 writel(control, phba->HCregaddr);
12621 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012622 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012623 }
12624 else
12625 work_ha_copy &= ~HA_LATT;
12626 }
12627
James Smart93996272008-08-24 21:50:30 -040012628 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012629 /*
12630 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12631 * the only slow ring.
12632 */
12633 status = (work_ha_copy &
12634 (HA_RXMASK << (4*LPFC_ELS_RING)));
12635 status >>= (4*LPFC_ELS_RING);
12636 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012637 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012638 if (lpfc_readl(phba->HCregaddr, &control))
12639 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012640
12641 lpfc_debugfs_slow_ring_trc(phba,
12642 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12643 control, status,
12644 (uint32_t)phba->sli.slistat.sli_intr);
12645
James Smart858c9f62007-06-17 19:56:39 -050012646 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012647 lpfc_debugfs_slow_ring_trc(phba,
12648 "ISR Disable ring:"
12649 "pwork:x%x hawork:x%x wait:x%x",
12650 phba->work_ha, work_ha_copy,
12651 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012652 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012653
James Smart858c9f62007-06-17 19:56:39 -050012654 control &=
12655 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012656 writel(control, phba->HCregaddr);
12657 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012658 }
James Smarta58cbd52007-08-02 11:09:43 -040012659 else {
12660 lpfc_debugfs_slow_ring_trc(phba,
12661 "ISR slow ring: pwork:"
12662 "x%x hawork:x%x wait:x%x",
12663 phba->work_ha, work_ha_copy,
12664 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012665 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012666 }
James Smart5b75da22008-12-04 22:39:35 -050012667 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012668 }
12669 }
James Smart5b75da22008-12-04 22:39:35 -050012670 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012671 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012672 if (lpfc_sli_read_hs(phba))
12673 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012674 /*
12675 * Check if there is a deferred error condition
12676 * is active
12677 */
12678 if ((HS_FFER1 & phba->work_hs) &&
12679 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012680 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12681 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012682 phba->hba_flag |= DEFER_ERATT;
12683 /* Clear all interrupt enable conditions */
12684 writel(0, phba->HCregaddr);
12685 readl(phba->HCregaddr);
12686 }
12687 }
12688
James Smart93996272008-08-24 21:50:30 -040012689 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012690 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012691 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012692 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012693 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012694
12695 /* First check out the status word */
12696 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12697 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012698 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012699 /*
12700 * Stray Mailbox Interrupt, mbxCommand <cmd>
12701 * mbxStatus <status>
12702 */
James Smart09372822008-01-11 01:52:54 -050012703 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012704 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012705 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012706 "Interrupt mbxCommand x%x "
12707 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012708 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012709 pmbox->mbxCommand,
12710 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012711 /* clear mailbox attention bit */
12712 work_ha_copy &= ~HA_MBATT;
12713 } else {
James Smart97eab632008-04-07 10:16:05 -040012714 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012715 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012716 phba->last_completion_time = jiffies;
12717 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012718 if (pmb->mbox_cmpl) {
12719 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12720 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012721 if (pmb->out_ext_byte_len &&
James Smart3e1f0712018-11-29 16:09:29 -080012722 pmb->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -040012723 lpfc_sli_pcimem_bcopy(
12724 phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -080012725 pmb->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -040012726 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012727 }
James Smart09372822008-01-11 01:52:54 -050012728 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12729 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12730
12731 lpfc_debugfs_disc_trc(vport,
12732 LPFC_DISC_TRC_MBOX_VPORT,
12733 "MBOX dflt rpi: : "
12734 "status:x%x rpi:x%x",
12735 (uint32_t)pmbox->mbxStatus,
12736 pmbox->un.varWords[0], 0);
12737
12738 if (!pmbox->mbxStatus) {
12739 mp = (struct lpfc_dmabuf *)
James Smart3e1f0712018-11-29 16:09:29 -080012740 (pmb->ctx_buf);
James Smart09372822008-01-11 01:52:54 -050012741 ndlp = (struct lpfc_nodelist *)
James Smart3e1f0712018-11-29 16:09:29 -080012742 pmb->ctx_ndlp;
James Smart09372822008-01-11 01:52:54 -050012743
12744 /* Reg_LOGIN of dflt RPI was
12745 * successful. new lets get
12746 * rid of the RPI using the
12747 * same mbox buffer.
12748 */
12749 lpfc_unreg_login(phba,
12750 vport->vpi,
12751 pmbox->un.varWords[0],
12752 pmb);
12753 pmb->mbox_cmpl =
12754 lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080012755 pmb->ctx_buf = mp;
12756 pmb->ctx_ndlp = ndlp;
James Smart09372822008-01-11 01:52:54 -050012757 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012758 rc = lpfc_sli_issue_mbox(phba,
12759 pmb,
12760 MBX_NOWAIT);
12761 if (rc != MBX_BUSY)
12762 lpfc_printf_log(phba,
12763 KERN_ERR,
12764 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012765 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012766 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012767 if (rc != MBX_NOT_FINISHED)
12768 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012769 }
12770 }
James Smart5b75da22008-12-04 22:39:35 -050012771 spin_lock_irqsave(
12772 &phba->pport->work_port_lock,
12773 iflag);
James Smart09372822008-01-11 01:52:54 -050012774 phba->pport->work_port_events &=
12775 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012776 spin_unlock_irqrestore(
12777 &phba->pport->work_port_lock,
12778 iflag);
James Smart09372822008-01-11 01:52:54 -050012779 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012780 }
James Smart97eab632008-04-07 10:16:05 -040012781 } else
James Smart5b75da22008-12-04 22:39:35 -050012782 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012783
James Smart92d7f7b2007-06-17 19:56:38 -050012784 if ((work_ha_copy & HA_MBATT) &&
12785 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012786send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012787 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012788 do {
12789 rc = lpfc_sli_issue_mbox(phba, NULL,
12790 MBX_NOWAIT);
12791 } while (rc == MBX_NOT_FINISHED);
12792 if (rc != MBX_SUCCESS)
12793 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12794 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012795 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012796 }
12797
James Smart5b75da22008-12-04 22:39:35 -050012798 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012799 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012800 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012801 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012802 }
James Smart93996272008-08-24 21:50:30 -040012803 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012804unplug_error:
12805 spin_unlock_irqrestore(&phba->hbalock, iflag);
12806 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012807
James Smart3772a992009-05-22 14:50:54 -040012808} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012809
12810/**
James Smart3772a992009-05-22 14:50:54 -040012811 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012812 * @irq: Interrupt number.
12813 * @dev_id: The device context pointer.
12814 *
12815 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012816 * service routine when device with SLI-3 interface spec is enabled with
12817 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12818 * ring event in the HBA. However, when the device is enabled with either
12819 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12820 * device-level interrupt handler. When the PCI slot is in error recovery
12821 * or the HBA is undergoing initialization, the interrupt handler will not
12822 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12823 * the intrrupt context. This function is called without any lock held.
12824 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012825 *
12826 * This function returns IRQ_HANDLED when interrupt is handled else it
12827 * returns IRQ_NONE.
12828 **/
12829irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012830lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012831{
12832 struct lpfc_hba *phba;
12833 uint32_t ha_copy;
12834 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012835 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012836 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012837
12838 /* Get the driver's phba structure from the dev_id and
12839 * assume the HBA is not interrupting.
12840 */
12841 phba = (struct lpfc_hba *) dev_id;
12842
12843 if (unlikely(!phba))
12844 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012845
12846 /*
James Smart93996272008-08-24 21:50:30 -040012847 * Stuff needs to be attented to when this function is invoked as an
12848 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012849 */
James Smart93996272008-08-24 21:50:30 -040012850 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012851 /* Check device state for handling interrupt */
12852 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012853 return IRQ_NONE;
12854 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012855 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12856 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012857 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012858 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012859 /*
12860 * If there is deferred error attention, do not check for
12861 * any interrupt.
12862 */
12863 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012864 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012865 return IRQ_NONE;
12866 }
James Smart93996272008-08-24 21:50:30 -040012867 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12868 phba->HAregaddr);
12869 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012870 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012871 } else
12872 ha_copy = phba->ha_copy;
12873
12874 /*
12875 * Process all events on FCP ring. Take the optimized path for FCP IO.
12876 */
12877 ha_copy &= ~(phba->work_ha_mask);
12878
12879 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012880 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012881 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012882 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012883 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012884
12885 if (phba->cfg_multi_ring_support == 2) {
12886 /*
James Smart93996272008-08-24 21:50:30 -040012887 * Process all events on extra ring. Take the optimized path
12888 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012889 */
James Smart93996272008-08-24 21:50:30 -040012890 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012891 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012892 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012893 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012894 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012895 status);
12896 }
12897 }
dea31012005-04-17 16:05:31 -050012898 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012899} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012900
James Smart93996272008-08-24 21:50:30 -040012901/**
James Smart3772a992009-05-22 14:50:54 -040012902 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012903 * @irq: Interrupt number.
12904 * @dev_id: The device context pointer.
12905 *
James Smart3772a992009-05-22 14:50:54 -040012906 * This function is the HBA device-level interrupt handler to device with
12907 * SLI-3 interface spec, called from the PCI layer when either MSI or
12908 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12909 * requires driver attention. This function invokes the slow-path interrupt
12910 * attention handling function and fast-path interrupt attention handling
12911 * function in turn to process the relevant HBA attention events. This
12912 * function is called without any lock held. It gets the hbalock to access
12913 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012914 *
12915 * This function returns IRQ_HANDLED when interrupt is handled, else it
12916 * returns IRQ_NONE.
12917 **/
12918irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012919lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012920{
12921 struct lpfc_hba *phba;
12922 irqreturn_t sp_irq_rc, fp_irq_rc;
12923 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012924 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012925
12926 /*
12927 * Get the driver's phba structure from the dev_id and
12928 * assume the HBA is not interrupting.
12929 */
12930 phba = (struct lpfc_hba *) dev_id;
12931
12932 if (unlikely(!phba))
12933 return IRQ_NONE;
12934
James Smart3772a992009-05-22 14:50:54 -040012935 /* Check device state for handling interrupt */
12936 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012937 return IRQ_NONE;
12938
12939 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012940 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12941 spin_unlock(&phba->hbalock);
12942 return IRQ_HANDLED;
12943 }
12944
James Smart93996272008-08-24 21:50:30 -040012945 if (unlikely(!phba->ha_copy)) {
12946 spin_unlock(&phba->hbalock);
12947 return IRQ_NONE;
12948 } else if (phba->ha_copy & HA_ERATT) {
12949 if (phba->hba_flag & HBA_ERATT_HANDLED)
12950 /* ERATT polling has handled ERATT */
12951 phba->ha_copy &= ~HA_ERATT;
12952 else
12953 /* Indicate interrupt handler handles ERATT */
12954 phba->hba_flag |= HBA_ERATT_HANDLED;
12955 }
12956
James Smarta257bf92009-04-06 18:48:10 -040012957 /*
12958 * If there is deferred error attention, do not check for any interrupt.
12959 */
12960 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012961 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012962 return IRQ_NONE;
12963 }
12964
James Smart93996272008-08-24 21:50:30 -040012965 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012966 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12967 spin_unlock(&phba->hbalock);
12968 return IRQ_HANDLED;
12969 }
James Smarta747c9c2009-11-18 15:41:10 -050012970 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12971 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12972 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012973 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012974 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012975 readl(phba->HAregaddr); /* flush */
12976 spin_unlock(&phba->hbalock);
12977
12978 /*
12979 * Invokes slow-path host attention interrupt handling as appropriate.
12980 */
12981
12982 /* status of events with mailbox and link attention */
12983 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12984
12985 /* status of events with ELS ring */
12986 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12987 status2 >>= (4*LPFC_ELS_RING);
12988
12989 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012990 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012991 else
12992 sp_irq_rc = IRQ_NONE;
12993
12994 /*
12995 * Invoke fast-path host attention interrupt handling as appropriate.
12996 */
12997
12998 /* status of events with FCP ring */
12999 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13000 status1 >>= (4*LPFC_FCP_RING);
13001
13002 /* status of events with extra ring */
13003 if (phba->cfg_multi_ring_support == 2) {
13004 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
13005 status2 >>= (4*LPFC_EXTRA_RING);
13006 } else
13007 status2 = 0;
13008
13009 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040013010 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040013011 else
13012 fp_irq_rc = IRQ_NONE;
13013
13014 /* Return device-level interrupt handling status */
13015 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040013016} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040013017
13018/**
James Smart4f774512009-05-22 14:52:35 -040013019 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
13020 * @phba: pointer to lpfc hba data structure.
13021 *
13022 * This routine is invoked by the worker thread to process all the pending
13023 * SLI4 els abort xri events.
13024 **/
13025void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
13026{
13027 struct lpfc_cq_event *cq_event;
13028
13029 /* First, declare the els xri abort event has been handled */
13030 spin_lock_irq(&phba->hbalock);
13031 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
13032 spin_unlock_irq(&phba->hbalock);
13033 /* Now, handle all the els xri abort events */
13034 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
13035 /* Get the first event from the head of the event queue */
13036 spin_lock_irq(&phba->hbalock);
13037 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
13038 cq_event, struct lpfc_cq_event, list);
13039 spin_unlock_irq(&phba->hbalock);
13040 /* Notify aborted XRI for ELS work queue */
13041 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
13042 /* Free the event processed back to the free pool */
13043 lpfc_sli4_cq_event_release(phba, cq_event);
13044 }
13045}
13046
James Smart341af102010-01-26 23:07:37 -050013047/**
13048 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
13049 * @phba: pointer to lpfc hba data structure
13050 * @pIocbIn: pointer to the rspiocbq
13051 * @pIocbOut: pointer to the cmdiocbq
13052 * @wcqe: pointer to the complete wcqe
13053 *
13054 * This routine transfers the fields of a command iocbq to a response iocbq
13055 * by copying all the IOCB fields from command iocbq and transferring the
13056 * completion status information from the complete wcqe.
13057 **/
James Smart4f774512009-05-22 14:52:35 -040013058static void
James Smart341af102010-01-26 23:07:37 -050013059lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
13060 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040013061 struct lpfc_iocbq *pIocbOut,
13062 struct lpfc_wcqe_complete *wcqe)
13063{
James Smartaf227412013-10-10 12:23:10 -040013064 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050013065 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040013066 uint32_t status, max_response;
13067 struct lpfc_dmabuf *dmabuf;
13068 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040013069 size_t offset = offsetof(struct lpfc_iocbq, iocb);
13070
13071 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
13072 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040013073 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050013074 status = bf_get(lpfc_wcqe_c_status, wcqe);
13075 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040013076 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
13077 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
13078 pIocbIn->iocb.un.fcpi.fcpi_parm =
13079 pIocbOut->iocb.un.fcpi.fcpi_parm -
13080 wcqe->total_data_placed;
13081 else
13082 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050013083 else {
James Smart4f774512009-05-22 14:52:35 -040013084 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040013085 switch (pIocbOut->iocb.ulpCommand) {
13086 case CMD_ELS_REQUEST64_CR:
13087 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
13088 bpl = (struct ulp_bde64 *)dmabuf->virt;
13089 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
13090 max_response = bde.tus.f.bdeSize;
13091 break;
13092 case CMD_GEN_REQUEST64_CR:
13093 max_response = 0;
13094 if (!pIocbOut->context3)
13095 break;
13096 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
13097 sizeof(struct ulp_bde64);
13098 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
13099 bpl = (struct ulp_bde64 *)dmabuf->virt;
13100 for (i = 0; i < numBdes; i++) {
13101 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
13102 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
13103 max_response += bde.tus.f.bdeSize;
13104 }
13105 break;
13106 default:
13107 max_response = wcqe->total_data_placed;
13108 break;
13109 }
13110 if (max_response < wcqe->total_data_placed)
13111 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
13112 else
13113 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
13114 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050013115 }
James Smart341af102010-01-26 23:07:37 -050013116
James Smartacd68592012-01-18 16:25:09 -050013117 /* Convert BG errors for completion status */
13118 if (status == CQE_STATUS_DI_ERROR) {
13119 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
13120
13121 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
13122 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
13123 else
13124 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
13125
13126 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
13127 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
13128 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13129 BGS_GUARD_ERR_MASK;
13130 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
13131 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13132 BGS_APPTAG_ERR_MASK;
13133 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
13134 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13135 BGS_REFTAG_ERR_MASK;
13136
13137 /* Check to see if there was any good data before the error */
13138 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
13139 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13140 BGS_HI_WATER_MARK_PRESENT_MASK;
13141 pIocbIn->iocb.unsli3.sli3_bg.bghm =
13142 wcqe->total_data_placed;
13143 }
13144
13145 /*
13146 * Set ALL the error bits to indicate we don't know what
13147 * type of error it is.
13148 */
13149 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
13150 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13151 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
13152 BGS_GUARD_ERR_MASK);
13153 }
13154
James Smart341af102010-01-26 23:07:37 -050013155 /* Pick up HBA exchange busy condition */
13156 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
13157 spin_lock_irqsave(&phba->hbalock, iflags);
13158 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
13159 spin_unlock_irqrestore(&phba->hbalock, iflags);
13160 }
James Smart4f774512009-05-22 14:52:35 -040013161}
13162
13163/**
James Smart45ed1192009-10-02 15:17:02 -040013164 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
13165 * @phba: Pointer to HBA context object.
13166 * @wcqe: Pointer to work-queue completion queue entry.
13167 *
13168 * This routine handles an ELS work-queue completion event and construct
13169 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
13170 * discovery engine to handle.
13171 *
13172 * Return: Pointer to the receive IOCBQ, NULL otherwise.
13173 **/
13174static struct lpfc_iocbq *
13175lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
13176 struct lpfc_iocbq *irspiocbq)
13177{
James Smart895427b2017-02-12 13:52:30 -080013178 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040013179 struct lpfc_iocbq *cmdiocbq;
13180 struct lpfc_wcqe_complete *wcqe;
13181 unsigned long iflags;
13182
James Smart895427b2017-02-12 13:52:30 -080013183 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070013184 if (unlikely(!pring))
13185 return NULL;
James Smart895427b2017-02-12 13:52:30 -080013186
James Smart45ed1192009-10-02 15:17:02 -040013187 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart45ed1192009-10-02 15:17:02 -040013188 pring->stats.iocb_event++;
13189 /* Look up the ELS command IOCB and create pseudo response IOCB */
13190 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13191 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040013192 if (unlikely(!cmdiocbq)) {
13193 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13194 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070013195 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
13196 wcqe->word0, wcqe->total_data_placed,
13197 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040013198 lpfc_sli_release_iocbq(phba, irspiocbq);
13199 return NULL;
13200 }
13201
James Smarte2a8be52019-05-06 17:26:47 -070013202 spin_lock_irqsave(&pring->ring_lock, iflags);
Dick Kennedy401bb412017-09-29 17:34:28 -070013203 /* Put the iocb back on the txcmplq */
13204 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13205 spin_unlock_irqrestore(&pring->ring_lock, iflags);
13206
James Smart45ed1192009-10-02 15:17:02 -040013207 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013208 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040013209
13210 return irspiocbq;
13211}
13212
James Smart8a5ca102017-11-20 16:00:30 -080013213inline struct lpfc_cq_event *
13214lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13215{
13216 struct lpfc_cq_event *cq_event;
13217
13218 /* Allocate a new internal CQ_EVENT entry */
13219 cq_event = lpfc_sli4_cq_event_alloc(phba);
13220 if (!cq_event) {
13221 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13222 "0602 Failed to alloc CQ_EVENT entry\n");
13223 return NULL;
13224 }
13225
13226 /* Move the CQE into the event */
13227 memcpy(&cq_event->cqe, entry, size);
13228 return cq_event;
13229}
13230
James Smart45ed1192009-10-02 15:17:02 -040013231/**
Colin Ian King291c2542019-12-18 08:43:01 +000013232 * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
James Smart04c68492009-05-22 14:52:52 -040013233 * @phba: Pointer to HBA context object.
13234 * @cqe: Pointer to mailbox completion queue entry.
13235 *
Colin Ian King291c2542019-12-18 08:43:01 +000013236 * This routine process a mailbox completion queue entry with asynchronous
James Smart04c68492009-05-22 14:52:52 -040013237 * event.
13238 *
13239 * Return: true if work posted to worker thread, otherwise false.
13240 **/
13241static bool
13242lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13243{
13244 struct lpfc_cq_event *cq_event;
13245 unsigned long iflags;
13246
13247 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13248 "0392 Async Event: word0:x%x, word1:x%x, "
13249 "word2:x%x, word3:x%x\n", mcqe->word0,
13250 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13251
James Smart8a5ca102017-11-20 16:00:30 -080013252 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13253 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040013254 return false;
James Smart04c68492009-05-22 14:52:52 -040013255 spin_lock_irqsave(&phba->hbalock, iflags);
13256 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13257 /* Set the async event flag */
13258 phba->hba_flag |= ASYNC_EVENT;
13259 spin_unlock_irqrestore(&phba->hbalock, iflags);
13260
13261 return true;
13262}
13263
13264/**
13265 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13266 * @phba: Pointer to HBA context object.
13267 * @cqe: Pointer to mailbox completion queue entry.
13268 *
13269 * This routine process a mailbox completion queue entry with mailbox
13270 * completion event.
13271 *
13272 * Return: true if work posted to worker thread, otherwise false.
13273 **/
13274static bool
13275lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13276{
13277 uint32_t mcqe_status;
13278 MAILBOX_t *mbox, *pmbox;
13279 struct lpfc_mqe *mqe;
13280 struct lpfc_vport *vport;
13281 struct lpfc_nodelist *ndlp;
13282 struct lpfc_dmabuf *mp;
13283 unsigned long iflags;
13284 LPFC_MBOXQ_t *pmb;
13285 bool workposted = false;
13286 int rc;
13287
13288 /* If not a mailbox complete MCQE, out by checking mailbox consume */
13289 if (!bf_get(lpfc_trailer_completed, mcqe))
13290 goto out_no_mqe_complete;
13291
13292 /* Get the reference to the active mbox command */
13293 spin_lock_irqsave(&phba->hbalock, iflags);
13294 pmb = phba->sli.mbox_active;
13295 if (unlikely(!pmb)) {
13296 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13297 "1832 No pending MBOX command to handle\n");
13298 spin_unlock_irqrestore(&phba->hbalock, iflags);
13299 goto out_no_mqe_complete;
13300 }
13301 spin_unlock_irqrestore(&phba->hbalock, iflags);
13302 mqe = &pmb->u.mqe;
13303 pmbox = (MAILBOX_t *)&pmb->u.mqe;
13304 mbox = phba->mbox;
13305 vport = pmb->vport;
13306
13307 /* Reset heartbeat timer */
13308 phba->last_completion_time = jiffies;
13309 del_timer(&phba->sli.mbox_tmo);
13310
13311 /* Move mbox data to caller's mailbox region, do endian swapping */
13312 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070013313 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040013314
James Smart73d91e52011-10-10 21:32:10 -040013315 /*
13316 * For mcqe errors, conditionally move a modified error code to
13317 * the mbox so that the error will not be missed.
13318 */
13319 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13320 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13321 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13322 bf_set(lpfc_mqe_status, mqe,
13323 (LPFC_MBX_ERROR_RANGE | mcqe_status));
13324 }
James Smart04c68492009-05-22 14:52:52 -040013325 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13326 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13327 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13328 "MBOX dflt rpi: status:x%x rpi:x%x",
13329 mcqe_status,
13330 pmbox->un.varWords[0], 0);
13331 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
James Smart3e1f0712018-11-29 16:09:29 -080013332 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13333 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart04c68492009-05-22 14:52:52 -040013334 /* Reg_LOGIN of dflt RPI was successful. Now lets get
13335 * RID of the PPI using the same mbox buffer.
13336 */
13337 lpfc_unreg_login(phba, vport->vpi,
13338 pmbox->un.varWords[0], pmb);
13339 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080013340 pmb->ctx_buf = mp;
13341 pmb->ctx_ndlp = ndlp;
James Smart04c68492009-05-22 14:52:52 -040013342 pmb->vport = vport;
13343 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13344 if (rc != MBX_BUSY)
13345 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
13346 LOG_SLI, "0385 rc should "
13347 "have been MBX_BUSY\n");
13348 if (rc != MBX_NOT_FINISHED)
13349 goto send_current_mbox;
13350 }
13351 }
13352 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13353 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13354 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13355
13356 /* There is mailbox completion work to do */
13357 spin_lock_irqsave(&phba->hbalock, iflags);
13358 __lpfc_mbox_cmpl_put(phba, pmb);
13359 phba->work_ha |= HA_MBATT;
13360 spin_unlock_irqrestore(&phba->hbalock, iflags);
13361 workposted = true;
13362
13363send_current_mbox:
13364 spin_lock_irqsave(&phba->hbalock, iflags);
13365 /* Release the mailbox command posting token */
13366 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13367 /* Setting active mailbox pointer need to be in sync to flag clear */
13368 phba->sli.mbox_active = NULL;
James Smart07b85822019-09-21 20:58:53 -070013369 if (bf_get(lpfc_trailer_consumed, mcqe))
13370 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
James Smart04c68492009-05-22 14:52:52 -040013371 spin_unlock_irqrestore(&phba->hbalock, iflags);
13372 /* Wake up worker thread to post the next pending mailbox command */
13373 lpfc_worker_wake_up(phba);
James Smart07b85822019-09-21 20:58:53 -070013374 return workposted;
13375
James Smart04c68492009-05-22 14:52:52 -040013376out_no_mqe_complete:
James Smart07b85822019-09-21 20:58:53 -070013377 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart04c68492009-05-22 14:52:52 -040013378 if (bf_get(lpfc_trailer_consumed, mcqe))
13379 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
James Smart07b85822019-09-21 20:58:53 -070013380 spin_unlock_irqrestore(&phba->hbalock, iflags);
13381 return false;
James Smart04c68492009-05-22 14:52:52 -040013382}
13383
13384/**
13385 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13386 * @phba: Pointer to HBA context object.
13387 * @cqe: Pointer to mailbox completion queue entry.
13388 *
13389 * This routine process a mailbox completion queue entry, it invokes the
Colin Ian King291c2542019-12-18 08:43:01 +000013390 * proper mailbox complete handling or asynchronous event handling routine
James Smart04c68492009-05-22 14:52:52 -040013391 * according to the MCQE's async bit.
13392 *
13393 * Return: true if work posted to worker thread, otherwise false.
13394 **/
13395static bool
James Smart32517fc2019-01-28 11:14:33 -080013396lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13397 struct lpfc_cqe *cqe)
James Smart04c68492009-05-22 14:52:52 -040013398{
13399 struct lpfc_mcqe mcqe;
13400 bool workposted;
13401
James Smart32517fc2019-01-28 11:14:33 -080013402 cq->CQ_mbox++;
13403
James Smart04c68492009-05-22 14:52:52 -040013404 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013405 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040013406
13407 /* Invoke the proper event handling routine */
13408 if (!bf_get(lpfc_trailer_async, &mcqe))
13409 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13410 else
13411 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13412 return workposted;
13413}
13414
13415/**
James Smart4f774512009-05-22 14:52:35 -040013416 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13417 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040013418 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040013419 * @wcqe: Pointer to work-queue completion queue entry.
13420 *
13421 * This routine handles an ELS work-queue completion event.
13422 *
13423 * Return: true if work posted to worker thread, otherwise false.
13424 **/
13425static bool
James Smart2a76a282012-08-03 12:35:54 -040013426lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013427 struct lpfc_wcqe_complete *wcqe)
13428{
James Smart4f774512009-05-22 14:52:35 -040013429 struct lpfc_iocbq *irspiocbq;
13430 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040013431 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050013432 int txq_cnt = 0;
13433 int txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040013434
James Smart11f0e342018-05-04 20:37:57 -070013435 /* Check for response status */
13436 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13437 /* Log the error status */
13438 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13439 "0357 ELS CQE error: status=x%x: "
13440 "CQE: %08x %08x %08x %08x\n",
13441 bf_get(lpfc_wcqe_c_status, wcqe),
13442 wcqe->word0, wcqe->total_data_placed,
13443 wcqe->parameter, wcqe->word3);
13444 }
13445
James Smart45ed1192009-10-02 15:17:02 -040013446 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040013447 irspiocbq = lpfc_sli_get_iocbq(phba);
13448 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013449 if (!list_empty(&pring->txq))
13450 txq_cnt++;
13451 if (!list_empty(&pring->txcmplq))
13452 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040013453 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040013454 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
James Smartff349bc2019-09-21 20:59:05 -070013455 "els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050013456 txq_cnt, phba->iocb_cnt,
James Smart0e9bb8d2013-03-01 16:35:12 -050013457 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040013458 return false;
James Smart4f774512009-05-22 14:52:35 -040013459 }
James Smart4f774512009-05-22 14:52:35 -040013460
James Smart45ed1192009-10-02 15:17:02 -040013461 /* Save off the slow-path queue event for work thread to process */
13462 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040013463 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040013464 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013465 &phba->sli4_hba.sp_queue_event);
13466 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013467 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013468
James Smart45ed1192009-10-02 15:17:02 -040013469 return true;
James Smart4f774512009-05-22 14:52:35 -040013470}
13471
13472/**
13473 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13474 * @phba: Pointer to HBA context object.
13475 * @wcqe: Pointer to work-queue completion queue entry.
13476 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013477 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013478 * proper WQ release routine to the slow-path WQ.
13479 **/
13480static void
13481lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13482 struct lpfc_wcqe_release *wcqe)
13483{
James Smart2e90f4b2011-12-13 13:22:37 -050013484 /* sanity check on queue memory */
13485 if (unlikely(!phba->sli4_hba.els_wq))
13486 return;
James Smart4f774512009-05-22 14:52:35 -040013487 /* Check for the slow-path ELS work queue */
13488 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13489 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13490 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13491 else
13492 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13493 "2579 Slow-path wqe consume event carries "
13494 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13495 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13496 phba->sli4_hba.els_wq->queue_id);
13497}
13498
13499/**
13500 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13501 * @phba: Pointer to HBA context object.
13502 * @cq: Pointer to a WQ completion queue.
13503 * @wcqe: Pointer to work-queue completion queue entry.
13504 *
13505 * This routine handles an XRI abort event.
13506 *
13507 * Return: true if work posted to worker thread, otherwise false.
13508 **/
13509static bool
13510lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13511 struct lpfc_queue *cq,
13512 struct sli4_wcqe_xri_aborted *wcqe)
13513{
13514 bool workposted = false;
13515 struct lpfc_cq_event *cq_event;
13516 unsigned long iflags;
13517
James Smart4f774512009-05-22 14:52:35 -040013518 switch (cq->subtype) {
James Smartc00f62e2019-08-14 16:57:11 -070013519 case LPFC_IO:
13520 lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
13521 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
13522 /* Notify aborted XRI for NVME work queue */
13523 if (phba->nvmet_support)
13524 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13525 }
James Smart5e5b5112019-01-28 11:14:22 -080013526 workposted = false;
James Smart4f774512009-05-22 14:52:35 -040013527 break;
James Smart422c4cb2017-11-20 16:00:32 -080013528 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013529 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013530 cq_event = lpfc_cq_event_setup(
13531 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13532 if (!cq_event)
13533 return false;
James Smart5e5b5112019-01-28 11:14:22 -080013534 cq_event->hdwq = cq->hdwq;
James Smart4f774512009-05-22 14:52:35 -040013535 spin_lock_irqsave(&phba->hbalock, iflags);
13536 list_add_tail(&cq_event->list,
13537 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13538 /* Set the els xri abort event flag */
13539 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13540 spin_unlock_irqrestore(&phba->hbalock, iflags);
13541 workposted = true;
13542 break;
13543 default:
13544 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013545 "0603 Invalid CQ subtype %d: "
13546 "%08x %08x %08x %08x\n",
13547 cq->subtype, wcqe->word0, wcqe->parameter,
13548 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013549 workposted = false;
13550 break;
13551 }
13552 return workposted;
13553}
13554
James Smarte817e5d2018-12-13 15:17:53 -080013555#define FC_RCTL_MDS_DIAGS 0xF4
13556
James Smart4f774512009-05-22 14:52:35 -040013557/**
James Smart4d9ab992009-10-02 15:16:39 -040013558 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013559 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013560 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013561 *
James Smart4d9ab992009-10-02 15:16:39 -040013562 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013563 *
13564 * Return: true if work posted to worker thread, otherwise false.
13565 **/
13566static bool
James Smart4d9ab992009-10-02 15:16:39 -040013567lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13568{
13569 bool workposted = false;
James Smarte817e5d2018-12-13 15:17:53 -080013570 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013571 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13572 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013573 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013574 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013575 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013576 unsigned long iflags;
13577
James Smart2e90f4b2011-12-13 13:22:37 -050013578 /* sanity check on queue memory */
13579 if (unlikely(!hrq) || unlikely(!drq))
13580 return workposted;
13581
James Smart7851fe22011-07-22 18:36:52 -040013582 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13583 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13584 else
13585 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13586 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013587 goto out;
13588
13589 status = bf_get(lpfc_rcqe_status, rcqe);
13590 switch (status) {
13591 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13593 "2537 Receive Frame Truncated!!\n");
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060013594 /* fall through */
James Smart4d9ab992009-10-02 15:16:39 -040013595 case FC_STATUS_RQ_SUCCESS:
13596 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013597 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013598 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13599 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013600 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013601 spin_unlock_irqrestore(&phba->hbalock, iflags);
13602 goto out;
13603 }
James Smartb84daac2012-08-03 12:35:13 -040013604 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013605 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013606 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013607
James Smarte817e5d2018-12-13 15:17:53 -080013608 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13609
13610 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
13611 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
13612 spin_unlock_irqrestore(&phba->hbalock, iflags);
13613 /* Handle MDS Loopback frames */
13614 lpfc_sli4_handle_mds_loopback(phba->pport, dma_buf);
13615 break;
13616 }
13617
13618 /* save off the frame for the work thread to process */
James Smart4d9ab992009-10-02 15:16:39 -040013619 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013620 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013621 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013622 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013623 spin_unlock_irqrestore(&phba->hbalock, iflags);
13624 workposted = true;
13625 break;
James Smart4d9ab992009-10-02 15:16:39 -040013626 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013627 if (phba->nvmet_support) {
13628 tgtp = phba->targetport->private;
13629 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13630 "6402 RQE Error x%x, posted %d err_cnt "
13631 "%d: %x %x %x\n",
13632 status, hrq->RQ_buf_posted,
13633 hrq->RQ_no_posted_buf,
13634 atomic_read(&tgtp->rcv_fcp_cmd_in),
13635 atomic_read(&tgtp->rcv_fcp_cmd_out),
13636 atomic_read(&tgtp->xmt_fcp_release));
13637 }
13638 /* fallthrough */
13639
13640 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013641 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013642 /* Post more buffers if possible */
13643 spin_lock_irqsave(&phba->hbalock, iflags);
13644 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13645 spin_unlock_irqrestore(&phba->hbalock, iflags);
13646 workposted = true;
13647 break;
13648 }
13649out:
13650 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013651}
13652
13653/**
13654 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13655 * @phba: Pointer to HBA context object.
13656 * @cq: Pointer to the completion queue.
James Smart32517fc2019-01-28 11:14:33 -080013657 * @cqe: Pointer to a completion queue entry.
James Smart4d9ab992009-10-02 15:16:39 -040013658 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013659 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013660 * entry.
13661 *
13662 * Return: true if work posted to worker thread, otherwise false.
13663 **/
13664static bool
13665lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013666 struct lpfc_cqe *cqe)
13667{
James Smart45ed1192009-10-02 15:17:02 -040013668 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013669 bool workposted = false;
13670
13671 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013672 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013673
13674 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013675 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013676 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013677 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013678 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013679 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013680 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013681 break;
13682 case CQE_CODE_RELEASE_WQE:
13683 /* Process the WQ release event */
13684 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013685 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013686 break;
13687 case CQE_CODE_XRI_ABORTED:
13688 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013689 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013690 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013691 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013692 break;
James Smart4d9ab992009-10-02 15:16:39 -040013693 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013694 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013695 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013696 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013697 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013698 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013699 break;
James Smart4f774512009-05-22 14:52:35 -040013700 default:
13701 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13702 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013703 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013704 break;
13705 }
13706 return workposted;
13707}
13708
13709/**
James Smart4f774512009-05-22 14:52:35 -040013710 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13711 * @phba: Pointer to HBA context object.
13712 * @eqe: Pointer to fast-path event queue entry.
13713 *
13714 * This routine process a event queue entry from the slow-path event queue.
13715 * It will check the MajorCode and MinorCode to determine this is for a
13716 * completion event on a completion queue, if not, an error shall be logged
13717 * and just return. Otherwise, it will get to the corresponding completion
13718 * queue and process all the entries on that completion queue, rearm the
13719 * completion queue, and then return.
13720 *
13721 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013722static void
James Smart67d12732012-08-03 12:36:13 -040013723lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13724 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013725{
James Smart67d12732012-08-03 12:36:13 -040013726 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013727 uint16_t cqid;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070013728 int ret = 0;
James Smart4f774512009-05-22 14:52:35 -040013729
James Smart4f774512009-05-22 14:52:35 -040013730 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013731 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013732
James Smart4f774512009-05-22 14:52:35 -040013733 list_for_each_entry(childq, &speq->child_list, list) {
13734 if (childq->queue_id == cqid) {
13735 cq = childq;
13736 break;
13737 }
13738 }
13739 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013740 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13741 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13742 "0365 Slow-path CQ identifier "
13743 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013744 return;
James Smart4f774512009-05-22 14:52:35 -040013745 }
13746
James Smart895427b2017-02-12 13:52:30 -080013747 /* Save EQ associated with this CQ */
13748 cq->assoc_qp = speq;
13749
Dick Kennedy86ee57a2020-06-30 14:49:55 -070013750 if (is_kdump_kernel())
13751 ret = queue_work(phba->wq, &cq->spwork);
13752 else
13753 ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
13754
13755 if (!ret)
Dick Kennedyf485c182017-09-29 17:34:34 -070013756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13757 "0390 Cannot schedule soft IRQ "
13758 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
Bart Van Assched6d189c2019-03-28 11:06:22 -070013759 cqid, cq->queue_id, raw_smp_processor_id());
Dick Kennedyf485c182017-09-29 17:34:34 -070013760}
13761
13762/**
James Smart32517fc2019-01-28 11:14:33 -080013763 * __lpfc_sli4_process_cq - Process elements of a CQ
Dick Kennedyf485c182017-09-29 17:34:34 -070013764 * @phba: Pointer to HBA context object.
James Smart32517fc2019-01-28 11:14:33 -080013765 * @cq: Pointer to CQ to be processed
13766 * @handler: Routine to process each cqe
13767 * @delay: Pointer to usdelay to set in case of rescheduling of the handler
Dick Kennedyf485c182017-09-29 17:34:34 -070013768 *
James Smart32517fc2019-01-28 11:14:33 -080013769 * This routine processes completion queue entries in a CQ. While a valid
13770 * queue element is found, the handler is called. During processing checks
13771 * are made for periodic doorbell writes to let the hardware know of
13772 * element consumption.
Dick Kennedyf485c182017-09-29 17:34:34 -070013773 *
James Smart32517fc2019-01-28 11:14:33 -080013774 * If the max limit on cqes to process is hit, or there are no more valid
13775 * entries, the loop stops. If we processed a sufficient number of elements,
13776 * meaning there is sufficient load, rather than rearming and generating
13777 * another interrupt, a cq rescheduling delay will be set. A delay of 0
13778 * indicates no rescheduling.
13779 *
13780 * Returns True if work scheduled, False otherwise.
Dick Kennedyf485c182017-09-29 17:34:34 -070013781 **/
James Smart32517fc2019-01-28 11:14:33 -080013782static bool
13783__lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
13784 bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
13785 struct lpfc_cqe *), unsigned long *delay)
Dick Kennedyf485c182017-09-29 17:34:34 -070013786{
Dick Kennedyf485c182017-09-29 17:34:34 -070013787 struct lpfc_cqe *cqe;
13788 bool workposted = false;
James Smart32517fc2019-01-28 11:14:33 -080013789 int count = 0, consumed = 0;
13790 bool arm = true;
13791
13792 /* default - no reschedule */
13793 *delay = 0;
13794
13795 if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
13796 goto rearm_and_exit;
Dick Kennedyf485c182017-09-29 17:34:34 -070013797
James Smart4f774512009-05-22 14:52:35 -040013798 /* Process all the entries to the CQ */
James Smartd74a89a2019-05-21 17:48:55 -070013799 cq->q_flag = 0;
James Smart32517fc2019-01-28 11:14:33 -080013800 cqe = lpfc_sli4_cq_get(cq);
13801 while (cqe) {
James Smart32517fc2019-01-28 11:14:33 -080013802 workposted |= handler(phba, cq, cqe);
13803 __lpfc_sli4_consume_cqe(phba, cq, cqe);
13804
13805 consumed++;
13806 if (!(++count % cq->max_proc_limit))
13807 break;
13808
13809 if (!(count % cq->notify_interval)) {
13810 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
13811 LPFC_QUEUE_NOARM);
13812 consumed = 0;
James Smart8156d372019-10-18 14:18:26 -070013813 cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
James Smart4f774512009-05-22 14:52:35 -040013814 }
James Smartb84daac2012-08-03 12:35:13 -040013815
James Smartd74a89a2019-05-21 17:48:55 -070013816 if (count == LPFC_NVMET_CQ_NOTIFY)
13817 cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
13818
James Smart32517fc2019-01-28 11:14:33 -080013819 cqe = lpfc_sli4_cq_get(cq);
13820 }
13821 if (count >= phba->cfg_cq_poll_threshold) {
13822 *delay = 1;
13823 arm = false;
13824 }
13825
13826 /* Track the max number of CQEs processed in 1 EQ */
13827 if (count > cq->CQ_max_cqe)
13828 cq->CQ_max_cqe = count;
13829
13830 cq->assoc_qp->EQ_cqe_cnt += count;
13831
13832 /* Catch the no cq entry condition */
13833 if (unlikely(count == 0))
13834 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13835 "0369 No entry from completion queue "
13836 "qid=%d\n", cq->queue_id);
13837
Dick Kennedy164ba8d2020-05-01 14:43:03 -070013838 xchg(&cq->queue_claimed, 0);
James Smart32517fc2019-01-28 11:14:33 -080013839
13840rearm_and_exit:
13841 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
13842 arm ? LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
13843
13844 return workposted;
13845}
13846
13847/**
13848 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13849 * @cq: pointer to CQ to process
13850 *
13851 * This routine calls the cq processing routine with a handler specific
13852 * to the type of queue bound to it.
13853 *
13854 * The CQ routine returns two values: the first is the calling status,
13855 * which indicates whether work was queued to the background discovery
13856 * thread. If true, the routine should wakeup the discovery thread;
13857 * the second is the delay parameter. If non-zero, rather than rearming
13858 * the CQ and yet another interrupt, the CQ handler should be queued so
13859 * that it is processed in a subsequent polling action. The value of
13860 * the delay indicates when to reschedule it.
13861 **/
13862static void
13863__lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
13864{
13865 struct lpfc_hba *phba = cq->phba;
13866 unsigned long delay;
13867 bool workposted = false;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070013868 int ret = 0;
James Smart32517fc2019-01-28 11:14:33 -080013869
13870 /* Process and rearm the CQ */
13871 switch (cq->type) {
13872 case LPFC_MCQ:
13873 workposted |= __lpfc_sli4_process_cq(phba, cq,
13874 lpfc_sli4_sp_handle_mcqe,
13875 &delay);
13876 break;
13877 case LPFC_WCQ:
James Smartc00f62e2019-08-14 16:57:11 -070013878 if (cq->subtype == LPFC_IO)
James Smart32517fc2019-01-28 11:14:33 -080013879 workposted |= __lpfc_sli4_process_cq(phba, cq,
13880 lpfc_sli4_fp_handle_cqe,
13881 &delay);
13882 else
13883 workposted |= __lpfc_sli4_process_cq(phba, cq,
13884 lpfc_sli4_sp_handle_cqe,
13885 &delay);
James Smart4f774512009-05-22 14:52:35 -040013886 break;
13887 default:
13888 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13889 "0370 Invalid completion queue type (%d)\n",
13890 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013891 return;
James Smart4f774512009-05-22 14:52:35 -040013892 }
13893
James Smart32517fc2019-01-28 11:14:33 -080013894 if (delay) {
Dick Kennedy86ee57a2020-06-30 14:49:55 -070013895 if (is_kdump_kernel())
13896 ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
13897 delay);
13898 else
13899 ret = queue_delayed_work_on(cq->chann, phba->wq,
13900 &cq->sched_spwork, delay);
13901 if (!ret)
James Smart32517fc2019-01-28 11:14:33 -080013902 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13903 "0394 Cannot schedule soft IRQ "
13904 "for cqid=%d on CPU %d\n",
13905 cq->queue_id, cq->chann);
13906 }
James Smart4f774512009-05-22 14:52:35 -040013907
13908 /* wake up worker thread if there are works to be done */
13909 if (workposted)
13910 lpfc_worker_wake_up(phba);
13911}
13912
13913/**
James Smart32517fc2019-01-28 11:14:33 -080013914 * lpfc_sli4_sp_process_cq - slow-path work handler when started by
13915 * interrupt
13916 * @work: pointer to work element
13917 *
13918 * translates from the work handler and calls the slow-path handler.
13919 **/
13920static void
13921lpfc_sli4_sp_process_cq(struct work_struct *work)
13922{
13923 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
13924
13925 __lpfc_sli4_sp_process_cq(cq);
13926}
13927
13928/**
13929 * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
13930 * @work: pointer to work element
13931 *
13932 * translates from the work handler and calls the slow-path handler.
13933 **/
13934static void
13935lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
13936{
13937 struct lpfc_queue *cq = container_of(to_delayed_work(work),
13938 struct lpfc_queue, sched_spwork);
13939
13940 __lpfc_sli4_sp_process_cq(cq);
13941}
13942
13943/**
James Smart4f774512009-05-22 14:52:35 -040013944 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013945 * @phba: Pointer to HBA context object.
13946 * @cq: Pointer to associated CQ
13947 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013948 *
13949 * This routine process a fast-path work queue completion entry from fast-path
13950 * event queue for FCP command response completion.
13951 **/
13952static void
James Smart2a76a282012-08-03 12:35:54 -040013953lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013954 struct lpfc_wcqe_complete *wcqe)
13955{
James Smart2a76a282012-08-03 12:35:54 -040013956 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013957 struct lpfc_iocbq *cmdiocbq;
13958 struct lpfc_iocbq irspiocbq;
13959 unsigned long iflags;
13960
James Smart4f774512009-05-22 14:52:35 -040013961 /* Check for response status */
13962 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13963 /* If resource errors reported from HBA, reduce queue
13964 * depth of the SCSI device.
13965 */
James Smarte3d2b802012-08-14 14:25:43 -040013966 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13967 IOSTAT_LOCAL_REJECT)) &&
13968 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13969 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013970 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013971
Dick Kennedy28ed7372020-06-30 14:49:56 -070013972 /* Log the cmpl status */
James Smart11f0e342018-05-04 20:37:57 -070013973 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
Dick Kennedy28ed7372020-06-30 14:49:56 -070013974 "0373 FCP CQE cmpl: status=x%x: "
James Smart11f0e342018-05-04 20:37:57 -070013975 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013976 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070013977 wcqe->word0, wcqe->total_data_placed,
13978 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013979 }
13980
13981 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013982 spin_lock_irqsave(&pring->ring_lock, iflags);
13983 pring->stats.iocb_event++;
James Smarte2a8be52019-05-06 17:26:47 -070013984 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013985 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13986 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart4f774512009-05-22 14:52:35 -040013987 if (unlikely(!cmdiocbq)) {
13988 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13989 "0374 FCP complete with no corresponding "
13990 "cmdiocb: iotag (%d)\n",
13991 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13992 return;
13993 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013994#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13995 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13996#endif
James Smart895427b2017-02-12 13:52:30 -080013997 if (cmdiocbq->iocb_cmpl == NULL) {
13998 if (cmdiocbq->wqe_cmpl) {
13999 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
14000 spin_lock_irqsave(&phba->hbalock, iflags);
14001 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
14002 spin_unlock_irqrestore(&phba->hbalock, iflags);
14003 }
14004
14005 /* Pass the cmd_iocb and the wcqe to the upper layer */
14006 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
14007 return;
14008 }
James Smart4f774512009-05-22 14:52:35 -040014009 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14010 "0375 FCP cmdiocb not callback function "
14011 "iotag: (%d)\n",
14012 bf_get(lpfc_wcqe_c_request_tag, wcqe));
14013 return;
14014 }
14015
14016 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050014017 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040014018
James Smart0f65ff62010-02-26 14:14:23 -050014019 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
14020 spin_lock_irqsave(&phba->hbalock, iflags);
14021 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
14022 spin_unlock_irqrestore(&phba->hbalock, iflags);
14023 }
14024
James Smart4f774512009-05-22 14:52:35 -040014025 /* Pass the cmd_iocb and the rsp state to the upper layer */
14026 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
14027}
14028
14029/**
14030 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
14031 * @phba: Pointer to HBA context object.
14032 * @cq: Pointer to completion queue.
14033 * @wcqe: Pointer to work-queue completion queue entry.
14034 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080014035 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040014036 * proper WQ release routine to the slow-path WQ.
14037 **/
14038static void
14039lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14040 struct lpfc_wcqe_release *wcqe)
14041{
14042 struct lpfc_queue *childwq;
14043 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080014044 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040014045
14046 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080014047 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040014048 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080014049 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040014050 lpfc_sli4_wq_release(childwq,
14051 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080014052 if (childwq->q_flag & HBA_NVMET_WQFULL)
14053 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040014054 wqid_matched = true;
14055 break;
14056 }
14057 }
14058 /* Report warning log message if no match found */
14059 if (wqid_matched != true)
14060 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14061 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080014062 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040014063}
14064
14065/**
James Smart2d7dbc42017-02-12 13:52:35 -080014066 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
14067 * @phba: Pointer to HBA context object.
14068 * @rcqe: Pointer to receive-queue completion queue entry.
14069 *
14070 * This routine process a receive-queue completion queue entry.
14071 *
14072 * Return: true if work posted to worker thread, otherwise false.
14073 **/
14074static bool
14075lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14076 struct lpfc_rcqe *rcqe)
14077{
14078 bool workposted = false;
14079 struct lpfc_queue *hrq;
14080 struct lpfc_queue *drq;
14081 struct rqb_dmabuf *dma_buf;
14082 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070014083 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080014084 uint32_t status, rq_id;
14085 unsigned long iflags;
14086 uint32_t fctl, idx;
14087
14088 if ((phba->nvmet_support == 0) ||
14089 (phba->sli4_hba.nvmet_cqset == NULL))
14090 return workposted;
14091
14092 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
14093 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
14094 drq = phba->sli4_hba.nvmet_mrq_data[idx];
14095
14096 /* sanity check on queue memory */
14097 if (unlikely(!hrq) || unlikely(!drq))
14098 return workposted;
14099
14100 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14101 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14102 else
14103 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14104
14105 if ((phba->nvmet_support == 0) ||
14106 (rq_id != hrq->queue_id))
14107 return workposted;
14108
14109 status = bf_get(lpfc_rcqe_status, rcqe);
14110 switch (status) {
14111 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
14112 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14113 "6126 Receive Frame Truncated!!\n");
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014114 /* fall through */
James Smart2d7dbc42017-02-12 13:52:35 -080014115 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080014116 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080014117 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080014118 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
14119 if (!dma_buf) {
14120 hrq->RQ_no_buf_found++;
14121 spin_unlock_irqrestore(&phba->hbalock, iflags);
14122 goto out;
14123 }
14124 spin_unlock_irqrestore(&phba->hbalock, iflags);
14125 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070014126 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080014127 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14128
14129 /* Just some basic sanity checks on FCP Command frame */
14130 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
James Smart3a8070c2020-03-31 09:50:05 -070014131 fc_hdr->fh_f_ctl[1] << 8 |
14132 fc_hdr->fh_f_ctl[2]);
James Smart2d7dbc42017-02-12 13:52:35 -080014133 if (((fctl &
14134 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
14135 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
14136 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
14137 goto drop;
14138
14139 if (fc_hdr->fh_type == FC_TYPE_FCP) {
James Smartd74a89a2019-05-21 17:48:55 -070014140 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080014141 lpfc_nvmet_unsol_fcp_event(
James Smartd74a89a2019-05-21 17:48:55 -070014142 phba, idx, dma_buf, cq->isr_timestamp,
14143 cq->q_flag & HBA_NVMET_CQ_NOTIFY);
James Smart2d7dbc42017-02-12 13:52:35 -080014144 return false;
14145 }
14146drop:
James Smart22b738a2019-03-12 16:30:11 -070014147 lpfc_rq_buf_free(phba, &dma_buf->hbuf);
James Smart2d7dbc42017-02-12 13:52:35 -080014148 break;
James Smart2d7dbc42017-02-12 13:52:35 -080014149 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070014150 if (phba->nvmet_support) {
14151 tgtp = phba->targetport->private;
14152 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
14153 "6401 RQE Error x%x, posted %d err_cnt "
14154 "%d: %x %x %x\n",
14155 status, hrq->RQ_buf_posted,
14156 hrq->RQ_no_posted_buf,
14157 atomic_read(&tgtp->rcv_fcp_cmd_in),
14158 atomic_read(&tgtp->rcv_fcp_cmd_out),
14159 atomic_read(&tgtp->xmt_fcp_release));
14160 }
14161 /* fallthrough */
14162
14163 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080014164 hrq->RQ_no_posted_buf++;
14165 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080014166 break;
14167 }
14168out:
14169 return workposted;
14170}
14171
14172/**
James Smart895427b2017-02-12 13:52:30 -080014173 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart32517fc2019-01-28 11:14:33 -080014174 * @phba: adapter with cq
James Smart4f774512009-05-22 14:52:35 -040014175 * @cq: Pointer to the completion queue.
14176 * @eqe: Pointer to fast-path completion queue entry.
14177 *
14178 * This routine process a fast-path work queue completion entry from fast-path
14179 * event queue for FCP command response completion.
James Smart32517fc2019-01-28 11:14:33 -080014180 *
14181 * Return: true if work posted to worker thread, otherwise false.
James Smart4f774512009-05-22 14:52:35 -040014182 **/
James Smart32517fc2019-01-28 11:14:33 -080014183static bool
James Smart895427b2017-02-12 13:52:30 -080014184lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040014185 struct lpfc_cqe *cqe)
14186{
14187 struct lpfc_wcqe_release wcqe;
14188 bool workposted = false;
14189
14190 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070014191 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040014192
14193 /* Check and process for different type of WCQE and dispatch */
14194 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
14195 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080014196 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040014197 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040014198 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040014199 phba->last_completion_time = jiffies;
James Smartc00f62e2019-08-14 16:57:11 -070014200 if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
James Smart895427b2017-02-12 13:52:30 -080014201 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040014202 (struct lpfc_wcqe_complete *)&wcqe);
14203 break;
14204 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040014205 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040014206 /* Process the WQ release event */
14207 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
14208 (struct lpfc_wcqe_release *)&wcqe);
14209 break;
14210 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040014211 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040014212 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040014213 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014214 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14215 (struct sli4_wcqe_xri_aborted *)&wcqe);
14216 break;
James Smart895427b2017-02-12 13:52:30 -080014217 case CQE_CODE_RECEIVE_V1:
14218 case CQE_CODE_RECEIVE:
14219 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080014220 if (cq->subtype == LPFC_NVMET) {
14221 workposted = lpfc_sli4_nvmet_handle_rcqe(
14222 phba, cq, (struct lpfc_rcqe *)&wcqe);
14223 }
James Smart895427b2017-02-12 13:52:30 -080014224 break;
James Smart4f774512009-05-22 14:52:35 -040014225 default:
14226 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080014227 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040014228 bf_get(lpfc_wcqe_c_code, &wcqe));
14229 break;
14230 }
14231 return workposted;
14232}
14233
14234/**
James Smart67d12732012-08-03 12:36:13 -040014235 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040014236 * @phba: Pointer to HBA context object.
14237 * @eqe: Pointer to fast-path event queue entry.
14238 *
14239 * This routine process a event queue entry from the fast-path event queue.
14240 * It will check the MajorCode and MinorCode to determine this is for a
14241 * completion event on a completion queue, if not, an error shall be logged
14242 * and just return. Otherwise, it will get to the corresponding completion
14243 * queue and process all the entries on the completion queue, rearm the
14244 * completion queue, and then return.
14245 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070014246static void
James Smart32517fc2019-01-28 11:14:33 -080014247lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
14248 struct lpfc_eqe *eqe)
James Smart4f774512009-05-22 14:52:35 -040014249{
James Smart895427b2017-02-12 13:52:30 -080014250 struct lpfc_queue *cq = NULL;
James Smart32517fc2019-01-28 11:14:33 -080014251 uint32_t qidx = eq->hdwq;
James Smart2d7dbc42017-02-12 13:52:35 -080014252 uint16_t cqid, id;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014253 int ret = 0;
James Smart4f774512009-05-22 14:52:35 -040014254
James Smartcb5172e2010-03-15 11:25:07 -040014255 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040014256 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040014257 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040014258 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040014259 bf_get_le32(lpfc_eqe_major_code, eqe),
14260 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070014261 return;
James Smart4f774512009-05-22 14:52:35 -040014262 }
14263
James Smart67d12732012-08-03 12:36:13 -040014264 /* Get the reference to the corresponding CQ */
14265 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14266
James Smart6a828b02019-01-28 11:14:31 -080014267 /* Use the fast lookup method first */
14268 if (cqid <= phba->sli4_hba.cq_max) {
14269 cq = phba->sli4_hba.cq_lookup[cqid];
14270 if (cq)
14271 goto work_cq;
James Smartcdb42be2019-01-28 11:14:21 -080014272 }
14273
14274 /* Next check for NVMET completion */
James Smart2d7dbc42017-02-12 13:52:35 -080014275 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14276 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14277 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14278 /* Process NVMET unsol rcv */
14279 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14280 goto process_cq;
14281 }
14282 }
14283
James Smart895427b2017-02-12 13:52:30 -080014284 if (phba->sli4_hba.nvmels_cq &&
14285 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14286 /* Process NVME unsol rcv */
14287 cq = phba->sli4_hba.nvmels_cq;
14288 }
14289
14290 /* Otherwise this is a Slow path event */
14291 if (cq == NULL) {
James Smartcdb42be2019-01-28 11:14:21 -080014292 lpfc_sli4_sp_handle_eqe(phba, eqe,
14293 phba->sli4_hba.hdwq[qidx].hba_eq);
Dick Kennedyf485c182017-09-29 17:34:34 -070014294 return;
James Smart67d12732012-08-03 12:36:13 -040014295 }
14296
James Smart895427b2017-02-12 13:52:30 -080014297process_cq:
James Smart4f774512009-05-22 14:52:35 -040014298 if (unlikely(cqid != cq->queue_id)) {
14299 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14300 "0368 Miss-matched fast-path completion "
14301 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14302 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070014303 return;
James Smart4f774512009-05-22 14:52:35 -040014304 }
14305
James Smart6a828b02019-01-28 11:14:31 -080014306work_cq:
James Smartd74a89a2019-05-21 17:48:55 -070014307#if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
14308 if (phba->ktime_on)
14309 cq->isr_timestamp = ktime_get_ns();
14310 else
14311 cq->isr_timestamp = 0;
14312#endif
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014313 if (is_kdump_kernel())
14314 ret = queue_work(phba->wq, &cq->irqwork);
14315 else
14316 ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
14317 if (!ret)
Dick Kennedyf485c182017-09-29 17:34:34 -070014318 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14319 "0363 Cannot schedule soft IRQ "
14320 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
Bart Van Assched6d189c2019-03-28 11:06:22 -070014321 cqid, cq->queue_id, raw_smp_processor_id());
Dick Kennedyf485c182017-09-29 17:34:34 -070014322}
14323
14324/**
James Smart32517fc2019-01-28 11:14:33 -080014325 * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14326 * @cq: Pointer to CQ to be processed
Dick Kennedyf485c182017-09-29 17:34:34 -070014327 *
James Smart32517fc2019-01-28 11:14:33 -080014328 * This routine calls the cq processing routine with the handler for
14329 * fast path CQEs.
14330 *
14331 * The CQ routine returns two values: the first is the calling status,
14332 * which indicates whether work was queued to the background discovery
14333 * thread. If true, the routine should wakeup the discovery thread;
14334 * the second is the delay parameter. If non-zero, rather than rearming
14335 * the CQ and yet another interrupt, the CQ handler should be queued so
14336 * that it is processed in a subsequent polling action. The value of
14337 * the delay indicates when to reschedule it.
Dick Kennedyf485c182017-09-29 17:34:34 -070014338 **/
14339static void
James Smart32517fc2019-01-28 11:14:33 -080014340__lpfc_sli4_hba_process_cq(struct lpfc_queue *cq)
Dick Kennedyf485c182017-09-29 17:34:34 -070014341{
Dick Kennedyf485c182017-09-29 17:34:34 -070014342 struct lpfc_hba *phba = cq->phba;
James Smart32517fc2019-01-28 11:14:33 -080014343 unsigned long delay;
Dick Kennedyf485c182017-09-29 17:34:34 -070014344 bool workposted = false;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014345 int ret = 0;
Dick Kennedyf485c182017-09-29 17:34:34 -070014346
James Smart32517fc2019-01-28 11:14:33 -080014347 /* process and rearm the CQ */
14348 workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
14349 &delay);
14350
14351 if (delay) {
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014352 if (is_kdump_kernel())
14353 ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
14354 delay);
14355 else
14356 ret = queue_delayed_work_on(cq->chann, phba->wq,
14357 &cq->sched_irqwork, delay);
14358 if (!ret)
James Smart32517fc2019-01-28 11:14:33 -080014359 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14360 "0367 Cannot schedule soft IRQ "
14361 "for cqid=%d on CPU %d\n",
14362 cq->queue_id, cq->chann);
James Smart4f774512009-05-22 14:52:35 -040014363 }
14364
James Smart4f774512009-05-22 14:52:35 -040014365 /* wake up worker thread if there are works to be done */
14366 if (workposted)
14367 lpfc_worker_wake_up(phba);
14368}
14369
James Smart1ba981f2014-02-20 09:56:45 -050014370/**
James Smart32517fc2019-01-28 11:14:33 -080014371 * lpfc_sli4_hba_process_cq - fast-path work handler when started by
14372 * interrupt
14373 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050014374 *
James Smart32517fc2019-01-28 11:14:33 -080014375 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050014376 **/
14377static void
James Smart32517fc2019-01-28 11:14:33 -080014378lpfc_sli4_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050014379{
James Smart32517fc2019-01-28 11:14:33 -080014380 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050014381
James Smart32517fc2019-01-28 11:14:33 -080014382 __lpfc_sli4_hba_process_cq(cq);
James Smart1ba981f2014-02-20 09:56:45 -050014383}
14384
14385/**
James Smart32517fc2019-01-28 11:14:33 -080014386 * lpfc_sli4_hba_process_cq - fast-path work handler when started by timer
14387 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050014388 *
James Smart32517fc2019-01-28 11:14:33 -080014389 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050014390 **/
James Smart32517fc2019-01-28 11:14:33 -080014391static void
14392lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050014393{
James Smart32517fc2019-01-28 11:14:33 -080014394 struct lpfc_queue *cq = container_of(to_delayed_work(work),
14395 struct lpfc_queue, sched_irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050014396
James Smart32517fc2019-01-28 11:14:33 -080014397 __lpfc_sli4_hba_process_cq(cq);
James Smart1ba981f2014-02-20 09:56:45 -050014398}
14399
James Smart4f774512009-05-22 14:52:35 -040014400/**
James Smart67d12732012-08-03 12:36:13 -040014401 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040014402 * @irq: Interrupt number.
14403 * @dev_id: The device context pointer.
14404 *
14405 * This function is directly called from the PCI layer as an interrupt
14406 * service routine when device with SLI-4 interface spec is enabled with
14407 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14408 * ring event in the HBA. However, when the device is enabled with either
14409 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14410 * device-level interrupt handler. When the PCI slot is in error recovery
14411 * or the HBA is undergoing initialization, the interrupt handler will not
14412 * process the interrupt. The SCSI FCP fast-path ring event are handled in
14413 * the intrrupt context. This function is called without any lock held.
14414 * It gets the hbalock to access and update SLI data structures. Note that,
14415 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14416 * equal to that of FCP CQ index.
14417 *
James Smart67d12732012-08-03 12:36:13 -040014418 * The link attention and ELS ring attention events are handled
14419 * by the worker thread. The interrupt handler signals the worker thread
14420 * and returns for these events. This function is called without any lock
14421 * held. It gets the hbalock to access and update SLI data structures.
14422 *
James Smart4f774512009-05-22 14:52:35 -040014423 * This function returns IRQ_HANDLED when interrupt is handled else it
14424 * returns IRQ_NONE.
14425 **/
14426irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040014427lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040014428{
14429 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080014430 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040014431 struct lpfc_queue *fpeq;
James Smart4f774512009-05-22 14:52:35 -040014432 unsigned long iflag;
14433 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080014434 int hba_eqidx;
James Smart32517fc2019-01-28 11:14:33 -080014435 struct lpfc_eq_intr_info *eqi;
James Smart4f774512009-05-22 14:52:35 -040014436
14437 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014438 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14439 phba = hba_eq_hdl->phba;
14440 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014441
14442 if (unlikely(!phba))
14443 return IRQ_NONE;
James Smartcdb42be2019-01-28 11:14:21 -080014444 if (unlikely(!phba->sli4_hba.hdwq))
James Smart5350d872011-10-10 21:33:49 -040014445 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014446
14447 /* Get to the EQ struct associated with this vector */
James Smart657add42019-05-21 17:49:06 -070014448 fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
James Smart2e90f4b2011-12-13 13:22:37 -050014449 if (unlikely(!fpeq))
14450 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014451
14452 /* Check device state for handling interrupt */
14453 if (unlikely(lpfc_intr_state_check(phba))) {
14454 /* Check again for link_state with lock held */
14455 spin_lock_irqsave(&phba->hbalock, iflag);
14456 if (phba->link_state < LPFC_LINK_DOWN)
14457 /* Flush, clear interrupt, and rearm the EQ */
James Smart24c7c0a2019-09-21 20:58:58 -070014458 lpfc_sli4_eqcq_flush(phba, fpeq);
James Smart4f774512009-05-22 14:52:35 -040014459 spin_unlock_irqrestore(&phba->hbalock, iflag);
14460 return IRQ_NONE;
14461 }
14462
Dick Kennedya7fc0712020-05-01 14:43:08 -070014463 eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
14464 eqi->icnt++;
14465
Bart Van Assched6d189c2019-03-28 11:06:22 -070014466 fpeq->last_cpu = raw_smp_processor_id();
James Smart4f774512009-05-22 14:52:35 -040014467
Dick Kennedya7fc0712020-05-01 14:43:08 -070014468 if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
James Smart8156d372019-10-18 14:18:26 -070014469 fpeq->q_flag & HBA_EQ_DELAY_CHK &&
James Smart32517fc2019-01-28 11:14:33 -080014470 phba->cfg_auto_imax &&
14471 fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
14472 phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
14473 lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
James Smartb84daac2012-08-03 12:35:13 -040014474
James Smart32517fc2019-01-28 11:14:33 -080014475 /* process and rearm the EQ */
James Smart93a4d6f2019-11-04 16:57:05 -080014476 ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014477
14478 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014479 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040014480 if (phba->intr_type == MSIX)
14481 /* MSI-X treated interrupt served as no EQ share INT */
14482 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14483 "0358 MSI-X interrupt with no EQE\n");
14484 else
14485 /* Non MSI-X treated on interrupt as EQ share INT */
14486 return IRQ_NONE;
14487 }
14488
14489 return IRQ_HANDLED;
14490} /* lpfc_sli4_fp_intr_handler */
14491
14492/**
14493 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14494 * @irq: Interrupt number.
14495 * @dev_id: The device context pointer.
14496 *
14497 * This function is the device-level interrupt handler to device with SLI-4
14498 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14499 * interrupt mode is enabled and there is an event in the HBA which requires
14500 * driver attention. This function invokes the slow-path interrupt attention
14501 * handling function and fast-path interrupt attention handling function in
14502 * turn to process the relevant HBA attention events. This function is called
14503 * without any lock held. It gets the hbalock to access and update SLI data
14504 * structures.
14505 *
14506 * This function returns IRQ_HANDLED when interrupt is handled, else it
14507 * returns IRQ_NONE.
14508 **/
14509irqreturn_t
14510lpfc_sli4_intr_handler(int irq, void *dev_id)
14511{
14512 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014513 irqreturn_t hba_irq_rc;
14514 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014515 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014516
14517 /* Get the driver's phba structure from the dev_id */
14518 phba = (struct lpfc_hba *)dev_id;
14519
14520 if (unlikely(!phba))
14521 return IRQ_NONE;
14522
14523 /*
James Smart4f774512009-05-22 14:52:35 -040014524 * Invoke fast-path host attention interrupt handling as appropriate.
14525 */
James Smart6a828b02019-01-28 11:14:31 -080014526 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014527 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014528 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014529 if (hba_irq_rc == IRQ_HANDLED)
14530 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014531 }
14532
James Smart67d12732012-08-03 12:36:13 -040014533 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014534} /* lpfc_sli4_intr_handler */
14535
James Smart93a4d6f2019-11-04 16:57:05 -080014536void lpfc_sli4_poll_hbtimer(struct timer_list *t)
14537{
14538 struct lpfc_hba *phba = from_timer(phba, t, cpuhp_poll_timer);
14539 struct lpfc_queue *eq;
14540 int i = 0;
14541
14542 rcu_read_lock();
14543
14544 list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
14545 i += lpfc_sli4_poll_eq(eq, LPFC_POLL_SLOWPATH);
14546 if (!list_empty(&phba->poll_list))
14547 mod_timer(&phba->cpuhp_poll_timer,
14548 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
14549
14550 rcu_read_unlock();
14551}
14552
14553inline int lpfc_sli4_poll_eq(struct lpfc_queue *eq, uint8_t path)
14554{
14555 struct lpfc_hba *phba = eq->phba;
14556 int i = 0;
14557
14558 /*
14559 * Unlocking an irq is one of the entry point to check
14560 * for re-schedule, but we are good for io submission
14561 * path as midlayer does a get_cpu to glue us in. Flush
14562 * out the invalidate queue so we can see the updated
14563 * value for flag.
14564 */
14565 smp_rmb();
14566
14567 if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
14568 /* We will not likely get the completion for the caller
14569 * during this iteration but i guess that's fine.
14570 * Future io's coming on this eq should be able to
14571 * pick it up. As for the case of single io's, they
14572 * will be handled through a sched from polling timer
14573 * function which is currently triggered every 1msec.
14574 */
14575 i = lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM);
14576
14577 return i;
14578}
14579
14580static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
14581{
14582 struct lpfc_hba *phba = eq->phba;
14583
James Smartf861f592020-03-22 11:12:54 -070014584 /* kickstart slowpath processing if needed */
14585 if (list_empty(&phba->poll_list))
James Smart93a4d6f2019-11-04 16:57:05 -080014586 mod_timer(&phba->cpuhp_poll_timer,
14587 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
James Smart93a4d6f2019-11-04 16:57:05 -080014588
14589 list_add_rcu(&eq->_poll_list, &phba->poll_list);
14590 synchronize_rcu();
14591}
14592
14593static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
14594{
14595 struct lpfc_hba *phba = eq->phba;
14596
14597 /* Disable slowpath processing for this eq. Kick start the eq
14598 * by RE-ARMING the eq's ASAP
14599 */
14600 list_del_rcu(&eq->_poll_list);
14601 synchronize_rcu();
14602
14603 if (list_empty(&phba->poll_list))
14604 del_timer_sync(&phba->cpuhp_poll_timer);
14605}
14606
James Smartd480e572019-11-11 15:03:58 -080014607void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
James Smart93a4d6f2019-11-04 16:57:05 -080014608{
14609 struct lpfc_queue *eq, *next;
14610
14611 list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
14612 list_del(&eq->_poll_list);
14613
14614 INIT_LIST_HEAD(&phba->poll_list);
14615 synchronize_rcu();
14616}
14617
14618static inline void
14619__lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
14620{
14621 if (mode == eq->mode)
14622 return;
14623 /*
14624 * currently this function is only called during a hotplug
14625 * event and the cpu on which this function is executing
14626 * is going offline. By now the hotplug has instructed
14627 * the scheduler to remove this cpu from cpu active mask.
14628 * So we don't need to work about being put aside by the
14629 * scheduler for a high priority process. Yes, the inte-
14630 * rrupts could come but they are known to retire ASAP.
14631 */
14632
14633 /* Disable polling in the fastpath */
14634 WRITE_ONCE(eq->mode, mode);
14635 /* flush out the store buffer */
14636 smp_wmb();
14637
14638 /*
14639 * Add this eq to the polling list and start polling. For
14640 * a grace period both interrupt handler and poller will
14641 * try to process the eq _but_ that's fine. We have a
14642 * synchronization mechanism in place (queue_claimed) to
14643 * deal with it. This is just a draining phase for int-
14644 * errupt handler (not eq's) as we have guranteed through
14645 * barrier that all the CPUs have seen the new CQ_POLLED
14646 * state. which will effectively disable the REARMING of
14647 * the EQ. The whole idea is eq's die off eventually as
14648 * we are not rearming EQ's anymore.
14649 */
14650 mode ? lpfc_sli4_add_to_poll_list(eq) :
14651 lpfc_sli4_remove_from_poll_list(eq);
14652}
14653
14654void lpfc_sli4_start_polling(struct lpfc_queue *eq)
14655{
14656 __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
14657}
14658
14659void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
14660{
14661 struct lpfc_hba *phba = eq->phba;
14662
14663 __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
14664
14665 /* Kick start for the pending io's in h/w.
14666 * Once we switch back to interrupt processing on a eq
14667 * the io path completion will only arm eq's when it
14668 * receives a completion. But since eq's are in disa-
14669 * rmed state it doesn't receive a completion. This
14670 * creates a deadlock scenaro.
14671 */
14672 phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
14673}
14674
James Smart4f774512009-05-22 14:52:35 -040014675/**
14676 * lpfc_sli4_queue_free - free a queue structure and associated memory
14677 * @queue: The queue structure to free.
14678 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014679 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014680 * the host resident queue. This function must be called after destroying the
14681 * queue on the HBA.
14682 **/
14683void
14684lpfc_sli4_queue_free(struct lpfc_queue *queue)
14685{
14686 struct lpfc_dmabuf *dmabuf;
14687
14688 if (!queue)
14689 return;
14690
James Smart4645f7b2019-03-12 16:30:14 -070014691 if (!list_empty(&queue->wq_list))
14692 list_del(&queue->wq_list);
14693
James Smart4f774512009-05-22 14:52:35 -040014694 while (!list_empty(&queue->page_list)) {
14695 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14696 list);
James Smart81b96ed2017-11-20 16:00:29 -080014697 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014698 dmabuf->virt, dmabuf->phys);
14699 kfree(dmabuf);
14700 }
James Smart895427b2017-02-12 13:52:30 -080014701 if (queue->rqbp) {
14702 lpfc_free_rq_buffer(queue->phba, queue);
14703 kfree(queue->rqbp);
14704 }
James Smartd1f525a2017-04-21 16:04:55 -070014705
James Smart32517fc2019-01-28 11:14:33 -080014706 if (!list_empty(&queue->cpu_list))
14707 list_del(&queue->cpu_list);
14708
James Smart4f774512009-05-22 14:52:35 -040014709 kfree(queue);
14710 return;
14711}
14712
14713/**
14714 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14715 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014716 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014717 * @entry_size: The size of each queue entry for this queue.
14718 * @entry count: The number of entries that this queue will handle.
James Smartc1a21eb2019-03-12 16:30:29 -070014719 * @cpu: The cpu that will primarily utilize this queue.
James Smart4f774512009-05-22 14:52:35 -040014720 *
14721 * This function allocates a queue structure and the DMAable memory used for
14722 * the host resident queue. This function must be called before creating the
14723 * queue on the HBA.
14724 **/
14725struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014726lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
James Smartc1a21eb2019-03-12 16:30:29 -070014727 uint32_t entry_size, uint32_t entry_count, int cpu)
James Smart4f774512009-05-22 14:52:35 -040014728{
14729 struct lpfc_queue *queue;
14730 struct lpfc_dmabuf *dmabuf;
James Smartcb5172e2010-03-15 11:25:07 -040014731 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart9afbee32019-03-12 16:30:28 -070014732 uint16_t x, pgcnt;
James Smart4f774512009-05-22 14:52:35 -040014733
James Smartcb5172e2010-03-15 11:25:07 -040014734 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014735 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014736
James Smart9afbee32019-03-12 16:30:28 -070014737 pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014738
14739 /* If needed, Adjust page count to match the max the adapter supports */
James Smart9afbee32019-03-12 16:30:28 -070014740 if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
14741 pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
14742
James Smartc1a21eb2019-03-12 16:30:29 -070014743 queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
14744 GFP_KERNEL, cpu_to_node(cpu));
James Smart9afbee32019-03-12 16:30:28 -070014745 if (!queue)
14746 return NULL;
James Smart895427b2017-02-12 13:52:30 -080014747
James Smart4f774512009-05-22 14:52:35 -040014748 INIT_LIST_HEAD(&queue->list);
James Smart93a4d6f2019-11-04 16:57:05 -080014749 INIT_LIST_HEAD(&queue->_poll_list);
James Smart895427b2017-02-12 13:52:30 -080014750 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014751 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014752 INIT_LIST_HEAD(&queue->page_list);
14753 INIT_LIST_HEAD(&queue->child_list);
James Smart32517fc2019-01-28 11:14:33 -080014754 INIT_LIST_HEAD(&queue->cpu_list);
James Smart81b96ed2017-11-20 16:00:29 -080014755
14756 /* Set queue parameters now. If the system cannot provide memory
14757 * resources, the free routine needs to know what was allocated.
14758 */
James Smart9afbee32019-03-12 16:30:28 -070014759 queue->page_count = pgcnt;
14760 queue->q_pgs = (void **)&queue[1];
14761 queue->entry_cnt_per_pg = hw_page_size / entry_size;
James Smart81b96ed2017-11-20 16:00:29 -080014762 queue->entry_size = entry_size;
14763 queue->entry_count = entry_count;
14764 queue->page_size = hw_page_size;
14765 queue->phba = phba;
14766
James Smart9afbee32019-03-12 16:30:28 -070014767 for (x = 0; x < queue->page_count; x++) {
James Smartc1a21eb2019-03-12 16:30:29 -070014768 dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
14769 dev_to_node(&phba->pcidev->dev));
James Smart4f774512009-05-22 14:52:35 -040014770 if (!dmabuf)
14771 goto out_fail;
Luis Chamberlain750afb02019-01-04 09:23:09 +010014772 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
14773 hw_page_size, &dmabuf->phys,
14774 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014775 if (!dmabuf->virt) {
14776 kfree(dmabuf);
14777 goto out_fail;
14778 }
14779 dmabuf->buffer_tag = x;
14780 list_add_tail(&dmabuf->list, &queue->page_list);
James Smart9afbee32019-03-12 16:30:28 -070014781 /* use lpfc_sli4_qe to index a paritcular entry in this page */
14782 queue->q_pgs[x] = dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -040014783 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014784 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14785 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart32517fc2019-01-28 11:14:33 -080014786 INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
14787 INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014788
James Smart32517fc2019-01-28 11:14:33 -080014789 /* notify_interval will be set during q creation */
James Smart64eb4dc2017-05-15 15:20:49 -070014790
James Smart4f774512009-05-22 14:52:35 -040014791 return queue;
14792out_fail:
14793 lpfc_sli4_queue_free(queue);
14794 return NULL;
14795}
14796
14797/**
James Smart962bc512013-01-03 15:44:00 -050014798 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14799 * @phba: HBA structure that indicates port to create a queue on.
14800 * @pci_barset: PCI BAR set flag.
14801 *
14802 * This function shall perform iomap of the specified PCI BAR address to host
14803 * memory address if not already done so and return it. The returned host
14804 * memory address can be NULL.
14805 */
14806static void __iomem *
14807lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14808{
James Smart962bc512013-01-03 15:44:00 -050014809 if (!phba->pcidev)
14810 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014811
14812 switch (pci_barset) {
14813 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014814 return phba->pci_bar0_memmap_p;
14815 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014816 return phba->pci_bar2_memmap_p;
14817 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014818 return phba->pci_bar4_memmap_p;
14819 default:
14820 break;
14821 }
14822 return NULL;
14823}
14824
14825/**
James Smartcb733e32019-01-28 11:14:32 -080014826 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
14827 * @phba: HBA structure that EQs are on.
14828 * @startq: The starting EQ index to modify
14829 * @numq: The number of EQs (consecutive indexes) to modify
14830 * @usdelay: amount of delay
James Smart173edbb2012-06-12 13:54:50 -040014831 *
James Smartcb733e32019-01-28 11:14:32 -080014832 * This function revises the EQ delay on 1 or more EQs. The EQ delay
14833 * is set either by writing to a register (if supported by the SLI Port)
14834 * or by mailbox command. The mailbox command allows several EQs to be
14835 * updated at once.
James Smart173edbb2012-06-12 13:54:50 -040014836 *
James Smartcb733e32019-01-28 11:14:32 -080014837 * The @phba struct is used to send a mailbox command to HBA. The @startq
14838 * is used to get the starting EQ index to change. The @numq value is
14839 * used to specify how many consecutive EQ indexes, starting at EQ index,
14840 * are to be changed. This function is asynchronous and will wait for any
14841 * mailbox commands to finish before returning.
James Smart173edbb2012-06-12 13:54:50 -040014842 *
James Smartcb733e32019-01-28 11:14:32 -080014843 * On success this function will return a zero. If unable to allocate
14844 * enough memory this function will return -ENOMEM. If a mailbox command
14845 * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
14846 * have had their delay multipler changed.
James Smart173edbb2012-06-12 13:54:50 -040014847 **/
James Smartcb733e32019-01-28 11:14:32 -080014848void
James Smart0cf07f842017-06-01 21:07:10 -070014849lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
James Smartcb733e32019-01-28 11:14:32 -080014850 uint32_t numq, uint32_t usdelay)
James Smart173edbb2012-06-12 13:54:50 -040014851{
14852 struct lpfc_mbx_modify_eq_delay *eq_delay;
14853 LPFC_MBOXQ_t *mbox;
14854 struct lpfc_queue *eq;
James Smartcb733e32019-01-28 11:14:32 -080014855 int cnt = 0, rc, length;
James Smart173edbb2012-06-12 13:54:50 -040014856 uint32_t shdr_status, shdr_add_status;
James Smartcb733e32019-01-28 11:14:32 -080014857 uint32_t dmult;
James Smart895427b2017-02-12 13:52:30 -080014858 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014859 union lpfc_sli4_cfg_shdr *shdr;
James Smart173edbb2012-06-12 13:54:50 -040014860
James Smart6a828b02019-01-28 11:14:31 -080014861 if (startq >= phba->cfg_irq_chann)
James Smartcb733e32019-01-28 11:14:32 -080014862 return;
14863
14864 if (usdelay > 0xFFFF) {
14865 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
14866 "6429 usdelay %d too large. Scaled down to "
14867 "0xFFFF.\n", usdelay);
14868 usdelay = 0xFFFF;
14869 }
14870
14871 /* set values by EQ_DELAY register if supported */
14872 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14873 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070014874 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smartcb733e32019-01-28 11:14:32 -080014875 if (!eq)
14876 continue;
14877
James Smart32517fc2019-01-28 11:14:33 -080014878 lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
James Smartcb733e32019-01-28 11:14:32 -080014879
14880 if (++cnt >= numq)
14881 break;
14882 }
James Smartcb733e32019-01-28 11:14:32 -080014883 return;
14884 }
14885
14886 /* Otherwise, set values by mailbox cmd */
James Smart173edbb2012-06-12 13:54:50 -040014887
14888 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smartcb733e32019-01-28 11:14:32 -080014889 if (!mbox) {
14890 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_FCP | LOG_NVME,
14891 "6428 Failed allocating mailbox cmd buffer."
14892 " EQ delay was not set.\n");
14893 return;
14894 }
James Smart173edbb2012-06-12 13:54:50 -040014895 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14896 sizeof(struct lpfc_sli4_cfg_mhdr));
14897 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14898 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14899 length, LPFC_SLI4_MBX_EMBED);
14900 eq_delay = &mbox->u.mqe.un.eq_delay;
14901
14902 /* Calculate delay multiper from maximum interrupt per second */
James Smartcb733e32019-01-28 11:14:32 -080014903 dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
14904 if (dmult)
14905 dmult--;
James Smart0cf07f842017-06-01 21:07:10 -070014906 if (dmult > LPFC_DMULT_MAX)
14907 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014908
James Smart6a828b02019-01-28 11:14:31 -080014909 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070014910 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart173edbb2012-06-12 13:54:50 -040014911 if (!eq)
14912 continue;
James Smartcb733e32019-01-28 11:14:32 -080014913 eq->q_mode = usdelay;
James Smart173edbb2012-06-12 13:54:50 -040014914 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14915 eq_delay->u.request.eq[cnt].phase = 0;
14916 eq_delay->u.request.eq[cnt].delay_multi = dmult;
James Smart0cf07f842017-06-01 21:07:10 -070014917
James Smartcb733e32019-01-28 11:14:32 -080014918 if (++cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014919 break;
14920 }
14921 eq_delay->u.request.num_eq = cnt;
14922
14923 mbox->vport = phba->pport;
14924 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014925 mbox->ctx_buf = NULL;
14926 mbox->ctx_ndlp = NULL;
James Smart173edbb2012-06-12 13:54:50 -040014927 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14928 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14929 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14930 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14931 if (shdr_status || shdr_add_status || rc) {
14932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14933 "2512 MODIFY_EQ_DELAY mailbox failed with "
14934 "status x%x add_status x%x, mbx status x%x\n",
14935 shdr_status, shdr_add_status, rc);
James Smart173edbb2012-06-12 13:54:50 -040014936 }
14937 mempool_free(mbox, phba->mbox_mem_pool);
James Smartcb733e32019-01-28 11:14:32 -080014938 return;
James Smart173edbb2012-06-12 13:54:50 -040014939}
14940
14941/**
James Smart4f774512009-05-22 14:52:35 -040014942 * lpfc_eq_create - Create an Event Queue on the HBA
14943 * @phba: HBA structure that indicates port to create a queue on.
14944 * @eq: The queue structure to use to create the event queue.
14945 * @imax: The maximum interrupt per second limit.
14946 *
14947 * This function creates an event queue, as detailed in @eq, on a port,
14948 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14949 *
14950 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14951 * is used to get the entry count and entry size that are necessary to
14952 * determine the number of pages to allocate and use for this queue. This
14953 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14954 * event queue. This function is asynchronous and will wait for the mailbox
14955 * command to finish before continuing.
14956 *
14957 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014958 * memory this function will return -ENOMEM. If the queue create mailbox command
14959 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014960 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014961int
James Smartee020062012-09-29 11:28:52 -040014962lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014963{
14964 struct lpfc_mbx_eq_create *eq_create;
14965 LPFC_MBOXQ_t *mbox;
14966 int rc, length, status = 0;
14967 struct lpfc_dmabuf *dmabuf;
14968 uint32_t shdr_status, shdr_add_status;
14969 union lpfc_sli4_cfg_shdr *shdr;
14970 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014971 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14972
James Smart2e90f4b2011-12-13 13:22:37 -050014973 /* sanity check on queue memory */
14974 if (!eq)
14975 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014976 if (!phba->sli4_hba.pc_sli4_params.supported)
14977 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014978
14979 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14980 if (!mbox)
14981 return -ENOMEM;
14982 length = (sizeof(struct lpfc_mbx_eq_create) -
14983 sizeof(struct lpfc_sli4_cfg_mhdr));
14984 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14985 LPFC_MBOX_OPCODE_EQ_CREATE,
14986 length, LPFC_SLI4_MBX_EMBED);
14987 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014988 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014989 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14990 eq->page_count);
14991 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14992 LPFC_EQE_SIZE);
14993 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014994
14995 /* Use version 2 of CREATE_EQ if eqav is set */
14996 if (phba->sli4_hba.pc_sli4_params.eqav) {
14997 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14998 LPFC_Q_CREATE_VERSION_2);
14999 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
15000 phba->sli4_hba.pc_sli4_params.eqav);
15001 }
15002
James Smart2c9c5a02015-04-07 15:07:15 -040015003 /* don't setup delay multiplier using EQ_CREATE */
15004 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040015005 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
15006 dmult);
15007 switch (eq->entry_count) {
15008 default:
15009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15010 "0360 Unsupported EQ count. (%d)\n",
15011 eq->entry_count);
James Smart04d210c2019-05-21 17:49:03 -070015012 if (eq->entry_count < 256) {
15013 status = -EINVAL;
15014 goto out;
15015 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015016 /* fall through - otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040015017 case 256:
15018 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15019 LPFC_EQ_CNT_256);
15020 break;
15021 case 512:
15022 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15023 LPFC_EQ_CNT_512);
15024 break;
15025 case 1024:
15026 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15027 LPFC_EQ_CNT_1024);
15028 break;
15029 case 2048:
15030 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15031 LPFC_EQ_CNT_2048);
15032 break;
15033 case 4096:
15034 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15035 LPFC_EQ_CNT_4096);
15036 break;
15037 }
15038 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015039 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015040 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15041 putPaddrLow(dmabuf->phys);
15042 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15043 putPaddrHigh(dmabuf->phys);
15044 }
15045 mbox->vport = phba->pport;
15046 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080015047 mbox->ctx_buf = NULL;
15048 mbox->ctx_ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -040015049 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040015050 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15051 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15052 if (shdr_status || shdr_add_status || rc) {
15053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15054 "2500 EQ_CREATE mailbox failed with "
15055 "status x%x add_status x%x, mbx status x%x\n",
15056 shdr_status, shdr_add_status, rc);
15057 status = -ENXIO;
15058 }
15059 eq->type = LPFC_EQ;
15060 eq->subtype = LPFC_NONE;
15061 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
15062 if (eq->queue_id == 0xFFFF)
15063 status = -ENXIO;
15064 eq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015065 eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
15066 eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
James Smart04d210c2019-05-21 17:49:03 -070015067out:
James Smart8fa38512009-07-19 10:01:03 -040015068 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015069 return status;
15070}
15071
15072/**
15073 * lpfc_cq_create - Create a Completion Queue on the HBA
15074 * @phba: HBA structure that indicates port to create a queue on.
15075 * @cq: The queue structure to use to create the completion queue.
15076 * @eq: The event queue to bind this completion queue to.
15077 *
15078 * This function creates a completion queue, as detailed in @wq, on a port,
15079 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
15080 *
15081 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15082 * is used to get the entry count and entry size that are necessary to
15083 * determine the number of pages to allocate and use for this queue. The @eq
15084 * is used to indicate which event queue to bind this completion queue to. This
15085 * function will send the CQ_CREATE mailbox command to the HBA to setup the
15086 * completion queue. This function is asynchronous and will wait for the mailbox
15087 * command to finish before continuing.
15088 *
15089 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015090 * memory this function will return -ENOMEM. If the queue create mailbox command
15091 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015092 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015093int
James Smart4f774512009-05-22 14:52:35 -040015094lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
15095 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
15096{
15097 struct lpfc_mbx_cq_create *cq_create;
15098 struct lpfc_dmabuf *dmabuf;
15099 LPFC_MBOXQ_t *mbox;
15100 int rc, length, status = 0;
15101 uint32_t shdr_status, shdr_add_status;
15102 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015103
James Smart2e90f4b2011-12-13 13:22:37 -050015104 /* sanity check on queue memory */
15105 if (!cq || !eq)
15106 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015107
James Smart4f774512009-05-22 14:52:35 -040015108 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15109 if (!mbox)
15110 return -ENOMEM;
15111 length = (sizeof(struct lpfc_mbx_cq_create) -
15112 sizeof(struct lpfc_sli4_cfg_mhdr));
15113 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15114 LPFC_MBOX_OPCODE_CQ_CREATE,
15115 length, LPFC_SLI4_MBX_EMBED);
15116 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015117 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015118 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
15119 cq->page_count);
15120 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
15121 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015122 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15123 phba->sli4_hba.pc_sli4_params.cqv);
15124 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080015125 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
15126 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015127 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
15128 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080015129 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
15130 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050015131 } else {
15132 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
15133 eq->queue_id);
15134 }
James Smart4f774512009-05-22 14:52:35 -040015135 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080015136 case 2048:
15137 case 4096:
15138 if (phba->sli4_hba.pc_sli4_params.cqv ==
15139 LPFC_Q_CREATE_VERSION_2) {
15140 cq_create->u.request.context.lpfc_cq_context_count =
15141 cq->entry_count;
15142 bf_set(lpfc_cq_context_count,
15143 &cq_create->u.request.context,
15144 LPFC_CQ_CNT_WORD7);
15145 break;
15146 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015147 /* fall through */
James Smart4f774512009-05-22 14:52:35 -040015148 default:
15149 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080015150 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070015151 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080015152 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070015153 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040015154 if (cq->entry_count < 256) {
15155 status = -EINVAL;
15156 goto out;
15157 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015158 /* fall through - otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040015159 case 256:
15160 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15161 LPFC_CQ_CNT_256);
15162 break;
15163 case 512:
15164 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15165 LPFC_CQ_CNT_512);
15166 break;
15167 case 1024:
15168 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15169 LPFC_CQ_CNT_1024);
15170 break;
15171 }
15172 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080015173 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040015174 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15175 putPaddrLow(dmabuf->phys);
15176 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15177 putPaddrHigh(dmabuf->phys);
15178 }
15179 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15180
15181 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015182 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15183 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15184 if (shdr_status || shdr_add_status || rc) {
15185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15186 "2501 CQ_CREATE mailbox failed with "
15187 "status x%x add_status x%x, mbx status x%x\n",
15188 shdr_status, shdr_add_status, rc);
15189 status = -ENXIO;
15190 goto out;
15191 }
15192 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
15193 if (cq->queue_id == 0xFFFF) {
15194 status = -ENXIO;
15195 goto out;
15196 }
15197 /* link the cq onto the parent eq child list */
15198 list_add_tail(&cq->list, &eq->child_list);
15199 /* Set up completion queue's type and subtype */
15200 cq->type = type;
15201 cq->subtype = subtype;
15202 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050015203 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080015204 cq->assoc_qp = eq;
James Smart4f774512009-05-22 14:52:35 -040015205 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015206 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15207 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
James Smart4f774512009-05-22 14:52:35 -040015208
James Smart6a828b02019-01-28 11:14:31 -080015209 if (cq->queue_id > phba->sli4_hba.cq_max)
15210 phba->sli4_hba.cq_max = cq->queue_id;
James Smart8fa38512009-07-19 10:01:03 -040015211out:
15212 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015213 return status;
15214}
15215
15216/**
James Smart2d7dbc42017-02-12 13:52:35 -080015217 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
15218 * @phba: HBA structure that indicates port to create a queue on.
15219 * @cqp: The queue structure array to use to create the completion queues.
James Smartcdb42be2019-01-28 11:14:21 -080015220 * @hdwq: The hardware queue array with the EQ to bind completion queues to.
James Smart2d7dbc42017-02-12 13:52:35 -080015221 *
15222 * This function creates a set of completion queue, s to support MRQ
15223 * as detailed in @cqp, on a port,
15224 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
15225 *
15226 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15227 * is used to get the entry count and entry size that are necessary to
15228 * determine the number of pages to allocate and use for this queue. The @eq
15229 * is used to indicate which event queue to bind this completion queue to. This
15230 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
15231 * completion queue. This function is asynchronous and will wait for the mailbox
15232 * command to finish before continuing.
15233 *
15234 * On success this function will return a zero. If unable to allocate enough
15235 * memory this function will return -ENOMEM. If the queue create mailbox command
15236 * fails this function will return -ENXIO.
15237 **/
15238int
15239lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
James Smartcdb42be2019-01-28 11:14:21 -080015240 struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
15241 uint32_t subtype)
James Smart2d7dbc42017-02-12 13:52:35 -080015242{
15243 struct lpfc_queue *cq;
15244 struct lpfc_queue *eq;
15245 struct lpfc_mbx_cq_create_set *cq_set;
15246 struct lpfc_dmabuf *dmabuf;
15247 LPFC_MBOXQ_t *mbox;
15248 int rc, length, alloclen, status = 0;
15249 int cnt, idx, numcq, page_idx = 0;
15250 uint32_t shdr_status, shdr_add_status;
15251 union lpfc_sli4_cfg_shdr *shdr;
15252 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15253
15254 /* sanity check on queue memory */
15255 numcq = phba->cfg_nvmet_mrq;
James Smartcdb42be2019-01-28 11:14:21 -080015256 if (!cqp || !hdwq || !numcq)
James Smart2d7dbc42017-02-12 13:52:35 -080015257 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080015258
15259 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15260 if (!mbox)
15261 return -ENOMEM;
15262
15263 length = sizeof(struct lpfc_mbx_cq_create_set);
15264 length += ((numcq * cqp[0]->page_count) *
15265 sizeof(struct dma_address));
15266 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15267 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
15268 LPFC_SLI4_MBX_NEMBED);
15269 if (alloclen < length) {
15270 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15271 "3098 Allocated DMA memory size (%d) is "
15272 "less than the requested DMA memory size "
15273 "(%d)\n", alloclen, length);
15274 status = -ENOMEM;
15275 goto out;
15276 }
15277 cq_set = mbox->sge_array->addr[0];
15278 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
15279 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
15280
15281 for (idx = 0; idx < numcq; idx++) {
15282 cq = cqp[idx];
James Smartcdb42be2019-01-28 11:14:21 -080015283 eq = hdwq[idx].hba_eq;
James Smart2d7dbc42017-02-12 13:52:35 -080015284 if (!cq || !eq) {
15285 status = -ENOMEM;
15286 goto out;
15287 }
James Smart81b96ed2017-11-20 16:00:29 -080015288 if (!phba->sli4_hba.pc_sli4_params.supported)
15289 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080015290
15291 switch (idx) {
15292 case 0:
15293 bf_set(lpfc_mbx_cq_create_set_page_size,
15294 &cq_set->u.request,
15295 (hw_page_size / SLI4_PAGE_SIZE));
15296 bf_set(lpfc_mbx_cq_create_set_num_pages,
15297 &cq_set->u.request, cq->page_count);
15298 bf_set(lpfc_mbx_cq_create_set_evt,
15299 &cq_set->u.request, 1);
15300 bf_set(lpfc_mbx_cq_create_set_valid,
15301 &cq_set->u.request, 1);
15302 bf_set(lpfc_mbx_cq_create_set_cqe_size,
15303 &cq_set->u.request, 0);
15304 bf_set(lpfc_mbx_cq_create_set_num_cq,
15305 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080015306 bf_set(lpfc_mbx_cq_create_set_autovalid,
15307 &cq_set->u.request,
15308 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080015309 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080015310 case 2048:
15311 case 4096:
15312 if (phba->sli4_hba.pc_sli4_params.cqv ==
15313 LPFC_Q_CREATE_VERSION_2) {
15314 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15315 &cq_set->u.request,
15316 cq->entry_count);
15317 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15318 &cq_set->u.request,
15319 LPFC_CQ_CNT_WORD7);
15320 break;
15321 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015322 /* fall through */
James Smart2d7dbc42017-02-12 13:52:35 -080015323 default:
15324 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15325 "3118 Bad CQ count. (%d)\n",
15326 cq->entry_count);
15327 if (cq->entry_count < 256) {
15328 status = -EINVAL;
15329 goto out;
15330 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015331 /* fall through - otherwise default to smallest */
James Smart2d7dbc42017-02-12 13:52:35 -080015332 case 256:
15333 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15334 &cq_set->u.request, LPFC_CQ_CNT_256);
15335 break;
15336 case 512:
15337 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15338 &cq_set->u.request, LPFC_CQ_CNT_512);
15339 break;
15340 case 1024:
15341 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15342 &cq_set->u.request, LPFC_CQ_CNT_1024);
15343 break;
15344 }
15345 bf_set(lpfc_mbx_cq_create_set_eq_id0,
15346 &cq_set->u.request, eq->queue_id);
15347 break;
15348 case 1:
15349 bf_set(lpfc_mbx_cq_create_set_eq_id1,
15350 &cq_set->u.request, eq->queue_id);
15351 break;
15352 case 2:
15353 bf_set(lpfc_mbx_cq_create_set_eq_id2,
15354 &cq_set->u.request, eq->queue_id);
15355 break;
15356 case 3:
15357 bf_set(lpfc_mbx_cq_create_set_eq_id3,
15358 &cq_set->u.request, eq->queue_id);
15359 break;
15360 case 4:
15361 bf_set(lpfc_mbx_cq_create_set_eq_id4,
15362 &cq_set->u.request, eq->queue_id);
15363 break;
15364 case 5:
15365 bf_set(lpfc_mbx_cq_create_set_eq_id5,
15366 &cq_set->u.request, eq->queue_id);
15367 break;
15368 case 6:
15369 bf_set(lpfc_mbx_cq_create_set_eq_id6,
15370 &cq_set->u.request, eq->queue_id);
15371 break;
15372 case 7:
15373 bf_set(lpfc_mbx_cq_create_set_eq_id7,
15374 &cq_set->u.request, eq->queue_id);
15375 break;
15376 case 8:
15377 bf_set(lpfc_mbx_cq_create_set_eq_id8,
15378 &cq_set->u.request, eq->queue_id);
15379 break;
15380 case 9:
15381 bf_set(lpfc_mbx_cq_create_set_eq_id9,
15382 &cq_set->u.request, eq->queue_id);
15383 break;
15384 case 10:
15385 bf_set(lpfc_mbx_cq_create_set_eq_id10,
15386 &cq_set->u.request, eq->queue_id);
15387 break;
15388 case 11:
15389 bf_set(lpfc_mbx_cq_create_set_eq_id11,
15390 &cq_set->u.request, eq->queue_id);
15391 break;
15392 case 12:
15393 bf_set(lpfc_mbx_cq_create_set_eq_id12,
15394 &cq_set->u.request, eq->queue_id);
15395 break;
15396 case 13:
15397 bf_set(lpfc_mbx_cq_create_set_eq_id13,
15398 &cq_set->u.request, eq->queue_id);
15399 break;
15400 case 14:
15401 bf_set(lpfc_mbx_cq_create_set_eq_id14,
15402 &cq_set->u.request, eq->queue_id);
15403 break;
15404 case 15:
15405 bf_set(lpfc_mbx_cq_create_set_eq_id15,
15406 &cq_set->u.request, eq->queue_id);
15407 break;
15408 }
15409
15410 /* link the cq onto the parent eq child list */
15411 list_add_tail(&cq->list, &eq->child_list);
15412 /* Set up completion queue's type and subtype */
15413 cq->type = type;
15414 cq->subtype = subtype;
15415 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080015416 cq->assoc_qp = eq;
James Smart2d7dbc42017-02-12 13:52:35 -080015417 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015418 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15419 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
15420 cq->entry_count);
James Smart81b96ed2017-11-20 16:00:29 -080015421 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080015422
15423 rc = 0;
15424 list_for_each_entry(dmabuf, &cq->page_list, list) {
15425 memset(dmabuf->virt, 0, hw_page_size);
15426 cnt = page_idx + dmabuf->buffer_tag;
15427 cq_set->u.request.page[cnt].addr_lo =
15428 putPaddrLow(dmabuf->phys);
15429 cq_set->u.request.page[cnt].addr_hi =
15430 putPaddrHigh(dmabuf->phys);
15431 rc++;
15432 }
15433 page_idx += rc;
15434 }
15435
15436 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15437
15438 /* The IOCTL status is embedded in the mailbox subheader. */
15439 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15440 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15441 if (shdr_status || shdr_add_status || rc) {
15442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15443 "3119 CQ_CREATE_SET mailbox failed with "
15444 "status x%x add_status x%x, mbx status x%x\n",
15445 shdr_status, shdr_add_status, rc);
15446 status = -ENXIO;
15447 goto out;
15448 }
15449 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15450 if (rc == 0xFFFF) {
15451 status = -ENXIO;
15452 goto out;
15453 }
15454
15455 for (idx = 0; idx < numcq; idx++) {
15456 cq = cqp[idx];
15457 cq->queue_id = rc + idx;
James Smart6a828b02019-01-28 11:14:31 -080015458 if (cq->queue_id > phba->sli4_hba.cq_max)
15459 phba->sli4_hba.cq_max = cq->queue_id;
James Smart2d7dbc42017-02-12 13:52:35 -080015460 }
15461
15462out:
15463 lpfc_sli4_mbox_cmd_free(phba, mbox);
15464 return status;
15465}
15466
15467/**
James Smartb19a0612010-04-06 14:48:51 -040015468 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040015469 * @phba: HBA structure that indicates port to create a queue on.
15470 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040015471 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15472 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040015473 *
James Smartb19a0612010-04-06 14:48:51 -040015474 * This function provides failback (fb) functionality when the
15475 * mq_create_ext fails on older FW generations. It's purpose is identical
15476 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040015477 *
James Smartb19a0612010-04-06 14:48:51 -040015478 * This routine cannot fail as all attributes were previously accessed and
15479 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040015480 **/
James Smartb19a0612010-04-06 14:48:51 -040015481static void
15482lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15483 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040015484{
15485 struct lpfc_mbx_mq_create *mq_create;
15486 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040015487 int length;
James Smart04c68492009-05-22 14:52:52 -040015488
James Smart04c68492009-05-22 14:52:52 -040015489 length = (sizeof(struct lpfc_mbx_mq_create) -
15490 sizeof(struct lpfc_sli4_cfg_mhdr));
15491 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15492 LPFC_MBOX_OPCODE_MQ_CREATE,
15493 length, LPFC_SLI4_MBX_EMBED);
15494 mq_create = &mbox->u.mqe.un.mq_create;
15495 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040015496 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040015497 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040015498 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040015499 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15500 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040015501 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015502 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15503 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040015504 break;
15505 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015506 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15507 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040015508 break;
15509 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015510 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15511 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040015512 break;
15513 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015514 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15515 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040015516 break;
15517 }
15518 list_for_each_entry(dmabuf, &mq->page_list, list) {
15519 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040015520 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040015521 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040015522 putPaddrHigh(dmabuf->phys);
15523 }
15524}
15525
15526/**
15527 * lpfc_mq_create - Create a mailbox Queue on the HBA
15528 * @phba: HBA structure that indicates port to create a queue on.
15529 * @mq: The queue structure to use to create the mailbox queue.
15530 * @cq: The completion queue to associate with this cq.
15531 * @subtype: The queue's subtype.
15532 *
15533 * This function creates a mailbox queue, as detailed in @mq, on a port,
15534 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15535 *
15536 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15537 * is used to get the entry count and entry size that are necessary to
15538 * determine the number of pages to allocate and use for this queue. This
15539 * function will send the MQ_CREATE mailbox command to the HBA to setup the
15540 * mailbox queue. This function is asynchronous and will wait for the mailbox
15541 * command to finish before continuing.
15542 *
15543 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015544 * memory this function will return -ENOMEM. If the queue create mailbox command
15545 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040015546 **/
15547int32_t
15548lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15549 struct lpfc_queue *cq, uint32_t subtype)
15550{
15551 struct lpfc_mbx_mq_create *mq_create;
15552 struct lpfc_mbx_mq_create_ext *mq_create_ext;
15553 struct lpfc_dmabuf *dmabuf;
15554 LPFC_MBOXQ_t *mbox;
15555 int rc, length, status = 0;
15556 uint32_t shdr_status, shdr_add_status;
15557 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015558 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040015559
James Smart2e90f4b2011-12-13 13:22:37 -050015560 /* sanity check on queue memory */
15561 if (!mq || !cq)
15562 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015563 if (!phba->sli4_hba.pc_sli4_params.supported)
15564 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040015565
15566 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15567 if (!mbox)
15568 return -ENOMEM;
15569 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15570 sizeof(struct lpfc_sli4_cfg_mhdr));
15571 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15572 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15573 length, LPFC_SLI4_MBX_EMBED);
15574
15575 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015576 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015577 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15578 &mq_create_ext->u.request, mq->page_count);
15579 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15580 &mq_create_ext->u.request, 1);
15581 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015582 &mq_create_ext->u.request, 1);
15583 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15584 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015585 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15586 &mq_create_ext->u.request, 1);
15587 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15588 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015589 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015590 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15591 phba->sli4_hba.pc_sli4_params.mqv);
15592 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15593 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15594 cq->queue_id);
15595 else
15596 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15597 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015598 switch (mq->entry_count) {
15599 default:
15600 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15601 "0362 Unsupported MQ count. (%d)\n",
15602 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015603 if (mq->entry_count < 16) {
15604 status = -EINVAL;
15605 goto out;
15606 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015607 /* fall through - otherwise default to smallest count */
James Smartb19a0612010-04-06 14:48:51 -040015608 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015609 bf_set(lpfc_mq_context_ring_size,
15610 &mq_create_ext->u.request.context,
15611 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015612 break;
15613 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015614 bf_set(lpfc_mq_context_ring_size,
15615 &mq_create_ext->u.request.context,
15616 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015617 break;
15618 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015619 bf_set(lpfc_mq_context_ring_size,
15620 &mq_create_ext->u.request.context,
15621 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015622 break;
15623 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015624 bf_set(lpfc_mq_context_ring_size,
15625 &mq_create_ext->u.request.context,
15626 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015627 break;
15628 }
15629 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015630 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015631 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15632 putPaddrLow(dmabuf->phys);
15633 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015634 putPaddrHigh(dmabuf->phys);
15635 }
15636 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015637 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15638 &mq_create_ext->u.response);
15639 if (rc != MBX_SUCCESS) {
15640 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15641 "2795 MQ_CREATE_EXT failed with "
15642 "status x%x. Failback to MQ_CREATE.\n",
15643 rc);
15644 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15645 mq_create = &mbox->u.mqe.un.mq_create;
15646 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15647 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15648 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15649 &mq_create->u.response);
15650 }
15651
James Smart04c68492009-05-22 14:52:52 -040015652 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015653 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15654 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15655 if (shdr_status || shdr_add_status || rc) {
15656 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15657 "2502 MQ_CREATE mailbox failed with "
15658 "status x%x add_status x%x, mbx status x%x\n",
15659 shdr_status, shdr_add_status, rc);
15660 status = -ENXIO;
15661 goto out;
15662 }
James Smart04c68492009-05-22 14:52:52 -040015663 if (mq->queue_id == 0xFFFF) {
15664 status = -ENXIO;
15665 goto out;
15666 }
15667 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015668 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015669 mq->subtype = subtype;
15670 mq->host_index = 0;
15671 mq->hba_index = 0;
15672
15673 /* link the mq onto the parent cq child list */
15674 list_add_tail(&mq->list, &cq->child_list);
15675out:
James Smart8fa38512009-07-19 10:01:03 -040015676 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015677 return status;
15678}
15679
15680/**
James Smart4f774512009-05-22 14:52:35 -040015681 * lpfc_wq_create - Create a Work Queue on the HBA
15682 * @phba: HBA structure that indicates port to create a queue on.
15683 * @wq: The queue structure to use to create the work queue.
15684 * @cq: The completion queue to bind this work queue to.
15685 * @subtype: The subtype of the work queue indicating its functionality.
15686 *
15687 * This function creates a work queue, as detailed in @wq, on a port, described
15688 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15689 *
15690 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15691 * is used to get the entry count and entry size that are necessary to
15692 * determine the number of pages to allocate and use for this queue. The @cq
15693 * is used to indicate which completion queue to bind this work queue to. This
15694 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15695 * work queue. This function is asynchronous and will wait for the mailbox
15696 * command to finish before continuing.
15697 *
15698 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015699 * memory this function will return -ENOMEM. If the queue create mailbox command
15700 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015701 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015702int
James Smart4f774512009-05-22 14:52:35 -040015703lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15704 struct lpfc_queue *cq, uint32_t subtype)
15705{
15706 struct lpfc_mbx_wq_create *wq_create;
15707 struct lpfc_dmabuf *dmabuf;
15708 LPFC_MBOXQ_t *mbox;
15709 int rc, length, status = 0;
15710 uint32_t shdr_status, shdr_add_status;
15711 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015712 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015713 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015714 void __iomem *bar_memmap_p;
15715 uint32_t db_offset;
15716 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015717 uint8_t dpp_barset;
15718 uint32_t dpp_offset;
15719 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015720 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015721
James Smart2e90f4b2011-12-13 13:22:37 -050015722 /* sanity check on queue memory */
15723 if (!wq || !cq)
15724 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015725 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015726 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015727
15728 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15729 if (!mbox)
15730 return -ENOMEM;
15731 length = (sizeof(struct lpfc_mbx_wq_create) -
15732 sizeof(struct lpfc_sli4_cfg_mhdr));
15733 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15734 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15735 length, LPFC_SLI4_MBX_EMBED);
15736 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015737 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015738 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15739 wq->page_count);
15740 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15741 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015742
15743 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015744 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15745 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015746
James Smartc176ffa2018-01-30 15:58:46 -080015747 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15748 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015749 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15750 else
15751 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15752
James Smart0c651872013-07-15 18:33:23 -040015753
James Smart1351e692018-02-22 08:18:43 -080015754 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15755 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15756 else
15757 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15758
15759 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015760 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015761 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15762 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015763 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15764 LPFC_Q_CREATE_VERSION_1);
15765
James Smart5a6f1332011-03-11 16:05:35 -050015766 switch (wq->entry_size) {
15767 default:
15768 case 64:
15769 bf_set(lpfc_mbx_wq_create_wqe_size,
15770 &wq_create->u.request_1,
15771 LPFC_WQ_WQE_SIZE_64);
15772 break;
15773 case 128:
15774 bf_set(lpfc_mbx_wq_create_wqe_size,
15775 &wq_create->u.request_1,
15776 LPFC_WQ_WQE_SIZE_128);
15777 break;
15778 }
James Smart1351e692018-02-22 08:18:43 -080015779 /* Request DPP by default */
15780 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015781 bf_set(lpfc_mbx_wq_create_page_size,
15782 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015783 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015784 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015785 break;
15786 default:
James Smart1351e692018-02-22 08:18:43 -080015787 page = wq_create->u.request.page;
15788 break;
James Smart5a6f1332011-03-11 16:05:35 -050015789 }
James Smart0c651872013-07-15 18:33:23 -040015790
James Smart4f774512009-05-22 14:52:35 -040015791 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015792 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015793 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15794 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015795 }
James Smart962bc512013-01-03 15:44:00 -050015796
15797 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15798 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15799
James Smart4f774512009-05-22 14:52:35 -040015800 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15801 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015802 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15803 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15804 if (shdr_status || shdr_add_status || rc) {
15805 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15806 "2503 WQ_CREATE mailbox failed with "
15807 "status x%x add_status x%x, mbx status x%x\n",
15808 shdr_status, shdr_add_status, rc);
15809 status = -ENXIO;
15810 goto out;
15811 }
James Smart1351e692018-02-22 08:18:43 -080015812
15813 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15814 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15815 &wq_create->u.response);
15816 else
15817 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15818 &wq_create->u.response_1);
15819
James Smart4f774512009-05-22 14:52:35 -040015820 if (wq->queue_id == 0xFFFF) {
15821 status = -ENXIO;
15822 goto out;
15823 }
James Smart1351e692018-02-22 08:18:43 -080015824
15825 wq->db_format = LPFC_DB_LIST_FORMAT;
15826 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15827 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15828 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15829 &wq_create->u.response);
15830 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15831 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15833 "3265 WQ[%d] doorbell format "
15834 "not supported: x%x\n",
15835 wq->queue_id, wq->db_format);
15836 status = -EINVAL;
15837 goto out;
15838 }
15839 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15840 &wq_create->u.response);
15841 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15842 pci_barset);
15843 if (!bar_memmap_p) {
15844 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15845 "3263 WQ[%d] failed to memmap "
15846 "pci barset:x%x\n",
15847 wq->queue_id, pci_barset);
15848 status = -ENOMEM;
15849 goto out;
15850 }
15851 db_offset = wq_create->u.response.doorbell_offset;
15852 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15853 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15855 "3252 WQ[%d] doorbell offset "
15856 "not supported: x%x\n",
15857 wq->queue_id, db_offset);
15858 status = -EINVAL;
15859 goto out;
15860 }
15861 wq->db_regaddr = bar_memmap_p + db_offset;
15862 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15863 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15864 "format:x%x\n", wq->queue_id,
15865 pci_barset, db_offset, wq->db_format);
15866 } else
15867 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015868 } else {
James Smart1351e692018-02-22 08:18:43 -080015869 /* Check if DPP was honored by the firmware */
15870 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15871 &wq_create->u.response_1);
15872 if (wq->dpp_enable) {
15873 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15874 &wq_create->u.response_1);
15875 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15876 pci_barset);
15877 if (!bar_memmap_p) {
15878 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15879 "3267 WQ[%d] failed to memmap "
15880 "pci barset:x%x\n",
15881 wq->queue_id, pci_barset);
15882 status = -ENOMEM;
15883 goto out;
15884 }
15885 db_offset = wq_create->u.response_1.doorbell_offset;
15886 wq->db_regaddr = bar_memmap_p + db_offset;
15887 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15888 &wq_create->u.response_1);
15889 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15890 &wq_create->u.response_1);
15891 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15892 dpp_barset);
15893 if (!bar_memmap_p) {
15894 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15895 "3268 WQ[%d] failed to memmap "
15896 "pci barset:x%x\n",
15897 wq->queue_id, dpp_barset);
15898 status = -ENOMEM;
15899 goto out;
15900 }
15901 dpp_offset = wq_create->u.response_1.dpp_offset;
15902 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15903 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15904 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15905 "dpp_id:x%x dpp_barset:x%x "
15906 "dpp_offset:x%x\n",
15907 wq->queue_id, pci_barset, db_offset,
15908 wq->dpp_id, dpp_barset, dpp_offset);
15909
15910 /* Enable combined writes for DPP aperture */
15911 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15912#ifdef CONFIG_X86
15913 rc = set_memory_wc(pg_addr, 1);
15914 if (rc) {
15915 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15916 "3272 Cannot setup Combined "
15917 "Write on WQ[%d] - disable DPP\n",
15918 wq->queue_id);
15919 phba->cfg_enable_dpp = 0;
15920 }
15921#else
15922 phba->cfg_enable_dpp = 0;
15923#endif
15924 } else
15925 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015926 }
James Smart895427b2017-02-12 13:52:30 -080015927 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15928 if (wq->pring == NULL) {
15929 status = -ENOMEM;
15930 goto out;
15931 }
James Smart4f774512009-05-22 14:52:35 -040015932 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015933 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015934 wq->subtype = subtype;
15935 wq->host_index = 0;
15936 wq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015937 wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040015938
15939 /* link the wq onto the parent cq child list */
15940 list_add_tail(&wq->list, &cq->child_list);
15941out:
James Smart8fa38512009-07-19 10:01:03 -040015942 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015943 return status;
15944}
15945
15946/**
15947 * lpfc_rq_create - Create a Receive Queue on the HBA
15948 * @phba: HBA structure that indicates port to create a queue on.
15949 * @hrq: The queue structure to use to create the header receive queue.
15950 * @drq: The queue structure to use to create the data receive queue.
15951 * @cq: The completion queue to bind this work queue to.
15952 *
15953 * This function creates a receive buffer queue pair , as detailed in @hrq and
15954 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15955 * to the HBA.
15956 *
15957 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15958 * struct is used to get the entry count that is necessary to determine the
15959 * number of pages to use for this queue. The @cq is used to indicate which
15960 * completion queue to bind received buffers that are posted to these queues to.
15961 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15962 * receive queue pair. This function is asynchronous and will wait for the
15963 * mailbox command to finish before continuing.
15964 *
15965 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015966 * memory this function will return -ENOMEM. If the queue create mailbox command
15967 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015968 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015969int
James Smart4f774512009-05-22 14:52:35 -040015970lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15971 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15972{
15973 struct lpfc_mbx_rq_create *rq_create;
15974 struct lpfc_dmabuf *dmabuf;
15975 LPFC_MBOXQ_t *mbox;
15976 int rc, length, status = 0;
15977 uint32_t shdr_status, shdr_add_status;
15978 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015979 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015980 void __iomem *bar_memmap_p;
15981 uint32_t db_offset;
15982 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015983
James Smart2e90f4b2011-12-13 13:22:37 -050015984 /* sanity check on queue memory */
15985 if (!hrq || !drq || !cq)
15986 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015987 if (!phba->sli4_hba.pc_sli4_params.supported)
15988 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015989
15990 if (hrq->entry_count != drq->entry_count)
15991 return -EINVAL;
15992 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15993 if (!mbox)
15994 return -ENOMEM;
15995 length = (sizeof(struct lpfc_mbx_rq_create) -
15996 sizeof(struct lpfc_sli4_cfg_mhdr));
15997 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15998 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15999 length, LPFC_SLI4_MBX_EMBED);
16000 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050016001 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
16002 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16003 phba->sli4_hba.pc_sli4_params.rqv);
16004 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
16005 bf_set(lpfc_rq_context_rqe_count_1,
16006 &rq_create->u.request.context,
16007 hrq->entry_count);
16008 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040016009 bf_set(lpfc_rq_context_rqe_size,
16010 &rq_create->u.request.context,
16011 LPFC_RQE_SIZE_8);
16012 bf_set(lpfc_rq_context_page_size,
16013 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080016014 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050016015 } else {
16016 switch (hrq->entry_count) {
16017 default:
16018 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16019 "2535 Unsupported RQ count. (%d)\n",
16020 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040016021 if (hrq->entry_count < 512) {
16022 status = -EINVAL;
16023 goto out;
16024 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060016025 /* fall through - otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050016026 case 512:
16027 bf_set(lpfc_rq_context_rqe_count,
16028 &rq_create->u.request.context,
16029 LPFC_RQ_RING_SIZE_512);
16030 break;
16031 case 1024:
16032 bf_set(lpfc_rq_context_rqe_count,
16033 &rq_create->u.request.context,
16034 LPFC_RQ_RING_SIZE_1024);
16035 break;
16036 case 2048:
16037 bf_set(lpfc_rq_context_rqe_count,
16038 &rq_create->u.request.context,
16039 LPFC_RQ_RING_SIZE_2048);
16040 break;
16041 case 4096:
16042 bf_set(lpfc_rq_context_rqe_count,
16043 &rq_create->u.request.context,
16044 LPFC_RQ_RING_SIZE_4096);
16045 break;
16046 }
16047 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
16048 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040016049 }
16050 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
16051 cq->queue_id);
16052 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
16053 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040016054 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040016055 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040016056 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16057 putPaddrLow(dmabuf->phys);
16058 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16059 putPaddrHigh(dmabuf->phys);
16060 }
James Smart962bc512013-01-03 15:44:00 -050016061 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16062 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
16063
James Smart4f774512009-05-22 14:52:35 -040016064 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16065 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040016066 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16067 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16068 if (shdr_status || shdr_add_status || rc) {
16069 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16070 "2504 RQ_CREATE mailbox failed with "
16071 "status x%x add_status x%x, mbx status x%x\n",
16072 shdr_status, shdr_add_status, rc);
16073 status = -ENXIO;
16074 goto out;
16075 }
16076 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16077 if (hrq->queue_id == 0xFFFF) {
16078 status = -ENXIO;
16079 goto out;
16080 }
James Smart962bc512013-01-03 15:44:00 -050016081
16082 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16083 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
16084 &rq_create->u.response);
16085 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
16086 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
16087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16088 "3262 RQ [%d] doorbell format not "
16089 "supported: x%x\n", hrq->queue_id,
16090 hrq->db_format);
16091 status = -EINVAL;
16092 goto out;
16093 }
16094
16095 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
16096 &rq_create->u.response);
16097 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
16098 if (!bar_memmap_p) {
16099 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16100 "3269 RQ[%d] failed to memmap pci "
16101 "barset:x%x\n", hrq->queue_id,
16102 pci_barset);
16103 status = -ENOMEM;
16104 goto out;
16105 }
16106
16107 db_offset = rq_create->u.response.doorbell_offset;
16108 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
16109 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
16110 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16111 "3270 RQ[%d] doorbell offset not "
16112 "supported: x%x\n", hrq->queue_id,
16113 db_offset);
16114 status = -EINVAL;
16115 goto out;
16116 }
16117 hrq->db_regaddr = bar_memmap_p + db_offset;
16118 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040016119 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
16120 "format:x%x\n", hrq->queue_id, pci_barset,
16121 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050016122 } else {
16123 hrq->db_format = LPFC_DB_RING_FORMAT;
16124 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16125 }
James Smart4f774512009-05-22 14:52:35 -040016126 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050016127 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040016128 hrq->subtype = subtype;
16129 hrq->host_index = 0;
16130 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016131 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040016132
16133 /* now create the data queue */
16134 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16135 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
16136 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050016137 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16138 phba->sli4_hba.pc_sli4_params.rqv);
16139 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
16140 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040016141 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070016142 if (subtype == LPFC_NVMET)
16143 rq_create->u.request.context.buffer_size =
16144 LPFC_NVMET_DATA_BUF_SIZE;
16145 else
16146 rq_create->u.request.context.buffer_size =
16147 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040016148 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
16149 LPFC_RQE_SIZE_8);
16150 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
16151 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050016152 } else {
16153 switch (drq->entry_count) {
16154 default:
16155 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16156 "2536 Unsupported RQ count. (%d)\n",
16157 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040016158 if (drq->entry_count < 512) {
16159 status = -EINVAL;
16160 goto out;
16161 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060016162 /* fall through - otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050016163 case 512:
16164 bf_set(lpfc_rq_context_rqe_count,
16165 &rq_create->u.request.context,
16166 LPFC_RQ_RING_SIZE_512);
16167 break;
16168 case 1024:
16169 bf_set(lpfc_rq_context_rqe_count,
16170 &rq_create->u.request.context,
16171 LPFC_RQ_RING_SIZE_1024);
16172 break;
16173 case 2048:
16174 bf_set(lpfc_rq_context_rqe_count,
16175 &rq_create->u.request.context,
16176 LPFC_RQ_RING_SIZE_2048);
16177 break;
16178 case 4096:
16179 bf_set(lpfc_rq_context_rqe_count,
16180 &rq_create->u.request.context,
16181 LPFC_RQ_RING_SIZE_4096);
16182 break;
16183 }
James Smart3c603be2017-05-15 15:20:44 -070016184 if (subtype == LPFC_NVMET)
16185 bf_set(lpfc_rq_context_buf_size,
16186 &rq_create->u.request.context,
16187 LPFC_NVMET_DATA_BUF_SIZE);
16188 else
16189 bf_set(lpfc_rq_context_buf_size,
16190 &rq_create->u.request.context,
16191 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040016192 }
16193 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
16194 cq->queue_id);
16195 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
16196 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040016197 list_for_each_entry(dmabuf, &drq->page_list, list) {
16198 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16199 putPaddrLow(dmabuf->phys);
16200 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16201 putPaddrHigh(dmabuf->phys);
16202 }
James Smart962bc512013-01-03 15:44:00 -050016203 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16204 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040016205 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16206 /* The IOCTL status is embedded in the mailbox subheader. */
16207 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
16208 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16209 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16210 if (shdr_status || shdr_add_status || rc) {
16211 status = -ENXIO;
16212 goto out;
16213 }
16214 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16215 if (drq->queue_id == 0xFFFF) {
16216 status = -ENXIO;
16217 goto out;
16218 }
16219 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050016220 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040016221 drq->subtype = subtype;
16222 drq->host_index = 0;
16223 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016224 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040016225
16226 /* link the header and data RQs onto the parent cq child list */
16227 list_add_tail(&hrq->list, &cq->child_list);
16228 list_add_tail(&drq->list, &cq->child_list);
16229
16230out:
James Smart8fa38512009-07-19 10:01:03 -040016231 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016232 return status;
16233}
16234
16235/**
James Smart2d7dbc42017-02-12 13:52:35 -080016236 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
16237 * @phba: HBA structure that indicates port to create a queue on.
16238 * @hrqp: The queue structure array to use to create the header receive queues.
16239 * @drqp: The queue structure array to use to create the data receive queues.
16240 * @cqp: The completion queue array to bind these receive queues to.
16241 *
16242 * This function creates a receive buffer queue pair , as detailed in @hrq and
16243 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
16244 * to the HBA.
16245 *
16246 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
16247 * struct is used to get the entry count that is necessary to determine the
16248 * number of pages to use for this queue. The @cq is used to indicate which
16249 * completion queue to bind received buffers that are posted to these queues to.
16250 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
16251 * receive queue pair. This function is asynchronous and will wait for the
16252 * mailbox command to finish before continuing.
16253 *
16254 * On success this function will return a zero. If unable to allocate enough
16255 * memory this function will return -ENOMEM. If the queue create mailbox command
16256 * fails this function will return -ENXIO.
16257 **/
16258int
16259lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
16260 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
16261 uint32_t subtype)
16262{
16263 struct lpfc_queue *hrq, *drq, *cq;
16264 struct lpfc_mbx_rq_create_v2 *rq_create;
16265 struct lpfc_dmabuf *dmabuf;
16266 LPFC_MBOXQ_t *mbox;
16267 int rc, length, alloclen, status = 0;
16268 int cnt, idx, numrq, page_idx = 0;
16269 uint32_t shdr_status, shdr_add_status;
16270 union lpfc_sli4_cfg_shdr *shdr;
16271 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16272
16273 numrq = phba->cfg_nvmet_mrq;
16274 /* sanity check on array memory */
16275 if (!hrqp || !drqp || !cqp || !numrq)
16276 return -ENODEV;
16277 if (!phba->sli4_hba.pc_sli4_params.supported)
16278 hw_page_size = SLI4_PAGE_SIZE;
16279
16280 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16281 if (!mbox)
16282 return -ENOMEM;
16283
16284 length = sizeof(struct lpfc_mbx_rq_create_v2);
16285 length += ((2 * numrq * hrqp[0]->page_count) *
16286 sizeof(struct dma_address));
16287
16288 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16289 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
16290 LPFC_SLI4_MBX_NEMBED);
16291 if (alloclen < length) {
16292 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16293 "3099 Allocated DMA memory size (%d) is "
16294 "less than the requested DMA memory size "
16295 "(%d)\n", alloclen, length);
16296 status = -ENOMEM;
16297 goto out;
16298 }
16299
16300
16301
16302 rq_create = mbox->sge_array->addr[0];
16303 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
16304
16305 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
16306 cnt = 0;
16307
16308 for (idx = 0; idx < numrq; idx++) {
16309 hrq = hrqp[idx];
16310 drq = drqp[idx];
16311 cq = cqp[idx];
16312
James Smart2d7dbc42017-02-12 13:52:35 -080016313 /* sanity check on queue memory */
16314 if (!hrq || !drq || !cq) {
16315 status = -ENODEV;
16316 goto out;
16317 }
16318
James Smart7aabe842017-03-04 09:30:22 -080016319 if (hrq->entry_count != drq->entry_count) {
16320 status = -EINVAL;
16321 goto out;
16322 }
16323
James Smart2d7dbc42017-02-12 13:52:35 -080016324 if (idx == 0) {
16325 bf_set(lpfc_mbx_rq_create_num_pages,
16326 &rq_create->u.request,
16327 hrq->page_count);
16328 bf_set(lpfc_mbx_rq_create_rq_cnt,
16329 &rq_create->u.request, (numrq * 2));
16330 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
16331 1);
16332 bf_set(lpfc_rq_context_base_cq,
16333 &rq_create->u.request.context,
16334 cq->queue_id);
16335 bf_set(lpfc_rq_context_data_size,
16336 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070016337 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080016338 bf_set(lpfc_rq_context_hdr_size,
16339 &rq_create->u.request.context,
16340 LPFC_HDR_BUF_SIZE);
16341 bf_set(lpfc_rq_context_rqe_count_1,
16342 &rq_create->u.request.context,
16343 hrq->entry_count);
16344 bf_set(lpfc_rq_context_rqe_size,
16345 &rq_create->u.request.context,
16346 LPFC_RQE_SIZE_8);
16347 bf_set(lpfc_rq_context_page_size,
16348 &rq_create->u.request.context,
16349 (PAGE_SIZE/SLI4_PAGE_SIZE));
16350 }
16351 rc = 0;
16352 list_for_each_entry(dmabuf, &hrq->page_list, list) {
16353 memset(dmabuf->virt, 0, hw_page_size);
16354 cnt = page_idx + dmabuf->buffer_tag;
16355 rq_create->u.request.page[cnt].addr_lo =
16356 putPaddrLow(dmabuf->phys);
16357 rq_create->u.request.page[cnt].addr_hi =
16358 putPaddrHigh(dmabuf->phys);
16359 rc++;
16360 }
16361 page_idx += rc;
16362
16363 rc = 0;
16364 list_for_each_entry(dmabuf, &drq->page_list, list) {
16365 memset(dmabuf->virt, 0, hw_page_size);
16366 cnt = page_idx + dmabuf->buffer_tag;
16367 rq_create->u.request.page[cnt].addr_lo =
16368 putPaddrLow(dmabuf->phys);
16369 rq_create->u.request.page[cnt].addr_hi =
16370 putPaddrHigh(dmabuf->phys);
16371 rc++;
16372 }
16373 page_idx += rc;
16374
16375 hrq->db_format = LPFC_DB_RING_FORMAT;
16376 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16377 hrq->type = LPFC_HRQ;
16378 hrq->assoc_qid = cq->queue_id;
16379 hrq->subtype = subtype;
16380 hrq->host_index = 0;
16381 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016382 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016383
16384 drq->db_format = LPFC_DB_RING_FORMAT;
16385 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16386 drq->type = LPFC_DRQ;
16387 drq->assoc_qid = cq->queue_id;
16388 drq->subtype = subtype;
16389 drq->host_index = 0;
16390 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016391 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016392
16393 list_add_tail(&hrq->list, &cq->child_list);
16394 list_add_tail(&drq->list, &cq->child_list);
16395 }
16396
16397 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16398 /* The IOCTL status is embedded in the mailbox subheader. */
16399 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16400 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16401 if (shdr_status || shdr_add_status || rc) {
16402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16403 "3120 RQ_CREATE mailbox failed with "
16404 "status x%x add_status x%x, mbx status x%x\n",
16405 shdr_status, shdr_add_status, rc);
16406 status = -ENXIO;
16407 goto out;
16408 }
16409 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16410 if (rc == 0xFFFF) {
16411 status = -ENXIO;
16412 goto out;
16413 }
16414
16415 /* Initialize all RQs with associated queue id */
16416 for (idx = 0; idx < numrq; idx++) {
16417 hrq = hrqp[idx];
16418 hrq->queue_id = rc + (2 * idx);
16419 drq = drqp[idx];
16420 drq->queue_id = rc + (2 * idx) + 1;
16421 }
16422
16423out:
16424 lpfc_sli4_mbox_cmd_free(phba, mbox);
16425 return status;
16426}
16427
16428/**
James Smart4f774512009-05-22 14:52:35 -040016429 * lpfc_eq_destroy - Destroy an event Queue on the HBA
16430 * @eq: The queue structure associated with the queue to destroy.
16431 *
16432 * This function destroys a queue, as detailed in @eq by sending an mailbox
16433 * command, specific to the type of queue, to the HBA.
16434 *
16435 * The @eq struct is used to get the queue ID of the queue to destroy.
16436 *
16437 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016438 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016439 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016440int
James Smart4f774512009-05-22 14:52:35 -040016441lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16442{
16443 LPFC_MBOXQ_t *mbox;
16444 int rc, length, status = 0;
16445 uint32_t shdr_status, shdr_add_status;
16446 union lpfc_sli4_cfg_shdr *shdr;
16447
James Smart2e90f4b2011-12-13 13:22:37 -050016448 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016449 if (!eq)
16450 return -ENODEV;
James Smart32517fc2019-01-28 11:14:33 -080016451
James Smart4f774512009-05-22 14:52:35 -040016452 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16453 if (!mbox)
16454 return -ENOMEM;
16455 length = (sizeof(struct lpfc_mbx_eq_destroy) -
16456 sizeof(struct lpfc_sli4_cfg_mhdr));
16457 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16458 LPFC_MBOX_OPCODE_EQ_DESTROY,
16459 length, LPFC_SLI4_MBX_EMBED);
16460 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16461 eq->queue_id);
16462 mbox->vport = eq->phba->pport;
16463 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16464
16465 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16466 /* The IOCTL status is embedded in the mailbox subheader. */
16467 shdr = (union lpfc_sli4_cfg_shdr *)
16468 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16469 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16470 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16471 if (shdr_status || shdr_add_status || rc) {
16472 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16473 "2505 EQ_DESTROY mailbox failed with "
16474 "status x%x add_status x%x, mbx status x%x\n",
16475 shdr_status, shdr_add_status, rc);
16476 status = -ENXIO;
16477 }
16478
16479 /* Remove eq from any list */
16480 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040016481 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016482 return status;
16483}
16484
16485/**
16486 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16487 * @cq: The queue structure associated with the queue to destroy.
16488 *
16489 * This function destroys a queue, as detailed in @cq by sending an mailbox
16490 * command, specific to the type of queue, to the HBA.
16491 *
16492 * The @cq struct is used to get the queue ID of the queue to destroy.
16493 *
16494 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016495 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016496 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016497int
James Smart4f774512009-05-22 14:52:35 -040016498lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16499{
16500 LPFC_MBOXQ_t *mbox;
16501 int rc, length, status = 0;
16502 uint32_t shdr_status, shdr_add_status;
16503 union lpfc_sli4_cfg_shdr *shdr;
16504
James Smart2e90f4b2011-12-13 13:22:37 -050016505 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016506 if (!cq)
16507 return -ENODEV;
16508 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16509 if (!mbox)
16510 return -ENOMEM;
16511 length = (sizeof(struct lpfc_mbx_cq_destroy) -
16512 sizeof(struct lpfc_sli4_cfg_mhdr));
16513 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16514 LPFC_MBOX_OPCODE_CQ_DESTROY,
16515 length, LPFC_SLI4_MBX_EMBED);
16516 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16517 cq->queue_id);
16518 mbox->vport = cq->phba->pport;
16519 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16520 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16521 /* The IOCTL status is embedded in the mailbox subheader. */
16522 shdr = (union lpfc_sli4_cfg_shdr *)
16523 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16524 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16525 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16526 if (shdr_status || shdr_add_status || rc) {
16527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16528 "2506 CQ_DESTROY mailbox failed with "
16529 "status x%x add_status x%x, mbx status x%x\n",
16530 shdr_status, shdr_add_status, rc);
16531 status = -ENXIO;
16532 }
16533 /* Remove cq from any list */
16534 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040016535 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016536 return status;
16537}
16538
16539/**
James Smart04c68492009-05-22 14:52:52 -040016540 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16541 * @qm: The queue structure associated with the queue to destroy.
16542 *
16543 * This function destroys a queue, as detailed in @mq by sending an mailbox
16544 * command, specific to the type of queue, to the HBA.
16545 *
16546 * The @mq struct is used to get the queue ID of the queue to destroy.
16547 *
16548 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016549 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040016550 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016551int
James Smart04c68492009-05-22 14:52:52 -040016552lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16553{
16554 LPFC_MBOXQ_t *mbox;
16555 int rc, length, status = 0;
16556 uint32_t shdr_status, shdr_add_status;
16557 union lpfc_sli4_cfg_shdr *shdr;
16558
James Smart2e90f4b2011-12-13 13:22:37 -050016559 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040016560 if (!mq)
16561 return -ENODEV;
16562 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16563 if (!mbox)
16564 return -ENOMEM;
16565 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16566 sizeof(struct lpfc_sli4_cfg_mhdr));
16567 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16568 LPFC_MBOX_OPCODE_MQ_DESTROY,
16569 length, LPFC_SLI4_MBX_EMBED);
16570 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16571 mq->queue_id);
16572 mbox->vport = mq->phba->pport;
16573 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16574 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16575 /* The IOCTL status is embedded in the mailbox subheader. */
16576 shdr = (union lpfc_sli4_cfg_shdr *)
16577 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16578 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16579 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16580 if (shdr_status || shdr_add_status || rc) {
16581 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16582 "2507 MQ_DESTROY mailbox failed with "
16583 "status x%x add_status x%x, mbx status x%x\n",
16584 shdr_status, shdr_add_status, rc);
16585 status = -ENXIO;
16586 }
16587 /* Remove mq from any list */
16588 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016589 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016590 return status;
16591}
16592
16593/**
James Smart4f774512009-05-22 14:52:35 -040016594 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16595 * @wq: The queue structure associated with the queue to destroy.
16596 *
16597 * This function destroys a queue, as detailed in @wq by sending an mailbox
16598 * command, specific to the type of queue, to the HBA.
16599 *
16600 * The @wq struct is used to get the queue ID of the queue to destroy.
16601 *
16602 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016603 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016604 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016605int
James Smart4f774512009-05-22 14:52:35 -040016606lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16607{
16608 LPFC_MBOXQ_t *mbox;
16609 int rc, length, status = 0;
16610 uint32_t shdr_status, shdr_add_status;
16611 union lpfc_sli4_cfg_shdr *shdr;
16612
James Smart2e90f4b2011-12-13 13:22:37 -050016613 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016614 if (!wq)
16615 return -ENODEV;
16616 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16617 if (!mbox)
16618 return -ENOMEM;
16619 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16620 sizeof(struct lpfc_sli4_cfg_mhdr));
16621 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16622 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16623 length, LPFC_SLI4_MBX_EMBED);
16624 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16625 wq->queue_id);
16626 mbox->vport = wq->phba->pport;
16627 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16628 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16629 shdr = (union lpfc_sli4_cfg_shdr *)
16630 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16631 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16632 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16633 if (shdr_status || shdr_add_status || rc) {
16634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16635 "2508 WQ_DESTROY mailbox failed with "
16636 "status x%x add_status x%x, mbx status x%x\n",
16637 shdr_status, shdr_add_status, rc);
16638 status = -ENXIO;
16639 }
16640 /* Remove wq from any list */
16641 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016642 kfree(wq->pring);
16643 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016644 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016645 return status;
16646}
16647
16648/**
16649 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16650 * @rq: The queue structure associated with the queue to destroy.
16651 *
16652 * This function destroys a queue, as detailed in @rq by sending an mailbox
16653 * command, specific to the type of queue, to the HBA.
16654 *
16655 * The @rq struct is used to get the queue ID of the queue to destroy.
16656 *
16657 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016658 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016659 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016660int
James Smart4f774512009-05-22 14:52:35 -040016661lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16662 struct lpfc_queue *drq)
16663{
16664 LPFC_MBOXQ_t *mbox;
16665 int rc, length, status = 0;
16666 uint32_t shdr_status, shdr_add_status;
16667 union lpfc_sli4_cfg_shdr *shdr;
16668
James Smart2e90f4b2011-12-13 13:22:37 -050016669 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016670 if (!hrq || !drq)
16671 return -ENODEV;
16672 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16673 if (!mbox)
16674 return -ENOMEM;
16675 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016676 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016677 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16678 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16679 length, LPFC_SLI4_MBX_EMBED);
16680 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16681 hrq->queue_id);
16682 mbox->vport = hrq->phba->pport;
16683 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16684 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16685 /* The IOCTL status is embedded in the mailbox subheader. */
16686 shdr = (union lpfc_sli4_cfg_shdr *)
16687 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16688 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16689 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16690 if (shdr_status || shdr_add_status || rc) {
16691 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16692 "2509 RQ_DESTROY mailbox failed with "
16693 "status x%x add_status x%x, mbx status x%x\n",
16694 shdr_status, shdr_add_status, rc);
16695 if (rc != MBX_TIMEOUT)
16696 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16697 return -ENXIO;
16698 }
16699 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16700 drq->queue_id);
16701 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16702 shdr = (union lpfc_sli4_cfg_shdr *)
16703 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16704 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16705 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16706 if (shdr_status || shdr_add_status || rc) {
16707 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16708 "2510 RQ_DESTROY mailbox failed with "
16709 "status x%x add_status x%x, mbx status x%x\n",
16710 shdr_status, shdr_add_status, rc);
16711 status = -ENXIO;
16712 }
16713 list_del_init(&hrq->list);
16714 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016715 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016716 return status;
16717}
16718
16719/**
16720 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16721 * @phba: The virtual port for which this call being executed.
16722 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16723 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16724 * @xritag: the xritag that ties this io to the SGL pages.
16725 *
16726 * This routine will post the sgl pages for the IO that has the xritag
16727 * that is in the iocbq structure. The xritag is assigned during iocbq
16728 * creation and persists for as long as the driver is loaded.
16729 * if the caller has fewer than 256 scatter gather segments to map then
16730 * pdma_phys_addr1 should be 0.
16731 * If the caller needs to map more than 256 scatter gather segment then
16732 * pdma_phys_addr1 should be a valid physical address.
16733 * physical address for SGLs must be 64 byte aligned.
16734 * If you are going to map 2 SGL's then the first one must have 256 entries
16735 * the second sgl can have between 1 and 256 entries.
16736 *
16737 * Return codes:
16738 * 0 - Success
16739 * -ENXIO, -ENOMEM - Failure
16740 **/
16741int
16742lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16743 dma_addr_t pdma_phys_addr0,
16744 dma_addr_t pdma_phys_addr1,
16745 uint16_t xritag)
16746{
16747 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16748 LPFC_MBOXQ_t *mbox;
16749 int rc;
16750 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016751 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016752 union lpfc_sli4_cfg_shdr *shdr;
16753
16754 if (xritag == NO_XRI) {
16755 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16756 "0364 Invalid param:\n");
16757 return -EINVAL;
16758 }
16759
16760 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16761 if (!mbox)
16762 return -ENOMEM;
16763
16764 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16765 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16766 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016767 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016768
16769 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16770 &mbox->u.mqe.un.post_sgl_pages;
16771 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16772 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16773
16774 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16775 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16776 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16777 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16778
16779 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16780 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16781 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16782 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16783 if (!phba->sli4_hba.intr_enable)
16784 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016785 else {
James Smarta183a152011-10-10 21:32:43 -040016786 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016787 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16788 }
James Smart4f774512009-05-22 14:52:35 -040016789 /* The IOCTL status is embedded in the mailbox subheader. */
16790 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16791 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16792 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16793 if (rc != MBX_TIMEOUT)
16794 mempool_free(mbox, phba->mbox_mem_pool);
16795 if (shdr_status || shdr_add_status || rc) {
16796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16797 "2511 POST_SGL mailbox failed with "
16798 "status x%x add_status x%x, mbx status x%x\n",
16799 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016800 }
16801 return 0;
16802}
James Smart4f774512009-05-22 14:52:35 -040016803
16804/**
James Smart88a2cfb2011-07-22 18:36:33 -040016805 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016806 * @phba: pointer to lpfc hba data structure.
16807 *
16808 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016809 * HBA consistent with the SLI-4 interface spec. This routine
16810 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16811 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016812 *
James Smart88a2cfb2011-07-22 18:36:33 -040016813 * Returns
16814 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16815 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16816 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016817static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016818lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16819{
16820 unsigned long xri;
16821
16822 /*
16823 * Fetch the next logical xri. Because this index is logical,
16824 * the driver starts at 0 each time.
16825 */
16826 spin_lock_irq(&phba->hbalock);
16827 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16828 phba->sli4_hba.max_cfg_param.max_xri, 0);
16829 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16830 spin_unlock_irq(&phba->hbalock);
16831 return NO_XRI;
16832 } else {
16833 set_bit(xri, phba->sli4_hba.xri_bmask);
16834 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016835 }
James Smart6d368e52011-05-24 11:44:12 -040016836 spin_unlock_irq(&phba->hbalock);
16837 return xri;
16838}
16839
16840/**
16841 * lpfc_sli4_free_xri - Release an xri for reuse.
16842 * @phba: pointer to lpfc hba data structure.
16843 *
16844 * This routine is invoked to release an xri to the pool of
16845 * available rpis maintained by the driver.
16846 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016847static void
James Smart6d368e52011-05-24 11:44:12 -040016848__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16849{
16850 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016851 phba->sli4_hba.max_cfg_param.xri_used--;
16852 }
16853}
16854
16855/**
16856 * lpfc_sli4_free_xri - Release an xri for reuse.
16857 * @phba: pointer to lpfc hba data structure.
16858 *
16859 * This routine is invoked to release an xri to the pool of
16860 * available rpis maintained by the driver.
16861 **/
16862void
16863lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16864{
16865 spin_lock_irq(&phba->hbalock);
16866 __lpfc_sli4_free_xri(phba, xri);
16867 spin_unlock_irq(&phba->hbalock);
16868}
16869
16870/**
James Smart4f774512009-05-22 14:52:35 -040016871 * lpfc_sli4_next_xritag - Get an xritag for the io
16872 * @phba: Pointer to HBA context object.
16873 *
16874 * This function gets an xritag for the iocb. If there is no unused xritag
16875 * it will return 0xffff.
16876 * The function returns the allocated xritag if successful, else returns zero.
16877 * Zero is not a valid xritag.
16878 * The caller is not required to hold any lock.
16879 **/
16880uint16_t
16881lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16882{
James Smart6d368e52011-05-24 11:44:12 -040016883 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016884
James Smart6d368e52011-05-24 11:44:12 -040016885 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016886 if (xri_index == NO_XRI)
16887 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16888 "2004 Failed to allocate XRI.last XRITAG is %d"
16889 " Max XRI is %d, Used XRI is %d\n",
16890 xri_index,
16891 phba->sli4_hba.max_cfg_param.max_xri,
16892 phba->sli4_hba.max_cfg_param.xri_used);
16893 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016894}
16895
16896/**
James Smart895427b2017-02-12 13:52:30 -080016897 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016898 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016899 * @post_sgl_list: pointer to els sgl entry list.
16900 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016901 *
16902 * This routine is invoked to post a block of driver's sgl pages to the
16903 * HBA using non-embedded mailbox command. No Lock is held. This routine
16904 * is only called when the driver is loading and after all IO has been
16905 * stopped.
16906 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016907static int
James Smart895427b2017-02-12 13:52:30 -080016908lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016909 struct list_head *post_sgl_list,
16910 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016911{
James Smart8a9d2e82012-05-09 21:16:12 -040016912 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016913 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16914 struct sgl_page_pairs *sgl_pg_pairs;
16915 void *viraddr;
16916 LPFC_MBOXQ_t *mbox;
16917 uint32_t reqlen, alloclen, pg_pairs;
16918 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016919 uint16_t xritag_start = 0;
16920 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016921 uint32_t shdr_status, shdr_add_status;
16922 union lpfc_sli4_cfg_shdr *shdr;
16923
James Smart895427b2017-02-12 13:52:30 -080016924 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016925 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016926 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016928 "2559 Block sgl registration required DMA "
16929 "size (%d) great than a page\n", reqlen);
16930 return -ENOMEM;
16931 }
James Smart895427b2017-02-12 13:52:30 -080016932
James Smart4f774512009-05-22 14:52:35 -040016933 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016934 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016935 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016936
16937 /* Allocate DMA memory and set up the non-embedded mailbox command */
16938 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16939 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16940 LPFC_SLI4_MBX_NEMBED);
16941
16942 if (alloclen < reqlen) {
16943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16944 "0285 Allocated DMA memory size (%d) is "
16945 "less than the requested DMA memory "
16946 "size (%d)\n", alloclen, reqlen);
16947 lpfc_sli4_mbox_cmd_free(phba, mbox);
16948 return -ENOMEM;
16949 }
James Smart4f774512009-05-22 14:52:35 -040016950 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016951 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016952 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16953 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16954
James Smart8a9d2e82012-05-09 21:16:12 -040016955 pg_pairs = 0;
16956 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016957 /* Set up the sge entry */
16958 sgl_pg_pairs->sgl_pg0_addr_lo =
16959 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16960 sgl_pg_pairs->sgl_pg0_addr_hi =
16961 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16962 sgl_pg_pairs->sgl_pg1_addr_lo =
16963 cpu_to_le32(putPaddrLow(0));
16964 sgl_pg_pairs->sgl_pg1_addr_hi =
16965 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016966
James Smart4f774512009-05-22 14:52:35 -040016967 /* Keep the first xritag on the list */
16968 if (pg_pairs == 0)
16969 xritag_start = sglq_entry->sli4_xritag;
16970 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016971 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016972 }
James Smart6d368e52011-05-24 11:44:12 -040016973
16974 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016975 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016976 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016977 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016978
James Smart4f774512009-05-22 14:52:35 -040016979 if (!phba->sli4_hba.intr_enable)
16980 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16981 else {
James Smarta183a152011-10-10 21:32:43 -040016982 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016983 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16984 }
16985 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16986 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16987 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16988 if (rc != MBX_TIMEOUT)
16989 lpfc_sli4_mbox_cmd_free(phba, mbox);
16990 if (shdr_status || shdr_add_status || rc) {
16991 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16992 "2513 POST_SGL_BLOCK mailbox command failed "
16993 "status x%x add_status x%x mbx status x%x\n",
16994 shdr_status, shdr_add_status, rc);
16995 rc = -ENXIO;
16996 }
16997 return rc;
16998}
16999
17000/**
James Smart5e5b5112019-01-28 11:14:22 -080017001 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
James Smart4f774512009-05-22 14:52:35 -040017002 * @phba: pointer to lpfc hba data structure.
James Smart0794d602019-01-28 11:14:19 -080017003 * @nblist: pointer to nvme buffer list.
James Smart4f774512009-05-22 14:52:35 -040017004 * @count: number of scsi buffers on the list.
17005 *
17006 * This routine is invoked to post a block of @count scsi sgl pages from a
James Smart0794d602019-01-28 11:14:19 -080017007 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
James Smart4f774512009-05-22 14:52:35 -040017008 * No Lock is held.
17009 *
17010 **/
James Smart0794d602019-01-28 11:14:19 -080017011static int
James Smart5e5b5112019-01-28 11:14:22 -080017012lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
17013 int count)
James Smart4f774512009-05-22 14:52:35 -040017014{
James Smartc4908502019-01-28 11:14:28 -080017015 struct lpfc_io_buf *lpfc_ncmd;
James Smart4f774512009-05-22 14:52:35 -040017016 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
17017 struct sgl_page_pairs *sgl_pg_pairs;
17018 void *viraddr;
17019 LPFC_MBOXQ_t *mbox;
17020 uint32_t reqlen, alloclen, pg_pairs;
17021 uint32_t mbox_tmo;
17022 uint16_t xritag_start = 0;
17023 int rc = 0;
17024 uint32_t shdr_status, shdr_add_status;
17025 dma_addr_t pdma_phys_bpl1;
17026 union lpfc_sli4_cfg_shdr *shdr;
17027
17028 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040017029 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040017030 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040017031 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040017032 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080017033 "6118 Block sgl registration required DMA "
James Smart4f774512009-05-22 14:52:35 -040017034 "size (%d) great than a page\n", reqlen);
17035 return -ENOMEM;
17036 }
17037 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17038 if (!mbox) {
17039 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080017040 "6119 Failed to allocate mbox cmd memory\n");
James Smart4f774512009-05-22 14:52:35 -040017041 return -ENOMEM;
17042 }
17043
17044 /* Allocate DMA memory and set up the non-embedded mailbox command */
17045 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
James Smart0794d602019-01-28 11:14:19 -080017046 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17047 reqlen, LPFC_SLI4_MBX_NEMBED);
James Smart4f774512009-05-22 14:52:35 -040017048
17049 if (alloclen < reqlen) {
17050 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080017051 "6120 Allocated DMA memory size (%d) is "
James Smart4f774512009-05-22 14:52:35 -040017052 "less than the requested DMA memory "
17053 "size (%d)\n", alloclen, reqlen);
17054 lpfc_sli4_mbox_cmd_free(phba, mbox);
17055 return -ENOMEM;
17056 }
James Smart6d368e52011-05-24 11:44:12 -040017057
James Smart4f774512009-05-22 14:52:35 -040017058 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040017059 viraddr = mbox->sge_array->addr[0];
17060
17061 /* Set up the SGL pages in the non-embedded DMA pages */
17062 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
17063 sgl_pg_pairs = &sgl->sgl_pg_pairs;
17064
17065 pg_pairs = 0;
James Smart0794d602019-01-28 11:14:19 -080017066 list_for_each_entry(lpfc_ncmd, nblist, list) {
James Smart4f774512009-05-22 14:52:35 -040017067 /* Set up the sge entry */
17068 sgl_pg_pairs->sgl_pg0_addr_lo =
James Smart0794d602019-01-28 11:14:19 -080017069 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040017070 sgl_pg_pairs->sgl_pg0_addr_hi =
James Smart0794d602019-01-28 11:14:19 -080017071 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040017072 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
James Smart0794d602019-01-28 11:14:19 -080017073 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
17074 SGL_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040017075 else
17076 pdma_phys_bpl1 = 0;
17077 sgl_pg_pairs->sgl_pg1_addr_lo =
17078 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
17079 sgl_pg_pairs->sgl_pg1_addr_hi =
17080 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
17081 /* Keep the first xritag on the list */
17082 if (pg_pairs == 0)
James Smart0794d602019-01-28 11:14:19 -080017083 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040017084 sgl_pg_pairs++;
17085 pg_pairs++;
17086 }
17087 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
17088 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
17089 /* Perform endian conversion if necessary */
17090 sgl->word0 = cpu_to_le32(sgl->word0);
17091
James Smart0794d602019-01-28 11:14:19 -080017092 if (!phba->sli4_hba.intr_enable) {
James Smart4f774512009-05-22 14:52:35 -040017093 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart0794d602019-01-28 11:14:19 -080017094 } else {
James Smarta183a152011-10-10 21:32:43 -040017095 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040017096 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17097 }
James Smart0794d602019-01-28 11:14:19 -080017098 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040017099 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17100 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17101 if (rc != MBX_TIMEOUT)
17102 lpfc_sli4_mbox_cmd_free(phba, mbox);
17103 if (shdr_status || shdr_add_status || rc) {
17104 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -080017105 "6125 POST_SGL_BLOCK mailbox command failed "
James Smart4f774512009-05-22 14:52:35 -040017106 "status x%x add_status x%x mbx status x%x\n",
17107 shdr_status, shdr_add_status, rc);
17108 rc = -ENXIO;
17109 }
17110 return rc;
17111}
17112
17113/**
James Smart5e5b5112019-01-28 11:14:22 -080017114 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
James Smart0794d602019-01-28 11:14:19 -080017115 * @phba: pointer to lpfc hba data structure.
17116 * @post_nblist: pointer to the nvme buffer list.
17117 *
17118 * This routine walks a list of nvme buffers that was passed in. It attempts
17119 * to construct blocks of nvme buffer sgls which contains contiguous xris and
17120 * uses the non-embedded SGL block post mailbox commands to post to the port.
17121 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
17122 * embedded SGL post mailbox command for posting. The @post_nblist passed in
17123 * must be local list, thus no lock is needed when manipulate the list.
17124 *
17125 * Returns: 0 = failure, non-zero number of successfully posted buffers.
17126 **/
17127int
James Smart5e5b5112019-01-28 11:14:22 -080017128lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
17129 struct list_head *post_nblist, int sb_count)
James Smart0794d602019-01-28 11:14:19 -080017130{
James Smartc4908502019-01-28 11:14:28 -080017131 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart0794d602019-01-28 11:14:19 -080017132 int status, sgl_size;
17133 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
17134 dma_addr_t pdma_phys_sgl1;
17135 int last_xritag = NO_XRI;
17136 int cur_xritag;
James Smart0794d602019-01-28 11:14:19 -080017137 LIST_HEAD(prep_nblist);
17138 LIST_HEAD(blck_nblist);
17139 LIST_HEAD(nvme_nblist);
17140
17141 /* sanity check */
17142 if (sb_count <= 0)
17143 return -EINVAL;
17144
17145 sgl_size = phba->cfg_sg_dma_buf_size;
17146 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
17147 list_del_init(&lpfc_ncmd->list);
17148 block_cnt++;
17149 if ((last_xritag != NO_XRI) &&
17150 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
17151 /* a hole in xri block, form a sgl posting block */
17152 list_splice_init(&prep_nblist, &blck_nblist);
17153 post_cnt = block_cnt - 1;
17154 /* prepare list for next posting block */
17155 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
17156 block_cnt = 1;
17157 } else {
17158 /* prepare list for next posting block */
17159 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
17160 /* enough sgls for non-embed sgl mbox command */
17161 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
17162 list_splice_init(&prep_nblist, &blck_nblist);
17163 post_cnt = block_cnt;
17164 block_cnt = 0;
17165 }
17166 }
17167 num_posting++;
17168 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
17169
17170 /* end of repost sgl list condition for NVME buffers */
17171 if (num_posting == sb_count) {
17172 if (post_cnt == 0) {
17173 /* last sgl posting block */
17174 list_splice_init(&prep_nblist, &blck_nblist);
17175 post_cnt = block_cnt;
17176 } else if (block_cnt == 1) {
17177 /* last single sgl with non-contiguous xri */
17178 if (sgl_size > SGL_PAGE_SIZE)
17179 pdma_phys_sgl1 =
17180 lpfc_ncmd->dma_phys_sgl +
17181 SGL_PAGE_SIZE;
17182 else
17183 pdma_phys_sgl1 = 0;
17184 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
17185 status = lpfc_sli4_post_sgl(
17186 phba, lpfc_ncmd->dma_phys_sgl,
17187 pdma_phys_sgl1, cur_xritag);
17188 if (status) {
James Smartc4908502019-01-28 11:14:28 -080017189 /* Post error. Buffer unavailable. */
17190 lpfc_ncmd->flags |=
17191 LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080017192 } else {
James Smartc4908502019-01-28 11:14:28 -080017193 /* Post success. Bffer available. */
17194 lpfc_ncmd->flags &=
17195 ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080017196 lpfc_ncmd->status = IOSTAT_SUCCESS;
17197 num_posted++;
17198 }
17199 /* success, put on NVME buffer sgl list */
17200 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
17201 }
17202 }
17203
17204 /* continue until a nembed page worth of sgls */
17205 if (post_cnt == 0)
17206 continue;
17207
17208 /* post block of NVME buffer list sgls */
James Smart5e5b5112019-01-28 11:14:22 -080017209 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
17210 post_cnt);
James Smart0794d602019-01-28 11:14:19 -080017211
17212 /* don't reset xirtag due to hole in xri block */
17213 if (block_cnt == 0)
17214 last_xritag = NO_XRI;
17215
17216 /* reset NVME buffer post count for next round of posting */
17217 post_cnt = 0;
17218
17219 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
17220 while (!list_empty(&blck_nblist)) {
17221 list_remove_head(&blck_nblist, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -080017222 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -080017223 if (status) {
James Smartc4908502019-01-28 11:14:28 -080017224 /* Post error. Mark buffer unavailable. */
17225 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080017226 } else {
James Smartc4908502019-01-28 11:14:28 -080017227 /* Post success, Mark buffer available. */
17228 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080017229 lpfc_ncmd->status = IOSTAT_SUCCESS;
17230 num_posted++;
17231 }
17232 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
17233 }
17234 }
17235 /* Push NVME buffers with sgl posted to the available list */
James Smart5e5b5112019-01-28 11:14:22 -080017236 lpfc_io_buf_replenish(phba, &nvme_nblist);
17237
James Smart0794d602019-01-28 11:14:19 -080017238 return num_posted;
17239}
17240
17241/**
James Smart4f774512009-05-22 14:52:35 -040017242 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
17243 * @phba: pointer to lpfc_hba struct that the frame was received on
17244 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17245 *
17246 * This function checks the fields in the @fc_hdr to see if the FC frame is a
17247 * valid type of frame that the LPFC driver will handle. This function will
17248 * return a zero if the frame is a valid frame or a non zero value when the
17249 * frame does not pass the check.
17250 **/
17251static int
17252lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
17253{
Tomas Henzl474ffb72010-12-22 16:52:40 +010017254 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040017255 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050017256 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040017257
James Smarte62245d2019-08-14 16:57:08 -070017258#define FC_RCTL_MDS_DIAGS 0xF4
17259
James Smart4f774512009-05-22 14:52:35 -040017260 switch (fc_hdr->fh_r_ctl) {
17261 case FC_RCTL_DD_UNCAT: /* uncategorized information */
17262 case FC_RCTL_DD_SOL_DATA: /* solicited data */
17263 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
17264 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
17265 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
17266 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
17267 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
17268 case FC_RCTL_DD_CMD_STATUS: /* command status */
17269 case FC_RCTL_ELS_REQ: /* extended link services request */
17270 case FC_RCTL_ELS_REP: /* extended link services reply */
17271 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
17272 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
17273 case FC_RCTL_BA_NOP: /* basic link service NOP */
17274 case FC_RCTL_BA_ABTS: /* basic link service abort */
17275 case FC_RCTL_BA_RMC: /* remove connection */
17276 case FC_RCTL_BA_ACC: /* basic accept */
17277 case FC_RCTL_BA_RJT: /* basic reject */
17278 case FC_RCTL_BA_PRMT:
17279 case FC_RCTL_ACK_1: /* acknowledge_1 */
17280 case FC_RCTL_ACK_0: /* acknowledge_0 */
17281 case FC_RCTL_P_RJT: /* port reject */
17282 case FC_RCTL_F_RJT: /* fabric reject */
17283 case FC_RCTL_P_BSY: /* port busy */
17284 case FC_RCTL_F_BSY: /* fabric busy to data frame */
17285 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
17286 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070017287 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040017288 case FC_RCTL_END: /* end */
17289 break;
17290 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
17291 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17292 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
17293 return lpfc_fc_frame_check(phba, fc_hdr);
17294 default:
17295 goto drop;
17296 }
James Smartae9e28f2017-05-15 15:20:51 -070017297
James Smart4f774512009-05-22 14:52:35 -040017298 switch (fc_hdr->fh_type) {
17299 case FC_TYPE_BLS:
17300 case FC_TYPE_ELS:
17301 case FC_TYPE_FCP:
17302 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080017303 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040017304 break;
17305 case FC_TYPE_IP:
17306 case FC_TYPE_ILS:
17307 default:
17308 goto drop;
17309 }
James Smart546fc852011-03-11 16:06:29 -050017310
James Smart4f774512009-05-22 14:52:35 -040017311 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070017312 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040017313 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070017314 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
17315 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
17316 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
17317 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
17318 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040017319 return 0;
17320drop:
17321 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070017322 "2539 Dropped frame rctl:x%x type:x%x\n",
17323 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040017324 return 1;
17325}
17326
17327/**
17328 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
17329 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17330 *
17331 * This function processes the FC header to retrieve the VFI from the VF
17332 * header, if one exists. This function will return the VFI if one exists
17333 * or 0 if no VSAN Header exists.
17334 **/
17335static uint32_t
17336lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
17337{
17338 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17339
17340 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
17341 return 0;
17342 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
17343}
17344
17345/**
17346 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
17347 * @phba: Pointer to the HBA structure to search for the vport on
17348 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17349 * @fcfi: The FC Fabric ID that the frame came from
17350 *
17351 * This function searches the @phba for a vport that matches the content of the
17352 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
17353 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
17354 * returns the matching vport pointer or NULL if unable to match frame to a
17355 * vport.
17356 **/
17357static struct lpfc_vport *
17358lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080017359 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040017360{
17361 struct lpfc_vport **vports;
17362 struct lpfc_vport *vport = NULL;
17363 int i;
James Smart939723a2012-05-09 21:19:03 -040017364
James Smartbf086112011-08-21 21:48:13 -040017365 if (did == Fabric_DID)
17366 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040017367 if ((phba->pport->fc_flag & FC_PT2PT) &&
17368 !(phba->link_state == LPFC_HBA_READY))
17369 return phba->pport;
17370
James Smart4f774512009-05-22 14:52:35 -040017371 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080017372 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040017373 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17374 if (phba->fcf.fcfi == fcfi &&
17375 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
17376 vports[i]->fc_myDID == did) {
17377 vport = vports[i];
17378 break;
17379 }
17380 }
James Smart895427b2017-02-12 13:52:30 -080017381 }
James Smart4f774512009-05-22 14:52:35 -040017382 lpfc_destroy_vport_work_array(phba, vports);
17383 return vport;
17384}
17385
17386/**
James Smart45ed1192009-10-02 15:17:02 -040017387 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
17388 * @vport: The vport to work on.
17389 *
17390 * This function updates the receive sequence time stamp for this vport. The
17391 * receive sequence time stamp indicates the time that the last frame of the
17392 * the sequence that has been idle for the longest amount of time was received.
17393 * the driver uses this time stamp to indicate if any received sequences have
17394 * timed out.
17395 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017396static void
James Smart45ed1192009-10-02 15:17:02 -040017397lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17398{
17399 struct lpfc_dmabuf *h_buf;
17400 struct hbq_dmabuf *dmabuf = NULL;
17401
17402 /* get the oldest sequence on the rcv list */
17403 h_buf = list_get_first(&vport->rcv_buffer_list,
17404 struct lpfc_dmabuf, list);
17405 if (!h_buf)
17406 return;
17407 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17408 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17409}
17410
17411/**
17412 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17413 * @vport: The vport that the received sequences were sent to.
17414 *
17415 * This function cleans up all outstanding received sequences. This is called
17416 * by the driver when a link event or user action invalidates all the received
17417 * sequences.
17418 **/
17419void
17420lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17421{
17422 struct lpfc_dmabuf *h_buf, *hnext;
17423 struct lpfc_dmabuf *d_buf, *dnext;
17424 struct hbq_dmabuf *dmabuf = NULL;
17425
17426 /* start with the oldest sequence on the rcv list */
17427 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17428 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17429 list_del_init(&dmabuf->hbuf.list);
17430 list_for_each_entry_safe(d_buf, dnext,
17431 &dmabuf->dbuf.list, list) {
17432 list_del_init(&d_buf->list);
17433 lpfc_in_buf_free(vport->phba, d_buf);
17434 }
17435 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17436 }
17437}
17438
17439/**
17440 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17441 * @vport: The vport that the received sequences were sent to.
17442 *
17443 * This function determines whether any received sequences have timed out by
17444 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17445 * indicates that there is at least one timed out sequence this routine will
17446 * go through the received sequences one at a time from most inactive to most
17447 * active to determine which ones need to be cleaned up. Once it has determined
17448 * that a sequence needs to be cleaned up it will simply free up the resources
17449 * without sending an abort.
17450 **/
17451void
17452lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17453{
17454 struct lpfc_dmabuf *h_buf, *hnext;
17455 struct lpfc_dmabuf *d_buf, *dnext;
17456 struct hbq_dmabuf *dmabuf = NULL;
17457 unsigned long timeout;
17458 int abort_count = 0;
17459
17460 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17461 vport->rcv_buffer_time_stamp);
17462 if (list_empty(&vport->rcv_buffer_list) ||
17463 time_before(jiffies, timeout))
17464 return;
17465 /* start with the oldest sequence on the rcv list */
17466 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17467 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17468 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17469 dmabuf->time_stamp);
17470 if (time_before(jiffies, timeout))
17471 break;
17472 abort_count++;
17473 list_del_init(&dmabuf->hbuf.list);
17474 list_for_each_entry_safe(d_buf, dnext,
17475 &dmabuf->dbuf.list, list) {
17476 list_del_init(&d_buf->list);
17477 lpfc_in_buf_free(vport->phba, d_buf);
17478 }
17479 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17480 }
17481 if (abort_count)
17482 lpfc_update_rcv_time_stamp(vport);
17483}
17484
17485/**
James Smart4f774512009-05-22 14:52:35 -040017486 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17487 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17488 *
17489 * This function searches through the existing incomplete sequences that have
17490 * been sent to this @vport. If the frame matches one of the incomplete
17491 * sequences then the dbuf in the @dmabuf is added to the list of frames that
17492 * make up that sequence. If no sequence is found that matches this frame then
17493 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17494 * This function returns a pointer to the first dmabuf in the sequence list that
17495 * the frame was linked to.
17496 **/
17497static struct hbq_dmabuf *
17498lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17499{
17500 struct fc_frame_header *new_hdr;
17501 struct fc_frame_header *temp_hdr;
17502 struct lpfc_dmabuf *d_buf;
17503 struct lpfc_dmabuf *h_buf;
17504 struct hbq_dmabuf *seq_dmabuf = NULL;
17505 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050017506 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040017507
James Smart4d9ab992009-10-02 15:16:39 -040017508 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017509 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040017510 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050017511
James Smart4f774512009-05-22 14:52:35 -040017512 /* Use the hdr_buf to find the sequence that this frame belongs to */
17513 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17514 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17515 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17516 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17517 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17518 continue;
17519 /* found a pending sequence that matches this frame */
17520 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17521 break;
17522 }
17523 if (!seq_dmabuf) {
17524 /*
17525 * This indicates first frame received for this sequence.
17526 * Queue the buffer on the vport's rcv_buffer_list.
17527 */
17528 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040017529 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017530 return dmabuf;
17531 }
17532 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050017533 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17534 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040017535 list_del_init(&seq_dmabuf->hbuf.list);
17536 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17537 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017538 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017539 return dmabuf;
17540 }
James Smart45ed1192009-10-02 15:17:02 -040017541 /* move this sequence to the tail to indicate a young sequence */
17542 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17543 seq_dmabuf->time_stamp = jiffies;
17544 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050017545 if (list_empty(&seq_dmabuf->dbuf.list)) {
17546 temp_hdr = dmabuf->hbuf.virt;
17547 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17548 return seq_dmabuf;
17549 }
James Smart4f774512009-05-22 14:52:35 -040017550 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050017551 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17552 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040017553 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17554 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17555 /*
17556 * If the frame's sequence count is greater than the frame on
17557 * the list then insert the frame right after this frame
17558 */
James Smarteeead812009-12-21 17:01:23 -050017559 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17560 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040017561 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050017562 found = 1;
17563 break;
James Smart4f774512009-05-22 14:52:35 -040017564 }
James Smart4360ca92015-12-16 18:12:04 -050017565
17566 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17567 break;
17568 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040017569 }
James Smart4360ca92015-12-16 18:12:04 -050017570
17571 if (found)
17572 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017573 return NULL;
17574}
17575
17576/**
James Smart6669f9b2009-10-02 15:16:45 -040017577 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17578 * @vport: pointer to a vitural port
17579 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17580 *
17581 * This function tries to abort from the partially assembed sequence, described
17582 * by the information from basic abbort @dmabuf. It checks to see whether such
17583 * partially assembled sequence held by the driver. If so, it shall free up all
17584 * the frames from the partially assembled sequence.
17585 *
17586 * Return
17587 * true -- if there is matching partially assembled sequence present and all
17588 * the frames freed with the sequence;
17589 * false -- if there is no matching partially assembled sequence present so
17590 * nothing got aborted in the lower layer driver
17591 **/
17592static bool
17593lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17594 struct hbq_dmabuf *dmabuf)
17595{
17596 struct fc_frame_header *new_hdr;
17597 struct fc_frame_header *temp_hdr;
17598 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17599 struct hbq_dmabuf *seq_dmabuf = NULL;
17600
17601 /* Use the hdr_buf to find the sequence that matches this frame */
17602 INIT_LIST_HEAD(&dmabuf->dbuf.list);
17603 INIT_LIST_HEAD(&dmabuf->hbuf.list);
17604 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17605 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17606 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17607 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17608 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17609 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17610 continue;
17611 /* found a pending sequence that matches this frame */
17612 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17613 break;
17614 }
17615
17616 /* Free up all the frames from the partially assembled sequence */
17617 if (seq_dmabuf) {
17618 list_for_each_entry_safe(d_buf, n_buf,
17619 &seq_dmabuf->dbuf.list, list) {
17620 list_del_init(&d_buf->list);
17621 lpfc_in_buf_free(vport->phba, d_buf);
17622 }
17623 return true;
17624 }
17625 return false;
17626}
17627
17628/**
James Smart6dd9e312013-01-03 15:43:37 -050017629 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
17630 * @vport: pointer to a vitural port
17631 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17632 *
17633 * This function tries to abort from the assembed sequence from upper level
17634 * protocol, described by the information from basic abbort @dmabuf. It
17635 * checks to see whether such pending context exists at upper level protocol.
17636 * If so, it shall clean up the pending context.
17637 *
17638 * Return
17639 * true -- if there is matching pending context of the sequence cleaned
17640 * at ulp;
17641 * false -- if there is no matching pending context of the sequence present
17642 * at ulp.
17643 **/
17644static bool
17645lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17646{
17647 struct lpfc_hba *phba = vport->phba;
17648 int handled;
17649
17650 /* Accepting abort at ulp with SLI4 only */
17651 if (phba->sli_rev < LPFC_SLI_REV4)
17652 return false;
17653
17654 /* Register all caring upper level protocols to attend abort */
17655 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
17656 if (handled)
17657 return true;
17658
17659 return false;
17660}
17661
17662/**
James Smart546fc852011-03-11 16:06:29 -050017663 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040017664 * @phba: Pointer to HBA context object.
17665 * @cmd_iocbq: pointer to the command iocbq structure.
17666 * @rsp_iocbq: pointer to the response iocbq structure.
17667 *
James Smart546fc852011-03-11 16:06:29 -050017668 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040017669 * event. It properly releases the memory allocated to the sequence abort
17670 * accept iocb.
17671 **/
17672static void
James Smart546fc852011-03-11 16:06:29 -050017673lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040017674 struct lpfc_iocbq *cmd_iocbq,
17675 struct lpfc_iocbq *rsp_iocbq)
17676{
James Smart6dd9e312013-01-03 15:43:37 -050017677 struct lpfc_nodelist *ndlp;
17678
17679 if (cmd_iocbq) {
17680 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17681 lpfc_nlp_put(ndlp);
17682 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017683 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050017684 }
James Smart6b5151f2012-01-18 16:24:06 -050017685
17686 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17687 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17688 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17689 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
17690 rsp_iocbq->iocb.ulpStatus,
17691 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040017692}
17693
17694/**
James Smart6d368e52011-05-24 11:44:12 -040017695 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17696 * @phba: Pointer to HBA context object.
17697 * @xri: xri id in transaction.
17698 *
17699 * This function validates the xri maps to the known range of XRIs allocated an
17700 * used by the driver.
17701 **/
James Smart7851fe22011-07-22 18:36:52 -040017702uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017703lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17704 uint16_t xri)
17705{
James Smarta2fc4aef2014-09-03 12:57:55 -040017706 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040017707
17708 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17709 if (xri == phba->sli4_hba.xri_ids[i])
17710 return i;
17711 }
17712 return NO_XRI;
17713}
17714
James Smart6d368e52011-05-24 11:44:12 -040017715/**
James Smart546fc852011-03-11 16:06:29 -050017716 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017717 * @phba: Pointer to HBA context object.
17718 * @fc_hdr: pointer to a FC frame header.
17719 *
James Smart546fc852011-03-11 16:06:29 -050017720 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017721 * event after aborting the sequence handling.
17722 **/
James Smart86c67372017-04-21 16:05:04 -070017723void
James Smart6dd9e312013-01-03 15:43:37 -050017724lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17725 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040017726{
James Smart6dd9e312013-01-03 15:43:37 -050017727 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017728 struct lpfc_iocbq *ctiocb = NULL;
17729 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017730 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017731 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017732 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017733 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017734
17735 if (!lpfc_is_link_up(phba))
17736 return;
17737
17738 sid = sli4_sid_from_fc_hdr(fc_hdr);
17739 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017740 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017741
James Smart6dd9e312013-01-03 15:43:37 -050017742 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017743 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017744 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017745 if (!ndlp) {
17746 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17747 "1268 Failed to allocate ndlp for "
17748 "oxid:x%x SID:x%x\n", oxid, sid);
17749 return;
17750 }
James Smart6dd9e312013-01-03 15:43:37 -050017751 /* Put ndlp onto pport node list */
17752 lpfc_enqueue_node(vport, ndlp);
17753 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17754 /* re-setup ndlp without removing from node list */
17755 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17756 if (!ndlp) {
17757 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17758 "3275 Failed to active ndlp found "
17759 "for oxid:x%x SID:x%x\n", oxid, sid);
17760 return;
17761 }
James Smart6669f9b2009-10-02 15:16:45 -040017762 }
17763
James Smart546fc852011-03-11 16:06:29 -050017764 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017765 ctiocb = lpfc_sli_get_iocbq(phba);
17766 if (!ctiocb)
17767 return;
17768
James Smart5ffc2662009-11-18 15:39:44 -050017769 /* Extract the F_CTL field from FC_HDR */
17770 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17771
James Smart6669f9b2009-10-02 15:16:45 -040017772 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017773 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017774 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017775 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17776 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17777 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17778
17779 /* Fill in the rest of iocb fields */
17780 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17781 icmd->ulpBdeCount = 0;
17782 icmd->ulpLe = 1;
17783 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017784 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017785 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017786
James Smart6669f9b2009-10-02 15:16:45 -040017787 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017788 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017789 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017790 ctiocb->sli4_xritag = NO_XRI;
17791
James Smartee0f4fe2012-05-09 21:19:14 -040017792 if (fctl & FC_FC_EX_CTX)
17793 /* Exchange responder sent the abort so we
17794 * own the oxid.
17795 */
17796 xri = oxid;
17797 else
17798 xri = rxid;
17799 lxri = lpfc_sli4_xri_inrange(phba, xri);
17800 if (lxri != NO_XRI)
17801 lpfc_set_rrq_active(phba, ndlp, lxri,
17802 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017803 /* For BA_ABTS from exchange responder, if the logical xri with
17804 * the oxid maps to the FCP XRI range, the port no longer has
17805 * that exchange context, send a BLS_RJT. Override the IOCB for
17806 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017807 */
James Smart6dd9e312013-01-03 15:43:37 -050017808 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017809 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050017810 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17811 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17812 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17813 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17814 }
17815
17816 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17817 * the driver no longer has that exchange, send a BLS_RJT. Override
17818 * the IOCB for a BA_RJT.
17819 */
17820 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017821 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17822 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17823 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17824 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17825 }
James Smart6669f9b2009-10-02 15:16:45 -040017826
James Smart5ffc2662009-11-18 15:39:44 -050017827 if (fctl & FC_FC_EX_CTX) {
17828 /* ABTS sent by responder to CT exchange, construction
17829 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17830 * field and RX_ID from ABTS for RX_ID field.
17831 */
James Smart546fc852011-03-11 16:06:29 -050017832 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017833 } else {
17834 /* ABTS sent by initiator to CT exchange, construction
17835 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017836 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017837 */
James Smart546fc852011-03-11 16:06:29 -050017838 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017839 }
James Smartf09c3ac2012-03-01 22:33:29 -050017840 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017841 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017842
James Smart546fc852011-03-11 16:06:29 -050017843 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017844 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17845 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17846 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017847
17848 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17849 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017850 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17851 "2925 Failed to issue CT ABTS RSP x%x on "
17852 "xri x%x, Data x%x\n",
17853 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17854 phba->link_state);
17855 lpfc_nlp_put(ndlp);
17856 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017857 lpfc_sli_release_iocbq(phba, ctiocb);
17858 }
James Smart6669f9b2009-10-02 15:16:45 -040017859}
17860
17861/**
17862 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17863 * @vport: Pointer to the vport on which this sequence was received
17864 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17865 *
17866 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17867 * receive sequence is only partially assembed by the driver, it shall abort
17868 * the partially assembled frames for the sequence. Otherwise, if the
17869 * unsolicited receive sequence has been completely assembled and passed to
17870 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17871 * unsolicited sequence has been aborted. After that, it will issue a basic
17872 * accept to accept the abort.
17873 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017874static void
James Smart6669f9b2009-10-02 15:16:45 -040017875lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17876 struct hbq_dmabuf *dmabuf)
17877{
17878 struct lpfc_hba *phba = vport->phba;
17879 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017880 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017881 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017882
James Smart6669f9b2009-10-02 15:16:45 -040017883 /* Make a copy of fc_hdr before the dmabuf being released */
17884 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017885 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017886
James Smart5ffc2662009-11-18 15:39:44 -050017887 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017888 /* ABTS by responder to exchange, no cleanup needed */
17889 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017890 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017891 /* ABTS by initiator to exchange, need to do cleanup */
17892 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17893 if (aborted == false)
17894 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017895 }
James Smart6dd9e312013-01-03 15:43:37 -050017896 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17897
James Smart86c67372017-04-21 16:05:04 -070017898 if (phba->nvmet_support) {
17899 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17900 return;
17901 }
17902
James Smart6dd9e312013-01-03 15:43:37 -050017903 /* Respond with BA_ACC or BA_RJT accordingly */
17904 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017905}
17906
17907/**
James Smart4f774512009-05-22 14:52:35 -040017908 * lpfc_seq_complete - Indicates if a sequence is complete
17909 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17910 *
17911 * This function checks the sequence, starting with the frame described by
17912 * @dmabuf, to see if all the frames associated with this sequence are present.
17913 * the frames associated with this sequence are linked to the @dmabuf using the
17914 * dbuf list. This function looks for two major things. 1) That the first frame
17915 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17916 * set. 3) That there are no holes in the sequence count. The function will
17917 * return 1 when the sequence is complete, otherwise it will return 0.
17918 **/
17919static int
17920lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17921{
17922 struct fc_frame_header *hdr;
17923 struct lpfc_dmabuf *d_buf;
17924 struct hbq_dmabuf *seq_dmabuf;
17925 uint32_t fctl;
17926 int seq_count = 0;
17927
17928 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17929 /* make sure first fame of sequence has a sequence count of zero */
17930 if (hdr->fh_seq_cnt != seq_count)
17931 return 0;
17932 fctl = (hdr->fh_f_ctl[0] << 16 |
17933 hdr->fh_f_ctl[1] << 8 |
17934 hdr->fh_f_ctl[2]);
17935 /* If last frame of sequence we can return success. */
17936 if (fctl & FC_FC_END_SEQ)
17937 return 1;
17938 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17939 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17940 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17941 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017942 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017943 return 0;
17944 fctl = (hdr->fh_f_ctl[0] << 16 |
17945 hdr->fh_f_ctl[1] << 8 |
17946 hdr->fh_f_ctl[2]);
17947 /* If last frame of sequence we can return success. */
17948 if (fctl & FC_FC_END_SEQ)
17949 return 1;
17950 }
17951 return 0;
17952}
17953
17954/**
17955 * lpfc_prep_seq - Prep sequence for ULP processing
17956 * @vport: Pointer to the vport on which this sequence was received
17957 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17958 *
17959 * This function takes a sequence, described by a list of frames, and creates
17960 * a list of iocbq structures to describe the sequence. This iocbq list will be
17961 * used to issue to the generic unsolicited sequence handler. This routine
17962 * returns a pointer to the first iocbq in the list. If the function is unable
17963 * to allocate an iocbq then it throw out the received frames that were not
17964 * able to be described and return a pointer to the first iocbq. If unable to
17965 * allocate any iocbqs (including the first) this function will return NULL.
17966 **/
17967static struct lpfc_iocbq *
17968lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17969{
James Smart7851fe22011-07-22 18:36:52 -040017970 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017971 struct lpfc_dmabuf *d_buf, *n_buf;
17972 struct lpfc_iocbq *first_iocbq, *iocbq;
17973 struct fc_frame_header *fc_hdr;
17974 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017975 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017976 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017977
17978 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17979 /* remove from receive buffer list */
17980 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017981 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017982 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017983 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017984 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017985 /* Get an iocbq struct to fill in. */
17986 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17987 if (first_iocbq) {
17988 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017989 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017990 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017991 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017992
17993 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17994 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17995 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17996 first_iocbq->iocb.un.rcvels.parmRo =
17997 sli4_did_from_fc_hdr(fc_hdr);
17998 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17999 } else
18000 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040018001 first_iocbq->iocb.ulpContext = NO_XRI;
18002 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
18003 be16_to_cpu(fc_hdr->fh_ox_id);
18004 /* iocbq is prepped for internal consumption. Physical vpi. */
18005 first_iocbq->iocb.unsli3.rcvsli3.vpi =
18006 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040018007 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040018008 tot_len = bf_get(lpfc_rcqe_length,
18009 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
18010
James Smart4f774512009-05-22 14:52:35 -040018011 first_iocbq->context2 = &seq_dmabuf->dbuf;
18012 first_iocbq->context3 = NULL;
18013 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040018014 if (tot_len > LPFC_DATA_BUF_SIZE)
18015 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040018016 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040018017 else
18018 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
18019
James Smart4f774512009-05-22 14:52:35 -040018020 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040018021
James Smart7851fe22011-07-22 18:36:52 -040018022 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040018023 }
18024 iocbq = first_iocbq;
18025 /*
18026 * Each IOCBq can have two Buffers assigned, so go through the list
18027 * of buffers for this sequence and save two buffers in each IOCBq
18028 */
18029 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
18030 if (!iocbq) {
18031 lpfc_in_buf_free(vport->phba, d_buf);
18032 continue;
18033 }
18034 if (!iocbq->context3) {
18035 iocbq->context3 = d_buf;
18036 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040018037 /* We need to get the size out of the right CQE */
18038 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18039 len = bf_get(lpfc_rcqe_length,
18040 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040018041 pbde = (struct ulp_bde64 *)
18042 &iocbq->iocb.unsli3.sli3Words[4];
18043 if (len > LPFC_DATA_BUF_SIZE)
18044 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
18045 else
18046 pbde->tus.f.bdeSize = len;
18047
James Smart7851fe22011-07-22 18:36:52 -040018048 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
18049 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040018050 } else {
18051 iocbq = lpfc_sli_get_iocbq(vport->phba);
18052 if (!iocbq) {
18053 if (first_iocbq) {
18054 first_iocbq->iocb.ulpStatus =
18055 IOSTAT_FCP_RSP_ERROR;
18056 first_iocbq->iocb.un.ulpWord[4] =
18057 IOERR_NO_RESOURCES;
18058 }
18059 lpfc_in_buf_free(vport->phba, d_buf);
18060 continue;
18061 }
James Smart7851fe22011-07-22 18:36:52 -040018062 /* We need to get the size out of the right CQE */
18063 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18064 len = bf_get(lpfc_rcqe_length,
18065 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040018066 iocbq->context2 = d_buf;
18067 iocbq->context3 = NULL;
18068 iocbq->iocb.ulpBdeCount = 1;
18069 if (len > LPFC_DATA_BUF_SIZE)
18070 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
18071 LPFC_DATA_BUF_SIZE;
18072 else
18073 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
18074
James Smart7851fe22011-07-22 18:36:52 -040018075 tot_len += len;
18076 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
18077
James Smart4f774512009-05-22 14:52:35 -040018078 iocbq->iocb.un.rcvels.remoteID = sid;
18079 list_add_tail(&iocbq->list, &first_iocbq->list);
18080 }
18081 }
James Smart39c4f1a2020-01-27 16:23:01 -080018082 /* Free the sequence's header buffer */
18083 if (!first_iocbq)
18084 lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
18085
James Smart4f774512009-05-22 14:52:35 -040018086 return first_iocbq;
18087}
18088
James Smart6669f9b2009-10-02 15:16:45 -040018089static void
18090lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
18091 struct hbq_dmabuf *seq_dmabuf)
18092{
18093 struct fc_frame_header *fc_hdr;
18094 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
18095 struct lpfc_hba *phba = vport->phba;
18096
18097 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18098 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
18099 if (!iocbq) {
18100 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18101 "2707 Ring %d handler: Failed to allocate "
18102 "iocb Rctl x%x Type x%x received\n",
18103 LPFC_ELS_RING,
18104 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18105 return;
18106 }
18107 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080018108 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040018109 iocbq, fc_hdr->fh_r_ctl,
18110 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040018111 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040018112 "2540 Ring %d handler: unexpected Rctl "
18113 "x%x Type x%x received\n",
18114 LPFC_ELS_RING,
18115 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18116
18117 /* Free iocb created in lpfc_prep_seq */
18118 list_for_each_entry_safe(curr_iocb, next_iocb,
18119 &iocbq->list, list) {
18120 list_del_init(&curr_iocb->list);
18121 lpfc_sli_release_iocbq(phba, curr_iocb);
18122 }
18123 lpfc_sli_release_iocbq(phba, iocbq);
18124}
18125
James Smartae9e28f2017-05-15 15:20:51 -070018126static void
18127lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
18128 struct lpfc_iocbq *rspiocb)
18129{
18130 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
18131
18132 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020018133 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070018134 kfree(pcmd);
18135 lpfc_sli_release_iocbq(phba, cmdiocb);
James Smarte817e5d2018-12-13 15:17:53 -080018136 lpfc_drain_txq(phba);
James Smartae9e28f2017-05-15 15:20:51 -070018137}
18138
18139static void
18140lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
18141 struct hbq_dmabuf *dmabuf)
18142{
18143 struct fc_frame_header *fc_hdr;
18144 struct lpfc_hba *phba = vport->phba;
18145 struct lpfc_iocbq *iocbq = NULL;
18146 union lpfc_wqe *wqe;
18147 struct lpfc_dmabuf *pcmd = NULL;
18148 uint32_t frame_len;
18149 int rc;
James Smarte817e5d2018-12-13 15:17:53 -080018150 unsigned long iflags;
James Smartae9e28f2017-05-15 15:20:51 -070018151
18152 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18153 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
18154
18155 /* Send the received frame back */
18156 iocbq = lpfc_sli_get_iocbq(phba);
James Smarte817e5d2018-12-13 15:17:53 -080018157 if (!iocbq) {
18158 /* Queue cq event and wakeup worker thread to process it */
18159 spin_lock_irqsave(&phba->hbalock, iflags);
18160 list_add_tail(&dmabuf->cq_event.list,
18161 &phba->sli4_hba.sp_queue_event);
18162 phba->hba_flag |= HBA_SP_QUEUE_EVT;
18163 spin_unlock_irqrestore(&phba->hbalock, iflags);
18164 lpfc_worker_wake_up(phba);
18165 return;
18166 }
James Smartae9e28f2017-05-15 15:20:51 -070018167
18168 /* Allocate buffer for command payload */
18169 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
18170 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020018171 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070018172 &pcmd->phys);
18173 if (!pcmd || !pcmd->virt)
18174 goto exit;
18175
18176 INIT_LIST_HEAD(&pcmd->list);
18177
18178 /* copyin the payload */
18179 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
18180
18181 /* fill in BDE's for command */
18182 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
18183 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
18184 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
18185 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
18186
18187 iocbq->context2 = pcmd;
18188 iocbq->vport = vport;
18189 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
18190 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
18191
18192 /*
18193 * Setup rest of the iocb as though it were a WQE
18194 * Build the SEND_FRAME WQE
18195 */
18196 wqe = (union lpfc_wqe *)&iocbq->iocb;
18197
18198 wqe->send_frame.frame_len = frame_len;
18199 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
18200 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
18201 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
18202 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
18203 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
18204 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
18205
18206 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
18207 iocbq->iocb.ulpLe = 1;
18208 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
18209 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
18210 if (rc == IOCB_ERROR)
18211 goto exit;
18212
18213 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18214 return;
18215
18216exit:
18217 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18218 "2023 Unable to process MDS loopback frame\n");
18219 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020018220 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070018221 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070018222 if (iocbq)
18223 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070018224 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18225}
18226
James Smart4f774512009-05-22 14:52:35 -040018227/**
18228 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
18229 * @phba: Pointer to HBA context object.
18230 *
18231 * This function is called with no lock held. This function processes all
18232 * the received buffers and gives it to upper layers when a received buffer
18233 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080018234 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040018235 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
18236 * appropriate receive function when the final frame in a sequence is received.
18237 **/
James Smart4d9ab992009-10-02 15:16:39 -040018238void
18239lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
18240 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040018241{
James Smart4d9ab992009-10-02 15:16:39 -040018242 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040018243 struct fc_frame_header *fc_hdr;
18244 struct lpfc_vport *vport;
18245 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040018246 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040018247
James Smart4f774512009-05-22 14:52:35 -040018248 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040018249 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080018250
James Smarte817e5d2018-12-13 15:17:53 -080018251 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
18252 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
18253 vport = phba->pport;
18254 /* Handle MDS Loopback frames */
18255 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
18256 return;
18257 }
18258
James Smart4d9ab992009-10-02 15:16:39 -040018259 /* check to see if this a valid type of frame */
18260 if (lpfc_fc_frame_check(phba, fc_hdr)) {
18261 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18262 return;
18263 }
James Smart2ea259e2017-02-12 13:52:27 -080018264
James Smart7851fe22011-07-22 18:36:52 -040018265 if ((bf_get(lpfc_cqe_code,
18266 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
18267 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
18268 &dmabuf->cq_event.cqe.rcqe_cmpl);
18269 else
18270 fcfi = bf_get(lpfc_rcqe_fcf_id,
18271 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040018272
James Smarte62245d2019-08-14 16:57:08 -070018273 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
18274 vport = phba->pport;
18275 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18276 "2023 MDS Loopback %d bytes\n",
18277 bf_get(lpfc_rcqe_length,
18278 &dmabuf->cq_event.cqe.rcqe_cmpl));
18279 /* Handle MDS Loopback frames */
18280 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
18281 return;
18282 }
18283
James Smart895427b2017-02-12 13:52:30 -080018284 /* d_id this frame is directed to */
18285 did = sli4_did_from_fc_hdr(fc_hdr);
18286
18287 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040018288 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040018289 /* throw out the frame */
18290 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18291 return;
18292 }
James Smart939723a2012-05-09 21:19:03 -040018293
James Smart939723a2012-05-09 21:19:03 -040018294 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
18295 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
18296 (did != Fabric_DID)) {
18297 /*
18298 * Throw out the frame if we are not pt2pt.
18299 * The pt2pt protocol allows for discovery frames
18300 * to be received without a registered VPI.
18301 */
18302 if (!(vport->fc_flag & FC_PT2PT) ||
18303 (phba->link_state == LPFC_HBA_READY)) {
18304 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18305 return;
18306 }
18307 }
18308
James Smart6669f9b2009-10-02 15:16:45 -040018309 /* Handle the basic abort sequence (BA_ABTS) event */
18310 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
18311 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
18312 return;
18313 }
18314
James Smart4d9ab992009-10-02 15:16:39 -040018315 /* Link this frame */
18316 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
18317 if (!seq_dmabuf) {
18318 /* unable to add frame to vport - throw it out */
18319 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18320 return;
18321 }
18322 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050018323 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040018324 return;
James Smartdef9c7a2009-12-21 17:02:28 -050018325
James Smart6669f9b2009-10-02 15:16:45 -040018326 /* Send the complete sequence to the upper layer protocol */
18327 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040018328}
James Smart6fb120a2009-05-22 14:52:59 -040018329
18330/**
18331 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
18332 * @phba: pointer to lpfc hba data structure.
18333 *
18334 * This routine is invoked to post rpi header templates to the
18335 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018336 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18337 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018338 *
18339 * This routine does not require any locks. It's usage is expected
18340 * to be driver load or reset recovery when the driver is
18341 * sequential.
18342 *
18343 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018344 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040018345 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040018346 * When this error occurs, the driver is not guaranteed
18347 * to have any rpi regions posted to the device and
18348 * must either attempt to repost the regions or take a
18349 * fatal error.
18350 **/
18351int
18352lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
18353{
18354 struct lpfc_rpi_hdr *rpi_page;
18355 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040018356 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018357
James Smart6d368e52011-05-24 11:44:12 -040018358 /* SLI4 ports that support extents do not require RPI headers. */
18359 if (!phba->sli4_hba.rpi_hdrs_in_use)
18360 goto exit;
18361 if (phba->sli4_hba.extents_in_use)
18362 return -EIO;
18363
James Smart6fb120a2009-05-22 14:52:59 -040018364 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040018365 /*
18366 * Assign the rpi headers a physical rpi only if the driver
18367 * has not initialized those resources. A port reset only
18368 * needs the headers posted.
18369 */
18370 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
18371 LPFC_RPI_RSRC_RDY)
18372 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
18373
James Smart6fb120a2009-05-22 14:52:59 -040018374 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
18375 if (rc != MBX_SUCCESS) {
18376 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18377 "2008 Error %d posting all rpi "
18378 "headers\n", rc);
18379 rc = -EIO;
18380 break;
18381 }
18382 }
18383
James Smart6d368e52011-05-24 11:44:12 -040018384 exit:
18385 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
18386 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040018387 return rc;
18388}
18389
18390/**
18391 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
18392 * @phba: pointer to lpfc hba data structure.
18393 * @rpi_page: pointer to the rpi memory region.
18394 *
18395 * This routine is invoked to post a single rpi header to the
18396 * HBA consistent with the SLI-4 interface spec. This memory region
18397 * maps up to 64 rpi context regions.
18398 *
18399 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018400 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040018401 * -ENOMEM - No available memory
18402 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040018403 **/
18404int
18405lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18406{
18407 LPFC_MBOXQ_t *mboxq;
18408 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18409 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018410 uint32_t shdr_status, shdr_add_status;
18411 union lpfc_sli4_cfg_shdr *shdr;
18412
James Smart6d368e52011-05-24 11:44:12 -040018413 /* SLI4 ports that support extents do not require RPI headers. */
18414 if (!phba->sli4_hba.rpi_hdrs_in_use)
18415 return rc;
18416 if (phba->sli4_hba.extents_in_use)
18417 return -EIO;
18418
James Smart6fb120a2009-05-22 14:52:59 -040018419 /* The port is notified of the header region via a mailbox command. */
18420 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18421 if (!mboxq) {
18422 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18423 "2001 Unable to allocate memory for issuing "
18424 "SLI_CONFIG_SPECIAL mailbox command\n");
18425 return -ENOMEM;
18426 }
18427
18428 /* Post all rpi memory regions to the port. */
18429 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040018430 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18431 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18432 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050018433 sizeof(struct lpfc_sli4_cfg_mhdr),
18434 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040018435
18436
18437 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040018438 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18439 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040018440 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18441 hdr_tmpl, rpi_page->page_count);
18442
James Smart6fb120a2009-05-22 14:52:59 -040018443 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18444 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040018445 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040018446 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18447 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18448 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18449 if (rc != MBX_TIMEOUT)
18450 mempool_free(mboxq, phba->mbox_mem_pool);
18451 if (shdr_status || shdr_add_status || rc) {
18452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18453 "2514 POST_RPI_HDR mailbox failed with "
18454 "status x%x add_status x%x, mbx status x%x\n",
18455 shdr_status, shdr_add_status, rc);
18456 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070018457 } else {
18458 /*
18459 * The next_rpi stores the next logical module-64 rpi value used
18460 * to post physical rpis in subsequent rpi postings.
18461 */
18462 spin_lock_irq(&phba->hbalock);
18463 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18464 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040018465 }
18466 return rc;
18467}
18468
18469/**
18470 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18471 * @phba: pointer to lpfc hba data structure.
18472 *
18473 * This routine is invoked to post rpi header templates to the
18474 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018475 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18476 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018477 *
18478 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018479 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040018480 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
18481 **/
18482int
18483lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18484{
James Smart6d368e52011-05-24 11:44:12 -040018485 unsigned long rpi;
18486 uint16_t max_rpi, rpi_limit;
18487 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018488 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040018489 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040018490
James Smart6fb120a2009-05-22 14:52:59 -040018491 /*
James Smart6d368e52011-05-24 11:44:12 -040018492 * Fetch the next logical rpi. Because this index is logical,
18493 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040018494 */
James Smart4902b382013-10-10 12:20:35 -040018495 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040018496 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18497 rpi_limit = phba->sli4_hba.next_rpi;
18498
James Smart6d368e52011-05-24 11:44:12 -040018499 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18500 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040018501 rpi = LPFC_RPI_ALLOC_ERROR;
18502 else {
18503 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18504 phba->sli4_hba.max_cfg_param.rpi_used++;
18505 phba->sli4_hba.rpi_count++;
18506 }
James Smart0f154222019-09-21 20:58:52 -070018507 lpfc_printf_log(phba, KERN_INFO,
18508 LOG_NODE | LOG_DISCOVERY,
18509 "0001 Allocated rpi:x%x max:x%x lim:x%x\n",
James Smartbe6bb942015-04-07 15:07:22 -040018510 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040018511
18512 /*
18513 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040018514 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040018515 */
18516 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18517 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040018518 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018519 return rpi;
18520 }
18521
18522 /*
James Smart6d368e52011-05-24 11:44:12 -040018523 * RPI header postings are not required for SLI4 ports capable of
18524 * extents.
18525 */
18526 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040018527 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040018528 return rpi;
18529 }
18530
18531 /*
James Smart6fb120a2009-05-22 14:52:59 -040018532 * If the driver is running low on rpi resources, allocate another
18533 * page now. Note that the next_rpi value is used because
18534 * it represents how many are actually in use whereas max_rpi notes
18535 * how many are supported max by the device.
18536 */
James Smart6d368e52011-05-24 11:44:12 -040018537 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040018538 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018539 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18540 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18541 if (!rpi_hdr) {
18542 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18543 "2002 Error Could not grow rpi "
18544 "count\n");
18545 } else {
James Smart6d368e52011-05-24 11:44:12 -040018546 lrpi = rpi_hdr->start_rpi;
18547 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040018548 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18549 }
18550 }
18551
18552 return rpi;
18553}
18554
18555/**
18556 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18557 * @phba: pointer to lpfc hba data structure.
18558 *
18559 * This routine is invoked to release an rpi to the pool of
18560 * available rpis maintained by the driver.
18561 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018562static void
James Smartd7c47992010-06-08 18:31:54 -040018563__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18564{
James Smart7cfd5632019-11-04 16:56:58 -080018565 /*
18566 * if the rpi value indicates a prior unreg has already
18567 * been done, skip the unreg.
18568 */
18569 if (rpi == LPFC_RPI_ALLOC_ERROR)
18570 return;
18571
James Smartd7c47992010-06-08 18:31:54 -040018572 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18573 phba->sli4_hba.rpi_count--;
18574 phba->sli4_hba.max_cfg_param.rpi_used--;
James Smartb95b2112019-08-14 16:56:47 -070018575 } else {
James Smart0f154222019-09-21 20:58:52 -070018576 lpfc_printf_log(phba, KERN_INFO,
18577 LOG_NODE | LOG_DISCOVERY,
James Smartb95b2112019-08-14 16:56:47 -070018578 "2016 rpi %x not inuse\n",
18579 rpi);
James Smartd7c47992010-06-08 18:31:54 -040018580 }
18581}
18582
18583/**
18584 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18585 * @phba: pointer to lpfc hba data structure.
18586 *
18587 * This routine is invoked to release an rpi to the pool of
18588 * available rpis maintained by the driver.
18589 **/
18590void
James Smart6fb120a2009-05-22 14:52:59 -040018591lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18592{
18593 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040018594 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040018595 spin_unlock_irq(&phba->hbalock);
18596}
18597
18598/**
18599 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18600 * @phba: pointer to lpfc hba data structure.
18601 *
18602 * This routine is invoked to remove the memory region that
18603 * provided rpi via a bitmask.
18604 **/
18605void
18606lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18607{
18608 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040018609 kfree(phba->sli4_hba.rpi_ids);
18610 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040018611}
18612
18613/**
18614 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18615 * @phba: pointer to lpfc hba data structure.
18616 *
18617 * This routine is invoked to remove the memory region that
18618 * provided rpi via a bitmask.
18619 **/
18620int
James Smart6b5151f2012-01-18 16:24:06 -050018621lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18622 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040018623{
18624 LPFC_MBOXQ_t *mboxq;
18625 struct lpfc_hba *phba = ndlp->phba;
18626 int rc;
18627
18628 /* The port is notified of the header region via a mailbox command. */
18629 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18630 if (!mboxq)
18631 return -ENOMEM;
18632
18633 /* Post all rpi memory regions to the port. */
18634 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050018635 if (cmpl) {
18636 mboxq->mbox_cmpl = cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080018637 mboxq->ctx_buf = arg;
18638 mboxq->ctx_ndlp = ndlp;
James Smart72859902012-01-18 16:25:38 -050018639 } else
18640 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050018641 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040018642 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18643 if (rc == MBX_NOT_FINISHED) {
18644 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18645 "2010 Resume RPI Mailbox failed "
18646 "status %d, mbxStatus x%x\n", rc,
18647 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18648 mempool_free(mboxq, phba->mbox_mem_pool);
18649 return -EIO;
18650 }
18651 return 0;
18652}
18653
18654/**
18655 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050018656 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040018657 *
James Smart76a95d72010-11-20 23:11:48 -050018658 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040018659 *
18660 * Returns:
18661 * 0 success
18662 * -Evalue otherwise
18663 **/
18664int
James Smart76a95d72010-11-20 23:11:48 -050018665lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040018666{
18667 LPFC_MBOXQ_t *mboxq;
18668 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040018669 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040018670 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050018671 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040018672 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18673 if (!mboxq)
18674 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050018675 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040018676 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040018677 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040018678 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050018679 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040018680 "2022 INIT VPI Mailbox failed "
18681 "status %d, mbxStatus x%x\n", rc,
18682 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040018683 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040018684 }
James Smart6a9c52c2009-10-02 15:16:51 -040018685 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050018686 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040018687
18688 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040018689}
18690
18691/**
18692 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
18693 * @phba: pointer to lpfc hba data structure.
18694 * @mboxq: Pointer to mailbox object.
18695 *
18696 * This routine is invoked to manually add a single FCF record. The caller
18697 * must pass a completely initialized FCF_Record. This routine takes
18698 * care of the nonembedded mailbox operations.
18699 **/
18700static void
18701lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
18702{
18703 void *virt_addr;
18704 union lpfc_sli4_cfg_shdr *shdr;
18705 uint32_t shdr_status, shdr_add_status;
18706
18707 virt_addr = mboxq->sge_array->addr[0];
18708 /* The IOCTL status is embedded in the mailbox subheader. */
18709 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18710 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18711 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18712
18713 if ((shdr_status || shdr_add_status) &&
18714 (shdr_status != STATUS_FCF_IN_USE))
18715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18716 "2558 ADD_FCF_RECORD mailbox failed with "
18717 "status x%x add_status x%x\n",
18718 shdr_status, shdr_add_status);
18719
18720 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18721}
18722
18723/**
18724 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18725 * @phba: pointer to lpfc hba data structure.
18726 * @fcf_record: pointer to the initialized fcf record to add.
18727 *
18728 * This routine is invoked to manually add a single FCF record. The caller
18729 * must pass a completely initialized FCF_Record. This routine takes
18730 * care of the nonembedded mailbox operations.
18731 **/
18732int
18733lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18734{
18735 int rc = 0;
18736 LPFC_MBOXQ_t *mboxq;
18737 uint8_t *bytep;
18738 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040018739 struct lpfc_mbx_sge sge;
18740 uint32_t alloc_len, req_len;
18741 uint32_t fcfindex;
18742
18743 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18744 if (!mboxq) {
18745 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18746 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18747 return -ENOMEM;
18748 }
18749
18750 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18751 sizeof(uint32_t);
18752
18753 /* Allocate DMA memory and set up the non-embedded mailbox command */
18754 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18755 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18756 req_len, LPFC_SLI4_MBX_NEMBED);
18757 if (alloc_len < req_len) {
18758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18759 "2523 Allocated DMA memory size (x%x) is "
18760 "less than the requested DMA memory "
18761 "size (x%x)\n", alloc_len, req_len);
18762 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18763 return -ENOMEM;
18764 }
18765
18766 /*
18767 * Get the first SGE entry from the non-embedded DMA memory. This
18768 * routine only uses a single SGE.
18769 */
18770 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018771 virt_addr = mboxq->sge_array->addr[0];
18772 /*
18773 * Configure the FCF record for FCFI 0. This is the driver's
18774 * hardcoded default and gets used in nonFIP mode.
18775 */
18776 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18777 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18778 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18779
18780 /*
18781 * Copy the fcf_index and the FCF Record Data. The data starts after
18782 * the FCoE header plus word10. The data copy needs to be endian
18783 * correct.
18784 */
18785 bytep += sizeof(uint32_t);
18786 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18787 mboxq->vport = phba->pport;
18788 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18789 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18790 if (rc == MBX_NOT_FINISHED) {
18791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18792 "2515 ADD_FCF_RECORD mailbox failed with "
18793 "status 0x%x\n", rc);
18794 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18795 rc = -EIO;
18796 } else
18797 rc = 0;
18798
18799 return rc;
18800}
18801
18802/**
18803 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18804 * @phba: pointer to lpfc hba data structure.
18805 * @fcf_record: pointer to the fcf record to write the default data.
18806 * @fcf_index: FCF table entry index.
18807 *
18808 * This routine is invoked to build the driver's default FCF record. The
18809 * values used are hardcoded. This routine handles memory initialization.
18810 *
18811 **/
18812void
18813lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18814 struct fcf_record *fcf_record,
18815 uint16_t fcf_index)
18816{
18817 memset(fcf_record, 0, sizeof(struct fcf_record));
18818 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18819 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18820 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18821 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18822 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18823 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18824 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18825 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18826 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18827 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18828 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18829 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18830 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018831 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018832 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18833 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18834 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18835 /* Set the VLAN bit map */
18836 if (phba->valid_vlan) {
18837 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18838 = 1 << (phba->vlan_id % 8);
18839 }
18840}
18841
18842/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018843 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018844 * @phba: pointer to lpfc hba data structure.
18845 * @fcf_index: FCF table entry offset.
18846 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018847 * This routine is invoked to scan the entire FCF table by reading FCF
18848 * record and processing it one at a time starting from the @fcf_index
18849 * for initial FCF discovery or fast FCF failover rediscovery.
18850 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018851 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018852 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018853 **/
18854int
James Smart0c9ab6f2010-02-26 14:15:57 -050018855lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018856{
18857 int rc = 0, error;
18858 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018859
James Smart32b97932009-07-19 10:01:21 -040018860 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018861 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018862 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18863 if (!mboxq) {
18864 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18865 "2000 Failed to allocate mbox for "
18866 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018867 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018868 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018869 }
James Smartecfd03c2010-02-12 14:41:27 -050018870 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018871 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018872 if (rc) {
18873 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018874 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018875 }
James Smartecfd03c2010-02-12 14:41:27 -050018876 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018877 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018878 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018879
18880 spin_lock_irq(&phba->hbalock);
18881 phba->hba_flag |= FCF_TS_INPROG;
18882 spin_unlock_irq(&phba->hbalock);
18883
James Smart6fb120a2009-05-22 14:52:59 -040018884 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018885 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018886 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018887 else {
James Smart38b92ef2010-08-04 16:11:39 -040018888 /* Reset eligible FCF count for new scan */
18889 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018890 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018891 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018892 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018893fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018894 if (error) {
18895 if (mboxq)
18896 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018897 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018898 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018899 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018900 spin_unlock_irq(&phba->hbalock);
18901 }
James Smart6fb120a2009-05-22 14:52:59 -040018902 return error;
18903}
James Smarta0c87cb2009-07-19 10:01:10 -040018904
18905/**
James Smarta93ff372010-10-22 11:06:08 -040018906 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018907 * @phba: pointer to lpfc hba data structure.
18908 * @fcf_index: FCF table entry offset.
18909 *
18910 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018911 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018912 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018913 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018914 * otherwise.
18915 **/
18916int
18917lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18918{
18919 int rc = 0, error;
18920 LPFC_MBOXQ_t *mboxq;
18921
18922 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18923 if (!mboxq) {
18924 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18925 "2763 Failed to allocate mbox for "
18926 "READ_FCF cmd\n");
18927 error = -ENOMEM;
18928 goto fail_fcf_read;
18929 }
18930 /* Construct the read FCF record mailbox command */
18931 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18932 if (rc) {
18933 error = -EINVAL;
18934 goto fail_fcf_read;
18935 }
18936 /* Issue the mailbox command asynchronously */
18937 mboxq->vport = phba->pport;
18938 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18939 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18940 if (rc == MBX_NOT_FINISHED)
18941 error = -EIO;
18942 else
18943 error = 0;
18944
18945fail_fcf_read:
18946 if (error && mboxq)
18947 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18948 return error;
18949}
18950
18951/**
18952 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18953 * @phba: pointer to lpfc hba data structure.
18954 * @fcf_index: FCF table entry offset.
18955 *
18956 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018957 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018958 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018959 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018960 * otherwise.
18961 **/
18962int
18963lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18964{
18965 int rc = 0, error;
18966 LPFC_MBOXQ_t *mboxq;
18967
18968 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18969 if (!mboxq) {
18970 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18971 "2758 Failed to allocate mbox for "
18972 "READ_FCF cmd\n");
18973 error = -ENOMEM;
18974 goto fail_fcf_read;
18975 }
18976 /* Construct the read FCF record mailbox command */
18977 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18978 if (rc) {
18979 error = -EINVAL;
18980 goto fail_fcf_read;
18981 }
18982 /* Issue the mailbox command asynchronously */
18983 mboxq->vport = phba->pport;
18984 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18985 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18986 if (rc == MBX_NOT_FINISHED)
18987 error = -EIO;
18988 else
18989 error = 0;
18990
18991fail_fcf_read:
18992 if (error && mboxq)
18993 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18994 return error;
18995}
18996
18997/**
James Smartf5cb5302015-12-16 18:11:52 -050018998 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018999 * phba pointer to the lpfc_hba struct for this port.
19000 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
19001 * routine when the rr_bmask is empty. The FCF indecies are put into the
19002 * rr_bmask based on their priority level. Starting from the highest priority
19003 * to the lowest. The most likely FCF candidate will be in the highest
19004 * priority group. When this routine is called it searches the fcf_pri list for
19005 * next lowest priority group and repopulates the rr_bmask with only those
19006 * fcf_indexes.
19007 * returns:
19008 * 1=success 0=failure
19009 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040019010static int
James Smart7d791df2011-07-22 18:37:52 -040019011lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
19012{
19013 uint16_t next_fcf_pri;
19014 uint16_t last_index;
19015 struct lpfc_fcf_pri *fcf_pri;
19016 int rc;
19017 int ret = 0;
19018
19019 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
19020 LPFC_SLI4_FCF_TBL_INDX_MAX);
19021 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19022 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050019023
19024 /* Verify the priority list has 2 or more entries */
19025 spin_lock_irq(&phba->hbalock);
19026 if (list_empty(&phba->fcf.fcf_pri_list) ||
19027 list_is_singular(&phba->fcf.fcf_pri_list)) {
19028 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040019029 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
19030 "3061 Last IDX %d\n", last_index);
19031 return 0; /* Empty rr list */
19032 }
James Smart25626692013-03-01 16:36:54 -050019033 spin_unlock_irq(&phba->hbalock);
19034
James Smart7d791df2011-07-22 18:37:52 -040019035 next_fcf_pri = 0;
19036 /*
19037 * Clear the rr_bmask and set all of the bits that are at this
19038 * priority.
19039 */
19040 memset(phba->fcf.fcf_rr_bmask, 0,
19041 sizeof(*phba->fcf.fcf_rr_bmask));
19042 spin_lock_irq(&phba->hbalock);
19043 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
19044 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
19045 continue;
19046 /*
19047 * the 1st priority that has not FLOGI failed
19048 * will be the highest.
19049 */
19050 if (!next_fcf_pri)
19051 next_fcf_pri = fcf_pri->fcf_rec.priority;
19052 spin_unlock_irq(&phba->hbalock);
19053 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
19054 rc = lpfc_sli4_fcf_rr_index_set(phba,
19055 fcf_pri->fcf_rec.fcf_index);
19056 if (rc)
19057 return 0;
19058 }
19059 spin_lock_irq(&phba->hbalock);
19060 }
19061 /*
19062 * if next_fcf_pri was not set above and the list is not empty then
19063 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070019064 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040019065 */
19066 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
19067 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
19068 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
19069 /*
19070 * the 1st priority that has not FLOGI failed
19071 * will be the highest.
19072 */
19073 if (!next_fcf_pri)
19074 next_fcf_pri = fcf_pri->fcf_rec.priority;
19075 spin_unlock_irq(&phba->hbalock);
19076 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
19077 rc = lpfc_sli4_fcf_rr_index_set(phba,
19078 fcf_pri->fcf_rec.fcf_index);
19079 if (rc)
19080 return 0;
19081 }
19082 spin_lock_irq(&phba->hbalock);
19083 }
19084 } else
19085 ret = 1;
19086 spin_unlock_irq(&phba->hbalock);
19087
19088 return ret;
19089}
19090/**
James Smart0c9ab6f2010-02-26 14:15:57 -050019091 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
19092 * @phba: pointer to lpfc hba data structure.
19093 *
19094 * This routine is to get the next eligible FCF record index in a round
19095 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040019096 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050019097 * shall be returned, otherwise, the next eligible FCF record's index
19098 * shall be returned.
19099 **/
19100uint16_t
19101lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
19102{
19103 uint16_t next_fcf_index;
19104
James Smart421c6622013-01-03 15:44:16 -050019105initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040019106 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050019107 next_fcf_index = phba->fcf.current_rec.fcf_indx;
19108
James Smart7d791df2011-07-22 18:37:52 -040019109next_priority:
James Smart421c6622013-01-03 15:44:16 -050019110 /* Determine the next fcf index to check */
19111 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050019112 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
19113 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040019114 next_fcf_index);
19115
James Smart0c9ab6f2010-02-26 14:15:57 -050019116 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040019117 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19118 /*
19119 * If we have wrapped then we need to clear the bits that
19120 * have been tested so that we can detect when we should
19121 * change the priority level.
19122 */
James Smart0c9ab6f2010-02-26 14:15:57 -050019123 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
19124 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040019125 }
19126
James Smart0c9ab6f2010-02-26 14:15:57 -050019127
James Smart3804dc82010-07-14 15:31:37 -040019128 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040019129 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
19130 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
19131 /*
19132 * If next fcf index is not found check if there are lower
19133 * Priority level fcf's in the fcf_priority list.
19134 * Set up the rr_bmask with all of the avaiable fcf bits
19135 * at that level and continue the selection process.
19136 */
19137 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050019138 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040019139 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
19140 "2844 No roundrobin failover FCF available\n");
James Smart036cad12018-10-23 13:41:06 -070019141
19142 return LPFC_FCOE_FCF_NEXT_NONE;
James Smart3804dc82010-07-14 15:31:37 -040019143 }
19144
James Smart7d791df2011-07-22 18:37:52 -040019145 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
19146 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050019147 LPFC_FCF_FLOGI_FAILED) {
19148 if (list_is_singular(&phba->fcf.fcf_pri_list))
19149 return LPFC_FCOE_FCF_NEXT_NONE;
19150
James Smart7d791df2011-07-22 18:37:52 -040019151 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050019152 }
James Smart7d791df2011-07-22 18:37:52 -040019153
James Smart3804dc82010-07-14 15:31:37 -040019154 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019155 "2845 Get next roundrobin failover FCF (x%x)\n",
19156 next_fcf_index);
19157
James Smart0c9ab6f2010-02-26 14:15:57 -050019158 return next_fcf_index;
19159}
19160
19161/**
19162 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
19163 * @phba: pointer to lpfc hba data structure.
19164 *
19165 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040019166 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050019167 * does not go beyond the range of the driver allocated bmask dimension
19168 * before setting the bit.
19169 *
19170 * Returns 0 if the index bit successfully set, otherwise, it returns
19171 * -EINVAL.
19172 **/
19173int
19174lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
19175{
19176 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19177 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019178 "2610 FCF (x%x) reached driver's book "
19179 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050019180 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
19181 return -EINVAL;
19182 }
19183 /* Set the eligible FCF record index bmask */
19184 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
19185
James Smart3804dc82010-07-14 15:31:37 -040019186 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019187 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040019188 "bmask\n", fcf_index);
19189
James Smart0c9ab6f2010-02-26 14:15:57 -050019190 return 0;
19191}
19192
19193/**
James Smart3804dc82010-07-14 15:31:37 -040019194 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050019195 * @phba: pointer to lpfc hba data structure.
19196 *
19197 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040019198 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050019199 * does not go beyond the range of the driver allocated bmask dimension
19200 * before clearing the bit.
19201 **/
19202void
19203lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
19204{
James Smart9a803a72013-09-06 12:17:56 -040019205 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050019206 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19207 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019208 "2762 FCF (x%x) reached driver's book "
19209 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050019210 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
19211 return;
19212 }
19213 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040019214 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040019215 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
19216 list) {
James Smart7d791df2011-07-22 18:37:52 -040019217 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
19218 list_del_init(&fcf_pri->list);
19219 break;
19220 }
19221 }
19222 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050019223 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040019224
19225 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019226 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040019227 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050019228}
19229
19230/**
James Smartecfd03c2010-02-12 14:41:27 -050019231 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
19232 * @phba: pointer to lpfc hba data structure.
19233 *
19234 * This routine is the completion routine for the rediscover FCF table mailbox
19235 * command. If the mailbox command returned failure, it will try to stop the
19236 * FCF rediscover wait timer.
19237 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040019238static void
James Smartecfd03c2010-02-12 14:41:27 -050019239lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
19240{
19241 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
19242 uint32_t shdr_status, shdr_add_status;
19243
19244 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
19245
19246 shdr_status = bf_get(lpfc_mbox_hdr_status,
19247 &redisc_fcf->header.cfg_shdr.response);
19248 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19249 &redisc_fcf->header.cfg_shdr.response);
19250 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050019251 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050019252 "2746 Requesting for FCF rediscovery failed "
19253 "status x%x add_status x%x\n",
19254 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050019255 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050019256 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050019257 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050019258 spin_unlock_irq(&phba->hbalock);
19259 /*
19260 * CVL event triggered FCF rediscover request failed,
19261 * last resort to re-try current registered FCF entry.
19262 */
19263 lpfc_retry_pport_discovery(phba);
19264 } else {
19265 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050019266 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050019267 spin_unlock_irq(&phba->hbalock);
19268 /*
19269 * DEAD FCF event triggered FCF rediscover request
19270 * failed, last resort to fail over as a link down
19271 * to FCF registration.
19272 */
19273 lpfc_sli4_fcf_dead_failthrough(phba);
19274 }
James Smart0c9ab6f2010-02-26 14:15:57 -050019275 } else {
19276 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019277 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050019278 /*
19279 * Start FCF rediscovery wait timer for pending FCF
19280 * before rescan FCF record table.
19281 */
19282 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050019283 }
James Smartecfd03c2010-02-12 14:41:27 -050019284
19285 mempool_free(mbox, phba->mbox_mem_pool);
19286}
19287
19288/**
James Smart3804dc82010-07-14 15:31:37 -040019289 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050019290 * @phba: pointer to lpfc hba data structure.
19291 *
19292 * This routine is invoked to request for rediscovery of the entire FCF table
19293 * by the port.
19294 **/
19295int
19296lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
19297{
19298 LPFC_MBOXQ_t *mbox;
19299 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
19300 int rc, length;
19301
James Smart0c9ab6f2010-02-26 14:15:57 -050019302 /* Cancel retry delay timers to all vports before FCF rediscover */
19303 lpfc_cancel_all_vport_retry_delay_timer(phba);
19304
James Smartecfd03c2010-02-12 14:41:27 -050019305 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19306 if (!mbox) {
19307 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19308 "2745 Failed to allocate mbox for "
19309 "requesting FCF rediscover.\n");
19310 return -ENOMEM;
19311 }
19312
19313 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
19314 sizeof(struct lpfc_sli4_cfg_mhdr));
19315 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
19316 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
19317 length, LPFC_SLI4_MBX_EMBED);
19318
19319 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
19320 /* Set count to 0 for invalidating the entire FCF database */
19321 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
19322
19323 /* Issue the mailbox command asynchronously */
19324 mbox->vport = phba->pport;
19325 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
19326 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
19327
19328 if (rc == MBX_NOT_FINISHED) {
19329 mempool_free(mbox, phba->mbox_mem_pool);
19330 return -EIO;
19331 }
19332 return 0;
19333}
19334
19335/**
James Smartfc2b9892010-02-26 14:15:29 -050019336 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
19337 * @phba: pointer to lpfc hba data structure.
19338 *
19339 * This function is the failover routine as a last resort to the FCF DEAD
19340 * event when driver failed to perform fast FCF failover.
19341 **/
19342void
19343lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
19344{
19345 uint32_t link_state;
19346
19347 /*
19348 * Last resort as FCF DEAD event failover will treat this as
19349 * a link down, but save the link state because we don't want
19350 * it to be changed to Link Down unless it is already down.
19351 */
19352 link_state = phba->link_state;
19353 lpfc_linkdown(phba);
19354 phba->link_state = link_state;
19355
19356 /* Unregister FCF if no devices connected to it */
19357 lpfc_unregister_unused_fcf(phba);
19358}
19359
19360/**
James Smart026abb82011-12-13 13:20:45 -050019361 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040019362 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050019363 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040019364 *
James Smart026abb82011-12-13 13:20:45 -050019365 * This function gets SLI3 port configure region 23 data through memory dump
19366 * mailbox command. When it successfully retrieves data, the size of the data
19367 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040019368 **/
James Smart026abb82011-12-13 13:20:45 -050019369static uint32_t
19370lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040019371{
19372 LPFC_MBOXQ_t *pmb = NULL;
19373 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050019374 uint32_t offset = 0;
Dick Kennedyd91e3ab2020-06-30 14:49:52 -070019375 int i, rc;
James Smarta0c87cb2009-07-19 10:01:10 -040019376
James Smart026abb82011-12-13 13:20:45 -050019377 if (!rgn23_data)
19378 return 0;
19379
James Smarta0c87cb2009-07-19 10:01:10 -040019380 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19381 if (!pmb) {
19382 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050019383 "2600 failed to allocate mailbox memory\n");
19384 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040019385 }
19386 mb = &pmb->u.mb;
19387
James Smarta0c87cb2009-07-19 10:01:10 -040019388 do {
19389 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
19390 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
19391
19392 if (rc != MBX_SUCCESS) {
19393 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050019394 "2601 failed to read config "
19395 "region 23, rc 0x%x Status 0x%x\n",
19396 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040019397 mb->un.varDmp.word_cnt = 0;
19398 }
19399 /*
19400 * dump mem may return a zero when finished or we got a
19401 * mailbox error, either way we are done.
19402 */
19403 if (mb->un.varDmp.word_cnt == 0)
19404 break;
James Smarta0c87cb2009-07-19 10:01:10 -040019405
Dick Kennedyd91e3ab2020-06-30 14:49:52 -070019406 i = mb->un.varDmp.word_cnt * sizeof(uint32_t);
19407 if (offset + i > DMP_RGN23_SIZE)
19408 i = DMP_RGN23_SIZE - offset;
James Smarta0c87cb2009-07-19 10:01:10 -040019409 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
Dick Kennedyd91e3ab2020-06-30 14:49:52 -070019410 rgn23_data + offset, i);
19411 offset += i;
19412 } while (offset < DMP_RGN23_SIZE);
James Smarta0c87cb2009-07-19 10:01:10 -040019413
James Smart026abb82011-12-13 13:20:45 -050019414 mempool_free(pmb, phba->mbox_mem_pool);
19415 return offset;
19416}
19417
19418/**
19419 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19420 * @phba: pointer to lpfc hba data structure.
19421 * @rgn23_data: pointer to configure region 23 data.
19422 *
19423 * This function gets SLI4 port configure region 23 data through memory dump
19424 * mailbox command. When it successfully retrieves data, the size of the data
19425 * will be returned, otherwise, 0 will be returned.
19426 **/
19427static uint32_t
19428lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19429{
19430 LPFC_MBOXQ_t *mboxq = NULL;
19431 struct lpfc_dmabuf *mp = NULL;
19432 struct lpfc_mqe *mqe;
19433 uint32_t data_length = 0;
19434 int rc;
19435
19436 if (!rgn23_data)
19437 return 0;
19438
19439 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19440 if (!mboxq) {
19441 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19442 "3105 failed to allocate mailbox memory\n");
19443 return 0;
19444 }
19445
19446 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19447 goto out;
19448 mqe = &mboxq->u.mqe;
James Smart3e1f0712018-11-29 16:09:29 -080019449 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smart026abb82011-12-13 13:20:45 -050019450 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19451 if (rc)
19452 goto out;
19453 data_length = mqe->un.mb_words[5];
19454 if (data_length == 0)
19455 goto out;
19456 if (data_length > DMP_RGN23_SIZE) {
19457 data_length = 0;
19458 goto out;
19459 }
19460 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19461out:
19462 mempool_free(mboxq, phba->mbox_mem_pool);
19463 if (mp) {
19464 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19465 kfree(mp);
19466 }
19467 return data_length;
19468}
19469
19470/**
19471 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19472 * @phba: pointer to lpfc hba data structure.
19473 *
19474 * This function read region 23 and parse TLV for port status to
19475 * decide if the user disaled the port. If the TLV indicates the
19476 * port is disabled, the hba_flag is set accordingly.
19477 **/
19478void
19479lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19480{
19481 uint8_t *rgn23_data = NULL;
19482 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19483 uint32_t offset = 0;
19484
19485 /* Get adapter Region 23 data */
19486 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19487 if (!rgn23_data)
19488 goto out;
19489
19490 if (phba->sli_rev < LPFC_SLI_REV4)
19491 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19492 else {
19493 if_type = bf_get(lpfc_sli_intf_if_type,
19494 &phba->sli4_hba.sli_intf);
19495 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19496 goto out;
19497 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19498 }
James Smarta0c87cb2009-07-19 10:01:10 -040019499
19500 if (!data_size)
19501 goto out;
19502
19503 /* Check the region signature first */
19504 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19506 "2619 Config region 23 has bad signature\n");
19507 goto out;
19508 }
19509 offset += 4;
19510
19511 /* Check the data structure version */
19512 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19513 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19514 "2620 Config region 23 has bad version\n");
19515 goto out;
19516 }
19517 offset += 4;
19518
19519 /* Parse TLV entries in the region */
19520 while (offset < data_size) {
19521 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19522 break;
19523 /*
19524 * If the TLV is not driver specific TLV or driver id is
19525 * not linux driver id, skip the record.
19526 */
19527 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19528 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19529 (rgn23_data[offset + 3] != 0)) {
19530 offset += rgn23_data[offset + 1] * 4 + 4;
19531 continue;
19532 }
19533
19534 /* Driver found a driver specific TLV in the config region */
19535 sub_tlv_len = rgn23_data[offset + 1] * 4;
19536 offset += 4;
19537 tlv_offset = 0;
19538
19539 /*
19540 * Search for configured port state sub-TLV.
19541 */
19542 while ((offset < data_size) &&
19543 (tlv_offset < sub_tlv_len)) {
19544 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19545 offset += 4;
19546 tlv_offset += 4;
19547 break;
19548 }
19549 if (rgn23_data[offset] != PORT_STE_TYPE) {
19550 offset += rgn23_data[offset + 1] * 4 + 4;
19551 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19552 continue;
19553 }
19554
19555 /* This HBA contains PORT_STE configured */
19556 if (!rgn23_data[offset + 2])
19557 phba->hba_flag |= LINK_DISABLED;
19558
19559 goto out;
19560 }
19561 }
James Smart026abb82011-12-13 13:20:45 -050019562
James Smarta0c87cb2009-07-19 10:01:10 -040019563out:
James Smarta0c87cb2009-07-19 10:01:10 -040019564 kfree(rgn23_data);
19565 return;
19566}
James Smart695a8142010-01-26 23:08:03 -050019567
19568/**
James Smart52d52442011-05-24 11:42:45 -040019569 * lpfc_wr_object - write an object to the firmware
19570 * @phba: HBA structure that indicates port to create a queue on.
19571 * @dmabuf_list: list of dmabufs to write to the port.
19572 * @size: the total byte value of the objects to write to the port.
19573 * @offset: the current offset to be used to start the transfer.
19574 *
19575 * This routine will create a wr_object mailbox command to send to the port.
19576 * the mailbox command will be constructed using the dma buffers described in
19577 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19578 * BDEs that the imbedded mailbox can support. The @offset variable will be
19579 * used to indicate the starting offset of the transfer and will also return
19580 * the offset after the write object mailbox has completed. @size is used to
19581 * determine the end of the object and whether the eof bit should be set.
19582 *
19583 * Return 0 is successful and offset will contain the the new offset to use
19584 * for the next write.
19585 * Return negative value for error cases.
19586 **/
19587int
19588lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19589 uint32_t size, uint32_t *offset)
19590{
19591 struct lpfc_mbx_wr_object *wr_object;
19592 LPFC_MBOXQ_t *mbox;
19593 int rc = 0, i = 0;
James Smartf3d0a8a2019-12-18 15:58:01 -080019594 uint32_t shdr_status, shdr_add_status, shdr_change_status, shdr_csf;
James Smart52d52442011-05-24 11:42:45 -040019595 uint32_t mbox_tmo;
James Smart52d52442011-05-24 11:42:45 -040019596 struct lpfc_dmabuf *dmabuf;
19597 uint32_t written = 0;
James Smart50212672018-12-13 15:17:57 -080019598 bool check_change_status = false;
James Smart52d52442011-05-24 11:42:45 -040019599
19600 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19601 if (!mbox)
19602 return -ENOMEM;
19603
19604 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19605 LPFC_MBOX_OPCODE_WRITE_OBJECT,
19606 sizeof(struct lpfc_mbx_wr_object) -
19607 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19608
19609 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19610 wr_object->u.request.write_offset = *offset;
19611 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19612 wr_object->u.request.object_name[0] =
19613 cpu_to_le32(wr_object->u.request.object_name[0]);
19614 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19615 list_for_each_entry(dmabuf, dmabuf_list, list) {
19616 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19617 break;
19618 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19619 wr_object->u.request.bde[i].addrHigh =
19620 putPaddrHigh(dmabuf->phys);
19621 if (written + SLI4_PAGE_SIZE >= size) {
19622 wr_object->u.request.bde[i].tus.f.bdeSize =
19623 (size - written);
19624 written += (size - written);
19625 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
James Smart50212672018-12-13 15:17:57 -080019626 bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
19627 check_change_status = true;
James Smart52d52442011-05-24 11:42:45 -040019628 } else {
19629 wr_object->u.request.bde[i].tus.f.bdeSize =
19630 SLI4_PAGE_SIZE;
19631 written += SLI4_PAGE_SIZE;
19632 }
19633 i++;
19634 }
19635 wr_object->u.request.bde_count = i;
19636 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19637 if (!phba->sli4_hba.intr_enable)
19638 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19639 else {
James Smarta183a152011-10-10 21:32:43 -040019640 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040019641 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19642 }
19643 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart50212672018-12-13 15:17:57 -080019644 shdr_status = bf_get(lpfc_mbox_hdr_status,
19645 &wr_object->header.cfg_shdr.response);
19646 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19647 &wr_object->header.cfg_shdr.response);
19648 if (check_change_status) {
19649 shdr_change_status = bf_get(lpfc_wr_object_change_status,
19650 &wr_object->u.response);
James Smartf3d0a8a2019-12-18 15:58:01 -080019651
19652 if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
19653 shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
19654 shdr_csf = bf_get(lpfc_wr_object_csf,
19655 &wr_object->u.response);
19656 if (shdr_csf)
19657 shdr_change_status =
19658 LPFC_CHANGE_STATUS_PCI_RESET;
19659 }
19660
James Smart50212672018-12-13 15:17:57 -080019661 switch (shdr_change_status) {
19662 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
19663 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19664 "3198 Firmware write complete: System "
19665 "reboot required to instantiate\n");
19666 break;
19667 case (LPFC_CHANGE_STATUS_FW_RESET):
19668 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19669 "3199 Firmware write complete: Firmware"
19670 " reset required to instantiate\n");
19671 break;
19672 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
19673 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19674 "3200 Firmware write complete: Port "
19675 "Migration or PCI Reset required to "
19676 "instantiate\n");
19677 break;
19678 case (LPFC_CHANGE_STATUS_PCI_RESET):
19679 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19680 "3201 Firmware write complete: PCI "
19681 "Reset required to instantiate\n");
19682 break;
19683 default:
19684 break;
19685 }
19686 }
James Smart52d52442011-05-24 11:42:45 -040019687 if (rc != MBX_TIMEOUT)
19688 mempool_free(mbox, phba->mbox_mem_pool);
19689 if (shdr_status || shdr_add_status || rc) {
19690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19691 "3025 Write Object mailbox failed with "
19692 "status x%x add_status x%x, mbx status x%x\n",
19693 shdr_status, shdr_add_status, rc);
19694 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080019695 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040019696 } else
19697 *offset += wr_object->u.response.actual_write_length;
19698 return rc;
19699}
19700
19701/**
James Smart695a8142010-01-26 23:08:03 -050019702 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
19703 * @vport: pointer to vport data structure.
19704 *
19705 * This function iterate through the mailboxq and clean up all REG_LOGIN
19706 * and REG_VPI mailbox commands associated with the vport. This function
19707 * is called when driver want to restart discovery of the vport due to
19708 * a Clear Virtual Link event.
19709 **/
19710void
19711lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
19712{
19713 struct lpfc_hba *phba = vport->phba;
19714 LPFC_MBOXQ_t *mb, *nextmb;
19715 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040019716 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040019717 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040019718 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040019719 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050019720 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050019721
James Smartd439d282010-09-29 11:18:45 -040019722 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050019723 spin_lock_irq(&phba->hbalock);
19724 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
19725 if (mb->vport != vport)
19726 continue;
19727
19728 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19729 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19730 continue;
19731
James Smartd439d282010-09-29 11:18:45 -040019732 list_del(&mb->list);
19733 list_add_tail(&mb->list, &mbox_cmd_list);
19734 }
19735 /* Clean up active mailbox command with the vport */
19736 mb = phba->sli.mbox_active;
19737 if (mb && (mb->vport == vport)) {
19738 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
19739 (mb->u.mb.mbxCommand == MBX_REG_VPI))
19740 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19741 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019742 act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -040019743 /* Put reference count for delayed processing */
19744 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19745 /* Unregister the RPI when mailbox complete */
19746 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19747 }
19748 }
James Smart63e801c2010-11-20 23:14:19 -050019749 /* Cleanup any mailbox completions which are not yet processed */
19750 do {
19751 restart_loop = 0;
19752 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19753 /*
19754 * If this mailox is already processed or it is
19755 * for another vport ignore it.
19756 */
19757 if ((mb->vport != vport) ||
19758 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19759 continue;
19760
19761 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19762 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19763 continue;
19764
19765 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19766 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019767 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smart63e801c2010-11-20 23:14:19 -050019768 /* Unregister the RPI when mailbox complete */
19769 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19770 restart_loop = 1;
19771 spin_unlock_irq(&phba->hbalock);
19772 spin_lock(shost->host_lock);
19773 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19774 spin_unlock(shost->host_lock);
19775 spin_lock_irq(&phba->hbalock);
19776 break;
19777 }
19778 }
19779 } while (restart_loop);
19780
James Smartd439d282010-09-29 11:18:45 -040019781 spin_unlock_irq(&phba->hbalock);
19782
19783 /* Release the cleaned-up mailbox commands */
19784 while (!list_empty(&mbox_cmd_list)) {
19785 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050019786 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019787 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
James Smart695a8142010-01-26 23:08:03 -050019788 if (mp) {
19789 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19790 kfree(mp);
19791 }
James Smart3e1f0712018-11-29 16:09:29 -080019792 mb->ctx_buf = NULL;
19793 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
19794 mb->ctx_ndlp = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019795 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019796 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019797 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019798 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019799 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019800 }
James Smart695a8142010-01-26 23:08:03 -050019801 }
James Smart695a8142010-01-26 23:08:03 -050019802 mempool_free(mb, phba->mbox_mem_pool);
19803 }
James Smartd439d282010-09-29 11:18:45 -040019804
19805 /* Release the ndlp with the cleaned-up active mailbox command */
19806 if (act_mbx_ndlp) {
19807 spin_lock(shost->host_lock);
19808 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19809 spin_unlock(shost->host_lock);
19810 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019811 }
James Smart695a8142010-01-26 23:08:03 -050019812}
19813
James Smart2a9bf3d2010-06-07 15:24:45 -040019814/**
19815 * lpfc_drain_txq - Drain the txq
19816 * @phba: Pointer to HBA context object.
19817 *
19818 * This function attempt to submit IOCBs on the txq
19819 * to the adapter. For SLI4 adapters, the txq contains
19820 * ELS IOCBs that have been deferred because the there
19821 * are no SGLs. This congestion can occur with large
19822 * vport counts during node discovery.
19823 **/
19824
19825uint32_t
19826lpfc_drain_txq(struct lpfc_hba *phba)
19827{
19828 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019829 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019830 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019831 unsigned long iflags = 0;
19832 char *fail_msg = NULL;
19833 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019834 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019835 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070019836 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040019837
James Smartdc19e3b2018-05-24 21:08:57 -070019838 if (phba->link_flag & LS_MDS_LOOPBACK) {
19839 /* MDS WQE are posted only to first WQ*/
James Smartc00f62e2019-08-14 16:57:11 -070019840 wq = phba->sli4_hba.hdwq[0].io_wq;
James Smartdc19e3b2018-05-24 21:08:57 -070019841 if (unlikely(!wq))
19842 return 0;
19843 pring = wq->pring;
19844 } else {
19845 wq = phba->sli4_hba.els_wq;
19846 if (unlikely(!wq))
19847 return 0;
19848 pring = lpfc_phba_elsring(phba);
19849 }
19850
19851 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019852 return 0;
James Smart895427b2017-02-12 13:52:30 -080019853
James Smart398d81c2013-05-31 17:04:19 -040019854 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019855 list_for_each_entry(piocbq, &pring->txq, list) {
19856 txq_cnt++;
19857 }
19858
19859 if (txq_cnt > pring->txq_max)
19860 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019861
James Smart398d81c2013-05-31 17:04:19 -040019862 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019863
James Smart0e9bb8d2013-03-01 16:35:12 -050019864 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019865 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019866
James Smart19ca7602010-11-20 23:11:55 -050019867 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019868 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019869 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019870 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19871 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019872 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019873 break;
19874 }
James Smart895427b2017-02-12 13:52:30 -080019875 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019876 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019877 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019878 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019879 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019880 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019881 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019882
19883 /* The xri and iocb resources secured,
19884 * attempt to issue request
19885 */
James Smart6d368e52011-05-24 11:44:12 -040019886 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019887 piocbq->sli4_xritag = sglq->sli4_xritag;
19888 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19889 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019890 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019891 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070019892 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019893 fail_msg = " - Wq is full";
19894 else
19895 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19896
19897 if (fail_msg) {
19898 /* Failed means we can't issue and need to cancel */
19899 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19900 "2822 IOCB failed %s iotag 0x%x "
19901 "xri 0x%x\n",
19902 fail_msg,
19903 piocbq->iotag, piocbq->sli4_xritag);
19904 list_add_tail(&piocbq->list, &completions);
19905 }
James Smart398d81c2013-05-31 17:04:19 -040019906 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019907 }
19908
James Smart2a9bf3d2010-06-07 15:24:45 -040019909 /* Cancel all the IOCBs that cannot be issued */
19910 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19911 IOERR_SLI_ABORTED);
19912
James Smart0e9bb8d2013-03-01 16:35:12 -050019913 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019914}
James Smart895427b2017-02-12 13:52:30 -080019915
19916/**
19917 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19918 * @phba: Pointer to HBA context object.
19919 * @pwqe: Pointer to command WQE.
19920 * @sglq: Pointer to the scatter gather queue object.
19921 *
19922 * This routine converts the bpl or bde that is in the WQE
19923 * to a sgl list for the sli4 hardware. The physical address
19924 * of the bpl/bde is converted back to a virtual address.
19925 * If the WQE contains a BPL then the list of BDE's is
19926 * converted to sli4_sge's. If the WQE contains a single
19927 * BDE then it is converted to a single sli_sge.
19928 * The WQE is still in cpu endianness so the contents of
19929 * the bpl can be used without byte swapping.
19930 *
19931 * Returns valid XRI = Success, NO_XRI = Failure.
19932 */
19933static uint16_t
19934lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19935 struct lpfc_sglq *sglq)
19936{
19937 uint16_t xritag = NO_XRI;
19938 struct ulp_bde64 *bpl = NULL;
19939 struct ulp_bde64 bde;
19940 struct sli4_sge *sgl = NULL;
19941 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080019942 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080019943 int numBdes = 0;
19944 int i = 0;
19945 uint32_t offset = 0; /* accumulated offset in the sg request list */
19946 int inbound = 0; /* number of sg reply entries inbound from firmware */
19947 uint32_t cmd;
19948
19949 if (!pwqeq || !sglq)
19950 return xritag;
19951
19952 sgl = (struct sli4_sge *)sglq->sgl;
19953 wqe = &pwqeq->wqe;
19954 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19955
19956 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19957 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19958 return sglq->sli4_xritag;
19959 numBdes = pwqeq->rsvd2;
19960 if (numBdes) {
19961 /* The addrHigh and addrLow fields within the WQE
19962 * have not been byteswapped yet so there is no
19963 * need to swap them back.
19964 */
19965 if (pwqeq->context3)
19966 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19967 else
19968 return xritag;
19969
19970 bpl = (struct ulp_bde64 *)dmabuf->virt;
19971 if (!bpl)
19972 return xritag;
19973
19974 for (i = 0; i < numBdes; i++) {
19975 /* Should already be byte swapped. */
19976 sgl->addr_hi = bpl->addrHigh;
19977 sgl->addr_lo = bpl->addrLow;
19978
19979 sgl->word2 = le32_to_cpu(sgl->word2);
19980 if ((i+1) == numBdes)
19981 bf_set(lpfc_sli4_sge_last, sgl, 1);
19982 else
19983 bf_set(lpfc_sli4_sge_last, sgl, 0);
19984 /* swap the size field back to the cpu so we
19985 * can assign it to the sgl.
19986 */
19987 bde.tus.w = le32_to_cpu(bpl->tus.w);
19988 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19989 /* The offsets in the sgl need to be accumulated
19990 * separately for the request and reply lists.
19991 * The request is always first, the reply follows.
19992 */
19993 switch (cmd) {
19994 case CMD_GEN_REQUEST64_WQE:
19995 /* add up the reply sg entries */
19996 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19997 inbound++;
19998 /* first inbound? reset the offset */
19999 if (inbound == 1)
20000 offset = 0;
20001 bf_set(lpfc_sli4_sge_offset, sgl, offset);
20002 bf_set(lpfc_sli4_sge_type, sgl,
20003 LPFC_SGE_TYPE_DATA);
20004 offset += bde.tus.f.bdeSize;
20005 break;
20006 case CMD_FCP_TRSP64_WQE:
20007 bf_set(lpfc_sli4_sge_offset, sgl, 0);
20008 bf_set(lpfc_sli4_sge_type, sgl,
20009 LPFC_SGE_TYPE_DATA);
20010 break;
20011 case CMD_FCP_TSEND64_WQE:
20012 case CMD_FCP_TRECEIVE64_WQE:
20013 bf_set(lpfc_sli4_sge_type, sgl,
20014 bpl->tus.f.bdeFlags);
20015 if (i < 3)
20016 offset = 0;
20017 else
20018 offset += bde.tus.f.bdeSize;
20019 bf_set(lpfc_sli4_sge_offset, sgl, offset);
20020 break;
20021 }
20022 sgl->word2 = cpu_to_le32(sgl->word2);
20023 bpl++;
20024 sgl++;
20025 }
20026 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
20027 /* The addrHigh and addrLow fields of the BDE have not
20028 * been byteswapped yet so they need to be swapped
20029 * before putting them in the sgl.
20030 */
20031 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
20032 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
20033 sgl->word2 = le32_to_cpu(sgl->word2);
20034 bf_set(lpfc_sli4_sge_last, sgl, 1);
20035 sgl->word2 = cpu_to_le32(sgl->word2);
20036 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
20037 }
20038 return sglq->sli4_xritag;
20039}
20040
20041/**
20042 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
20043 * @phba: Pointer to HBA context object.
20044 * @ring_number: Base sli ring number
20045 * @pwqe: Pointer to command WQE.
20046 **/
20047int
James Smart1fbf9742019-01-28 11:14:26 -080020048lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
James Smart895427b2017-02-12 13:52:30 -080020049 struct lpfc_iocbq *pwqe)
20050{
James Smart205e8242018-03-05 12:04:03 -080020051 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smart7cacae22020-03-31 09:50:03 -070020052 struct lpfc_async_xchg_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080020053 struct lpfc_queue *wq;
20054 struct lpfc_sglq *sglq;
20055 struct lpfc_sli_ring *pring;
20056 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070020057 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080020058
20059 /* NVME_LS and NVME_LS ABTS requests. */
20060 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
20061 pring = phba->sli4_hba.nvmels_wq->pring;
James Smart6a828b02019-01-28 11:14:31 -080020062 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20063 qp, wq_access);
James Smart895427b2017-02-12 13:52:30 -080020064 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
20065 if (!sglq) {
20066 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20067 return WQE_BUSY;
20068 }
20069 pwqe->sli4_lxritag = sglq->sli4_lxritag;
20070 pwqe->sli4_xritag = sglq->sli4_xritag;
20071 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
20072 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20073 return WQE_ERROR;
20074 }
20075 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
20076 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070020077 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
20078 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080020079 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070020080 return ret;
James Smart895427b2017-02-12 13:52:30 -080020081 }
Dick Kennedycd22d602017-08-23 16:55:35 -070020082
James Smart895427b2017-02-12 13:52:30 -080020083 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20084 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080020085
20086 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smart895427b2017-02-12 13:52:30 -080020087 return 0;
20088 }
20089
20090 /* NVME_FCREQ and NVME_ABTS requests */
20091 if (pwqe->iocb_flag & LPFC_IO_NVME) {
20092 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070020093 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080020094 pring = wq->pring;
James Smart895427b2017-02-12 13:52:30 -080020095
James Smartc00f62e2019-08-14 16:57:11 -070020096 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart895427b2017-02-12 13:52:30 -080020097
James Smart6a828b02019-01-28 11:14:31 -080020098 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20099 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070020100 ret = lpfc_sli4_wq_put(wq, wqe);
20101 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080020102 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070020103 return ret;
James Smart895427b2017-02-12 13:52:30 -080020104 }
20105 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20106 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080020107
20108 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smart895427b2017-02-12 13:52:30 -080020109 return 0;
20110 }
20111
James Smartf358dd02017-02-12 13:52:34 -080020112 /* NVMET requests */
20113 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
20114 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070020115 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080020116 pring = wq->pring;
James Smartf358dd02017-02-12 13:52:34 -080020117
James Smartf358dd02017-02-12 13:52:34 -080020118 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070020119 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080020120 if (pwqe->sli4_xritag == NO_XRI) {
20121 pwqe->sli4_lxritag = sglq->sli4_lxritag;
20122 pwqe->sli4_xritag = sglq->sli4_xritag;
20123 }
20124 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
20125 pwqe->sli4_xritag);
James Smartc00f62e2019-08-14 16:57:11 -070020126 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart1fbf9742019-01-28 11:14:26 -080020127
James Smart6a828b02019-01-28 11:14:31 -080020128 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20129 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070020130 ret = lpfc_sli4_wq_put(wq, wqe);
20131 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080020132 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070020133 return ret;
James Smartf358dd02017-02-12 13:52:34 -080020134 }
20135 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20136 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080020137
20138 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smartf358dd02017-02-12 13:52:34 -080020139 return 0;
20140 }
James Smart895427b2017-02-12 13:52:30 -080020141 return WQE_ERROR;
20142}
James Smartc4908502019-01-28 11:14:28 -080020143
20144#ifdef LPFC_MXP_STAT
20145/**
20146 * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
20147 * @phba: pointer to lpfc hba data structure.
20148 * @hwqid: belong to which HWQ.
20149 *
20150 * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
20151 * 15 seconds after a test case is running.
20152 *
20153 * The user should call lpfc_debugfs_multixripools_write before running a test
20154 * case to clear stat_snapshot_taken. Then the user starts a test case. During
20155 * test case is running, stat_snapshot_taken is incremented by 1 every time when
20156 * this routine is called from heartbeat timer. When stat_snapshot_taken is
20157 * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
20158 **/
20159void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
20160{
20161 struct lpfc_sli4_hdw_queue *qp;
20162 struct lpfc_multixri_pool *multixri_pool;
20163 struct lpfc_pvt_pool *pvt_pool;
20164 struct lpfc_pbl_pool *pbl_pool;
20165 u32 txcmplq_cnt;
20166
20167 qp = &phba->sli4_hba.hdwq[hwqid];
20168 multixri_pool = qp->p_multixri_pool;
20169 if (!multixri_pool)
20170 return;
20171
20172 if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
20173 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20174 pbl_pool = &qp->p_multixri_pool->pbl_pool;
James Smartc00f62e2019-08-14 16:57:11 -070020175 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080020176
20177 multixri_pool->stat_pbl_count = pbl_pool->count;
20178 multixri_pool->stat_pvt_count = pvt_pool->count;
20179 multixri_pool->stat_busy_count = txcmplq_cnt;
20180 }
20181
20182 multixri_pool->stat_snapshot_taken++;
20183}
20184#endif
20185
20186/**
20187 * lpfc_adjust_pvt_pool_count - Adjust private pool count
20188 * @phba: pointer to lpfc hba data structure.
20189 * @hwqid: belong to which HWQ.
20190 *
20191 * This routine moves some XRIs from private to public pool when private pool
20192 * is not busy.
20193 **/
20194void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
20195{
20196 struct lpfc_multixri_pool *multixri_pool;
20197 u32 io_req_count;
20198 u32 prev_io_req_count;
20199
20200 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20201 if (!multixri_pool)
20202 return;
20203 io_req_count = multixri_pool->io_req_count;
20204 prev_io_req_count = multixri_pool->prev_io_req_count;
20205
20206 if (prev_io_req_count != io_req_count) {
20207 /* Private pool is busy */
20208 multixri_pool->prev_io_req_count = io_req_count;
20209 } else {
20210 /* Private pool is not busy.
20211 * Move XRIs from private to public pool.
20212 */
20213 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
20214 }
20215}
20216
20217/**
20218 * lpfc_adjust_high_watermark - Adjust high watermark
20219 * @phba: pointer to lpfc hba data structure.
20220 * @hwqid: belong to which HWQ.
20221 *
20222 * This routine sets high watermark as number of outstanding XRIs,
20223 * but make sure the new value is between xri_limit/2 and xri_limit.
20224 **/
20225void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
20226{
20227 u32 new_watermark;
20228 u32 watermark_max;
20229 u32 watermark_min;
20230 u32 xri_limit;
20231 u32 txcmplq_cnt;
20232 u32 abts_io_bufs;
20233 struct lpfc_multixri_pool *multixri_pool;
20234 struct lpfc_sli4_hdw_queue *qp;
20235
20236 qp = &phba->sli4_hba.hdwq[hwqid];
20237 multixri_pool = qp->p_multixri_pool;
20238 if (!multixri_pool)
20239 return;
20240 xri_limit = multixri_pool->xri_limit;
20241
20242 watermark_max = xri_limit;
20243 watermark_min = xri_limit / 2;
20244
James Smartc00f62e2019-08-14 16:57:11 -070020245 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080020246 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070020247 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080020248
20249 new_watermark = txcmplq_cnt + abts_io_bufs;
20250 new_watermark = min(watermark_max, new_watermark);
20251 new_watermark = max(watermark_min, new_watermark);
20252 multixri_pool->pvt_pool.high_watermark = new_watermark;
20253
20254#ifdef LPFC_MXP_STAT
20255 multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
20256 new_watermark);
20257#endif
20258}
20259
20260/**
20261 * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
20262 * @phba: pointer to lpfc hba data structure.
20263 * @hwqid: belong to which HWQ.
20264 *
20265 * This routine is called from hearbeat timer when pvt_pool is idle.
20266 * All free XRIs are moved from private to public pool on hwqid with 2 steps.
20267 * The first step moves (all - low_watermark) amount of XRIs.
20268 * The second step moves the rest of XRIs.
20269 **/
20270void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
20271{
20272 struct lpfc_pbl_pool *pbl_pool;
20273 struct lpfc_pvt_pool *pvt_pool;
James Smart6a828b02019-01-28 11:14:31 -080020274 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080020275 struct lpfc_io_buf *lpfc_ncmd;
20276 struct lpfc_io_buf *lpfc_ncmd_next;
20277 unsigned long iflag;
20278 struct list_head tmp_list;
20279 u32 tmp_count;
20280
James Smart6a828b02019-01-28 11:14:31 -080020281 qp = &phba->sli4_hba.hdwq[hwqid];
20282 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20283 pvt_pool = &qp->p_multixri_pool->pvt_pool;
James Smartc4908502019-01-28 11:14:28 -080020284 tmp_count = 0;
20285
James Smart6a828b02019-01-28 11:14:31 -080020286 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
20287 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020288
20289 if (pvt_pool->count > pvt_pool->low_watermark) {
20290 /* Step 1: move (all - low_watermark) from pvt_pool
20291 * to pbl_pool
20292 */
20293
20294 /* Move low watermark of bufs from pvt_pool to tmp_list */
20295 INIT_LIST_HEAD(&tmp_list);
20296 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20297 &pvt_pool->list, list) {
20298 list_move_tail(&lpfc_ncmd->list, &tmp_list);
20299 tmp_count++;
20300 if (tmp_count >= pvt_pool->low_watermark)
20301 break;
20302 }
20303
20304 /* Move all bufs from pvt_pool to pbl_pool */
20305 list_splice_init(&pvt_pool->list, &pbl_pool->list);
20306
20307 /* Move all bufs from tmp_list to pvt_pool */
20308 list_splice(&tmp_list, &pvt_pool->list);
20309
20310 pbl_pool->count += (pvt_pool->count - tmp_count);
20311 pvt_pool->count = tmp_count;
20312 } else {
20313 /* Step 2: move the rest from pvt_pool to pbl_pool */
20314 list_splice_init(&pvt_pool->list, &pbl_pool->list);
20315 pbl_pool->count += pvt_pool->count;
20316 pvt_pool->count = 0;
20317 }
20318
20319 spin_unlock(&pvt_pool->lock);
20320 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20321}
20322
20323/**
20324 * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
20325 * @phba: pointer to lpfc hba data structure
20326 * @pbl_pool: specified public free XRI pool
20327 * @pvt_pool: specified private free XRI pool
20328 * @count: number of XRIs to move
20329 *
20330 * This routine tries to move some free common bufs from the specified pbl_pool
20331 * to the specified pvt_pool. It might move less than count XRIs if there's not
20332 * enough in public pool.
20333 *
20334 * Return:
20335 * true - if XRIs are successfully moved from the specified pbl_pool to the
20336 * specified pvt_pool
20337 * false - if the specified pbl_pool is empty or locked by someone else
20338 **/
20339static bool
James Smart6a828b02019-01-28 11:14:31 -080020340_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
20341 struct lpfc_pbl_pool *pbl_pool,
James Smartc4908502019-01-28 11:14:28 -080020342 struct lpfc_pvt_pool *pvt_pool, u32 count)
20343{
20344 struct lpfc_io_buf *lpfc_ncmd;
20345 struct lpfc_io_buf *lpfc_ncmd_next;
20346 unsigned long iflag;
20347 int ret;
20348
20349 ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
20350 if (ret) {
20351 if (pbl_pool->count) {
20352 /* Move a batch of XRIs from public to private pool */
James Smart6a828b02019-01-28 11:14:31 -080020353 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020354 list_for_each_entry_safe(lpfc_ncmd,
20355 lpfc_ncmd_next,
20356 &pbl_pool->list,
20357 list) {
20358 list_move_tail(&lpfc_ncmd->list,
20359 &pvt_pool->list);
20360 pvt_pool->count++;
20361 pbl_pool->count--;
20362 count--;
20363 if (count == 0)
20364 break;
20365 }
20366
20367 spin_unlock(&pvt_pool->lock);
20368 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20369 return true;
20370 }
20371 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20372 }
20373
20374 return false;
20375}
20376
20377/**
20378 * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
20379 * @phba: pointer to lpfc hba data structure.
20380 * @hwqid: belong to which HWQ.
20381 * @count: number of XRIs to move
20382 *
20383 * This routine tries to find some free common bufs in one of public pools with
20384 * Round Robin method. The search always starts from local hwqid, then the next
20385 * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
20386 * a batch of free common bufs are moved to private pool on hwqid.
20387 * It might move less than count XRIs if there's not enough in public pool.
20388 **/
20389void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
20390{
20391 struct lpfc_multixri_pool *multixri_pool;
20392 struct lpfc_multixri_pool *next_multixri_pool;
20393 struct lpfc_pvt_pool *pvt_pool;
20394 struct lpfc_pbl_pool *pbl_pool;
James Smart6a828b02019-01-28 11:14:31 -080020395 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080020396 u32 next_hwqid;
20397 u32 hwq_count;
20398 int ret;
20399
James Smart6a828b02019-01-28 11:14:31 -080020400 qp = &phba->sli4_hba.hdwq[hwqid];
20401 multixri_pool = qp->p_multixri_pool;
James Smartc4908502019-01-28 11:14:28 -080020402 pvt_pool = &multixri_pool->pvt_pool;
20403 pbl_pool = &multixri_pool->pbl_pool;
20404
20405 /* Check if local pbl_pool is available */
James Smart6a828b02019-01-28 11:14:31 -080020406 ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020407 if (ret) {
20408#ifdef LPFC_MXP_STAT
20409 multixri_pool->local_pbl_hit_count++;
20410#endif
20411 return;
20412 }
20413
20414 hwq_count = phba->cfg_hdw_queue;
20415
20416 /* Get the next hwqid which was found last time */
20417 next_hwqid = multixri_pool->rrb_next_hwqid;
20418
20419 do {
20420 /* Go to next hwq */
20421 next_hwqid = (next_hwqid + 1) % hwq_count;
20422
20423 next_multixri_pool =
20424 phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
20425 pbl_pool = &next_multixri_pool->pbl_pool;
20426
20427 /* Check if the public free xri pool is available */
20428 ret = _lpfc_move_xri_pbl_to_pvt(
James Smart6a828b02019-01-28 11:14:31 -080020429 phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020430
20431 /* Exit while-loop if success or all hwqid are checked */
20432 } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
20433
20434 /* Starting point for the next time */
20435 multixri_pool->rrb_next_hwqid = next_hwqid;
20436
20437 if (!ret) {
20438 /* stats: all public pools are empty*/
20439 multixri_pool->pbl_empty_count++;
20440 }
20441
20442#ifdef LPFC_MXP_STAT
20443 if (ret) {
20444 if (next_hwqid == hwqid)
20445 multixri_pool->local_pbl_hit_count++;
20446 else
20447 multixri_pool->other_pbl_hit_count++;
20448 }
20449#endif
20450}
20451
20452/**
20453 * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
20454 * @phba: pointer to lpfc hba data structure.
20455 * @qp: belong to which HWQ.
20456 *
20457 * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
20458 * low watermark.
20459 **/
20460void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
20461{
20462 struct lpfc_multixri_pool *multixri_pool;
20463 struct lpfc_pvt_pool *pvt_pool;
20464
20465 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20466 pvt_pool = &multixri_pool->pvt_pool;
20467
20468 if (pvt_pool->count < pvt_pool->low_watermark)
20469 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20470}
20471
20472/**
20473 * lpfc_release_io_buf - Return one IO buf back to free pool
20474 * @phba: pointer to lpfc hba data structure.
20475 * @lpfc_ncmd: IO buf to be returned.
20476 * @qp: belong to which HWQ.
20477 *
20478 * This routine returns one IO buf back to free pool. If this is an urgent IO,
20479 * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
20480 * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
20481 * xri_limit. If cfg_xri_rebalancing==0, the IO buf is returned to
20482 * lpfc_io_buf_list_put.
20483 **/
20484void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
20485 struct lpfc_sli4_hdw_queue *qp)
20486{
20487 unsigned long iflag;
20488 struct lpfc_pbl_pool *pbl_pool;
20489 struct lpfc_pvt_pool *pvt_pool;
20490 struct lpfc_epd_pool *epd_pool;
20491 u32 txcmplq_cnt;
20492 u32 xri_owned;
20493 u32 xri_limit;
20494 u32 abts_io_bufs;
20495
20496 /* MUST zero fields if buffer is reused by another protocol */
20497 lpfc_ncmd->nvmeCmd = NULL;
20498 lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
20499 lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
20500
James Smart35a635a2019-09-21 20:59:02 -070020501 if (phba->cfg_xpsgl && !phba->nvmet_support &&
20502 !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
20503 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
20504
20505 if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
20506 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
20507
James Smartc4908502019-01-28 11:14:28 -080020508 if (phba->cfg_xri_rebalancing) {
20509 if (lpfc_ncmd->expedite) {
20510 /* Return to expedite pool */
20511 epd_pool = &phba->epd_pool;
20512 spin_lock_irqsave(&epd_pool->lock, iflag);
20513 list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
20514 epd_pool->count++;
20515 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20516 return;
20517 }
20518
20519 /* Avoid invalid access if an IO sneaks in and is being rejected
20520 * just _after_ xri pools are destroyed in lpfc_offline.
20521 * Nothing much can be done at this point.
20522 */
20523 if (!qp->p_multixri_pool)
20524 return;
20525
20526 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20527 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20528
James Smartc00f62e2019-08-14 16:57:11 -070020529 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080020530 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070020531 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080020532
20533 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
20534 xri_limit = qp->p_multixri_pool->xri_limit;
20535
20536#ifdef LPFC_MXP_STAT
20537 if (xri_owned <= xri_limit)
20538 qp->p_multixri_pool->below_limit_count++;
20539 else
20540 qp->p_multixri_pool->above_limit_count++;
20541#endif
20542
20543 /* XRI goes to either public or private free xri pool
20544 * based on watermark and xri_limit
20545 */
20546 if ((pvt_pool->count < pvt_pool->low_watermark) ||
20547 (xri_owned < xri_limit &&
20548 pvt_pool->count < pvt_pool->high_watermark)) {
James Smart6a828b02019-01-28 11:14:31 -080020549 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
20550 qp, free_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020551 list_add_tail(&lpfc_ncmd->list,
20552 &pvt_pool->list);
20553 pvt_pool->count++;
20554 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20555 } else {
James Smart6a828b02019-01-28 11:14:31 -080020556 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
20557 qp, free_pub_pool);
James Smartc4908502019-01-28 11:14:28 -080020558 list_add_tail(&lpfc_ncmd->list,
20559 &pbl_pool->list);
20560 pbl_pool->count++;
20561 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20562 }
20563 } else {
James Smart6a828b02019-01-28 11:14:31 -080020564 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
20565 qp, free_xri);
James Smartc4908502019-01-28 11:14:28 -080020566 list_add_tail(&lpfc_ncmd->list,
20567 &qp->lpfc_io_buf_list_put);
20568 qp->put_io_bufs++;
20569 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
20570 iflag);
20571 }
20572}
20573
20574/**
20575 * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
20576 * @phba: pointer to lpfc hba data structure.
20577 * @pvt_pool: pointer to private pool data structure.
20578 * @ndlp: pointer to lpfc nodelist data structure.
20579 *
20580 * This routine tries to get one free IO buf from private pool.
20581 *
20582 * Return:
20583 * pointer to one free IO buf - if private pool is not empty
20584 * NULL - if private pool is empty
20585 **/
20586static struct lpfc_io_buf *
20587lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
James Smart6a828b02019-01-28 11:14:31 -080020588 struct lpfc_sli4_hdw_queue *qp,
James Smartc4908502019-01-28 11:14:28 -080020589 struct lpfc_pvt_pool *pvt_pool,
20590 struct lpfc_nodelist *ndlp)
20591{
20592 struct lpfc_io_buf *lpfc_ncmd;
20593 struct lpfc_io_buf *lpfc_ncmd_next;
20594 unsigned long iflag;
20595
James Smart6a828b02019-01-28 11:14:31 -080020596 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020597 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20598 &pvt_pool->list, list) {
20599 if (lpfc_test_rrq_active(
20600 phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
20601 continue;
20602 list_del(&lpfc_ncmd->list);
20603 pvt_pool->count--;
20604 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20605 return lpfc_ncmd;
20606 }
20607 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20608
20609 return NULL;
20610}
20611
20612/**
20613 * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
20614 * @phba: pointer to lpfc hba data structure.
20615 *
20616 * This routine tries to get one free IO buf from expedite pool.
20617 *
20618 * Return:
20619 * pointer to one free IO buf - if expedite pool is not empty
20620 * NULL - if expedite pool is empty
20621 **/
20622static struct lpfc_io_buf *
20623lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
20624{
20625 struct lpfc_io_buf *lpfc_ncmd;
20626 struct lpfc_io_buf *lpfc_ncmd_next;
20627 unsigned long iflag;
20628 struct lpfc_epd_pool *epd_pool;
20629
20630 epd_pool = &phba->epd_pool;
20631 lpfc_ncmd = NULL;
20632
20633 spin_lock_irqsave(&epd_pool->lock, iflag);
20634 if (epd_pool->count > 0) {
20635 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20636 &epd_pool->list, list) {
20637 list_del(&lpfc_ncmd->list);
20638 epd_pool->count--;
20639 break;
20640 }
20641 }
20642 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20643
20644 return lpfc_ncmd;
20645}
20646
20647/**
20648 * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
20649 * @phba: pointer to lpfc hba data structure.
20650 * @ndlp: pointer to lpfc nodelist data structure.
20651 * @hwqid: belong to which HWQ
20652 * @expedite: 1 means this request is urgent.
20653 *
20654 * This routine will do the following actions and then return a pointer to
20655 * one free IO buf.
20656 *
20657 * 1. If private free xri count is empty, move some XRIs from public to
20658 * private pool.
20659 * 2. Get one XRI from private free xri pool.
20660 * 3. If we fail to get one from pvt_pool and this is an expedite request,
20661 * get one free xri from expedite pool.
20662 *
20663 * Note: ndlp is only used on SCSI side for RRQ testing.
20664 * The caller should pass NULL for ndlp on NVME side.
20665 *
20666 * Return:
20667 * pointer to one free IO buf - if private pool is not empty
20668 * NULL - if private pool is empty
20669 **/
20670static struct lpfc_io_buf *
20671lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
20672 struct lpfc_nodelist *ndlp,
20673 int hwqid, int expedite)
20674{
20675 struct lpfc_sli4_hdw_queue *qp;
20676 struct lpfc_multixri_pool *multixri_pool;
20677 struct lpfc_pvt_pool *pvt_pool;
20678 struct lpfc_io_buf *lpfc_ncmd;
20679
20680 qp = &phba->sli4_hba.hdwq[hwqid];
20681 lpfc_ncmd = NULL;
20682 multixri_pool = qp->p_multixri_pool;
20683 pvt_pool = &multixri_pool->pvt_pool;
20684 multixri_pool->io_req_count++;
20685
20686 /* If pvt_pool is empty, move some XRIs from public to private pool */
20687 if (pvt_pool->count == 0)
20688 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20689
20690 /* Get one XRI from private free xri pool */
James Smart6a828b02019-01-28 11:14:31 -080020691 lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
James Smartc4908502019-01-28 11:14:28 -080020692
20693 if (lpfc_ncmd) {
20694 lpfc_ncmd->hdwq = qp;
20695 lpfc_ncmd->hdwq_no = hwqid;
20696 } else if (expedite) {
20697 /* If we fail to get one from pvt_pool and this is an expedite
20698 * request, get one free xri from expedite pool.
20699 */
20700 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
20701 }
20702
20703 return lpfc_ncmd;
20704}
20705
20706static inline struct lpfc_io_buf *
20707lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
20708{
20709 struct lpfc_sli4_hdw_queue *qp;
20710 struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
20711
20712 qp = &phba->sli4_hba.hdwq[idx];
20713 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
20714 &qp->lpfc_io_buf_list_get, list) {
20715 if (lpfc_test_rrq_active(phba, ndlp,
20716 lpfc_cmd->cur_iocbq.sli4_lxritag))
20717 continue;
20718
20719 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
20720 continue;
20721
20722 list_del_init(&lpfc_cmd->list);
20723 qp->get_io_bufs--;
20724 lpfc_cmd->hdwq = qp;
20725 lpfc_cmd->hdwq_no = idx;
20726 return lpfc_cmd;
20727 }
20728 return NULL;
20729}
20730
20731/**
20732 * lpfc_get_io_buf - Get one IO buffer from free pool
20733 * @phba: The HBA for which this call is being executed.
20734 * @ndlp: pointer to lpfc nodelist data structure.
20735 * @hwqid: belong to which HWQ
20736 * @expedite: 1 means this request is urgent.
20737 *
20738 * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
20739 * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
20740 * a IO buffer from head of @hdwq io_buf_list and returns to caller.
20741 *
20742 * Note: ndlp is only used on SCSI side for RRQ testing.
20743 * The caller should pass NULL for ndlp on NVME side.
20744 *
20745 * Return codes:
20746 * NULL - Error
20747 * Pointer to lpfc_io_buf - Success
20748 **/
20749struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
20750 struct lpfc_nodelist *ndlp,
20751 u32 hwqid, int expedite)
20752{
20753 struct lpfc_sli4_hdw_queue *qp;
20754 unsigned long iflag;
20755 struct lpfc_io_buf *lpfc_cmd;
20756
20757 qp = &phba->sli4_hba.hdwq[hwqid];
20758 lpfc_cmd = NULL;
20759
20760 if (phba->cfg_xri_rebalancing)
20761 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
20762 phba, ndlp, hwqid, expedite);
20763 else {
James Smart6a828b02019-01-28 11:14:31 -080020764 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
20765 qp, alloc_xri_get);
James Smartc4908502019-01-28 11:14:28 -080020766 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
20767 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20768 if (!lpfc_cmd) {
James Smart6a828b02019-01-28 11:14:31 -080020769 lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
20770 qp, alloc_xri_put);
James Smartc4908502019-01-28 11:14:28 -080020771 list_splice(&qp->lpfc_io_buf_list_put,
20772 &qp->lpfc_io_buf_list_get);
20773 qp->get_io_bufs += qp->put_io_bufs;
20774 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
20775 qp->put_io_bufs = 0;
20776 spin_unlock(&qp->io_buf_list_put_lock);
20777 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
20778 expedite)
20779 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20780 }
20781 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
20782 }
20783
20784 return lpfc_cmd;
20785}
James Smartd79c9e92019-08-14 16:57:09 -070020786
20787/**
20788 * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
20789 * @phba: The HBA for which this call is being executed.
20790 * @lpfc_buf: IO buf structure to append the SGL chunk
20791 *
20792 * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
20793 * and will allocate an SGL chunk if the pool is empty.
20794 *
20795 * Return codes:
20796 * NULL - Error
20797 * Pointer to sli4_hybrid_sgl - Success
20798 **/
20799struct sli4_hybrid_sgl *
20800lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
20801{
20802 struct sli4_hybrid_sgl *list_entry = NULL;
20803 struct sli4_hybrid_sgl *tmp = NULL;
20804 struct sli4_hybrid_sgl *allocated_sgl = NULL;
20805 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20806 struct list_head *buf_list = &hdwq->sgl_list;
James Smarta4c21ac2019-09-21 20:59:01 -070020807 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070020808
James Smarta4c21ac2019-09-21 20:59:01 -070020809 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020810
20811 if (likely(!list_empty(buf_list))) {
20812 /* break off 1 chunk from the sgl_list */
20813 list_for_each_entry_safe(list_entry, tmp,
20814 buf_list, list_node) {
20815 list_move_tail(&list_entry->list_node,
20816 &lpfc_buf->dma_sgl_xtra_list);
20817 break;
20818 }
20819 } else {
20820 /* allocate more */
James Smarta4c21ac2019-09-21 20:59:01 -070020821 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020822 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
James Smart4583a4f2019-11-15 16:38:47 -080020823 cpu_to_node(hdwq->io_wq->chann));
James Smartd79c9e92019-08-14 16:57:09 -070020824 if (!tmp) {
20825 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20826 "8353 error kmalloc memory for HDWQ "
20827 "%d %s\n",
20828 lpfc_buf->hdwq_no, __func__);
20829 return NULL;
20830 }
20831
20832 tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
20833 GFP_ATOMIC, &tmp->dma_phys_sgl);
20834 if (!tmp->dma_sgl) {
20835 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20836 "8354 error pool_alloc memory for HDWQ "
20837 "%d %s\n",
20838 lpfc_buf->hdwq_no, __func__);
20839 kfree(tmp);
20840 return NULL;
20841 }
20842
James Smarta4c21ac2019-09-21 20:59:01 -070020843 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020844 list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
20845 }
20846
20847 allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
20848 struct sli4_hybrid_sgl,
20849 list_node);
20850
James Smarta4c21ac2019-09-21 20:59:01 -070020851 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020852
20853 return allocated_sgl;
20854}
20855
20856/**
20857 * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
20858 * @phba: The HBA for which this call is being executed.
20859 * @lpfc_buf: IO buf structure with the SGL chunk
20860 *
20861 * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
20862 *
20863 * Return codes:
20864 * 0 - Success
20865 * -EINVAL - Error
20866 **/
20867int
20868lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
20869{
20870 int rc = 0;
20871 struct sli4_hybrid_sgl *list_entry = NULL;
20872 struct sli4_hybrid_sgl *tmp = NULL;
20873 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20874 struct list_head *buf_list = &hdwq->sgl_list;
James Smarta4c21ac2019-09-21 20:59:01 -070020875 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070020876
James Smarta4c21ac2019-09-21 20:59:01 -070020877 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020878
20879 if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
20880 list_for_each_entry_safe(list_entry, tmp,
20881 &lpfc_buf->dma_sgl_xtra_list,
20882 list_node) {
20883 list_move_tail(&list_entry->list_node,
20884 buf_list);
20885 }
20886 } else {
20887 rc = -EINVAL;
20888 }
20889
James Smarta4c21ac2019-09-21 20:59:01 -070020890 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020891 return rc;
20892}
20893
20894/**
20895 * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
20896 * @phba: phba object
20897 * @hdwq: hdwq to cleanup sgl buff resources on
20898 *
20899 * This routine frees all SGL chunks of hdwq SGL chunk pool.
20900 *
20901 * Return codes:
20902 * None
20903 **/
20904void
20905lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
20906 struct lpfc_sli4_hdw_queue *hdwq)
20907{
20908 struct list_head *buf_list = &hdwq->sgl_list;
20909 struct sli4_hybrid_sgl *list_entry = NULL;
20910 struct sli4_hybrid_sgl *tmp = NULL;
James Smarta4c21ac2019-09-21 20:59:01 -070020911 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070020912
James Smarta4c21ac2019-09-21 20:59:01 -070020913 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020914
20915 /* Free sgl pool */
20916 list_for_each_entry_safe(list_entry, tmp,
20917 buf_list, list_node) {
20918 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
20919 list_entry->dma_sgl,
20920 list_entry->dma_phys_sgl);
20921 list_del(&list_entry->list_node);
20922 kfree(list_entry);
20923 }
20924
James Smarta4c21ac2019-09-21 20:59:01 -070020925 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020926}
20927
20928/**
20929 * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
20930 * @phba: The HBA for which this call is being executed.
20931 * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
20932 *
20933 * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
20934 * and will allocate an CMD/RSP buffer if the pool is empty.
20935 *
20936 * Return codes:
20937 * NULL - Error
20938 * Pointer to fcp_cmd_rsp_buf - Success
20939 **/
20940struct fcp_cmd_rsp_buf *
20941lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
20942 struct lpfc_io_buf *lpfc_buf)
20943{
20944 struct fcp_cmd_rsp_buf *list_entry = NULL;
20945 struct fcp_cmd_rsp_buf *tmp = NULL;
20946 struct fcp_cmd_rsp_buf *allocated_buf = NULL;
20947 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20948 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
James Smarta4c21ac2019-09-21 20:59:01 -070020949 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070020950
James Smarta4c21ac2019-09-21 20:59:01 -070020951 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020952
20953 if (likely(!list_empty(buf_list))) {
20954 /* break off 1 chunk from the list */
20955 list_for_each_entry_safe(list_entry, tmp,
20956 buf_list,
20957 list_node) {
20958 list_move_tail(&list_entry->list_node,
20959 &lpfc_buf->dma_cmd_rsp_list);
20960 break;
20961 }
20962 } else {
20963 /* allocate more */
James Smarta4c21ac2019-09-21 20:59:01 -070020964 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020965 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
James Smart4583a4f2019-11-15 16:38:47 -080020966 cpu_to_node(hdwq->io_wq->chann));
James Smartd79c9e92019-08-14 16:57:09 -070020967 if (!tmp) {
20968 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20969 "8355 error kmalloc memory for HDWQ "
20970 "%d %s\n",
20971 lpfc_buf->hdwq_no, __func__);
20972 return NULL;
20973 }
20974
20975 tmp->fcp_cmnd = dma_pool_alloc(phba->lpfc_cmd_rsp_buf_pool,
20976 GFP_ATOMIC,
20977 &tmp->fcp_cmd_rsp_dma_handle);
20978
20979 if (!tmp->fcp_cmnd) {
20980 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20981 "8356 error pool_alloc memory for HDWQ "
20982 "%d %s\n",
20983 lpfc_buf->hdwq_no, __func__);
20984 kfree(tmp);
20985 return NULL;
20986 }
20987
20988 tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
20989 sizeof(struct fcp_cmnd));
20990
James Smarta4c21ac2019-09-21 20:59:01 -070020991 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020992 list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
20993 }
20994
20995 allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
20996 struct fcp_cmd_rsp_buf,
20997 list_node);
20998
James Smarta4c21ac2019-09-21 20:59:01 -070020999 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021000
21001 return allocated_buf;
21002}
21003
21004/**
21005 * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
21006 * @phba: The HBA for which this call is being executed.
21007 * @lpfc_buf: IO buf structure with the CMD/RSP buf
21008 *
21009 * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
21010 *
21011 * Return codes:
21012 * 0 - Success
21013 * -EINVAL - Error
21014 **/
21015int
21016lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21017 struct lpfc_io_buf *lpfc_buf)
21018{
21019 int rc = 0;
21020 struct fcp_cmd_rsp_buf *list_entry = NULL;
21021 struct fcp_cmd_rsp_buf *tmp = NULL;
21022 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21023 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
James Smarta4c21ac2019-09-21 20:59:01 -070021024 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070021025
James Smarta4c21ac2019-09-21 20:59:01 -070021026 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021027
21028 if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
21029 list_for_each_entry_safe(list_entry, tmp,
21030 &lpfc_buf->dma_cmd_rsp_list,
21031 list_node) {
21032 list_move_tail(&list_entry->list_node,
21033 buf_list);
21034 }
21035 } else {
21036 rc = -EINVAL;
21037 }
21038
James Smarta4c21ac2019-09-21 20:59:01 -070021039 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021040 return rc;
21041}
21042
21043/**
21044 * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
21045 * @phba: phba object
21046 * @hdwq: hdwq to cleanup cmd rsp buff resources on
21047 *
21048 * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
21049 *
21050 * Return codes:
21051 * None
21052 **/
21053void
21054lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21055 struct lpfc_sli4_hdw_queue *hdwq)
21056{
21057 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
21058 struct fcp_cmd_rsp_buf *list_entry = NULL;
21059 struct fcp_cmd_rsp_buf *tmp = NULL;
James Smarta4c21ac2019-09-21 20:59:01 -070021060 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070021061
James Smarta4c21ac2019-09-21 20:59:01 -070021062 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021063
21064 /* Free cmd_rsp buf pool */
21065 list_for_each_entry_safe(list_entry, tmp,
21066 buf_list,
21067 list_node) {
21068 dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
21069 list_entry->fcp_cmnd,
21070 list_entry->fcp_cmd_rsp_dma_handle);
21071 list_del(&list_entry->list_node);
21072 kfree(list_entry);
21073 }
21074
James Smarta4c21ac2019-09-21 20:59:01 -070021075 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021076}