blob: 0cc81321643d85a1884ff8bed4f46b9e9e0f6bc4 [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 Smart128bdda2018-01-30 15:59:03 -08004 * Copyright (C) 2017-2018 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>
James Smart1351e692018-02-22 08:18:43 -080038#ifdef CONFIG_X86
39#include <asm/set_memory.h>
40#endif
dea31012005-04-17 16:05:31 -050041
James Smart895427b2017-02-12 13:52:30 -080042#include <linux/nvme-fc-driver.h>
43
James Smartda0436e2009-05-22 14:51:39 -040044#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050045#include "lpfc_hw.h"
46#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040047#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040048#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050050#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080051#include "lpfc_scsi.h"
52#include "lpfc_nvme.h"
James Smartf358dd02017-02-12 13:52:34 -080053#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050054#include "lpfc_crtn.h"
55#include "lpfc_logmsg.h"
56#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050057#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040058#include "lpfc_vport.h"
James Smart61bda8f2016-10-13 15:06:05 -070059#include "lpfc_version.h"
dea31012005-04-17 16:05:31 -050060
61/* There are only four IOCB completion types. */
62typedef enum _lpfc_iocb_type {
63 LPFC_UNKNOWN_IOCB,
64 LPFC_UNSOL_IOCB,
65 LPFC_SOL_IOCB,
66 LPFC_ABORT_IOCB
67} lpfc_iocb_type;
68
James Smart4f774512009-05-22 14:52:35 -040069
70/* Provide function prototypes local to this module. */
71static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
72 uint32_t);
73static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040074 uint8_t *, uint32_t *);
75static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
76 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040077static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
78 struct hbq_dmabuf *);
James Smartae9e28f2017-05-15 15:20:51 -070079static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
80 struct hbq_dmabuf *dmabuf);
James Smart895427b2017-02-12 13:52:30 -080081static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
James Smart05580562011-05-24 11:40:48 -040082 struct lpfc_cqe *);
James Smart895427b2017-02-12 13:52:30 -080083static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
James Smart8a9d2e82012-05-09 21:16:12 -040084 int);
Dick Kennedyf485c182017-09-29 17:34:34 -070085static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
86 struct lpfc_eqe *eqe, uint32_t qidx);
James Smarte8d3c3b2013-10-10 12:21:30 -040087static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
88static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart895427b2017-02-12 13:52:30 -080089static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
90 struct lpfc_sli_ring *pring,
91 struct lpfc_iocbq *cmdiocb);
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 **/
113void
114lpfc_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;
153 temp_wqe = q->qe[q->host_index].wqe;
154
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 Smartff78d8f2011-12-13 13:21:35 -0500163 if (!((q->host_index + 1) % q->entry_repost))
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
231 * pointers. This routine returns the number of entries that were consumed by
232 * the HBA.
233 **/
234static uint32_t
235lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
236{
237 uint32_t released = 0;
238
James Smart2e90f4b2011-12-13 13:22:37 -0500239 /* sanity check on queue memory */
240 if (unlikely(!q))
241 return 0;
242
James Smart4f774512009-05-22 14:52:35 -0400243 if (q->hba_index == index)
244 return 0;
245 do {
246 q->hba_index = ((q->hba_index + 1) % q->entry_count);
247 released++;
248 } while (q->hba_index != index);
249 return released;
250}
251
252/**
253 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
254 * @q: The Mailbox Queue to operate on.
255 * @wqe: The Mailbox Queue Entry to put on the Work queue.
256 *
257 * This routine will copy the contents of @mqe to the next available entry on
258 * the @q. This function will then ring the Work Queue Doorbell to signal the
259 * HBA to start processing the Work Queue Entry. This function returns 0 if
260 * successful. If no entries are available on @q then this function will return
261 * -ENOMEM.
262 * The caller is expected to hold the hbalock when calling this routine.
263 **/
264static uint32_t
265lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
266{
James Smart2e90f4b2011-12-13 13:22:37 -0500267 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400268 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400269
James Smart2e90f4b2011-12-13 13:22:37 -0500270 /* sanity check on queue memory */
271 if (unlikely(!q))
272 return -ENOMEM;
273 temp_mqe = q->qe[q->host_index].mqe;
274
James Smart4f774512009-05-22 14:52:35 -0400275 /* If the host has not yet processed the next entry then we are done */
276 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
277 return -ENOMEM;
James Smart48f8fdb2018-05-04 20:37:51 -0700278 lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400279 /* Save off the mailbox pointer for completion */
280 q->phba->mbox = (MAILBOX_t *)temp_mqe;
281
282 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400283 q->host_index = ((q->host_index + 1) % q->entry_count);
284
285 /* Ring Doorbell */
286 doorbell.word0 = 0;
287 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
288 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
289 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400290 return 0;
291}
292
293/**
294 * lpfc_sli4_mq_release - Updates internal hba index for MQ
295 * @q: The Mailbox Queue to operate on.
296 *
297 * This routine will update the HBA index of a queue to reflect consumption of
298 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
299 * an entry the host calls this function to update the queue's internal
300 * pointers. This routine returns the number of entries that were consumed by
301 * the HBA.
302 **/
303static uint32_t
304lpfc_sli4_mq_release(struct lpfc_queue *q)
305{
James Smart2e90f4b2011-12-13 13:22:37 -0500306 /* sanity check on queue memory */
307 if (unlikely(!q))
308 return 0;
309
James Smart4f774512009-05-22 14:52:35 -0400310 /* Clear the mailbox pointer for completion */
311 q->phba->mbox = NULL;
312 q->hba_index = ((q->hba_index + 1) % q->entry_count);
313 return 1;
314}
315
316/**
317 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
318 * @q: The Event Queue to get the first valid EQE from
319 *
320 * This routine will get the first valid Event Queue Entry from @q, update
321 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
322 * the Queue (no more work to do), or the Queue is full of EQEs that have been
323 * processed, but not popped back to the HBA then this routine will return NULL.
324 **/
325static struct lpfc_eqe *
326lpfc_sli4_eq_get(struct lpfc_queue *q)
327{
James Smart7365f6f2018-02-22 08:18:46 -0800328 struct lpfc_hba *phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500329 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400330 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500331
332 /* sanity check on queue memory */
333 if (unlikely(!q))
334 return NULL;
James Smart7365f6f2018-02-22 08:18:46 -0800335 phba = q->phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500336 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400337
338 /* If the next EQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800339 if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400340 return NULL;
341 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400342 idx = ((q->hba_index + 1) % q->entry_count);
343 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400344 return NULL;
345
James Smart027140e2012-08-03 12:35:44 -0400346 q->hba_index = idx;
James Smart7365f6f2018-02-22 08:18:46 -0800347 /* if the index wrapped around, toggle the valid bit */
348 if (phba->sli4_hba.pc_sli4_params.eqav && !q->hba_index)
349 q->qe_valid = (q->qe_valid) ? 0 : 1;
350
James Smart27f344e2014-05-07 17:16:46 -0400351
352 /*
353 * insert barrier for instruction interlock : data from the hardware
354 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800355 * upon. Speculative instructions were allowing a bcopy at the start
356 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
357 * after our return, to copy data before the valid bit check above
358 * was done. As such, some of the copied data was stale. The barrier
359 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400360 */
361 mb();
James Smart4f774512009-05-22 14:52:35 -0400362 return eqe;
363}
364
365/**
James Smartba20c852012-08-03 12:36:52 -0400366 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
367 * @q: The Event Queue to disable interrupts
368 *
369 **/
James Smartb71413d2018-02-22 08:18:40 -0800370inline void
James Smartba20c852012-08-03 12:36:52 -0400371lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
372{
373 struct lpfc_register doorbell;
374
375 doorbell.word0 = 0;
376 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
377 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
378 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
379 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
380 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800381 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smartba20c852012-08-03 12:36:52 -0400382}
383
384/**
James Smart27d6ac02018-02-22 08:18:42 -0800385 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
386 * @q: The Event Queue to disable interrupts
387 *
388 **/
389inline void
390lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
391{
392 struct lpfc_register doorbell;
393
394 doorbell.word0 = 0;
James Smartaad59d52018-09-10 10:30:47 -0700395 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
James Smart27d6ac02018-02-22 08:18:42 -0800396 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
397}
398
399/**
James Smart4f774512009-05-22 14:52:35 -0400400 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
401 * @q: The Event Queue that the host has completed processing for.
402 * @arm: Indicates whether the host wants to arms this CQ.
403 *
404 * This routine will mark all Event Queue Entries on @q, from the last
405 * known completed entry to the last entry that was processed, as completed
406 * by clearing the valid bit for each completion queue entry. Then it will
407 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
408 * The internal host index in the @q will be updated by this routine to indicate
409 * that the host has finished processing the entries. The @arm parameter
410 * indicates that the queue should be rearmed when ringing the doorbell.
411 *
412 * This function will return the number of EQEs that were popped.
413 **/
414uint32_t
415lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
416{
417 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800418 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400419 struct lpfc_eqe *temp_eqe;
420 struct lpfc_register doorbell;
421
James Smart2e90f4b2011-12-13 13:22:37 -0500422 /* sanity check on queue memory */
423 if (unlikely(!q))
424 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800425 phba = q->phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500426
James Smart4f774512009-05-22 14:52:35 -0400427 /* while there are valid entries */
428 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800429 if (!phba->sli4_hba.pc_sli4_params.eqav) {
430 temp_eqe = q->qe[q->host_index].eqe;
431 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
432 }
James Smart4f774512009-05-22 14:52:35 -0400433 released++;
434 q->host_index = ((q->host_index + 1) % q->entry_count);
435 }
436 if (unlikely(released == 0 && !arm))
437 return 0;
438
439 /* ring doorbell for number popped */
440 doorbell.word0 = 0;
441 if (arm) {
442 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
443 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
444 }
445 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
446 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500447 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
448 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
449 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800450 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500451 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
452 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
James Smart9dd35422018-02-22 08:18:41 -0800453 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400454 return released;
455}
456
457/**
James Smart27d6ac02018-02-22 08:18:42 -0800458 * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
459 * @q: The Event Queue that the host has completed processing for.
460 * @arm: Indicates whether the host wants to arms this CQ.
461 *
462 * This routine will mark all Event Queue Entries on @q, from the last
463 * known completed entry to the last entry that was processed, as completed
464 * by clearing the valid bit for each completion queue entry. Then it will
465 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
466 * The internal host index in the @q will be updated by this routine to indicate
467 * that the host has finished processing the entries. The @arm parameter
468 * indicates that the queue should be rearmed when ringing the doorbell.
469 *
470 * This function will return the number of EQEs that were popped.
471 **/
472uint32_t
473lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
474{
475 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800476 struct lpfc_hba *phba;
James Smart27d6ac02018-02-22 08:18:42 -0800477 struct lpfc_eqe *temp_eqe;
478 struct lpfc_register doorbell;
479
480 /* sanity check on queue memory */
481 if (unlikely(!q))
482 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800483 phba = q->phba;
James Smart27d6ac02018-02-22 08:18:42 -0800484
485 /* while there are valid entries */
486 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800487 if (!phba->sli4_hba.pc_sli4_params.eqav) {
488 temp_eqe = q->qe[q->host_index].eqe;
489 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
490 }
James Smart27d6ac02018-02-22 08:18:42 -0800491 released++;
492 q->host_index = ((q->host_index + 1) % q->entry_count);
493 }
494 if (unlikely(released == 0 && !arm))
495 return 0;
496
497 /* ring doorbell for number popped */
498 doorbell.word0 = 0;
499 if (arm)
500 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
501 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
502 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
503 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
504 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
505 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
506 readl(q->phba->sli4_hba.EQDBregaddr);
507 return released;
508}
509
510/**
James Smart4f774512009-05-22 14:52:35 -0400511 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
512 * @q: The Completion Queue to get the first valid CQE from
513 *
514 * This routine will get the first valid Completion Queue Entry from @q, update
515 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
516 * the Queue (no more work to do), or the Queue is full of CQEs that have been
517 * processed, but not popped back to the HBA then this routine will return NULL.
518 **/
519static struct lpfc_cqe *
520lpfc_sli4_cq_get(struct lpfc_queue *q)
521{
James Smart7365f6f2018-02-22 08:18:46 -0800522 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400523 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400524 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400525
James Smart2e90f4b2011-12-13 13:22:37 -0500526 /* sanity check on queue memory */
527 if (unlikely(!q))
528 return NULL;
James Smart7365f6f2018-02-22 08:18:46 -0800529 phba = q->phba;
530 cqe = q->qe[q->hba_index].cqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500531
James Smart4f774512009-05-22 14:52:35 -0400532 /* If the next CQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800533 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400534 return NULL;
535 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400536 idx = ((q->hba_index + 1) % q->entry_count);
537 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400538 return NULL;
539
James Smart027140e2012-08-03 12:35:44 -0400540 q->hba_index = idx;
James Smart7365f6f2018-02-22 08:18:46 -0800541 /* if the index wrapped around, toggle the valid bit */
542 if (phba->sli4_hba.pc_sli4_params.cqav && !q->hba_index)
543 q->qe_valid = (q->qe_valid) ? 0 : 1;
James Smart27f344e2014-05-07 17:16:46 -0400544
545 /*
546 * insert barrier for instruction interlock : data from the hardware
547 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800548 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
549 * instructions allowing action on content before valid bit checked,
550 * add barrier here as well. May not be needed as "content" is a
551 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400552 */
553 mb();
James Smart4f774512009-05-22 14:52:35 -0400554 return cqe;
555}
556
557/**
558 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
559 * @q: The Completion Queue that the host has completed processing for.
560 * @arm: Indicates whether the host wants to arms this CQ.
561 *
562 * This routine will mark all Completion queue entries on @q, from the last
563 * known completed entry to the last entry that was processed, as completed
564 * by clearing the valid bit for each completion queue entry. Then it will
565 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
566 * The internal host index in the @q will be updated by this routine to indicate
567 * that the host has finished processing the entries. The @arm parameter
568 * indicates that the queue should be rearmed when ringing the doorbell.
569 *
570 * This function will return the number of CQEs that were released.
571 **/
572uint32_t
573lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
574{
575 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800576 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400577 struct lpfc_cqe *temp_qe;
578 struct lpfc_register doorbell;
579
James Smart2e90f4b2011-12-13 13:22:37 -0500580 /* sanity check on queue memory */
581 if (unlikely(!q))
582 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800583 phba = q->phba;
584
James Smart4f774512009-05-22 14:52:35 -0400585 /* while there are valid entries */
586 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800587 if (!phba->sli4_hba.pc_sli4_params.cqav) {
588 temp_qe = q->qe[q->host_index].cqe;
589 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
590 }
James Smart4f774512009-05-22 14:52:35 -0400591 released++;
592 q->host_index = ((q->host_index + 1) % q->entry_count);
593 }
594 if (unlikely(released == 0 && !arm))
595 return 0;
596
597 /* ring doorbell for number popped */
598 doorbell.word0 = 0;
599 if (arm)
600 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
601 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
602 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500603 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
604 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
605 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800606 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400607 return released;
608}
609
610/**
James Smart27d6ac02018-02-22 08:18:42 -0800611 * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
612 * @q: The Completion Queue that the host has completed processing for.
613 * @arm: Indicates whether the host wants to arms this CQ.
614 *
615 * This routine will mark all Completion queue entries on @q, from the last
616 * known completed entry to the last entry that was processed, as completed
617 * by clearing the valid bit for each completion queue entry. Then it will
618 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
619 * The internal host index in the @q will be updated by this routine to indicate
620 * that the host has finished processing the entries. The @arm parameter
621 * indicates that the queue should be rearmed when ringing the doorbell.
622 *
623 * This function will return the number of CQEs that were released.
624 **/
625uint32_t
626lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
627{
628 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800629 struct lpfc_hba *phba;
James Smart27d6ac02018-02-22 08:18:42 -0800630 struct lpfc_cqe *temp_qe;
631 struct lpfc_register doorbell;
632
633 /* sanity check on queue memory */
634 if (unlikely(!q))
635 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800636 phba = q->phba;
637
James Smart27d6ac02018-02-22 08:18:42 -0800638 /* while there are valid entries */
639 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800640 if (!phba->sli4_hba.pc_sli4_params.cqav) {
641 temp_qe = q->qe[q->host_index].cqe;
642 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
643 }
James Smart27d6ac02018-02-22 08:18:42 -0800644 released++;
645 q->host_index = ((q->host_index + 1) % q->entry_count);
646 }
647 if (unlikely(released == 0 && !arm))
648 return 0;
649
650 /* ring doorbell for number popped */
651 doorbell.word0 = 0;
652 if (arm)
653 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
654 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
655 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
656 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
657 return released;
658}
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;
687 temp_hrqe = hq->qe[hq_put_index].rqe;
688 temp_drqe = dq->qe[dq_put_index].rqe;
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 Smart73d91e52011-10-10 21:32:10 -0400706 if (!(hq->host_index % hq->entry_repost)) {
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,
710 hq->entry_repost);
711 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,
714 hq->entry_repost);
715 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);
927 if (!rrq->send_rrq)
928 /* this call will free the rrq */
929 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
930 else if (lpfc_send_rrq(phba, rrq)) {
931 /* 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 *
1014 * This function is called with hbalock held. This function
1015 * returns 0 = rrq not active for this xri
1016 * 1 = rrq is valid for this xri.
1017 **/
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{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001022 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -05001023 if (!ndlp)
1024 return 0;
James Smartcff261f2013-12-17 20:29:47 -05001025 if (!ndlp->active_rrqs_xri_bitmap)
1026 return 0;
1027 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -05001028 return 1;
1029 else
1030 return 0;
1031}
1032
1033/**
1034 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1035 * @phba: Pointer to HBA context object.
1036 * @ndlp: nodelist pointer for this target.
1037 * @xritag: xri used in this exchange.
1038 * @rxid: Remote Exchange ID.
1039 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1040 *
1041 * This function takes the hbalock.
1042 * The active bit is always set in the active rrq xri_bitmap even
1043 * if there is no slot avaiable for the other rrq information.
1044 *
1045 * returns 0 rrq actived for this xri
1046 * < 0 No memory or invalid ndlp.
1047 **/
1048int
1049lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -05001050 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -05001051{
James Smart19ca7602010-11-20 23:11:55 -05001052 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -05001053 struct lpfc_node_rrq *rrq;
1054 int empty;
1055
1056 if (!ndlp)
1057 return -EINVAL;
1058
1059 if (!phba->cfg_enable_rrq)
1060 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001061
1062 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001063 if (phba->pport->load_flag & FC_UNLOADING) {
1064 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1065 goto out;
1066 }
1067
1068 /*
1069 * set the active bit even if there is no mem available.
1070 */
1071 if (NLP_CHK_FREE_REQ(ndlp))
1072 goto out;
1073
1074 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1075 goto out;
1076
James Smartcff261f2013-12-17 20:29:47 -05001077 if (!ndlp->active_rrqs_xri_bitmap)
1078 goto out;
1079
1080 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -05001081 goto out;
1082
James Smart19ca7602010-11-20 23:11:55 -05001083 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001084 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1085 if (!rrq) {
1086 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1087 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1088 " DID:0x%x Send:%d\n",
1089 xritag, rxid, ndlp->nlp_DID, send_rrq);
1090 return -EINVAL;
1091 }
James Smarte5771b42013-03-01 16:37:14 -05001092 if (phba->cfg_enable_rrq == 1)
1093 rrq->send_rrq = send_rrq;
1094 else
1095 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001096 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001097 rrq->rrq_stop_time = jiffies +
1098 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001099 rrq->ndlp = ndlp;
1100 rrq->nlp_DID = ndlp->nlp_DID;
1101 rrq->vport = ndlp->vport;
1102 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001103 spin_lock_irqsave(&phba->hbalock, iflags);
1104 empty = list_empty(&phba->active_rrq_list);
1105 list_add_tail(&rrq->list, &phba->active_rrq_list);
1106 phba->hba_flag |= HBA_RRQ_ACTIVE;
1107 if (empty)
1108 lpfc_worker_wake_up(phba);
1109 spin_unlock_irqrestore(&phba->hbalock, iflags);
1110 return 0;
1111out:
1112 spin_unlock_irqrestore(&phba->hbalock, iflags);
1113 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1114 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1115 " DID:0x%x Send:%d\n",
1116 xritag, rxid, ndlp->nlp_DID, send_rrq);
1117 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001118}
1119
1120/**
James Smart895427b2017-02-12 13:52:30 -08001121 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001122 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -05001123 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001124 *
James Smartdafe8ce2014-09-03 12:56:40 -04001125 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -04001126 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -04001127 * list is not empty then it is successful, it returns pointer to the newly
1128 * allocated sglq object else it returns NULL.
1129 **/
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;
1138 int found = 0;
1139
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001140 lockdep_assert_held(&phba->hbalock);
1141
James Smart19ca7602010-11-20 23:11:55 -05001142 if (piocbq->iocb_flag & LPFC_IO_FCP) {
James Smartc4908502019-01-28 11:14:28 -08001143 lpfc_cmd = (struct lpfc_io_buf *) piocbq->context1;
James Smart19ca7602010-11-20 23:11:55 -05001144 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001145 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001146 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001147 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001148 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1149 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1150 ndlp = NULL;
1151 else
1152 ndlp = piocbq->context_un.ndlp;
1153 } else {
James Smart19ca7602010-11-20 23:11:55 -05001154 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001155 }
James Smart19ca7602010-11-20 23:11:55 -05001156
James Smart895427b2017-02-12 13:52:30 -08001157 spin_lock(&phba->sli4_hba.sgl_list_lock);
1158 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001159 start_sglq = sglq;
1160 while (!found) {
1161 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001162 break;
James Smart895427b2017-02-12 13:52:30 -08001163 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1164 test_bit(sglq->sli4_lxritag,
1165 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001166 /* This xri has an rrq outstanding for this DID.
1167 * put it back in the list and get another xri.
1168 */
James Smart895427b2017-02-12 13:52:30 -08001169 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001170 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001171 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001172 struct lpfc_sglq, list);
1173 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001174 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001175 sglq = NULL;
1176 break;
1177 } else
1178 continue;
1179 }
1180 sglq->ndlp = ndlp;
1181 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001182 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001183 sglq->state = SGL_ALLOCATED;
1184 }
James Smart895427b2017-02-12 13:52:30 -08001185 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001186 return sglq;
1187}
1188
1189/**
James Smartf358dd02017-02-12 13:52:34 -08001190 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1191 * @phba: Pointer to HBA context object.
1192 * @piocb: Pointer to the iocbq.
1193 *
1194 * This function is called with the sgl_list lock held. This function
1195 * gets a new driver sglq object from the sglq list. If the
1196 * list is not empty then it is successful, it returns pointer to the newly
1197 * allocated sglq object else it returns NULL.
1198 **/
1199struct lpfc_sglq *
1200__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1201{
1202 struct list_head *lpfc_nvmet_sgl_list;
1203 struct lpfc_sglq *sglq = NULL;
1204
1205 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1206
1207 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1208
1209 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1210 if (!sglq)
1211 return NULL;
1212 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1213 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001214 return sglq;
1215}
1216
James Smarte59058c2008-08-24 21:49:00 -04001217/**
James Smart3621a712009-04-06 18:47:14 -04001218 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001219 * @phba: Pointer to HBA context object.
1220 *
1221 * This function is called with no lock held. This function
1222 * allocates a new driver iocb object from the iocb pool. If the
1223 * allocation is successful, it returns pointer to the newly
1224 * allocated iocb object else it returns NULL.
1225 **/
James Smart2e0fef82007-06-17 19:56:36 -05001226struct lpfc_iocbq *
1227lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001228{
James Smart2e0fef82007-06-17 19:56:36 -05001229 struct lpfc_iocbq * iocbq = NULL;
1230 unsigned long iflags;
1231
1232 spin_lock_irqsave(&phba->hbalock, iflags);
1233 iocbq = __lpfc_sli_get_iocbq(phba);
1234 spin_unlock_irqrestore(&phba->hbalock, iflags);
1235 return iocbq;
1236}
1237
James Smarte59058c2008-08-24 21:49:00 -04001238/**
James Smart4f774512009-05-22 14:52:35 -04001239 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1240 * @phba: Pointer to HBA context object.
1241 * @iocbq: Pointer to driver iocb object.
1242 *
1243 * This function is called with hbalock held to release driver
1244 * iocb object to the iocb pool. The iotag in the iocb object
1245 * does not change for each use of the iocb object. This function
1246 * clears all other fields of the iocb object when it is freed.
1247 * The sqlq structure that holds the xritag and phys and virtual
1248 * mappings for the scatter gather list is retrieved from the
1249 * active array of sglq. The get of the sglq pointer also clears
1250 * the entry in the array. If the status of the IO indiactes that
1251 * this IO was aborted then the sglq entry it put on the
1252 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1253 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001254 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001255 **/
1256static void
1257__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1258{
1259 struct lpfc_sglq *sglq;
1260 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001261 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001262 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001263
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001264 lockdep_assert_held(&phba->hbalock);
1265
James Smart4f774512009-05-22 14:52:35 -04001266 if (iocbq->sli4_xritag == NO_XRI)
1267 sglq = NULL;
1268 else
James Smart6d368e52011-05-24 11:44:12 -04001269 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1270
James Smart0e9bb8d2013-03-01 16:35:12 -05001271
James Smart4f774512009-05-22 14:52:35 -04001272 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001273 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1274 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1275 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001276 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001277 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001278 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001279 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1280 spin_unlock_irqrestore(
1281 &phba->sli4_hba.sgl_list_lock, iflag);
1282 goto out;
1283 }
1284
James Smart895427b2017-02-12 13:52:30 -08001285 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001286 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1287 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001288 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1289 iflag);
James Smart341af102010-01-26 23:07:37 -05001290 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001291 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001292 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001293 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001294 } else {
James Smart895427b2017-02-12 13:52:30 -08001295 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1296 iflag);
James Smart4f774512009-05-22 14:52:35 -04001297 sglq->state = SGL_FREED;
1298 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001299 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001300 &phba->sli4_hba.lpfc_els_sgl_list);
1301 spin_unlock_irqrestore(
1302 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001303
1304 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001305 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001306 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001307 }
James Smart4f774512009-05-22 14:52:35 -04001308 }
1309
James Smartf358dd02017-02-12 13:52:34 -08001310out:
James Smart4f774512009-05-22 14:52:35 -04001311 /*
1312 * Clean all volatile data fields, preserve iotag and node struct.
1313 */
1314 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001315 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001316 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001317 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1318 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001319 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1320}
1321
James Smart2a9bf3d2010-06-07 15:24:45 -04001322
James Smart4f774512009-05-22 14:52:35 -04001323/**
James Smart3772a992009-05-22 14:50:54 -04001324 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1325 * @phba: Pointer to HBA context object.
1326 * @iocbq: Pointer to driver iocb object.
1327 *
1328 * This function is called with hbalock held to release driver
1329 * iocb object to the iocb pool. The iotag in the iocb object
1330 * does not change for each use of the iocb object. This function
1331 * clears all other fields of the iocb object when it is freed.
1332 **/
1333static void
1334__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1335{
1336 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1337
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001338 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001339
1340 /*
James Smart3772a992009-05-22 14:50:54 -04001341 * Clean all volatile data fields, preserve iotag and node struct.
1342 */
1343 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1344 iocbq->sli4_xritag = NO_XRI;
1345 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1346}
1347
1348/**
James Smart3621a712009-04-06 18:47:14 -04001349 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001350 * @phba: Pointer to HBA context object.
1351 * @iocbq: Pointer to driver iocb object.
1352 *
1353 * This function is called with hbalock held to release driver
1354 * iocb object to the iocb pool. The iotag in the iocb object
1355 * does not change for each use of the iocb object. This function
1356 * clears all other fields of the iocb object when it is freed.
1357 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001358static void
James Smart2e0fef82007-06-17 19:56:36 -05001359__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1360{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001361 lockdep_assert_held(&phba->hbalock);
1362
James Smart3772a992009-05-22 14:50:54 -04001363 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001364 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001365}
1366
James Smarte59058c2008-08-24 21:49:00 -04001367/**
James Smart3621a712009-04-06 18:47:14 -04001368 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001369 * @phba: Pointer to HBA context object.
1370 * @iocbq: Pointer to driver iocb object.
1371 *
1372 * This function is called with no lock held to release the iocb to
1373 * iocb pool.
1374 **/
James Smart2e0fef82007-06-17 19:56:36 -05001375void
1376lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1377{
1378 unsigned long iflags;
1379
1380 /*
1381 * Clean all volatile data fields, preserve iotag and node struct.
1382 */
1383 spin_lock_irqsave(&phba->hbalock, iflags);
1384 __lpfc_sli_release_iocbq(phba, iocbq);
1385 spin_unlock_irqrestore(&phba->hbalock, iflags);
1386}
1387
James Smarte59058c2008-08-24 21:49:00 -04001388/**
James Smarta257bf92009-04-06 18:48:10 -04001389 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1390 * @phba: Pointer to HBA context object.
1391 * @iocblist: List of IOCBs.
1392 * @ulpstatus: ULP status in IOCB command field.
1393 * @ulpWord4: ULP word-4 in IOCB command field.
1394 *
1395 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1396 * on the list by invoking the complete callback function associated with the
1397 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1398 * fields.
1399 **/
1400void
1401lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1402 uint32_t ulpstatus, uint32_t ulpWord4)
1403{
1404 struct lpfc_iocbq *piocb;
1405
1406 while (!list_empty(iocblist)) {
1407 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001408 if (!piocb->iocb_cmpl)
1409 lpfc_sli_release_iocbq(phba, piocb);
1410 else {
1411 piocb->iocb.ulpStatus = ulpstatus;
1412 piocb->iocb.un.ulpWord[4] = ulpWord4;
1413 (piocb->iocb_cmpl) (phba, piocb, piocb);
1414 }
1415 }
1416 return;
1417}
1418
1419/**
James Smart3621a712009-04-06 18:47:14 -04001420 * lpfc_sli_iocb_cmd_type - Get the iocb type
1421 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001422 *
1423 * This function is called by ring event handler function to get the iocb type.
1424 * This function translates the iocb command to an iocb command type used to
1425 * decide the final disposition of each completed IOCB.
1426 * The function returns
1427 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1428 * LPFC_SOL_IOCB if it is a solicited iocb completion
1429 * LPFC_ABORT_IOCB if it is an abort iocb
1430 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1431 *
1432 * The caller is not required to hold any lock.
1433 **/
dea31012005-04-17 16:05:31 -05001434static lpfc_iocb_type
1435lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1436{
1437 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1438
1439 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1440 return 0;
1441
1442 switch (iocb_cmnd) {
1443 case CMD_XMIT_SEQUENCE_CR:
1444 case CMD_XMIT_SEQUENCE_CX:
1445 case CMD_XMIT_BCAST_CN:
1446 case CMD_XMIT_BCAST_CX:
1447 case CMD_ELS_REQUEST_CR:
1448 case CMD_ELS_REQUEST_CX:
1449 case CMD_CREATE_XRI_CR:
1450 case CMD_CREATE_XRI_CX:
1451 case CMD_GET_RPI_CN:
1452 case CMD_XMIT_ELS_RSP_CX:
1453 case CMD_GET_RPI_CR:
1454 case CMD_FCP_IWRITE_CR:
1455 case CMD_FCP_IWRITE_CX:
1456 case CMD_FCP_IREAD_CR:
1457 case CMD_FCP_IREAD_CX:
1458 case CMD_FCP_ICMND_CR:
1459 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001460 case CMD_FCP_TSEND_CX:
1461 case CMD_FCP_TRSP_CX:
1462 case CMD_FCP_TRECEIVE_CX:
1463 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001464 case CMD_ADAPTER_MSG:
1465 case CMD_ADAPTER_DUMP:
1466 case CMD_XMIT_SEQUENCE64_CR:
1467 case CMD_XMIT_SEQUENCE64_CX:
1468 case CMD_XMIT_BCAST64_CN:
1469 case CMD_XMIT_BCAST64_CX:
1470 case CMD_ELS_REQUEST64_CR:
1471 case CMD_ELS_REQUEST64_CX:
1472 case CMD_FCP_IWRITE64_CR:
1473 case CMD_FCP_IWRITE64_CX:
1474 case CMD_FCP_IREAD64_CR:
1475 case CMD_FCP_IREAD64_CX:
1476 case CMD_FCP_ICMND64_CR:
1477 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001478 case CMD_FCP_TSEND64_CX:
1479 case CMD_FCP_TRSP64_CX:
1480 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001481 case CMD_GEN_REQUEST64_CR:
1482 case CMD_GEN_REQUEST64_CX:
1483 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001484 case DSSCMD_IWRITE64_CR:
1485 case DSSCMD_IWRITE64_CX:
1486 case DSSCMD_IREAD64_CR:
1487 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001488 type = LPFC_SOL_IOCB;
1489 break;
1490 case CMD_ABORT_XRI_CN:
1491 case CMD_ABORT_XRI_CX:
1492 case CMD_CLOSE_XRI_CN:
1493 case CMD_CLOSE_XRI_CX:
1494 case CMD_XRI_ABORTED_CX:
1495 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001496 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001497 type = LPFC_ABORT_IOCB;
1498 break;
1499 case CMD_RCV_SEQUENCE_CX:
1500 case CMD_RCV_ELS_REQ_CX:
1501 case CMD_RCV_SEQUENCE64_CX:
1502 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001503 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001504 case CMD_IOCB_RCV_SEQ64_CX:
1505 case CMD_IOCB_RCV_ELS64_CX:
1506 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001507 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001508 type = LPFC_UNSOL_IOCB;
1509 break;
James Smart3163f722008-02-08 18:50:25 -05001510 case CMD_IOCB_XMIT_MSEQ64_CR:
1511 case CMD_IOCB_XMIT_MSEQ64_CX:
1512 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1513 case CMD_IOCB_RCV_ELS_LIST64_CX:
1514 case CMD_IOCB_CLOSE_EXTENDED_CN:
1515 case CMD_IOCB_ABORT_EXTENDED_CN:
1516 case CMD_IOCB_RET_HBQE64_CN:
1517 case CMD_IOCB_FCP_IBIDIR64_CR:
1518 case CMD_IOCB_FCP_IBIDIR64_CX:
1519 case CMD_IOCB_FCP_ITASKMGT64_CX:
1520 case CMD_IOCB_LOGENTRY_CN:
1521 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1522 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001523 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001524 type = LPFC_UNKNOWN_IOCB;
1525 break;
dea31012005-04-17 16:05:31 -05001526 default:
1527 type = LPFC_UNKNOWN_IOCB;
1528 break;
1529 }
1530
1531 return type;
1532}
1533
James Smarte59058c2008-08-24 21:49:00 -04001534/**
James Smart3621a712009-04-06 18:47:14 -04001535 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001536 * @phba: Pointer to HBA context object.
1537 *
1538 * This function is called from SLI initialization code
1539 * to configure every ring of the HBA's SLI interface. The
1540 * caller is not required to hold any lock. This function issues
1541 * a config_ring mailbox command for each ring.
1542 * This function returns zero if successful else returns a negative
1543 * error code.
1544 **/
dea31012005-04-17 16:05:31 -05001545static int
James Smarted957682007-06-17 19:56:37 -05001546lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001547{
1548 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001549 LPFC_MBOXQ_t *pmb;
1550 MAILBOX_t *pmbox;
1551 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001552
James Smarted957682007-06-17 19:56:37 -05001553 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1554 if (!pmb)
1555 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001556 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001557 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001558 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001559 lpfc_config_ring(phba, i, pmb);
1560 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1561 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001563 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001564 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1565 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001566 rc, pmbox->mbxCommand,
1567 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001568 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001569 ret = -ENXIO;
1570 break;
dea31012005-04-17 16:05:31 -05001571 }
1572 }
James Smarted957682007-06-17 19:56:37 -05001573 mempool_free(pmb, phba->mbox_mem_pool);
1574 return ret;
dea31012005-04-17 16:05:31 -05001575}
1576
James Smarte59058c2008-08-24 21:49:00 -04001577/**
James Smart3621a712009-04-06 18:47:14 -04001578 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001579 * @phba: Pointer to HBA context object.
1580 * @pring: Pointer to driver SLI ring object.
1581 * @piocb: Pointer to the driver iocb object.
1582 *
1583 * This function is called with hbalock held. The function adds the
1584 * new iocb to txcmplq of the given ring. This function always returns
1585 * 0. If this function is called for ELS ring, this function checks if
1586 * there is a vport associated with the ELS command. This function also
1587 * starts els_tmofunc timer if this is an ELS command.
1588 **/
dea31012005-04-17 16:05:31 -05001589static int
James Smart2e0fef82007-06-17 19:56:36 -05001590lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1591 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001592{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001593 lockdep_assert_held(&phba->hbalock);
1594
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001595 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001596
dea31012005-04-17 16:05:31 -05001597 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001598 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08001599 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001600
James Smart92d7f7b2007-06-17 19:56:38 -05001601 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1602 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001603 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1604 BUG_ON(!piocb->vport);
1605 if (!(piocb->vport->load_flag & FC_UNLOADING))
1606 mod_timer(&piocb->vport->els_tmofunc,
1607 jiffies +
1608 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1609 }
dea31012005-04-17 16:05:31 -05001610
James Smart2e0fef82007-06-17 19:56:36 -05001611 return 0;
dea31012005-04-17 16:05:31 -05001612}
1613
James Smarte59058c2008-08-24 21:49:00 -04001614/**
James Smart3621a712009-04-06 18:47:14 -04001615 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001616 * @phba: Pointer to HBA context object.
1617 * @pring: Pointer to driver SLI ring object.
1618 *
1619 * This function is called with hbalock held to get next
1620 * iocb in txq of the given ring. If there is any iocb in
1621 * the txq, the function returns first iocb in the list after
1622 * removing the iocb from the list, else it returns NULL.
1623 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001624struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001625lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001626{
dea31012005-04-17 16:05:31 -05001627 struct lpfc_iocbq *cmd_iocb;
1628
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001629 lockdep_assert_held(&phba->hbalock);
1630
James Smart858c9f62007-06-17 19:56:39 -05001631 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001632 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001633}
1634
James Smarte59058c2008-08-24 21:49:00 -04001635/**
James Smart3621a712009-04-06 18:47:14 -04001636 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001637 * @phba: Pointer to HBA context object.
1638 * @pring: Pointer to driver SLI ring object.
1639 *
1640 * This function is called with hbalock held and the caller must post the
1641 * iocb without releasing the lock. If the caller releases the lock,
1642 * iocb slot returned by the function is not guaranteed to be available.
1643 * The function returns pointer to the next available iocb slot if there
1644 * is available slot in the ring, else it returns NULL.
1645 * If the get index of the ring is ahead of the put index, the function
1646 * will post an error attention event to the worker thread to take the
1647 * HBA to offline state.
1648 **/
dea31012005-04-17 16:05:31 -05001649static IOCB_t *
1650lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1651{
James Smart34b02dc2008-08-24 21:49:55 -04001652 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001653 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001654
1655 lockdep_assert_held(&phba->hbalock);
1656
James Smart7e56aa22012-08-03 12:35:34 -04001657 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1658 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1659 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001660
James Smart7e56aa22012-08-03 12:35:34 -04001661 if (unlikely(pring->sli.sli3.local_getidx ==
1662 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001663
James Smart7e56aa22012-08-03 12:35:34 -04001664 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001665
James Smart7e56aa22012-08-03 12:35:34 -04001666 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001667 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001668 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001669 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001670 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001671 pring->sli.sli3.local_getidx,
1672 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001673
James Smart2e0fef82007-06-17 19:56:36 -05001674 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001675 /*
1676 * All error attention handlers are posted to
1677 * worker thread
1678 */
1679 phba->work_ha |= HA_ERATT;
1680 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001681
James Smart5e9d9b82008-06-14 22:52:53 -04001682 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001683
1684 return NULL;
1685 }
1686
James Smart7e56aa22012-08-03 12:35:34 -04001687 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001688 return NULL;
1689 }
1690
James Smarted957682007-06-17 19:56:37 -05001691 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001692}
1693
James Smarte59058c2008-08-24 21:49:00 -04001694/**
James Smart3621a712009-04-06 18:47:14 -04001695 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001696 * @phba: Pointer to HBA context object.
1697 * @iocbq: Pointer to driver iocb object.
1698 *
1699 * This function gets an iotag for the iocb. If there is no unused iotag and
1700 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1701 * array and assigns a new iotag.
1702 * The function returns the allocated iotag if successful, else returns zero.
1703 * Zero is not a valid iotag.
1704 * The caller is not required to hold any lock.
1705 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001706uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001707lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001708{
James Smart2e0fef82007-06-17 19:56:36 -05001709 struct lpfc_iocbq **new_arr;
1710 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001711 size_t new_len;
1712 struct lpfc_sli *psli = &phba->sli;
1713 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001714
James Smart2e0fef82007-06-17 19:56:36 -05001715 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001716 iotag = psli->last_iotag;
1717 if(++iotag < psli->iocbq_lookup_len) {
1718 psli->last_iotag = iotag;
1719 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001720 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001721 iocbq->iotag = iotag;
1722 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001723 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001724 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1725 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001726 spin_unlock_irq(&phba->hbalock);
Kees Cook6396bb22018-06-12 14:03:40 -07001727 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001728 GFP_KERNEL);
1729 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001730 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001731 old_arr = psli->iocbq_lookup;
1732 if (new_len <= psli->iocbq_lookup_len) {
1733 /* highly unprobable case */
1734 kfree(new_arr);
1735 iotag = psli->last_iotag;
1736 if(++iotag < psli->iocbq_lookup_len) {
1737 psli->last_iotag = iotag;
1738 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001739 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001740 iocbq->iotag = iotag;
1741 return iotag;
1742 }
James Smart2e0fef82007-06-17 19:56:36 -05001743 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001744 return 0;
1745 }
1746 if (psli->iocbq_lookup)
1747 memcpy(new_arr, old_arr,
1748 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001749 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001750 psli->iocbq_lookup = new_arr;
1751 psli->iocbq_lookup_len = new_len;
1752 psli->last_iotag = iotag;
1753 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001754 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001755 iocbq->iotag = iotag;
1756 kfree(old_arr);
1757 return iotag;
1758 }
James Smart8f6d98d2006-08-01 07:34:00 -04001759 } else
James Smart2e0fef82007-06-17 19:56:36 -05001760 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001761
James Smartbc739052010-08-04 16:11:18 -04001762 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001763 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1764 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001765
James Bottomley604a3e32005-10-29 10:28:33 -05001766 return 0;
dea31012005-04-17 16:05:31 -05001767}
1768
James Smarte59058c2008-08-24 21:49:00 -04001769/**
James Smart3621a712009-04-06 18:47:14 -04001770 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001771 * @phba: Pointer to HBA context object.
1772 * @pring: Pointer to driver SLI ring object.
1773 * @iocb: Pointer to iocb slot in the ring.
1774 * @nextiocb: Pointer to driver iocb object which need to be
1775 * posted to firmware.
1776 *
1777 * This function is called with hbalock held to post a new iocb to
1778 * the firmware. This function copies the new iocb to ring iocb slot and
1779 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1780 * a completion call back for this iocb else the function will free the
1781 * iocb object.
1782 **/
dea31012005-04-17 16:05:31 -05001783static void
1784lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1785 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1786{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001787 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001788 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001789 * Set up an iotag
dea31012005-04-17 16:05:31 -05001790 */
James Bottomley604a3e32005-10-29 10:28:33 -05001791 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001792
James Smarte2a0a9d2008-12-04 22:40:02 -05001793
James Smarta58cbd52007-08-02 11:09:43 -04001794 if (pring->ringno == LPFC_ELS_RING) {
1795 lpfc_debugfs_slow_ring_trc(phba,
1796 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1797 *(((uint32_t *) &nextiocb->iocb) + 4),
1798 *(((uint32_t *) &nextiocb->iocb) + 6),
1799 *(((uint32_t *) &nextiocb->iocb) + 7));
1800 }
1801
dea31012005-04-17 16:05:31 -05001802 /*
1803 * Issue iocb command to adapter
1804 */
James Smart92d7f7b2007-06-17 19:56:38 -05001805 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001806 wmb();
1807 pring->stats.iocb_cmd++;
1808
1809 /*
1810 * If there is no completion routine to call, we can release the
1811 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1812 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1813 */
1814 if (nextiocb->iocb_cmpl)
1815 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001816 else
James Smart2e0fef82007-06-17 19:56:36 -05001817 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001818
1819 /*
1820 * Let the HBA know what IOCB slot will be the next one the
1821 * driver will put a command into.
1822 */
James Smart7e56aa22012-08-03 12:35:34 -04001823 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1824 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001825}
1826
James Smarte59058c2008-08-24 21:49:00 -04001827/**
James Smart3621a712009-04-06 18:47:14 -04001828 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001829 * @phba: Pointer to HBA context object.
1830 * @pring: Pointer to driver SLI ring object.
1831 *
1832 * The caller is not required to hold any lock for calling this function.
1833 * This function updates the chip attention bits for the ring to inform firmware
1834 * that there are pending work to be done for this ring and requests an
1835 * interrupt when there is space available in the ring. This function is
1836 * called when the driver is unable to post more iocbs to the ring due
1837 * to unavailability of space in the ring.
1838 **/
dea31012005-04-17 16:05:31 -05001839static void
James Smart2e0fef82007-06-17 19:56:36 -05001840lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001841{
1842 int ringno = pring->ringno;
1843
1844 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1845
1846 wmb();
1847
1848 /*
1849 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1850 * The HBA will tell us when an IOCB entry is available.
1851 */
1852 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1853 readl(phba->CAregaddr); /* flush */
1854
1855 pring->stats.iocb_cmd_full++;
1856}
1857
James Smarte59058c2008-08-24 21:49:00 -04001858/**
James Smart3621a712009-04-06 18:47:14 -04001859 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001860 * @phba: Pointer to HBA context object.
1861 * @pring: Pointer to driver SLI ring object.
1862 *
1863 * This function updates the chip attention register bit for the
1864 * given ring to inform HBA that there is more work to be done
1865 * in this ring. The caller is not required to hold any lock.
1866 **/
dea31012005-04-17 16:05:31 -05001867static void
James Smart2e0fef82007-06-17 19:56:36 -05001868lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001869{
1870 int ringno = pring->ringno;
1871
1872 /*
1873 * Tell the HBA that there is work to do in this ring.
1874 */
James Smart34b02dc2008-08-24 21:49:55 -04001875 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1876 wmb();
1877 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1878 readl(phba->CAregaddr); /* flush */
1879 }
dea31012005-04-17 16:05:31 -05001880}
1881
James Smarte59058c2008-08-24 21:49:00 -04001882/**
James Smart3621a712009-04-06 18:47:14 -04001883 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001884 * @phba: Pointer to HBA context object.
1885 * @pring: Pointer to driver SLI ring object.
1886 *
1887 * This function is called with hbalock held to post pending iocbs
1888 * in the txq to the firmware. This function is called when driver
1889 * detects space available in the ring.
1890 **/
dea31012005-04-17 16:05:31 -05001891static void
James Smart2e0fef82007-06-17 19:56:36 -05001892lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001893{
1894 IOCB_t *iocb;
1895 struct lpfc_iocbq *nextiocb;
1896
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001897 lockdep_assert_held(&phba->hbalock);
1898
dea31012005-04-17 16:05:31 -05001899 /*
1900 * Check to see if:
1901 * (a) there is anything on the txq to send
1902 * (b) link is up
1903 * (c) link attention events can be processed (fcp ring only)
1904 * (d) IOCB processing is not blocked by the outstanding mbox command.
1905 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001906
1907 if (lpfc_is_link_up(phba) &&
1908 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001909 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001910 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001911
1912 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1913 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1914 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1915
1916 if (iocb)
1917 lpfc_sli_update_ring(phba, pring);
1918 else
1919 lpfc_sli_update_full_ring(phba, pring);
1920 }
1921
1922 return;
1923}
1924
James Smarte59058c2008-08-24 21:49:00 -04001925/**
James Smart3621a712009-04-06 18:47:14 -04001926 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001927 * @phba: Pointer to HBA context object.
1928 * @hbqno: HBQ number.
1929 *
1930 * This function is called with hbalock held to get the next
1931 * available slot for the given HBQ. If there is free slot
1932 * available for the HBQ it will return pointer to the next available
1933 * HBQ entry else it will return NULL.
1934 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001935static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001936lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1937{
1938 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1939
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001940 lockdep_assert_held(&phba->hbalock);
1941
James Smarted957682007-06-17 19:56:37 -05001942 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1943 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1944 hbqp->next_hbqPutIdx = 0;
1945
1946 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001947 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001948 uint32_t getidx = le32_to_cpu(raw_index);
1949
1950 hbqp->local_hbqGetIdx = getidx;
1951
1952 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1953 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001954 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001955 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001956 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001957 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001958 hbqp->entry_count);
1959
1960 phba->link_state = LPFC_HBA_ERROR;
1961 return NULL;
1962 }
1963
1964 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1965 return NULL;
1966 }
1967
James Smart51ef4c22007-08-02 11:10:31 -04001968 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1969 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001970}
1971
James Smarte59058c2008-08-24 21:49:00 -04001972/**
James Smart3621a712009-04-06 18:47:14 -04001973 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001974 * @phba: Pointer to HBA context object.
1975 *
1976 * This function is called with no lock held to free all the
1977 * hbq buffers while uninitializing the SLI interface. It also
1978 * frees the HBQ buffers returned by the firmware but not yet
1979 * processed by the upper layers.
1980 **/
James Smarted957682007-06-17 19:56:37 -05001981void
1982lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1983{
James Smart92d7f7b2007-06-17 19:56:38 -05001984 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1985 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001986 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001987 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001988
James Smart51ef4c22007-08-02 11:10:31 -04001989 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001990 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001991 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001992 for (i = 0; i < hbq_count; ++i) {
1993 list_for_each_entry_safe(dmabuf, next_dmabuf,
1994 &phba->hbqs[i].hbq_buffer_list, list) {
1995 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1996 list_del(&hbq_buf->dbuf.list);
1997 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1998 }
James Smarta8adb832007-10-27 13:37:53 -04001999 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05002000 }
James Smart3163f722008-02-08 18:50:25 -05002001
2002 /* Mark the HBQs not in use */
2003 phba->hbq_in_use = 0;
2004 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05002005}
2006
James Smarte59058c2008-08-24 21:49:00 -04002007/**
James Smart3621a712009-04-06 18:47:14 -04002008 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002009 * @phba: Pointer to HBA context object.
2010 * @hbqno: HBQ number.
2011 * @hbq_buf: Pointer to HBQ buffer.
2012 *
2013 * This function is called with the hbalock held to post a
2014 * hbq buffer to the firmware. If the function finds an empty
2015 * slot in the HBQ, it will post the buffer. The function will return
2016 * pointer to the hbq entry if it successfully post the buffer
2017 * else it will return NULL.
2018 **/
James Smart3772a992009-05-22 14:50:54 -04002019static int
James Smarted957682007-06-17 19:56:37 -05002020lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05002021 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05002022{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002023 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002024 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2025}
2026
2027/**
2028 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2029 * @phba: Pointer to HBA context object.
2030 * @hbqno: HBQ number.
2031 * @hbq_buf: Pointer to HBQ buffer.
2032 *
2033 * This function is called with the hbalock held to post a hbq buffer to the
2034 * firmware. If the function finds an empty slot in the HBQ, it will post the
2035 * buffer and place it on the hbq_buffer_list. The function will return zero if
2036 * it successfully post the buffer else it will return an error.
2037 **/
2038static int
2039lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2040 struct hbq_dmabuf *hbq_buf)
2041{
James Smarted957682007-06-17 19:56:37 -05002042 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002043 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002044
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002045 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002046 /* Get next HBQ entry slot to use */
2047 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2048 if (hbqe) {
2049 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2050
James Smart92d7f7b2007-06-17 19:56:38 -05002051 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2052 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002053 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002054 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002055 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2056 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2057 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002058 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2059 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002060 /* flush */
James Smarted957682007-06-17 19:56:37 -05002061 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002062 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002063 return 0;
2064 } else
2065 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002066}
2067
James Smart4f774512009-05-22 14:52:35 -04002068/**
2069 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2070 * @phba: Pointer to HBA context object.
2071 * @hbqno: HBQ number.
2072 * @hbq_buf: Pointer to HBQ buffer.
2073 *
2074 * This function is called with the hbalock held to post an RQE to the SLI4
2075 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2076 * the hbq_buffer_list and return zero, otherwise it will return an error.
2077 **/
2078static int
2079lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2080 struct hbq_dmabuf *hbq_buf)
2081{
2082 int rc;
2083 struct lpfc_rqe hrqe;
2084 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002085 struct lpfc_queue *hrq;
2086 struct lpfc_queue *drq;
2087
2088 if (hbqno != LPFC_ELS_HBQ)
2089 return 1;
2090 hrq = phba->sli4_hba.hdr_rq;
2091 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002092
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002093 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002094 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2095 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2096 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2097 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002098 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002099 if (rc < 0)
2100 return rc;
James Smart895427b2017-02-12 13:52:30 -08002101 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002102 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2103 return 0;
2104}
2105
James Smarte59058c2008-08-24 21:49:00 -04002106/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002107static struct lpfc_hbq_init lpfc_els_hbq = {
2108 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002109 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002110 .mask_count = 0,
2111 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002112 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002113 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002114 .init_count = 40,
2115 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002116};
James Smarted957682007-06-17 19:56:37 -05002117
James Smarte59058c2008-08-24 21:49:00 -04002118/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002119struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002120 &lpfc_els_hbq,
2121};
2122
James Smarte59058c2008-08-24 21:49:00 -04002123/**
James Smart3621a712009-04-06 18:47:14 -04002124 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002125 * @phba: Pointer to HBA context object.
2126 * @hbqno: HBQ number.
2127 * @count: Number of HBQ buffers to be posted.
2128 *
James Smartd7c255b2008-08-24 21:50:00 -04002129 * This function is called with no lock held to post more hbq buffers to the
2130 * given HBQ. The function returns the number of HBQ buffers successfully
2131 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002132 **/
James Smart311464e2007-08-02 11:10:37 -04002133static int
James Smart92d7f7b2007-06-17 19:56:38 -05002134lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2135{
James Smartd7c255b2008-08-24 21:50:00 -04002136 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002137 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002138 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002139 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002140 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002141 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002142
James Smartd7c255b2008-08-24 21:50:00 -04002143 if ((phba->hbqs[hbqno].buffer_count + count) >
2144 lpfc_hbq_defs[hbqno]->entry_count)
2145 count = lpfc_hbq_defs[hbqno]->entry_count -
2146 phba->hbqs[hbqno].buffer_count;
2147 if (!count)
2148 return 0;
2149 /* Allocate HBQ entries */
2150 for (i = 0; i < count; i++) {
2151 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2152 if (!hbq_buffer)
2153 break;
2154 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2155 }
James Smart3163f722008-02-08 18:50:25 -05002156 /* Check whether HBQ is still in use */
2157 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002158 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002159 goto err;
2160 while (!list_empty(&hbq_buf_list)) {
2161 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2162 dbuf.list);
2163 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2164 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002165 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002166 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002167 posted++;
2168 } else
James Smart51ef4c22007-08-02 11:10:31 -04002169 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002170 }
James Smart3163f722008-02-08 18:50:25 -05002171 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002172 return posted;
2173err:
2174 spin_unlock_irqrestore(&phba->hbalock, flags);
2175 while (!list_empty(&hbq_buf_list)) {
2176 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2177 dbuf.list);
2178 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2179 }
James Smart92d7f7b2007-06-17 19:56:38 -05002180 return 0;
James Smarted957682007-06-17 19:56:37 -05002181}
2182
James Smarte59058c2008-08-24 21:49:00 -04002183/**
James Smart3621a712009-04-06 18:47:14 -04002184 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002185 * @phba: Pointer to HBA context object.
2186 * @qno: HBQ number.
2187 *
2188 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002189 * is called with no lock held. The function returns the number of HBQ entries
2190 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002191 **/
James Smarted957682007-06-17 19:56:37 -05002192int
James Smart92d7f7b2007-06-17 19:56:38 -05002193lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002194{
James Smartdef9c7a2009-12-21 17:02:28 -05002195 if (phba->sli_rev == LPFC_SLI_REV4)
2196 return 0;
2197 else
2198 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2199 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002200}
2201
James Smarte59058c2008-08-24 21:49:00 -04002202/**
James Smart3621a712009-04-06 18:47:14 -04002203 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002204 * @phba: Pointer to HBA context object.
2205 * @qno: HBQ queue number.
2206 *
2207 * This function is called from SLI initialization code path with
2208 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002209 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002210 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002211static int
James Smart92d7f7b2007-06-17 19:56:38 -05002212lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002213{
James Smartdef9c7a2009-12-21 17:02:28 -05002214 if (phba->sli_rev == LPFC_SLI_REV4)
2215 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002216 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002217 else
2218 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2219 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002220}
2221
James Smarte59058c2008-08-24 21:49:00 -04002222/**
James Smart3772a992009-05-22 14:50:54 -04002223 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2224 * @phba: Pointer to HBA context object.
2225 * @hbqno: HBQ number.
2226 *
2227 * This function removes the first hbq buffer on an hbq list and returns a
2228 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2229 **/
2230static struct hbq_dmabuf *
2231lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2232{
2233 struct lpfc_dmabuf *d_buf;
2234
2235 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2236 if (!d_buf)
2237 return NULL;
2238 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2239}
2240
2241/**
James Smart2d7dbc42017-02-12 13:52:35 -08002242 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2243 * @phba: Pointer to HBA context object.
2244 * @hbqno: HBQ number.
2245 *
2246 * This function removes the first RQ buffer on an RQ buffer list and returns a
2247 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2248 **/
2249static struct rqb_dmabuf *
2250lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2251{
2252 struct lpfc_dmabuf *h_buf;
2253 struct lpfc_rqb *rqbp;
2254
2255 rqbp = hrq->rqbp;
2256 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2257 struct lpfc_dmabuf, list);
2258 if (!h_buf)
2259 return NULL;
2260 rqbp->buffer_count--;
2261 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2262}
2263
2264/**
James Smart3621a712009-04-06 18:47:14 -04002265 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002266 * @phba: Pointer to HBA context object.
2267 * @tag: Tag of the hbq buffer.
2268 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002269 * This function searches for the hbq buffer associated with the given tag in
2270 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2271 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002272 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002273static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002274lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2275{
James Smart92d7f7b2007-06-17 19:56:38 -05002276 struct lpfc_dmabuf *d_buf;
2277 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002278 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002279
James Smart51ef4c22007-08-02 11:10:31 -04002280 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002281 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002282 return NULL;
2283
James Smart3772a992009-05-22 14:50:54 -04002284 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002285 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002286 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002287 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002288 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002289 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002290 }
2291 }
James Smart3772a992009-05-22 14:50:54 -04002292 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002293 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002294 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002295 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002296 return NULL;
James Smarted957682007-06-17 19:56:37 -05002297}
2298
James Smarte59058c2008-08-24 21:49:00 -04002299/**
James Smart3621a712009-04-06 18:47:14 -04002300 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002301 * @phba: Pointer to HBA context object.
2302 * @hbq_buffer: Pointer to HBQ buffer.
2303 *
2304 * This function is called with hbalock. This function gives back
2305 * the hbq buffer to firmware. If the HBQ does not have space to
2306 * post the buffer, it will free the buffer.
2307 **/
James Smarted957682007-06-17 19:56:37 -05002308void
James Smart51ef4c22007-08-02 11:10:31 -04002309lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002310{
2311 uint32_t hbqno;
2312
James Smart51ef4c22007-08-02 11:10:31 -04002313 if (hbq_buffer) {
2314 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002315 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002316 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002317 }
2318}
2319
James Smarte59058c2008-08-24 21:49:00 -04002320/**
James Smart3621a712009-04-06 18:47:14 -04002321 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002322 * @mbxCommand: mailbox command code.
2323 *
2324 * This function is called by the mailbox event handler function to verify
2325 * that the completed mailbox command is a legitimate mailbox command. If the
2326 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2327 * and the mailbox event handler will take the HBA offline.
2328 **/
dea31012005-04-17 16:05:31 -05002329static int
2330lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2331{
2332 uint8_t ret;
2333
2334 switch (mbxCommand) {
2335 case MBX_LOAD_SM:
2336 case MBX_READ_NV:
2337 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002338 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002339 case MBX_RUN_BIU_DIAG:
2340 case MBX_INIT_LINK:
2341 case MBX_DOWN_LINK:
2342 case MBX_CONFIG_LINK:
2343 case MBX_CONFIG_RING:
2344 case MBX_RESET_RING:
2345 case MBX_READ_CONFIG:
2346 case MBX_READ_RCONFIG:
2347 case MBX_READ_SPARM:
2348 case MBX_READ_STATUS:
2349 case MBX_READ_RPI:
2350 case MBX_READ_XRI:
2351 case MBX_READ_REV:
2352 case MBX_READ_LNK_STAT:
2353 case MBX_REG_LOGIN:
2354 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002355 case MBX_CLEAR_LA:
2356 case MBX_DUMP_MEMORY:
2357 case MBX_DUMP_CONTEXT:
2358 case MBX_RUN_DIAGS:
2359 case MBX_RESTART:
2360 case MBX_UPDATE_CFG:
2361 case MBX_DOWN_LOAD:
2362 case MBX_DEL_LD_ENTRY:
2363 case MBX_RUN_PROGRAM:
2364 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002365 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002366 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002367 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002368 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002369 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002370 case MBX_LOAD_AREA:
2371 case MBX_RUN_BIU_DIAG64:
2372 case MBX_CONFIG_PORT:
2373 case MBX_READ_SPARM64:
2374 case MBX_READ_RPI64:
2375 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002376 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002377 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002378 case MBX_SET_DEBUG:
2379 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002380 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002381 case MBX_REG_VPI:
2382 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002383 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002384 case MBX_PORT_CAPABILITIES:
2385 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002386 case MBX_SLI4_CONFIG:
2387 case MBX_SLI4_REQ_FTRS:
2388 case MBX_REG_FCFI:
2389 case MBX_UNREG_FCFI:
2390 case MBX_REG_VFI:
2391 case MBX_UNREG_VFI:
2392 case MBX_INIT_VPI:
2393 case MBX_INIT_VFI:
2394 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002395 case MBX_READ_EVENT_LOG_STATUS:
2396 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002397 case MBX_SECURITY_MGMT:
2398 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002399 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002400 ret = mbxCommand;
2401 break;
2402 default:
2403 ret = MBX_SHUTDOWN;
2404 break;
2405 }
James Smart2e0fef82007-06-17 19:56:36 -05002406 return ret;
dea31012005-04-17 16:05:31 -05002407}
James Smarte59058c2008-08-24 21:49:00 -04002408
2409/**
James Smart3621a712009-04-06 18:47:14 -04002410 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002411 * @phba: Pointer to HBA context object.
2412 * @pmboxq: Pointer to mailbox command.
2413 *
2414 * This is completion handler function for mailbox commands issued from
2415 * lpfc_sli_issue_mbox_wait function. This function is called by the
2416 * mailbox event handler function with no lock held. This function
2417 * will wake up thread waiting on the wait queue pointed by context1
2418 * of the mailbox.
2419 **/
James Smart04c68492009-05-22 14:52:52 -04002420void
James Smart2e0fef82007-06-17 19:56:36 -05002421lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002422{
James Smart858c9f62007-06-17 19:56:39 -05002423 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002424 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002425
2426 /*
James Smarte29d74f2018-03-05 12:04:07 -08002427 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002428 * continued running.
2429 */
James Smart7054a602007-04-25 09:52:34 -04002430 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002431 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002432 pmbox_done = (struct completion *)pmboxq->context3;
2433 if (pmbox_done)
2434 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002435 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002436 return;
2437}
2438
James Smarte59058c2008-08-24 21:49:00 -04002439
2440/**
James Smart3621a712009-04-06 18:47:14 -04002441 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002442 * @phba: Pointer to HBA context object.
2443 * @pmb: Pointer to mailbox object.
2444 *
2445 * This function is the default mailbox completion handler. It
2446 * frees the memory resources associated with the completed mailbox
2447 * command. If the completed command is a REG_LOGIN mailbox command,
2448 * this function will issue a UREG_LOGIN to re-claim the RPI.
2449 **/
dea31012005-04-17 16:05:31 -05002450void
James Smart2e0fef82007-06-17 19:56:36 -05002451lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002452{
James Smartd439d282010-09-29 11:18:45 -04002453 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002454 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002455 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002456 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002457 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002458 int rc;
2459
James Smart3e1f0712018-11-29 16:09:29 -08002460 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smart7054a602007-04-25 09:52:34 -04002461
dea31012005-04-17 16:05:31 -05002462 if (mp) {
2463 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2464 kfree(mp);
2465 }
James Smart7054a602007-04-25 09:52:34 -04002466
2467 /*
2468 * If a REG_LOGIN succeeded after node is destroyed or node
2469 * is in re-discovery driver need to cleanup the RPI.
2470 */
James Smart2e0fef82007-06-17 19:56:36 -05002471 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002472 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2473 !pmb->u.mb.mbxStatus) {
2474 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002475 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002476 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002477 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002478 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002479 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2480 if (rc != MBX_NOT_FINISHED)
2481 return;
2482 }
2483
James Smart695a8142010-01-26 23:08:03 -05002484 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2485 !(phba->pport->load_flag & FC_UNLOADING) &&
2486 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002487 shost = lpfc_shost_from_vport(vport);
2488 spin_lock_irq(shost->host_lock);
2489 vport->vpi_state |= LPFC_VPI_REGISTERED;
2490 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2491 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002492 }
2493
James Smartd439d282010-09-29 11:18:45 -04002494 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -08002495 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -04002496 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002497 pmb->ctx_buf = NULL;
2498 pmb->ctx_ndlp = NULL;
2499 }
2500
2501 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2502 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2503
2504 /* Check to see if there are any deferred events to process */
2505 if (ndlp) {
2506 lpfc_printf_vlog(
2507 vport,
2508 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2509 "1438 UNREG cmpl deferred mbox x%x "
2510 "on NPort x%x Data: x%x x%x %p\n",
2511 ndlp->nlp_rpi, ndlp->nlp_DID,
2512 ndlp->nlp_flag, ndlp->nlp_defer_did, ndlp);
2513
2514 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2515 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
James Smart00292e02018-12-13 15:17:55 -08002516 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002517 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2518 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002519 } else {
2520 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002521 }
James Smartdea16bd2018-11-29 16:09:30 -08002522 }
James Smart3e1f0712018-11-29 16:09:29 -08002523 pmb->ctx_ndlp = NULL;
James Smartd439d282010-09-29 11:18:45 -04002524 }
2525
James Smartdcf2a4e2010-09-29 11:18:53 -04002526 /* Check security permission status on INIT_LINK mailbox command */
2527 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2528 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2529 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2530 "2860 SLI authentication is required "
2531 "for INIT_LINK but has not done yet\n");
2532
James Smart04c68492009-05-22 14:52:52 -04002533 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2534 lpfc_sli4_mbox_cmd_free(phba, pmb);
2535 else
2536 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002537}
James Smartbe6bb942015-04-07 15:07:22 -04002538 /**
2539 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2540 * @phba: Pointer to HBA context object.
2541 * @pmb: Pointer to mailbox object.
2542 *
2543 * This function is the unreg rpi mailbox completion handler. It
2544 * frees the memory resources associated with the completed mailbox
2545 * command. An additional refrenece is put on the ndlp to prevent
2546 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2547 * the unreg mailbox command completes, this routine puts the
2548 * reference back.
2549 *
2550 **/
2551void
2552lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2553{
2554 struct lpfc_vport *vport = pmb->vport;
2555 struct lpfc_nodelist *ndlp;
2556
James Smart3e1f0712018-11-29 16:09:29 -08002557 ndlp = pmb->ctx_ndlp;
James Smartbe6bb942015-04-07 15:07:22 -04002558 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2559 if (phba->sli_rev == LPFC_SLI_REV4 &&
2560 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002561 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002562 LPFC_SLI_INTF_IF_TYPE_2)) {
2563 if (ndlp) {
James Smartdea16bd2018-11-29 16:09:30 -08002564 lpfc_printf_vlog(
2565 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2566 "0010 UNREG_LOGIN vpi:%x "
2567 "rpi:%x DID:%x defer x%x flg x%x "
2568 "map:%x %p\n",
2569 vport->vpi, ndlp->nlp_rpi,
2570 ndlp->nlp_DID, ndlp->nlp_defer_did,
2571 ndlp->nlp_flag,
2572 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002573 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002574 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002575
2576 /* Check to see if there are any deferred
2577 * events to process
2578 */
2579 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2580 (ndlp->nlp_defer_did !=
2581 NLP_EVT_NOTHING_PENDING)) {
2582 lpfc_printf_vlog(
2583 vport, KERN_INFO, LOG_DISCOVERY,
2584 "4111 UNREG cmpl deferred "
2585 "clr x%x on "
2586 "NPort x%x Data: x%x %p\n",
2587 ndlp->nlp_rpi, ndlp->nlp_DID,
2588 ndlp->nlp_defer_did, ndlp);
James Smart00292e02018-12-13 15:17:55 -08002589 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002590 ndlp->nlp_defer_did =
2591 NLP_EVT_NOTHING_PENDING;
2592 lpfc_issue_els_plogi(
2593 vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002594 } else {
2595 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002596 }
James Smartbe6bb942015-04-07 15:07:22 -04002597 }
2598 }
2599 }
2600
2601 mempool_free(pmb, phba->mbox_mem_pool);
2602}
dea31012005-04-17 16:05:31 -05002603
James Smarte59058c2008-08-24 21:49:00 -04002604/**
James Smart3621a712009-04-06 18:47:14 -04002605 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002606 * @phba: Pointer to HBA context object.
2607 *
2608 * This function is called with no lock held. This function processes all
2609 * the completed mailbox commands and gives it to upper layers. The interrupt
2610 * service routine processes mailbox completion interrupt and adds completed
2611 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2612 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2613 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2614 * function returns the mailbox commands to the upper layer by calling the
2615 * completion handler function of each mailbox.
2616 **/
dea31012005-04-17 16:05:31 -05002617int
James Smart2e0fef82007-06-17 19:56:36 -05002618lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002619{
James Smart92d7f7b2007-06-17 19:56:38 -05002620 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002621 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002622 int rc;
2623 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002624
2625 phba->sli.slistat.mbox_event++;
2626
James Smart92d7f7b2007-06-17 19:56:38 -05002627 /* Get all completed mailboxe buffers into the cmplq */
2628 spin_lock_irq(&phba->hbalock);
2629 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2630 spin_unlock_irq(&phba->hbalock);
2631
dea31012005-04-17 16:05:31 -05002632 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002633 do {
2634 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2635 if (pmb == NULL)
2636 break;
2637
James Smart04c68492009-05-22 14:52:52 -04002638 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002639
James Smart858c9f62007-06-17 19:56:39 -05002640 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2641 if (pmb->vport) {
2642 lpfc_debugfs_disc_trc(pmb->vport,
2643 LPFC_DISC_TRC_MBOX_VPORT,
2644 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2645 (uint32_t)pmbox->mbxCommand,
2646 pmbox->un.varWords[0],
2647 pmbox->un.varWords[1]);
2648 }
2649 else {
2650 lpfc_debugfs_disc_trc(phba->pport,
2651 LPFC_DISC_TRC_MBOX,
2652 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2653 (uint32_t)pmbox->mbxCommand,
2654 pmbox->un.varWords[0],
2655 pmbox->un.varWords[1]);
2656 }
2657 }
2658
dea31012005-04-17 16:05:31 -05002659 /*
2660 * It is a fatal error if unknown mbox command completion.
2661 */
2662 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2663 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002664 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002665 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002666 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002667 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002668 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002669 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002670 lpfc_sli_config_mbox_subsys_get(phba,
2671 pmb),
2672 lpfc_sli_config_mbox_opcode_get(phba,
2673 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002674 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002675 phba->work_hs = HS_FFER3;
2676 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002677 continue;
dea31012005-04-17 16:05:31 -05002678 }
2679
dea31012005-04-17 16:05:31 -05002680 if (pmbox->mbxStatus) {
2681 phba->sli.slistat.mbox_stat_err++;
2682 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2683 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002684 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002685 LOG_MBOX | LOG_SLI,
2686 "(%d):0305 Mbox cmd cmpl "
2687 "error - RETRYing Data: x%x "
2688 "(x%x/x%x) x%x x%x x%x\n",
2689 pmb->vport ? pmb->vport->vpi : 0,
2690 pmbox->mbxCommand,
2691 lpfc_sli_config_mbox_subsys_get(phba,
2692 pmb),
2693 lpfc_sli_config_mbox_opcode_get(phba,
2694 pmb),
2695 pmbox->mbxStatus,
2696 pmbox->un.varWords[0],
2697 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002698 pmbox->mbxStatus = 0;
2699 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002700 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002701 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002702 continue;
dea31012005-04-17 16:05:31 -05002703 }
2704 }
2705
2706 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002707 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002708 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002709 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2710 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002711 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002712 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002713 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2714 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002715 pmb->mbox_cmpl,
2716 *((uint32_t *) pmbox),
2717 pmbox->un.varWords[0],
2718 pmbox->un.varWords[1],
2719 pmbox->un.varWords[2],
2720 pmbox->un.varWords[3],
2721 pmbox->un.varWords[4],
2722 pmbox->un.varWords[5],
2723 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002724 pmbox->un.varWords[7],
2725 pmbox->un.varWords[8],
2726 pmbox->un.varWords[9],
2727 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002728
James Smart92d7f7b2007-06-17 19:56:38 -05002729 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002730 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002731 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002732 return 0;
dea31012005-04-17 16:05:31 -05002733}
James Smart92d7f7b2007-06-17 19:56:38 -05002734
James Smarte59058c2008-08-24 21:49:00 -04002735/**
James Smart3621a712009-04-06 18:47:14 -04002736 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002737 * @phba: Pointer to HBA context object.
2738 * @pring: Pointer to driver SLI ring object.
2739 * @tag: buffer tag.
2740 *
2741 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2742 * is set in the tag the buffer is posted for a particular exchange,
2743 * the function will return the buffer without replacing the buffer.
2744 * If the buffer is for unsolicited ELS or CT traffic, this function
2745 * returns the buffer and also posts another buffer to the firmware.
2746 **/
James Smart76bb24e2007-10-27 13:38:00 -04002747static struct lpfc_dmabuf *
2748lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002749 struct lpfc_sli_ring *pring,
2750 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002751{
James Smart9f1e1b52008-12-04 22:39:40 -05002752 struct hbq_dmabuf *hbq_entry;
2753
James Smart76bb24e2007-10-27 13:38:00 -04002754 if (tag & QUE_BUFTAG_BIT)
2755 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002756 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2757 if (!hbq_entry)
2758 return NULL;
2759 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002760}
James Smart57127f12007-10-27 13:37:05 -04002761
James Smart3772a992009-05-22 14:50:54 -04002762/**
2763 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2764 * @phba: Pointer to HBA context object.
2765 * @pring: Pointer to driver SLI ring object.
2766 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2767 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2768 * @fch_type: the type for the first frame of the sequence.
2769 *
2770 * This function is called with no lock held. This function uses the r_ctl and
2771 * type of the received sequence to find the correct callback function to call
2772 * to process the sequence.
2773 **/
2774static int
2775lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2776 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2777 uint32_t fch_type)
2778{
2779 int i;
2780
James Smartf358dd02017-02-12 13:52:34 -08002781 switch (fch_type) {
2782 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002783 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002784 return 1;
2785 default:
2786 break;
2787 }
2788
James Smart3772a992009-05-22 14:50:54 -04002789 /* unSolicited Responses */
2790 if (pring->prt[0].profile) {
2791 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2792 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2793 saveq);
2794 return 1;
2795 }
2796 /* We must search, based on rctl / type
2797 for the right routine */
2798 for (i = 0; i < pring->num_mask; i++) {
2799 if ((pring->prt[i].rctl == fch_r_ctl) &&
2800 (pring->prt[i].type == fch_type)) {
2801 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2802 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2803 (phba, pring, saveq);
2804 return 1;
2805 }
2806 }
2807 return 0;
2808}
James Smarte59058c2008-08-24 21:49:00 -04002809
2810/**
James Smart3621a712009-04-06 18:47:14 -04002811 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002812 * @phba: Pointer to HBA context object.
2813 * @pring: Pointer to driver SLI ring object.
2814 * @saveq: Pointer to the unsolicited iocb.
2815 *
2816 * This function is called with no lock held by the ring event handler
2817 * when there is an unsolicited iocb posted to the response ring by the
2818 * firmware. This function gets the buffer associated with the iocbs
2819 * and calls the event handler for the ring. This function handles both
2820 * qring buffers and hbq buffers.
2821 * When the function returns 1 the caller can free the iocb object otherwise
2822 * upper layer functions will free the iocb objects.
2823 **/
dea31012005-04-17 16:05:31 -05002824static int
2825lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2826 struct lpfc_iocbq *saveq)
2827{
2828 IOCB_t * irsp;
2829 WORD5 * w5p;
2830 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002831 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002832 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002833
dea31012005-04-17 16:05:31 -05002834 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002835
2836 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2837 if (pring->lpfc_sli_rcv_async_status)
2838 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2839 else
2840 lpfc_printf_log(phba,
2841 KERN_WARNING,
2842 LOG_SLI,
2843 "0316 Ring %d handler: unexpected "
2844 "ASYNC_STATUS iocb received evt_code "
2845 "0x%x\n",
2846 pring->ringno,
2847 irsp->un.asyncstat.evt_code);
2848 return 1;
2849 }
2850
James Smart3163f722008-02-08 18:50:25 -05002851 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2852 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2853 if (irsp->ulpBdeCount > 0) {
2854 dmzbuf = lpfc_sli_get_buff(phba, pring,
2855 irsp->un.ulpWord[3]);
2856 lpfc_in_buf_free(phba, dmzbuf);
2857 }
2858
2859 if (irsp->ulpBdeCount > 1) {
2860 dmzbuf = lpfc_sli_get_buff(phba, pring,
2861 irsp->unsli3.sli3Words[3]);
2862 lpfc_in_buf_free(phba, dmzbuf);
2863 }
2864
2865 if (irsp->ulpBdeCount > 2) {
2866 dmzbuf = lpfc_sli_get_buff(phba, pring,
2867 irsp->unsli3.sli3Words[7]);
2868 lpfc_in_buf_free(phba, dmzbuf);
2869 }
2870
2871 return 1;
2872 }
2873
James Smart92d7f7b2007-06-17 19:56:38 -05002874 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002875 if (irsp->ulpBdeCount != 0) {
2876 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002877 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002878 if (!saveq->context2)
2879 lpfc_printf_log(phba,
2880 KERN_ERR,
2881 LOG_SLI,
2882 "0341 Ring %d Cannot find buffer for "
2883 "an unsolicited iocb. tag 0x%x\n",
2884 pring->ringno,
2885 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002886 }
2887 if (irsp->ulpBdeCount == 2) {
2888 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002889 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002890 if (!saveq->context3)
2891 lpfc_printf_log(phba,
2892 KERN_ERR,
2893 LOG_SLI,
2894 "0342 Ring %d Cannot find buffer for an"
2895 " unsolicited iocb. tag 0x%x\n",
2896 pring->ringno,
2897 irsp->unsli3.sli3Words[7]);
2898 }
2899 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002900 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002901 if (irsp->ulpBdeCount != 0) {
2902 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2903 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002904 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002905 lpfc_printf_log(phba,
2906 KERN_ERR,
2907 LOG_SLI,
2908 "0343 Ring %d Cannot find "
2909 "buffer for an unsolicited iocb"
2910 ". tag 0x%x\n", pring->ringno,
2911 irsp->un.ulpWord[3]);
2912 }
2913 if (irsp->ulpBdeCount == 2) {
2914 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2915 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002916 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002917 lpfc_printf_log(phba,
2918 KERN_ERR,
2919 LOG_SLI,
2920 "0344 Ring %d Cannot find "
2921 "buffer for an unsolicited "
2922 "iocb. tag 0x%x\n",
2923 pring->ringno,
2924 irsp->unsli3.sli3Words[7]);
2925 }
2926 }
James Smart92d7f7b2007-06-17 19:56:38 -05002927 }
James Smart9c2face2008-01-11 01:53:18 -05002928 if (irsp->ulpBdeCount != 0 &&
2929 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2930 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2931 int found = 0;
2932
2933 /* search continue save q for same XRI */
2934 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002935 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2936 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002937 list_add_tail(&saveq->list, &iocbq->list);
2938 found = 1;
2939 break;
2940 }
2941 }
2942 if (!found)
2943 list_add_tail(&saveq->clist,
2944 &pring->iocb_continue_saveq);
2945 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2946 list_del_init(&iocbq->clist);
2947 saveq = iocbq;
2948 irsp = &(saveq->iocb);
2949 } else
2950 return 0;
2951 }
2952 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2953 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2954 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002955 Rctl = FC_RCTL_ELS_REQ;
2956 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002957 } else {
2958 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2959 Rctl = w5p->hcsw.Rctl;
2960 Type = w5p->hcsw.Type;
2961
2962 /* Firmware Workaround */
2963 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2964 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2965 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002966 Rctl = FC_RCTL_ELS_REQ;
2967 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002968 w5p->hcsw.Rctl = Rctl;
2969 w5p->hcsw.Type = Type;
2970 }
2971 }
James Smart92d7f7b2007-06-17 19:56:38 -05002972
James Smart3772a992009-05-22 14:50:54 -04002973 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002974 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002975 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002976 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002977 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002978
James Smart92d7f7b2007-06-17 19:56:38 -05002979 return 1;
dea31012005-04-17 16:05:31 -05002980}
2981
James Smarte59058c2008-08-24 21:49:00 -04002982/**
James Smart3621a712009-04-06 18:47:14 -04002983 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002984 * @phba: Pointer to HBA context object.
2985 * @pring: Pointer to driver SLI ring object.
2986 * @prspiocb: Pointer to response iocb object.
2987 *
2988 * This function looks up the iocb_lookup table to get the command iocb
2989 * corresponding to the given response iocb using the iotag of the
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002990 * response iocb. This function is called with the hbalock held
2991 * for sli3 devices or the ring_lock for sli4 devices.
James Smarte59058c2008-08-24 21:49:00 -04002992 * This function returns the command iocb object if it finds the command
2993 * iocb else returns NULL.
2994 **/
dea31012005-04-17 16:05:31 -05002995static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002996lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2997 struct lpfc_sli_ring *pring,
2998 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002999{
dea31012005-04-17 16:05:31 -05003000 struct lpfc_iocbq *cmd_iocb = NULL;
3001 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003002 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003003
James Bottomley604a3e32005-10-29 10:28:33 -05003004 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05003005
James Bottomley604a3e32005-10-29 10:28:33 -05003006 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3007 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003008 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07003009 /* remove from txcmpl queue list */
3010 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04003011 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003012 pring->txcmplq_cnt--;
James Smart89533e92016-10-13 15:06:15 -07003013 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003014 }
dea31012005-04-17 16:05:31 -05003015 }
3016
dea31012005-04-17 16:05:31 -05003017 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07003018 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05003019 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003020 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05003021 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05003022 return NULL;
3023}
3024
James Smarte59058c2008-08-24 21:49:00 -04003025/**
James Smart3772a992009-05-22 14:50:54 -04003026 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3027 * @phba: Pointer to HBA context object.
3028 * @pring: Pointer to driver SLI ring object.
3029 * @iotag: IOCB tag.
3030 *
3031 * This function looks up the iocb_lookup table to get the command iocb
3032 * corresponding to the given iotag. This function is called with the
3033 * hbalock held.
3034 * This function returns the command iocb object if it finds the command
3035 * iocb else returns NULL.
3036 **/
3037static struct lpfc_iocbq *
3038lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3039 struct lpfc_sli_ring *pring, uint16_t iotag)
3040{
James Smart895427b2017-02-12 13:52:30 -08003041 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04003042
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003043 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04003044 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3045 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003046 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3047 /* remove from txcmpl queue list */
3048 list_del_init(&cmd_iocb->list);
3049 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003050 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04003051 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003052 }
James Smart3772a992009-05-22 14:50:54 -04003053 }
James Smart89533e92016-10-13 15:06:15 -07003054
James Smart3772a992009-05-22 14:50:54 -04003055 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08003056 "0372 iotag x%x lookup error: max iotag (x%x) "
3057 "iocb_flag x%x\n",
3058 iotag, phba->sli.last_iotag,
3059 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003060 return NULL;
3061}
3062
3063/**
James Smart3621a712009-04-06 18:47:14 -04003064 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003065 * @phba: Pointer to HBA context object.
3066 * @pring: Pointer to driver SLI ring object.
3067 * @saveq: Pointer to the response iocb to be processed.
3068 *
3069 * This function is called by the ring event handler for non-fcp
3070 * rings when there is a new response iocb in the response ring.
3071 * The caller is not required to hold any locks. This function
3072 * gets the command iocb associated with the response iocb and
3073 * calls the completion handler for the command iocb. If there
3074 * is no completion handler, the function will free the resources
3075 * associated with command iocb. If the response iocb is for
3076 * an already aborted command iocb, the status of the completion
3077 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3078 * This function always returns 1.
3079 **/
dea31012005-04-17 16:05:31 -05003080static int
James Smart2e0fef82007-06-17 19:56:36 -05003081lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003082 struct lpfc_iocbq *saveq)
3083{
James Smart2e0fef82007-06-17 19:56:36 -05003084 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003085 int rc = 1;
3086 unsigned long iflag;
3087
3088 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003089 if (phba->sli_rev == LPFC_SLI_REV4)
3090 spin_lock_irqsave(&pring->ring_lock, iflag);
3091 else
3092 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003093 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003094 if (phba->sli_rev == LPFC_SLI_REV4)
3095 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3096 else
3097 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart2e0fef82007-06-17 19:56:36 -05003098
dea31012005-04-17 16:05:31 -05003099 if (cmdiocbp) {
3100 if (cmdiocbp->iocb_cmpl) {
3101 /*
James Smartea2151b2008-09-07 11:52:10 -04003102 * If an ELS command failed send an event to mgmt
3103 * application.
3104 */
3105 if (saveq->iocb.ulpStatus &&
3106 (pring->ringno == LPFC_ELS_RING) &&
3107 (cmdiocbp->iocb.ulpCommand ==
3108 CMD_ELS_REQUEST64_CR))
3109 lpfc_send_els_failure_event(phba,
3110 cmdiocbp, saveq);
3111
3112 /*
dea31012005-04-17 16:05:31 -05003113 * Post all ELS completions to the worker thread.
3114 * All other are passed to the completion callback.
3115 */
3116 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003117 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3118 (cmdiocbp->iocb_flag &
3119 LPFC_DRIVER_ABORTED)) {
3120 spin_lock_irqsave(&phba->hbalock,
3121 iflag);
James Smart07951072007-04-25 09:51:38 -04003122 cmdiocbp->iocb_flag &=
3123 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003124 spin_unlock_irqrestore(&phba->hbalock,
3125 iflag);
James Smart07951072007-04-25 09:51:38 -04003126 saveq->iocb.ulpStatus =
3127 IOSTAT_LOCAL_REJECT;
3128 saveq->iocb.un.ulpWord[4] =
3129 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003130
3131 /* Firmware could still be in progress
3132 * of DMAing payload, so don't free data
3133 * buffer till after a hbeat.
3134 */
James Smart341af102010-01-26 23:07:37 -05003135 spin_lock_irqsave(&phba->hbalock,
3136 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003137 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003138 spin_unlock_irqrestore(&phba->hbalock,
3139 iflag);
3140 }
James Smart0f65ff62010-02-26 14:14:23 -05003141 if (phba->sli_rev == LPFC_SLI_REV4) {
3142 if (saveq->iocb_flag &
3143 LPFC_EXCHANGE_BUSY) {
3144 /* Set cmdiocb flag for the
3145 * exchange busy so sgl (xri)
3146 * will not be released until
3147 * the abort xri is received
3148 * from hba.
3149 */
3150 spin_lock_irqsave(
3151 &phba->hbalock, iflag);
3152 cmdiocbp->iocb_flag |=
3153 LPFC_EXCHANGE_BUSY;
3154 spin_unlock_irqrestore(
3155 &phba->hbalock, iflag);
3156 }
3157 if (cmdiocbp->iocb_flag &
3158 LPFC_DRIVER_ABORTED) {
3159 /*
3160 * Clear LPFC_DRIVER_ABORTED
3161 * bit in case it was driver
3162 * initiated abort.
3163 */
3164 spin_lock_irqsave(
3165 &phba->hbalock, iflag);
3166 cmdiocbp->iocb_flag &=
3167 ~LPFC_DRIVER_ABORTED;
3168 spin_unlock_irqrestore(
3169 &phba->hbalock, iflag);
3170 cmdiocbp->iocb.ulpStatus =
3171 IOSTAT_LOCAL_REJECT;
3172 cmdiocbp->iocb.un.ulpWord[4] =
3173 IOERR_ABORT_REQUESTED;
3174 /*
3175 * For SLI4, irsiocb contains
3176 * NO_XRI in sli_xritag, it
3177 * shall not affect releasing
3178 * sgl (xri) process.
3179 */
3180 saveq->iocb.ulpStatus =
3181 IOSTAT_LOCAL_REJECT;
3182 saveq->iocb.un.ulpWord[4] =
3183 IOERR_SLI_ABORTED;
3184 spin_lock_irqsave(
3185 &phba->hbalock, iflag);
3186 saveq->iocb_flag |=
3187 LPFC_DELAY_MEM_FREE;
3188 spin_unlock_irqrestore(
3189 &phba->hbalock, iflag);
3190 }
James Smart07951072007-04-25 09:51:38 -04003191 }
dea31012005-04-17 16:05:31 -05003192 }
James Smart2e0fef82007-06-17 19:56:36 -05003193 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003194 } else
3195 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003196 } else {
3197 /*
3198 * Unknown initiating command based on the response iotag.
3199 * This could be the case on the ELS ring because of
3200 * lpfc_els_abort().
3201 */
3202 if (pring->ringno != LPFC_ELS_RING) {
3203 /*
3204 * Ring <ringno> handler: unexpected completion IoTag
3205 * <IoTag>
3206 */
James Smarta257bf92009-04-06 18:48:10 -04003207 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003208 "0322 Ring %d handler: "
3209 "unexpected completion IoTag x%x "
3210 "Data: x%x x%x x%x x%x\n",
3211 pring->ringno,
3212 saveq->iocb.ulpIoTag,
3213 saveq->iocb.ulpStatus,
3214 saveq->iocb.un.ulpWord[4],
3215 saveq->iocb.ulpCommand,
3216 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003217 }
3218 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003219
dea31012005-04-17 16:05:31 -05003220 return rc;
3221}
3222
James Smarte59058c2008-08-24 21:49:00 -04003223/**
James Smart3621a712009-04-06 18:47:14 -04003224 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003225 * @phba: Pointer to HBA context object.
3226 * @pring: Pointer to driver SLI ring object.
3227 *
3228 * This function is called from the iocb ring event handlers when
3229 * put pointer is ahead of the get pointer for a ring. This function signal
3230 * an error attention condition to the worker thread and the worker
3231 * thread will transition the HBA to offline state.
3232 **/
James Smart2e0fef82007-06-17 19:56:36 -05003233static void
3234lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003235{
James Smart34b02dc2008-08-24 21:49:55 -04003236 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003237 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003238 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003239 * rsp ring <portRspMax>
3240 */
3241 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003242 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003243 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003244 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003245 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003246
James Smart2e0fef82007-06-17 19:56:36 -05003247 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003248
3249 /*
3250 * All error attention handlers are posted to
3251 * worker thread
3252 */
3253 phba->work_ha |= HA_ERATT;
3254 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003255
James Smart5e9d9b82008-06-14 22:52:53 -04003256 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003257
3258 return;
3259}
3260
James Smarte59058c2008-08-24 21:49:00 -04003261/**
James Smart3621a712009-04-06 18:47:14 -04003262 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003263 * @ptr: Pointer to address of HBA context object.
3264 *
3265 * This function is invoked by the Error Attention polling timer when the
3266 * timer times out. It will check the SLI Error Attention register for
3267 * possible attention events. If so, it will post an Error Attention event
3268 * and wake up worker thread to process it. Otherwise, it will set up the
3269 * Error Attention polling timer for the next poll.
3270 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003271void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003272{
3273 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003274 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003275 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003276
Kees Cookf22eb4d2017-09-06 20:24:26 -07003277 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003278
James Smartaa6fbb72012-08-03 12:36:03 -04003279 /* Here we will also keep track of interrupts per sec of the hba */
3280 sli_intr = phba->sli.slistat.sli_intr;
3281
3282 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3283 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3284 sli_intr);
3285 else
3286 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3287
James Smart65791f12016-07-06 12:35:56 -07003288 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3289 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003290 phba->sli.slistat.sli_ips = cnt;
3291
3292 phba->sli.slistat.sli_prev_intr = sli_intr;
3293
James Smart93996272008-08-24 21:50:30 -04003294 /* Check chip HA register for error event */
3295 eratt = lpfc_sli_check_eratt(phba);
3296
3297 if (eratt)
3298 /* Tell the worker thread there is work to do */
3299 lpfc_worker_wake_up(phba);
3300 else
3301 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003302 mod_timer(&phba->eratt_poll,
3303 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003304 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003305 return;
3306}
3307
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003308
James Smarte59058c2008-08-24 21:49:00 -04003309/**
James Smart3621a712009-04-06 18:47:14 -04003310 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003311 * @phba: Pointer to HBA context object.
3312 * @pring: Pointer to driver SLI ring object.
3313 * @mask: Host attention register mask for this ring.
3314 *
3315 * This function is called from the interrupt context when there is a ring
3316 * event for the fcp ring. The caller does not hold any lock.
3317 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003318 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003319 * LE bit set. The function will call the completion handler of the command iocb
3320 * if the response iocb indicates a completion for a command iocb or it is
3321 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3322 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003323 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003324 * to check it explicitly.
3325 */
3326int
James Smart2e0fef82007-06-17 19:56:36 -05003327lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3328 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003329{
James Smart34b02dc2008-08-24 21:49:55 -04003330 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003331 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003332 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003333 struct lpfc_iocbq *cmdiocbq = NULL;
3334 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003335 uint32_t status;
3336 uint32_t portRspPut, portRspMax;
3337 int rc = 1;
3338 lpfc_iocb_type type;
3339 unsigned long iflag;
3340 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003341
James Smart2e0fef82007-06-17 19:56:36 -05003342 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003343 pring->stats.iocb_event++;
3344
dea31012005-04-17 16:05:31 -05003345 /*
3346 * The next available response entry should never exceed the maximum
3347 * entries. If it does, treat it as an adapter hardware error.
3348 */
James Smart7e56aa22012-08-03 12:35:34 -04003349 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003350 portRspPut = le32_to_cpu(pgp->rspPutInx);
3351 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003352 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003353 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003354 return 1;
3355 }
James Smart45ed1192009-10-02 15:17:02 -04003356 if (phba->fcp_ring_in_use) {
3357 spin_unlock_irqrestore(&phba->hbalock, iflag);
3358 return 1;
3359 } else
3360 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003361
3362 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003363 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003364 /*
3365 * Fetch an entry off the ring and copy it into a local data
3366 * structure. The copy involves a byte-swap since the
3367 * network byte order and pci byte orders are different.
3368 */
James Smarted957682007-06-17 19:56:37 -05003369 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003370 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003371
James Smart7e56aa22012-08-03 12:35:34 -04003372 if (++pring->sli.sli3.rspidx >= portRspMax)
3373 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003374
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003375 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3376 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003377 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003378 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003379 irsp = &rspiocbq.iocb;
3380
dea31012005-04-17 16:05:31 -05003381 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3382 pring->stats.iocb_rsp++;
3383 rsp_cmpl++;
3384
3385 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003386 /*
3387 * If resource errors reported from HBA, reduce
3388 * queuedepths of the SCSI device.
3389 */
3390 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003391 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3392 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003393 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003394 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003395 spin_lock_irqsave(&phba->hbalock, iflag);
3396 }
3397
dea31012005-04-17 16:05:31 -05003398 /* Rsp ring <ringno> error: IOCB */
3399 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003400 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003401 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003402 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003403 irsp->un.ulpWord[0],
3404 irsp->un.ulpWord[1],
3405 irsp->un.ulpWord[2],
3406 irsp->un.ulpWord[3],
3407 irsp->un.ulpWord[4],
3408 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003409 *(uint32_t *)&irsp->un1,
3410 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003411 }
3412
3413 switch (type) {
3414 case LPFC_ABORT_IOCB:
3415 case LPFC_SOL_IOCB:
3416 /*
3417 * Idle exchange closed via ABTS from port. No iocb
3418 * resources need to be recovered.
3419 */
3420 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003421 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003422 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003423 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003424 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003425 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003426 break;
3427 }
3428
James Bottomley604a3e32005-10-29 10:28:33 -05003429 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3430 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003431 if (unlikely(!cmdiocbq))
3432 break;
3433 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3434 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3435 if (cmdiocbq->iocb_cmpl) {
3436 spin_unlock_irqrestore(&phba->hbalock, iflag);
3437 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3438 &rspiocbq);
3439 spin_lock_irqsave(&phba->hbalock, iflag);
3440 }
dea31012005-04-17 16:05:31 -05003441 break;
James Smarta4bc3372006-12-02 13:34:16 -05003442 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003443 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003444 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003445 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003446 break;
dea31012005-04-17 16:05:31 -05003447 default:
3448 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3449 char adaptermsg[LPFC_MAX_ADPTMSG];
3450 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3451 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3452 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003453 dev_warn(&((phba->pcidev)->dev),
3454 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003455 phba->brd_no, adaptermsg);
3456 } else {
3457 /* Unknown IOCB command */
3458 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003459 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003460 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003461 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003462 irsp->ulpStatus,
3463 irsp->ulpIoTag,
3464 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003465 }
3466 break;
3467 }
3468
3469 /*
3470 * The response IOCB has been processed. Update the ring
3471 * pointer in SLIM. If the port response put pointer has not
3472 * been updated, sync the pgp->rspPutInx and fetch the new port
3473 * response put pointer.
3474 */
James Smart7e56aa22012-08-03 12:35:34 -04003475 writel(pring->sli.sli3.rspidx,
3476 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003477
James Smart7e56aa22012-08-03 12:35:34 -04003478 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003479 portRspPut = le32_to_cpu(pgp->rspPutInx);
3480 }
3481
3482 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3483 pring->stats.iocb_rsp_full++;
3484 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3485 writel(status, phba->CAregaddr);
3486 readl(phba->CAregaddr);
3487 }
3488 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3489 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3490 pring->stats.iocb_cmd_empty++;
3491
3492 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003493 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003494 lpfc_sli_resume_iocb(phba, pring);
3495
3496 if ((pring->lpfc_sli_cmd_available))
3497 (pring->lpfc_sli_cmd_available) (phba, pring);
3498
3499 }
3500
James Smart45ed1192009-10-02 15:17:02 -04003501 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003502 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003503 return rc;
3504}
3505
James Smarte59058c2008-08-24 21:49:00 -04003506/**
James Smart3772a992009-05-22 14:50:54 -04003507 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3508 * @phba: Pointer to HBA context object.
3509 * @pring: Pointer to driver SLI ring object.
3510 * @rspiocbp: Pointer to driver response IOCB object.
3511 *
3512 * This function is called from the worker thread when there is a slow-path
3513 * response IOCB to process. This function chains all the response iocbs until
3514 * seeing the iocb with the LE bit set. The function will call
3515 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3516 * completion of a command iocb. The function will call the
3517 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3518 * The function frees the resources or calls the completion handler if this
3519 * iocb is an abort completion. The function returns NULL when the response
3520 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3521 * this function shall chain the iocb on to the iocb_continueq and return the
3522 * response iocb passed in.
3523 **/
3524static struct lpfc_iocbq *
3525lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3526 struct lpfc_iocbq *rspiocbp)
3527{
3528 struct lpfc_iocbq *saveq;
3529 struct lpfc_iocbq *cmdiocbp;
3530 struct lpfc_iocbq *next_iocb;
3531 IOCB_t *irsp = NULL;
3532 uint32_t free_saveq;
3533 uint8_t iocb_cmd_type;
3534 lpfc_iocb_type type;
3535 unsigned long iflag;
3536 int rc;
3537
3538 spin_lock_irqsave(&phba->hbalock, iflag);
3539 /* First add the response iocb to the countinueq list */
3540 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3541 pring->iocb_continueq_cnt++;
3542
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003543 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003544 irsp = &rspiocbp->iocb;
3545 if (irsp->ulpLe) {
3546 /*
3547 * By default, the driver expects to free all resources
3548 * associated with this iocb completion.
3549 */
3550 free_saveq = 1;
3551 saveq = list_get_first(&pring->iocb_continueq,
3552 struct lpfc_iocbq, list);
3553 irsp = &(saveq->iocb);
3554 list_del_init(&pring->iocb_continueq);
3555 pring->iocb_continueq_cnt = 0;
3556
3557 pring->stats.iocb_rsp++;
3558
3559 /*
3560 * If resource errors reported from HBA, reduce
3561 * queuedepths of the SCSI device.
3562 */
3563 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003564 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3565 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003566 spin_unlock_irqrestore(&phba->hbalock, iflag);
3567 phba->lpfc_rampdown_queue_depth(phba);
3568 spin_lock_irqsave(&phba->hbalock, iflag);
3569 }
3570
3571 if (irsp->ulpStatus) {
3572 /* Rsp ring <ringno> error: IOCB */
3573 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3574 "0328 Rsp Ring %d error: "
3575 "IOCB Data: "
3576 "x%x x%x x%x x%x "
3577 "x%x x%x x%x x%x "
3578 "x%x x%x x%x x%x "
3579 "x%x x%x x%x x%x\n",
3580 pring->ringno,
3581 irsp->un.ulpWord[0],
3582 irsp->un.ulpWord[1],
3583 irsp->un.ulpWord[2],
3584 irsp->un.ulpWord[3],
3585 irsp->un.ulpWord[4],
3586 irsp->un.ulpWord[5],
3587 *(((uint32_t *) irsp) + 6),
3588 *(((uint32_t *) irsp) + 7),
3589 *(((uint32_t *) irsp) + 8),
3590 *(((uint32_t *) irsp) + 9),
3591 *(((uint32_t *) irsp) + 10),
3592 *(((uint32_t *) irsp) + 11),
3593 *(((uint32_t *) irsp) + 12),
3594 *(((uint32_t *) irsp) + 13),
3595 *(((uint32_t *) irsp) + 14),
3596 *(((uint32_t *) irsp) + 15));
3597 }
3598
3599 /*
3600 * Fetch the IOCB command type and call the correct completion
3601 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3602 * get freed back to the lpfc_iocb_list by the discovery
3603 * kernel thread.
3604 */
3605 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3606 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3607 switch (type) {
3608 case LPFC_SOL_IOCB:
3609 spin_unlock_irqrestore(&phba->hbalock, iflag);
3610 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3611 spin_lock_irqsave(&phba->hbalock, iflag);
3612 break;
3613
3614 case LPFC_UNSOL_IOCB:
3615 spin_unlock_irqrestore(&phba->hbalock, iflag);
3616 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3617 spin_lock_irqsave(&phba->hbalock, iflag);
3618 if (!rc)
3619 free_saveq = 0;
3620 break;
3621
3622 case LPFC_ABORT_IOCB:
3623 cmdiocbp = NULL;
3624 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3625 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3626 saveq);
3627 if (cmdiocbp) {
3628 /* Call the specified completion routine */
3629 if (cmdiocbp->iocb_cmpl) {
3630 spin_unlock_irqrestore(&phba->hbalock,
3631 iflag);
3632 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3633 saveq);
3634 spin_lock_irqsave(&phba->hbalock,
3635 iflag);
3636 } else
3637 __lpfc_sli_release_iocbq(phba,
3638 cmdiocbp);
3639 }
3640 break;
3641
3642 case LPFC_UNKNOWN_IOCB:
3643 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3644 char adaptermsg[LPFC_MAX_ADPTMSG];
3645 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3646 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3647 MAX_MSG_DATA);
3648 dev_warn(&((phba->pcidev)->dev),
3649 "lpfc%d: %s\n",
3650 phba->brd_no, adaptermsg);
3651 } else {
3652 /* Unknown IOCB command */
3653 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3654 "0335 Unknown IOCB "
3655 "command Data: x%x "
3656 "x%x x%x x%x\n",
3657 irsp->ulpCommand,
3658 irsp->ulpStatus,
3659 irsp->ulpIoTag,
3660 irsp->ulpContext);
3661 }
3662 break;
3663 }
3664
3665 if (free_saveq) {
3666 list_for_each_entry_safe(rspiocbp, next_iocb,
3667 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003668 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003669 __lpfc_sli_release_iocbq(phba, rspiocbp);
3670 }
3671 __lpfc_sli_release_iocbq(phba, saveq);
3672 }
3673 rspiocbp = NULL;
3674 }
3675 spin_unlock_irqrestore(&phba->hbalock, iflag);
3676 return rspiocbp;
3677}
3678
3679/**
3680 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003681 * @phba: Pointer to HBA context object.
3682 * @pring: Pointer to driver SLI ring object.
3683 * @mask: Host attention register mask for this ring.
3684 *
James Smart3772a992009-05-22 14:50:54 -04003685 * This routine wraps the actual slow_ring event process routine from the
3686 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003687 **/
James Smart3772a992009-05-22 14:50:54 -04003688void
James Smart2e0fef82007-06-17 19:56:36 -05003689lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3690 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003691{
James Smart3772a992009-05-22 14:50:54 -04003692 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3693}
3694
3695/**
3696 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3697 * @phba: Pointer to HBA context object.
3698 * @pring: Pointer to driver SLI ring object.
3699 * @mask: Host attention register mask for this ring.
3700 *
3701 * This function is called from the worker thread when there is a ring event
3702 * for non-fcp rings. The caller does not hold any lock. The function will
3703 * remove each response iocb in the response ring and calls the handle
3704 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3705 **/
3706static void
3707lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3708 struct lpfc_sli_ring *pring, uint32_t mask)
3709{
James Smart34b02dc2008-08-24 21:49:55 -04003710 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003711 IOCB_t *entry;
3712 IOCB_t *irsp = NULL;
3713 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003714 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003715 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003716 uint32_t status;
dea31012005-04-17 16:05:31 -05003717
James Smart34b02dc2008-08-24 21:49:55 -04003718 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003719 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003720 pring->stats.iocb_event++;
3721
dea31012005-04-17 16:05:31 -05003722 /*
3723 * The next available response entry should never exceed the maximum
3724 * entries. If it does, treat it as an adapter hardware error.
3725 */
James Smart7e56aa22012-08-03 12:35:34 -04003726 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003727 portRspPut = le32_to_cpu(pgp->rspPutInx);
3728 if (portRspPut >= portRspMax) {
3729 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003730 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003731 * rsp ring <portRspMax>
3732 */
James Smarted957682007-06-17 19:56:37 -05003733 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003734 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003735 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003736 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003737
James Smart2e0fef82007-06-17 19:56:36 -05003738 phba->link_state = LPFC_HBA_ERROR;
3739 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003740
3741 phba->work_hs = HS_FFER3;
3742 lpfc_handle_eratt(phba);
3743
James Smart3772a992009-05-22 14:50:54 -04003744 return;
dea31012005-04-17 16:05:31 -05003745 }
3746
3747 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003748 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003749 /*
3750 * Build a completion list and call the appropriate handler.
3751 * The process is to get the next available response iocb, get
3752 * a free iocb from the list, copy the response data into the
3753 * free iocb, insert to the continuation list, and update the
3754 * next response index to slim. This process makes response
3755 * iocb's in the ring available to DMA as fast as possible but
3756 * pays a penalty for a copy operation. Since the iocb is
3757 * only 32 bytes, this penalty is considered small relative to
3758 * the PCI reads for register values and a slim write. When
3759 * the ulpLe field is set, the entire Command has been
3760 * received.
3761 */
James Smarted957682007-06-17 19:56:37 -05003762 entry = lpfc_resp_iocb(phba, pring);
3763
James Smart858c9f62007-06-17 19:56:39 -05003764 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003765 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003766 if (rspiocbp == NULL) {
3767 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003768 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003769 break;
3770 }
3771
James Smarted957682007-06-17 19:56:37 -05003772 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3773 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003774 irsp = &rspiocbp->iocb;
3775
James Smart7e56aa22012-08-03 12:35:34 -04003776 if (++pring->sli.sli3.rspidx >= portRspMax)
3777 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003778
James Smarta58cbd52007-08-02 11:09:43 -04003779 if (pring->ringno == LPFC_ELS_RING) {
3780 lpfc_debugfs_slow_ring_trc(phba,
3781 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3782 *(((uint32_t *) irsp) + 4),
3783 *(((uint32_t *) irsp) + 6),
3784 *(((uint32_t *) irsp) + 7));
3785 }
3786
James Smart7e56aa22012-08-03 12:35:34 -04003787 writel(pring->sli.sli3.rspidx,
3788 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003789
James Smart3772a992009-05-22 14:50:54 -04003790 spin_unlock_irqrestore(&phba->hbalock, iflag);
3791 /* Handle the response IOCB */
3792 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3793 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003794
3795 /*
3796 * If the port response put pointer has not been updated, sync
3797 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3798 * response put pointer.
3799 */
James Smart7e56aa22012-08-03 12:35:34 -04003800 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003801 portRspPut = le32_to_cpu(pgp->rspPutInx);
3802 }
James Smart7e56aa22012-08-03 12:35:34 -04003803 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003804
James Smart92d7f7b2007-06-17 19:56:38 -05003805 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003806 /* At least one response entry has been freed */
3807 pring->stats.iocb_rsp_full++;
3808 /* SET RxRE_RSP in Chip Att register */
3809 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3810 writel(status, phba->CAregaddr);
3811 readl(phba->CAregaddr); /* flush */
3812 }
3813 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3814 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3815 pring->stats.iocb_cmd_empty++;
3816
3817 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003818 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003819 lpfc_sli_resume_iocb(phba, pring);
3820
3821 if ((pring->lpfc_sli_cmd_available))
3822 (pring->lpfc_sli_cmd_available) (phba, pring);
3823
3824 }
3825
James Smart2e0fef82007-06-17 19:56:36 -05003826 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003827 return;
dea31012005-04-17 16:05:31 -05003828}
3829
James Smarte59058c2008-08-24 21:49:00 -04003830/**
James Smart4f774512009-05-22 14:52:35 -04003831 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3832 * @phba: Pointer to HBA context object.
3833 * @pring: Pointer to driver SLI ring object.
3834 * @mask: Host attention register mask for this ring.
3835 *
3836 * This function is called from the worker thread when there is a pending
3837 * ELS response iocb on the driver internal slow-path response iocb worker
3838 * queue. The caller does not hold any lock. The function will remove each
3839 * response iocb from the response worker queue and calls the handle
3840 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3841 **/
3842static void
3843lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3844 struct lpfc_sli_ring *pring, uint32_t mask)
3845{
3846 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003847 struct hbq_dmabuf *dmabuf;
3848 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003849 unsigned long iflag;
James Smart0ef01a22018-09-10 10:30:45 -07003850 int count = 0;
James Smart4f774512009-05-22 14:52:35 -04003851
James Smart45ed1192009-10-02 15:17:02 -04003852 spin_lock_irqsave(&phba->hbalock, iflag);
3853 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3854 spin_unlock_irqrestore(&phba->hbalock, iflag);
3855 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003856 /* Get the response iocb from the head of work queue */
3857 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003858 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003859 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003860 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003861
3862 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3863 case CQE_CODE_COMPL_WQE:
3864 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3865 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003866 /* Translate ELS WCQE to response IOCBQ */
3867 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3868 irspiocbq);
3869 if (irspiocbq)
3870 lpfc_sli_sp_handle_rspiocb(phba, pring,
3871 irspiocbq);
James Smart0ef01a22018-09-10 10:30:45 -07003872 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003873 break;
3874 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003875 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003876 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3877 cq_event);
3878 lpfc_sli4_handle_received_buffer(phba, dmabuf);
James Smart0ef01a22018-09-10 10:30:45 -07003879 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003880 break;
3881 default:
3882 break;
3883 }
James Smart0ef01a22018-09-10 10:30:45 -07003884
3885 /* Limit the number of events to 64 to avoid soft lockups */
3886 if (count == 64)
3887 break;
James Smart4f774512009-05-22 14:52:35 -04003888 }
3889}
3890
3891/**
James Smart3621a712009-04-06 18:47:14 -04003892 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003893 * @phba: Pointer to HBA context object.
3894 * @pring: Pointer to driver SLI ring object.
3895 *
3896 * This function aborts all iocbs in the given ring and frees all the iocb
3897 * objects in txq. This function issues an abort iocb for all the iocb commands
3898 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3899 * the return of this function. The caller is not required to hold any locks.
3900 **/
James Smart2e0fef82007-06-17 19:56:36 -05003901void
dea31012005-04-17 16:05:31 -05003902lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3903{
James Smart2534ba72007-04-25 09:52:20 -04003904 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003905 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003906
James Smart92d7f7b2007-06-17 19:56:38 -05003907 if (pring->ringno == LPFC_ELS_RING) {
3908 lpfc_fabric_abort_hba(phba);
3909 }
3910
dea31012005-04-17 16:05:31 -05003911 /* Error everything on txq and txcmplq
3912 * First do the txq.
3913 */
James Smartdb55fba2014-04-04 13:52:02 -04003914 if (phba->sli_rev >= LPFC_SLI_REV4) {
3915 spin_lock_irq(&pring->ring_lock);
3916 list_splice_init(&pring->txq, &completions);
3917 pring->txq_cnt = 0;
3918 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003919
James Smartdb55fba2014-04-04 13:52:02 -04003920 spin_lock_irq(&phba->hbalock);
3921 /* Next issue ABTS for everything on the txcmplq */
3922 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3923 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3924 spin_unlock_irq(&phba->hbalock);
3925 } else {
3926 spin_lock_irq(&phba->hbalock);
3927 list_splice_init(&pring->txq, &completions);
3928 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003929
James Smartdb55fba2014-04-04 13:52:02 -04003930 /* Next issue ABTS for everything on the txcmplq */
3931 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3932 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3933 spin_unlock_irq(&phba->hbalock);
3934 }
James Smart2534ba72007-04-25 09:52:20 -04003935
James Smarta257bf92009-04-06 18:48:10 -04003936 /* Cancel all the IOCBs from the completions list */
3937 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3938 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003939}
3940
James Smarte59058c2008-08-24 21:49:00 -04003941/**
James Smart895427b2017-02-12 13:52:30 -08003942 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3943 * @phba: Pointer to HBA context object.
3944 * @pring: Pointer to driver SLI ring object.
3945 *
3946 * This function aborts all iocbs in the given ring and frees all the iocb
3947 * objects in txq. This function issues an abort iocb for all the iocb commands
3948 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3949 * the return of this function. The caller is not required to hold any locks.
3950 **/
3951void
3952lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3953{
3954 LIST_HEAD(completions);
3955 struct lpfc_iocbq *iocb, *next_iocb;
3956
3957 if (pring->ringno == LPFC_ELS_RING)
3958 lpfc_fabric_abort_hba(phba);
3959
3960 spin_lock_irq(&phba->hbalock);
3961 /* Next issue ABTS for everything on the txcmplq */
3962 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3963 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3964 spin_unlock_irq(&phba->hbalock);
3965}
3966
3967
3968/**
James Smartdb55fba2014-04-04 13:52:02 -04003969 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3970 * @phba: Pointer to HBA context object.
3971 * @pring: Pointer to driver SLI ring object.
3972 *
3973 * This function aborts all iocbs in FCP rings and frees all the iocb
3974 * objects in txq. This function issues an abort iocb for all the iocb commands
3975 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3976 * the return of this function. The caller is not required to hold any locks.
3977 **/
3978void
3979lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3980{
3981 struct lpfc_sli *psli = &phba->sli;
3982 struct lpfc_sli_ring *pring;
3983 uint32_t i;
3984
3985 /* Look on all the FCP Rings for the iotag */
3986 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08003987 for (i = 0; i < phba->cfg_hdw_queue; i++) {
3988 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003989 lpfc_sli_abort_iocb_ring(phba, pring);
3990 }
3991 } else {
James Smart895427b2017-02-12 13:52:30 -08003992 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003993 lpfc_sli_abort_iocb_ring(phba, pring);
3994 }
3995}
3996
James Smart895427b2017-02-12 13:52:30 -08003997/**
3998 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3999 * @phba: Pointer to HBA context object.
4000 *
4001 * This function aborts all wqes in NVME rings. This function issues an
4002 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
4003 * the txcmplq is not guaranteed to complete before the return of this
4004 * function. The caller is not required to hold any locks.
4005 **/
4006void
4007lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
4008{
4009 struct lpfc_sli_ring *pring;
4010 uint32_t i;
4011
James Smartcdb42be2019-01-28 11:14:21 -08004012 if ((phba->sli_rev < LPFC_SLI_REV4) ||
4013 !(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
James Smart895427b2017-02-12 13:52:30 -08004014 return;
4015
4016 /* Abort all IO on each NVME ring. */
James Smartcdb42be2019-01-28 11:14:21 -08004017 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4018 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
James Smart895427b2017-02-12 13:52:30 -08004019 lpfc_sli_abort_wqe_ring(phba, pring);
4020 }
4021}
4022
James Smartdb55fba2014-04-04 13:52:02 -04004023
4024/**
James Smart3621a712009-04-06 18:47:14 -04004025 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04004026 * @phba: Pointer to HBA context object.
4027 *
4028 * This function flushes all iocbs in the fcp ring and frees all the iocb
4029 * objects in txq and txcmplq. This function will not issue abort iocbs
4030 * for all the iocb commands in txcmplq, they will just be returned with
4031 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4032 * slot has been permanently disabled.
4033 **/
4034void
4035lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
4036{
4037 LIST_HEAD(txq);
4038 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04004039 struct lpfc_sli *psli = &phba->sli;
4040 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04004041 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004042 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04004043
4044 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04004045 /* Indicate the I/O queues are flushed */
4046 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04004047 spin_unlock_irq(&phba->hbalock);
4048
James Smartdb55fba2014-04-04 13:52:02 -04004049 /* Look on all the FCP Rings for the iotag */
4050 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004051 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4052 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
James Smarta8e497d2008-08-24 21:50:11 -04004053
James Smartdb55fba2014-04-04 13:52:02 -04004054 spin_lock_irq(&pring->ring_lock);
4055 /* Retrieve everything on txq */
4056 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004057 list_for_each_entry_safe(piocb, next_iocb,
4058 &pring->txcmplq, list)
4059 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004060 /* Retrieve everything on the txcmplq */
4061 list_splice_init(&pring->txcmplq, &txcmplq);
4062 pring->txq_cnt = 0;
4063 pring->txcmplq_cnt = 0;
4064 spin_unlock_irq(&pring->ring_lock);
4065
4066 /* Flush the txq */
4067 lpfc_sli_cancel_iocbs(phba, &txq,
4068 IOSTAT_LOCAL_REJECT,
4069 IOERR_SLI_DOWN);
4070 /* Flush the txcmpq */
4071 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4072 IOSTAT_LOCAL_REJECT,
4073 IOERR_SLI_DOWN);
4074 }
4075 } else {
James Smart895427b2017-02-12 13:52:30 -08004076 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004077
4078 spin_lock_irq(&phba->hbalock);
4079 /* Retrieve everything on txq */
4080 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004081 list_for_each_entry_safe(piocb, next_iocb,
4082 &pring->txcmplq, list)
4083 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004084 /* Retrieve everything on the txcmplq */
4085 list_splice_init(&pring->txcmplq, &txcmplq);
4086 pring->txq_cnt = 0;
4087 pring->txcmplq_cnt = 0;
4088 spin_unlock_irq(&phba->hbalock);
4089
4090 /* Flush the txq */
4091 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4092 IOERR_SLI_DOWN);
4093 /* Flush the txcmpq */
4094 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4095 IOERR_SLI_DOWN);
4096 }
James Smarta8e497d2008-08-24 21:50:11 -04004097}
4098
4099/**
James Smart895427b2017-02-12 13:52:30 -08004100 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4101 * @phba: Pointer to HBA context object.
4102 *
4103 * This function flushes all wqes in the nvme rings and frees all resources
4104 * in the txcmplq. This function does not issue abort wqes for the IO
4105 * commands in txcmplq, they will just be returned with
4106 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4107 * slot has been permanently disabled.
4108 **/
4109void
4110lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4111{
4112 LIST_HEAD(txcmplq);
4113 struct lpfc_sli_ring *pring;
4114 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004115 struct lpfc_iocbq *piocb, *next_iocb;
James Smart895427b2017-02-12 13:52:30 -08004116
James Smartcdb42be2019-01-28 11:14:21 -08004117 if ((phba->sli_rev < LPFC_SLI_REV4) ||
4118 !(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
James Smart895427b2017-02-12 13:52:30 -08004119 return;
4120
4121 /* Hint to other driver operations that a flush is in progress. */
4122 spin_lock_irq(&phba->hbalock);
4123 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4124 spin_unlock_irq(&phba->hbalock);
4125
4126 /* Cycle through all NVME rings and complete each IO with
4127 * a local driver reason code. This is a flush so no
4128 * abort exchange to FW.
4129 */
James Smartcdb42be2019-01-28 11:14:21 -08004130 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4131 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
James Smart895427b2017-02-12 13:52:30 -08004132
James Smart895427b2017-02-12 13:52:30 -08004133 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -08004134 list_for_each_entry_safe(piocb, next_iocb,
4135 &pring->txcmplq, list)
4136 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4137 /* Retrieve everything on the txcmplq */
James Smart895427b2017-02-12 13:52:30 -08004138 list_splice_init(&pring->txcmplq, &txcmplq);
4139 pring->txcmplq_cnt = 0;
4140 spin_unlock_irq(&pring->ring_lock);
4141
4142 /* Flush the txcmpq &&&PAE */
4143 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4144 IOSTAT_LOCAL_REJECT,
4145 IOERR_SLI_DOWN);
4146 }
4147}
4148
4149/**
James Smart3772a992009-05-22 14:50:54 -04004150 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004151 * @phba: Pointer to HBA context object.
4152 * @mask: Bit mask to be checked.
4153 *
4154 * This function reads the host status register and compares
4155 * with the provided bit mask to check if HBA completed
4156 * the restart. This function will wait in a loop for the
4157 * HBA to complete restart. If the HBA does not restart within
4158 * 15 iterations, the function will reset the HBA again. The
4159 * function returns 1 when HBA fail to restart otherwise returns
4160 * zero.
4161 **/
James Smart3772a992009-05-22 14:50:54 -04004162static int
4163lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004164{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004165 uint32_t status;
4166 int i = 0;
4167 int retval = 0;
dea31012005-04-17 16:05:31 -05004168
Jamie Wellnitz41415862006-02-28 19:25:27 -05004169 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004170 if (lpfc_readl(phba->HSregaddr, &status))
4171 return 1;
dea31012005-04-17 16:05:31 -05004172
Jamie Wellnitz41415862006-02-28 19:25:27 -05004173 /*
4174 * Check status register every 100ms for 5 retries, then every
4175 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4176 * every 2.5 sec for 4.
4177 * Break our of the loop if errors occurred during init.
4178 */
4179 while (((status & mask) != mask) &&
4180 !(status & HS_FFERM) &&
4181 i++ < 20) {
dea31012005-04-17 16:05:31 -05004182
Jamie Wellnitz41415862006-02-28 19:25:27 -05004183 if (i <= 5)
4184 msleep(10);
4185 else if (i <= 10)
4186 msleep(500);
4187 else
4188 msleep(2500);
dea31012005-04-17 16:05:31 -05004189
Jamie Wellnitz41415862006-02-28 19:25:27 -05004190 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004191 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004192 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004193 lpfc_sli_brdrestart(phba);
4194 }
4195 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004196 if (lpfc_readl(phba->HSregaddr, &status)) {
4197 retval = 1;
4198 break;
4199 }
dea31012005-04-17 16:05:31 -05004200 }
dea31012005-04-17 16:05:31 -05004201
Jamie Wellnitz41415862006-02-28 19:25:27 -05004202 /* Check to see if any errors occurred during init */
4203 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004204 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4205 "2751 Adapter failed to restart, "
4206 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4207 status,
4208 readl(phba->MBslimaddr + 0xa8),
4209 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004210 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004211 retval = 1;
4212 }
dea31012005-04-17 16:05:31 -05004213
Jamie Wellnitz41415862006-02-28 19:25:27 -05004214 return retval;
dea31012005-04-17 16:05:31 -05004215}
4216
James Smartda0436e2009-05-22 14:51:39 -04004217/**
4218 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4219 * @phba: Pointer to HBA context object.
4220 * @mask: Bit mask to be checked.
4221 *
4222 * This function checks the host status register to check if HBA is
4223 * ready. This function will wait in a loop for the HBA to be ready
4224 * If the HBA is not ready , the function will will reset the HBA PCI
4225 * function again. The function returns 1 when HBA fail to be ready
4226 * otherwise returns zero.
4227 **/
4228static int
4229lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4230{
4231 uint32_t status;
4232 int retval = 0;
4233
4234 /* Read the HBA Host Status Register */
4235 status = lpfc_sli4_post_status_check(phba);
4236
4237 if (status) {
4238 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4239 lpfc_sli_brdrestart(phba);
4240 status = lpfc_sli4_post_status_check(phba);
4241 }
4242
4243 /* Check to see if any errors occurred during init */
4244 if (status) {
4245 phba->link_state = LPFC_HBA_ERROR;
4246 retval = 1;
4247 } else
4248 phba->sli4_hba.intr_enable = 0;
4249
4250 return retval;
4251}
4252
4253/**
4254 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4255 * @phba: Pointer to HBA context object.
4256 * @mask: Bit mask to be checked.
4257 *
4258 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4259 * from the API jump table function pointer from the lpfc_hba struct.
4260 **/
4261int
4262lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4263{
4264 return phba->lpfc_sli_brdready(phba, mask);
4265}
4266
James Smart92908312006-03-07 15:04:13 -05004267#define BARRIER_TEST_PATTERN (0xdeadbeef)
4268
James Smarte59058c2008-08-24 21:49:00 -04004269/**
James Smart3621a712009-04-06 18:47:14 -04004270 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004271 * @phba: Pointer to HBA context object.
4272 *
James Smart1b511972011-12-13 13:23:09 -05004273 * This function is called before resetting an HBA. This function is called
4274 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004275 **/
James Smart2e0fef82007-06-17 19:56:36 -05004276void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004277{
James Smart65a29c12006-07-06 15:50:50 -04004278 uint32_t __iomem *resp_buf;
4279 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004280 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004281 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004282 int i;
4283 uint8_t hdrtype;
4284
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004285 lockdep_assert_held(&phba->hbalock);
4286
James Smart92908312006-03-07 15:04:13 -05004287 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4288 if (hdrtype != 0x80 ||
4289 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4290 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4291 return;
4292
4293 /*
4294 * Tell the other part of the chip to suspend temporarily all
4295 * its DMA activity.
4296 */
James Smart65a29c12006-07-06 15:50:50 -04004297 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004298
4299 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004300 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4301 return;
James Smart92908312006-03-07 15:04:13 -05004302 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4303 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004304 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004305
James Smart9940b972011-03-11 16:06:12 -05004306 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4307 return;
4308 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004309 /* Clear Chip error bit */
4310 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004311 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004312 }
4313
4314 mbox = 0;
4315 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4316 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4317
4318 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004319 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004320 writel(mbox, mbox_buf);
4321
James Smart9940b972011-03-11 16:06:12 -05004322 for (i = 0; i < 50; i++) {
4323 if (lpfc_readl((resp_buf + 1), &resp_data))
4324 return;
4325 if (resp_data != ~(BARRIER_TEST_PATTERN))
4326 mdelay(1);
4327 else
4328 break;
4329 }
4330 resp_data = 0;
4331 if (lpfc_readl((resp_buf + 1), &resp_data))
4332 return;
4333 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004334 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004335 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004336 goto restore_hc;
4337 else
4338 goto clear_errat;
4339 }
4340
4341 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004342 resp_data = 0;
4343 for (i = 0; i < 500; i++) {
4344 if (lpfc_readl(resp_buf, &resp_data))
4345 return;
4346 if (resp_data != mbox)
4347 mdelay(1);
4348 else
4349 break;
4350 }
James Smart92908312006-03-07 15:04:13 -05004351
4352clear_errat:
4353
James Smart9940b972011-03-11 16:06:12 -05004354 while (++i < 500) {
4355 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4356 return;
4357 if (!(ha_copy & HA_ERATT))
4358 mdelay(1);
4359 else
4360 break;
4361 }
James Smart92908312006-03-07 15:04:13 -05004362
4363 if (readl(phba->HAregaddr) & HA_ERATT) {
4364 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004365 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004366 }
4367
4368restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004369 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004370 writel(hc_copy, phba->HCregaddr);
4371 readl(phba->HCregaddr); /* flush */
4372}
4373
James Smarte59058c2008-08-24 21:49:00 -04004374/**
James Smart3621a712009-04-06 18:47:14 -04004375 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004376 * @phba: Pointer to HBA context object.
4377 *
4378 * This function issues a kill_board mailbox command and waits for
4379 * the error attention interrupt. This function is called for stopping
4380 * the firmware processing. The caller is not required to hold any
4381 * locks. This function calls lpfc_hba_down_post function to free
4382 * any pending commands after the kill. The function will return 1 when it
4383 * fails to kill the board else will return 0.
4384 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004385int
James Smart2e0fef82007-06-17 19:56:36 -05004386lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004387{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004388 struct lpfc_sli *psli;
4389 LPFC_MBOXQ_t *pmb;
4390 uint32_t status;
4391 uint32_t ha_copy;
4392 int retval;
4393 int i = 0;
4394
4395 psli = &phba->sli;
4396
4397 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004398 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004399 "0329 Kill HBA Data: x%x x%x\n",
4400 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004401
James Smart98c9ea52007-10-27 13:37:33 -04004402 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4403 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004404 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004405
4406 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004407 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004408 if (lpfc_readl(phba->HCregaddr, &status)) {
4409 spin_unlock_irq(&phba->hbalock);
4410 mempool_free(pmb, phba->mbox_mem_pool);
4411 return 1;
4412 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004413 status &= ~HC_ERINT_ENA;
4414 writel(status, phba->HCregaddr);
4415 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004416 phba->link_flag |= LS_IGNORE_ERATT;
4417 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004418
4419 lpfc_kill_board(phba, pmb);
4420 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4421 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4422
4423 if (retval != MBX_SUCCESS) {
4424 if (retval != MBX_BUSY)
4425 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004426 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4427 "2752 KILL_BOARD command failed retval %d\n",
4428 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004429 spin_lock_irq(&phba->hbalock);
4430 phba->link_flag &= ~LS_IGNORE_ERATT;
4431 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004432 return 1;
4433 }
4434
James Smartf4b4c682009-05-22 14:53:12 -04004435 spin_lock_irq(&phba->hbalock);
4436 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4437 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004438
Jamie Wellnitz41415862006-02-28 19:25:27 -05004439 mempool_free(pmb, phba->mbox_mem_pool);
4440
4441 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4442 * attention every 100ms for 3 seconds. If we don't get ERATT after
4443 * 3 seconds we still set HBA_ERROR state because the status of the
4444 * board is now undefined.
4445 */
James Smart9940b972011-03-11 16:06:12 -05004446 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4447 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004448 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4449 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004450 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4451 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004452 }
4453
4454 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004455 if (ha_copy & HA_ERATT) {
4456 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004457 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004458 }
James Smart2e0fef82007-06-17 19:56:36 -05004459 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004460 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004461 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004462 phba->link_flag &= ~LS_IGNORE_ERATT;
4463 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004464
Jamie Wellnitz41415862006-02-28 19:25:27 -05004465 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004466 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004467
James Smart2e0fef82007-06-17 19:56:36 -05004468 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004469}
4470
James Smarte59058c2008-08-24 21:49:00 -04004471/**
James Smart3772a992009-05-22 14:50:54 -04004472 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004473 * @phba: Pointer to HBA context object.
4474 *
4475 * This function resets the HBA by writing HC_INITFF to the control
4476 * register. After the HBA resets, this function resets all the iocb ring
4477 * indices. This function disables PCI layer parity checking during
4478 * the reset.
4479 * This function returns 0 always.
4480 * The caller is not required to hold any locks.
4481 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004482int
James Smart2e0fef82007-06-17 19:56:36 -05004483lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004484{
4485 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004486 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004487 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004488 int i;
dea31012005-04-17 16:05:31 -05004489
Jamie Wellnitz41415862006-02-28 19:25:27 -05004490 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004491
Jamie Wellnitz41415862006-02-28 19:25:27 -05004492 /* Reset HBA */
4493 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004494 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004495 (phba->pport) ? phba->pport->port_state : 0,
4496 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004497
4498 /* perform board reset */
4499 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004500 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004501 if (phba->pport) {
4502 phba->pport->fc_myDID = 0;
4503 phba->pport->fc_prevDID = 0;
4504 }
dea31012005-04-17 16:05:31 -05004505
Jamie Wellnitz41415862006-02-28 19:25:27 -05004506 /* Turn off parity checking and serr during the physical reset */
4507 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4508 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4509 (cfg_value &
4510 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4511
James Smart3772a992009-05-22 14:50:54 -04004512 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4513
Jamie Wellnitz41415862006-02-28 19:25:27 -05004514 /* Now toggle INITFF bit in the Host Control Register */
4515 writel(HC_INITFF, phba->HCregaddr);
4516 mdelay(1);
4517 readl(phba->HCregaddr); /* flush */
4518 writel(0, phba->HCregaddr);
4519 readl(phba->HCregaddr); /* flush */
4520
4521 /* Restore PCI cmd register */
4522 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004523
4524 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004525 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004526 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004527 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004528 pring->sli.sli3.rspidx = 0;
4529 pring->sli.sli3.next_cmdidx = 0;
4530 pring->sli.sli3.local_getidx = 0;
4531 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004532 pring->missbufcnt = 0;
4533 }
dea31012005-04-17 16:05:31 -05004534
James Smart2e0fef82007-06-17 19:56:36 -05004535 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004536 return 0;
4537}
4538
James Smarte59058c2008-08-24 21:49:00 -04004539/**
James Smartda0436e2009-05-22 14:51:39 -04004540 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4541 * @phba: Pointer to HBA context object.
4542 *
4543 * This function resets a SLI4 HBA. This function disables PCI layer parity
4544 * checking during resets the device. The caller is not required to hold
4545 * any locks.
4546 *
4547 * This function returns 0 always.
4548 **/
4549int
4550lpfc_sli4_brdreset(struct lpfc_hba *phba)
4551{
4552 struct lpfc_sli *psli = &phba->sli;
4553 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004554 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004555
4556 /* Reset HBA */
4557 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004558 "0295 Reset HBA Data: x%x x%x x%x\n",
4559 phba->pport->port_state, psli->sli_flag,
4560 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004561
4562 /* perform board reset */
4563 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004564 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004565 phba->pport->fc_myDID = 0;
4566 phba->pport->fc_prevDID = 0;
4567
James Smartda0436e2009-05-22 14:51:39 -04004568 spin_lock_irq(&phba->hbalock);
4569 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4570 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004571 spin_unlock_irq(&phba->hbalock);
4572
James Smart02936352014-04-04 13:52:12 -04004573 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4574 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4575 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4576 return rc;
4577 }
4578
James Smartda0436e2009-05-22 14:51:39 -04004579 /* Now physically reset the device */
4580 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4581 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004582
4583 /* Turn off parity checking and serr during the physical reset */
4584 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4585 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4586 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4587
James Smart88318812012-09-29 11:29:29 -04004588 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004589 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004590
James Smartbe858b62010-12-15 17:57:20 -05004591 /* Restore PCI cmd register */
4592 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4593
James Smart27b01b82012-05-09 21:19:44 -04004594 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004595}
4596
4597/**
4598 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004599 * @phba: Pointer to HBA context object.
4600 *
4601 * This function is called in the SLI initialization code path to
4602 * restart the HBA. The caller is not required to hold any lock.
4603 * This function writes MBX_RESTART mailbox command to the SLIM and
4604 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4605 * function to free any pending commands. The function enables
4606 * POST only during the first initialization. The function returns zero.
4607 * The function does not guarantee completion of MBX_RESTART mailbox
4608 * command before the return of this function.
4609 **/
James Smartda0436e2009-05-22 14:51:39 -04004610static int
4611lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004612{
4613 MAILBOX_t *mb;
4614 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004615 volatile uint32_t word0;
4616 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004617 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004618
James Smart2e0fef82007-06-17 19:56:36 -05004619 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004620
James Smart0d878412009-10-02 15:16:56 -04004621 /* Take PCIe device Advanced Error Reporting (AER) state */
4622 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4623
Jamie Wellnitz41415862006-02-28 19:25:27 -05004624 psli = &phba->sli;
4625
4626 /* Restart HBA */
4627 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004628 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004629 (phba->pport) ? phba->pport->port_state : 0,
4630 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004631
4632 word0 = 0;
4633 mb = (MAILBOX_t *) &word0;
4634 mb->mbxCommand = MBX_RESTART;
4635 mb->mbxHc = 1;
4636
James Smart92908312006-03-07 15:04:13 -05004637 lpfc_reset_barrier(phba);
4638
Jamie Wellnitz41415862006-02-28 19:25:27 -05004639 to_slim = phba->MBslimaddr;
4640 writel(*(uint32_t *) mb, to_slim);
4641 readl(to_slim); /* flush */
4642
4643 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004644 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004645 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004646 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004647 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004648 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004649 writel(*(uint32_t *) mb, to_slim);
4650 readl(to_slim); /* flush */
4651
4652 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004653 if (phba->pport)
4654 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004655 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004656 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004657 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004658
James Smart64ba8812006-08-02 15:24:34 -04004659 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004660 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04004661
James Smarteaf15d52008-12-04 22:39:29 -05004662 /* Give the INITFF and Post time to settle. */
4663 mdelay(100);
dea31012005-04-17 16:05:31 -05004664
James Smart0d878412009-10-02 15:16:56 -04004665 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4666 if (hba_aer_enabled)
4667 pci_disable_pcie_error_reporting(phba->pcidev);
4668
Jamie Wellnitz41415862006-02-28 19:25:27 -05004669 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004670
4671 return 0;
4672}
4673
James Smarte59058c2008-08-24 21:49:00 -04004674/**
James Smartda0436e2009-05-22 14:51:39 -04004675 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4676 * @phba: Pointer to HBA context object.
4677 *
4678 * This function is called in the SLI initialization code path to restart
4679 * a SLI4 HBA. The caller is not required to hold any lock.
4680 * At the end of the function, it calls lpfc_hba_down_post function to
4681 * free any pending commands.
4682 **/
4683static int
4684lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4685{
4686 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004687 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004688 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004689
4690 /* Restart HBA */
4691 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4692 "0296 Restart HBA Data: x%x x%x\n",
4693 phba->pport->port_state, psli->sli_flag);
4694
James Smart75baf692010-06-08 18:31:21 -04004695 /* Take PCIe device Advanced Error Reporting (AER) state */
4696 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4697
James Smart27b01b82012-05-09 21:19:44 -04004698 rc = lpfc_sli4_brdreset(phba);
James Smart5a9eeff2018-11-29 16:09:32 -08004699 if (rc)
4700 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004701
4702 spin_lock_irq(&phba->hbalock);
4703 phba->pport->stopped = 0;
4704 phba->link_state = LPFC_INIT_START;
4705 phba->hba_flag = 0;
4706 spin_unlock_irq(&phba->hbalock);
4707
4708 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004709 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04004710
James Smart75baf692010-06-08 18:31:21 -04004711 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4712 if (hba_aer_enabled)
4713 pci_disable_pcie_error_reporting(phba->pcidev);
4714
James Smartda0436e2009-05-22 14:51:39 -04004715 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004716 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004717
James Smart27b01b82012-05-09 21:19:44 -04004718 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004719}
4720
4721/**
4722 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4723 * @phba: Pointer to HBA context object.
4724 *
4725 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4726 * API jump table function pointer from the lpfc_hba struct.
4727**/
4728int
4729lpfc_sli_brdrestart(struct lpfc_hba *phba)
4730{
4731 return phba->lpfc_sli_brdrestart(phba);
4732}
4733
4734/**
James Smart3621a712009-04-06 18:47:14 -04004735 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004736 * @phba: Pointer to HBA context object.
4737 *
4738 * This function is called after a HBA restart to wait for successful
4739 * restart of the HBA. Successful restart of the HBA is indicated by
4740 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4741 * iteration, the function will restart the HBA again. The function returns
4742 * zero if HBA successfully restarted else returns negative error code.
4743 **/
James Smart4492b732017-04-27 15:08:26 -07004744int
dea31012005-04-17 16:05:31 -05004745lpfc_sli_chipset_init(struct lpfc_hba *phba)
4746{
4747 uint32_t status, i = 0;
4748
4749 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004750 if (lpfc_readl(phba->HSregaddr, &status))
4751 return -EIO;
dea31012005-04-17 16:05:31 -05004752
4753 /* Check status register to see what current state is */
4754 i = 0;
4755 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4756
James Smartdcf2a4e2010-09-29 11:18:53 -04004757 /* Check every 10ms for 10 retries, then every 100ms for 90
4758 * retries, then every 1 sec for 50 retires for a total of
4759 * ~60 seconds before reset the board again and check every
4760 * 1 sec for 50 retries. The up to 60 seconds before the
4761 * board ready is required by the Falcon FIPS zeroization
4762 * complete, and any reset the board in between shall cause
4763 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004764 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004765 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004766 /* Adapter failed to init, timeout, status reg
4767 <status> */
James Smarted957682007-06-17 19:56:37 -05004768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004769 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004770 "timeout, status reg x%x, "
4771 "FW Data: A8 x%x AC x%x\n", status,
4772 readl(phba->MBslimaddr + 0xa8),
4773 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004774 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004775 return -ETIMEDOUT;
4776 }
4777
4778 /* Check to see if any errors occurred during init */
4779 if (status & HS_FFERM) {
4780 /* ERROR: During chipset initialization */
4781 /* Adapter failed to init, chipset, status reg
4782 <status> */
James Smarted957682007-06-17 19:56:37 -05004783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004784 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004785 "chipset, status reg x%x, "
4786 "FW Data: A8 x%x AC x%x\n", status,
4787 readl(phba->MBslimaddr + 0xa8),
4788 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004789 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004790 return -EIO;
4791 }
4792
James Smartdcf2a4e2010-09-29 11:18:53 -04004793 if (i <= 10)
dea31012005-04-17 16:05:31 -05004794 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004795 else if (i <= 100)
4796 msleep(100);
4797 else
4798 msleep(1000);
dea31012005-04-17 16:05:31 -05004799
James Smartdcf2a4e2010-09-29 11:18:53 -04004800 if (i == 150) {
4801 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004802 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004803 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004804 }
4805 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004806 if (lpfc_readl(phba->HSregaddr, &status))
4807 return -EIO;
dea31012005-04-17 16:05:31 -05004808 }
4809
4810 /* Check to see if any errors occurred during init */
4811 if (status & HS_FFERM) {
4812 /* ERROR: During chipset initialization */
4813 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004815 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004816 "status reg x%x, "
4817 "FW Data: A8 x%x AC x%x\n", status,
4818 readl(phba->MBslimaddr + 0xa8),
4819 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004820 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004821 return -EIO;
4822 }
4823
4824 /* Clear all interrupt enable conditions */
4825 writel(0, phba->HCregaddr);
4826 readl(phba->HCregaddr); /* flush */
4827
4828 /* setup host attn register */
4829 writel(0xffffffff, phba->HAregaddr);
4830 readl(phba->HAregaddr); /* flush */
4831 return 0;
4832}
4833
James Smarte59058c2008-08-24 21:49:00 -04004834/**
James Smart3621a712009-04-06 18:47:14 -04004835 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004836 *
4837 * This function calculates and returns the number of HBQs required to be
4838 * configured.
4839 **/
James Smart78b2d852007-08-02 11:10:21 -04004840int
James Smarted957682007-06-17 19:56:37 -05004841lpfc_sli_hbq_count(void)
4842{
James Smart92d7f7b2007-06-17 19:56:38 -05004843 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004844}
4845
James Smarte59058c2008-08-24 21:49:00 -04004846/**
James Smart3621a712009-04-06 18:47:14 -04004847 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004848 *
4849 * This function adds the number of hbq entries in every HBQ to get
4850 * the total number of hbq entries required for the HBA and returns
4851 * the total count.
4852 **/
James Smarted957682007-06-17 19:56:37 -05004853static int
4854lpfc_sli_hbq_entry_count(void)
4855{
4856 int hbq_count = lpfc_sli_hbq_count();
4857 int count = 0;
4858 int i;
4859
4860 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004861 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004862 return count;
4863}
4864
James Smarte59058c2008-08-24 21:49:00 -04004865/**
James Smart3621a712009-04-06 18:47:14 -04004866 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004867 *
4868 * This function calculates amount of memory required for all hbq entries
4869 * to be configured and returns the total memory required.
4870 **/
dea31012005-04-17 16:05:31 -05004871int
James Smarted957682007-06-17 19:56:37 -05004872lpfc_sli_hbq_size(void)
4873{
4874 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4875}
4876
James Smarte59058c2008-08-24 21:49:00 -04004877/**
James Smart3621a712009-04-06 18:47:14 -04004878 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004879 * @phba: Pointer to HBA context object.
4880 *
4881 * This function is called during the SLI initialization to configure
4882 * all the HBQs and post buffers to the HBQ. The caller is not
4883 * required to hold any locks. This function will return zero if successful
4884 * else it will return negative error code.
4885 **/
James Smarted957682007-06-17 19:56:37 -05004886static int
4887lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4888{
4889 int hbq_count = lpfc_sli_hbq_count();
4890 LPFC_MBOXQ_t *pmb;
4891 MAILBOX_t *pmbox;
4892 uint32_t hbqno;
4893 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004894
James Smart92d7f7b2007-06-17 19:56:38 -05004895 /* Get a Mailbox buffer to setup mailbox
4896 * commands for HBA initialization
4897 */
James Smarted957682007-06-17 19:56:37 -05004898 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4899
4900 if (!pmb)
4901 return -ENOMEM;
4902
James Smart04c68492009-05-22 14:52:52 -04004903 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004904
4905 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4906 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004907 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004908
4909 hbq_entry_index = 0;
4910 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4911 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4912 phba->hbqs[hbqno].hbqPutIdx = 0;
4913 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4914 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004915 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004916 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4917 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004918 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4919
4920 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4921 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4922 mbxStatus <status>, ring <num> */
4923
4924 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004925 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004926 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004927 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004928 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004929 pmbox->mbxStatus, hbqno);
4930
4931 phba->link_state = LPFC_HBA_ERROR;
4932 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004933 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004934 }
4935 }
4936 phba->hbq_count = hbq_count;
4937
James Smarted957682007-06-17 19:56:37 -05004938 mempool_free(pmb, phba->mbox_mem_pool);
4939
James Smart92d7f7b2007-06-17 19:56:38 -05004940 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004941 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4942 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004943 return 0;
4944}
4945
James Smarte59058c2008-08-24 21:49:00 -04004946/**
James Smart4f774512009-05-22 14:52:35 -04004947 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4948 * @phba: Pointer to HBA context object.
4949 *
4950 * This function is called during the SLI initialization to configure
4951 * all the HBQs and post buffers to the HBQ. The caller is not
4952 * required to hold any locks. This function will return zero if successful
4953 * else it will return negative error code.
4954 **/
4955static int
4956lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4957{
4958 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004959 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4960 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004961 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004962 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004963 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004964 return 0;
4965}
4966
4967/**
James Smart3621a712009-04-06 18:47:14 -04004968 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004969 * @phba: Pointer to HBA context object.
4970 * @sli_mode: sli mode - 2/3
4971 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004972 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004973 * to issue config_port mailbox command. This function restarts the
4974 * HBA firmware and issues a config_port mailbox command to configure
4975 * the SLI interface in the sli mode specified by sli_mode
4976 * variable. The caller is not required to hold any locks.
4977 * The function returns 0 if successful, else returns negative error
4978 * code.
4979 **/
James Smart93996272008-08-24 21:50:30 -04004980int
4981lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004982{
4983 LPFC_MBOXQ_t *pmb;
4984 uint32_t resetcount = 0, rc = 0, done = 0;
4985
4986 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4987 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004988 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004989 return -ENOMEM;
4990 }
4991
James Smarted957682007-06-17 19:56:37 -05004992 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004993 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004994 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004995 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004996 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004997 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004998 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004999 rc = lpfc_sli_chipset_init(phba);
5000 if (rc)
5001 break;
5002
James Smart2e0fef82007-06-17 19:56:36 -05005003 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04005004 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005005 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005006 resetcount++;
5007
James Smarted957682007-06-17 19:56:37 -05005008 /* Call pre CONFIG_PORT mailbox command initialization. A
5009 * value of 0 means the call was successful. Any other
5010 * nonzero value is a failure, but if ERESTART is returned,
5011 * the driver may reset the HBA and try again.
5012 */
dea31012005-04-17 16:05:31 -05005013 rc = lpfc_config_port_prep(phba);
5014 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05005015 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05005016 continue;
James Smart34b02dc2008-08-24 21:49:55 -04005017 } else if (rc)
dea31012005-04-17 16:05:31 -05005018 break;
James Smart6d368e52011-05-24 11:44:12 -04005019
James Smart2e0fef82007-06-17 19:56:36 -05005020 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05005021 lpfc_config_port(phba, pmb);
5022 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04005023 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5024 LPFC_SLI3_HBQ_ENABLED |
5025 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04005026 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05005027 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05005028 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005029 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05005030 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04005031 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005032 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04005033 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005034 spin_unlock_irq(&phba->hbalock);
5035 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04005036 } else {
5037 /* Allow asynchronous mailbox command to go through */
5038 spin_lock_irq(&phba->hbalock);
5039 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5040 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05005041 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05005042
5043 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5044 (pmb->u.mb.un.varCfgPort.gasabt == 0))
5045 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5046 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04005047 }
dea31012005-04-17 16:05:31 -05005048 }
James Smarted957682007-06-17 19:56:37 -05005049 if (!done) {
5050 rc = -EINVAL;
5051 goto do_prep_failed;
5052 }
James Smart04c68492009-05-22 14:52:52 -04005053 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5054 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04005055 rc = -ENXIO;
5056 goto do_prep_failed;
5057 }
James Smart04c68492009-05-22 14:52:52 -04005058 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04005059 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005060 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5061 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5062 phba->max_vpi : phba->max_vports;
5063
James Smart34b02dc2008-08-24 21:49:55 -04005064 } else
5065 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04005066 phba->fips_level = 0;
5067 phba->fips_spec_rev = 0;
5068 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04005069 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04005070 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
5071 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5072 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5073 "2850 Security Crypto Active. FIPS x%d "
5074 "(Spec Rev: x%d)",
5075 phba->fips_level, phba->fips_spec_rev);
5076 }
5077 if (pmb->u.mb.un.varCfgPort.sec_err) {
5078 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5079 "2856 Config Port Security Crypto "
5080 "Error: x%x ",
5081 pmb->u.mb.un.varCfgPort.sec_err);
5082 }
James Smart04c68492009-05-22 14:52:52 -04005083 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005084 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005085 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005086 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005087
5088 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5089 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005090
James Smartf44ac122018-03-05 12:04:08 -08005091 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5092 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5093 phba->cfg_enable_bg = 0;
5094 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smarte2a0a9d2008-12-04 22:40:02 -05005095 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5096 "0443 Adapter did not grant "
5097 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005098 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005099 }
James Smart34b02dc2008-08-24 21:49:55 -04005100 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005101 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005102 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005103 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005104 }
James Smart92d7f7b2007-06-17 19:56:38 -05005105do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005106 mempool_free(pmb, phba->mbox_mem_pool);
5107 return rc;
5108}
5109
James Smarte59058c2008-08-24 21:49:00 -04005110
5111/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005112 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005113 * @phba: Pointer to HBA context object.
5114 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005115 * This function is the main SLI initialization function. This function
5116 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005117 * error attention handler code. Caller is not required to hold any
5118 * locks. This function issues config_port mailbox command to configure
5119 * the SLI, setup iocb rings and HBQ rings. In the end the function
5120 * calls the config_port_post function to issue init_link mailbox
5121 * command and to start the discovery. The function will return zero
5122 * if successful, else it will return negative error code.
5123 **/
James Smarted957682007-06-17 19:56:37 -05005124int
5125lpfc_sli_hba_setup(struct lpfc_hba *phba)
5126{
5127 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005128 int mode = 3, i;
5129 int longs;
James Smarted957682007-06-17 19:56:37 -05005130
James Smart12247e82016-07-06 12:36:09 -07005131 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005132 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005133 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005134 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005135 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005136 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005137 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005138 break;
5139 }
James Smarted957682007-06-17 19:56:37 -05005140 mode = 2;
5141 break;
5142 case 0:
5143 case 3:
5144 break;
5145 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005146 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005147 "1819 Unrecognized sli_mode parameter: %d.\n",
5148 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005149
5150 break;
5151 }
James Smartb5c53952016-03-31 14:12:30 -07005152 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005153
James Smart93996272008-08-24 21:50:30 -04005154 rc = lpfc_sli_config_port(phba, mode);
5155
James Smart12247e82016-07-06 12:36:09 -07005156 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005158 "1820 Unable to select SLI-3. "
5159 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005160 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005161 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005162 else if (rc && mode == 2)
5163 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005164 if (rc)
dea31012005-04-17 16:05:31 -05005165 goto lpfc_sli_hba_setup_error;
5166
James Smart0d878412009-10-02 15:16:56 -04005167 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5168 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5169 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5170 if (!rc) {
5171 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5172 "2709 This device supports "
5173 "Advanced Error Reporting (AER)\n");
5174 spin_lock_irq(&phba->hbalock);
5175 phba->hba_flag |= HBA_AER_ENABLED;
5176 spin_unlock_irq(&phba->hbalock);
5177 } else {
5178 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5179 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005180 "Advanced Error Reporting (AER): %d\n",
5181 rc);
James Smart0d878412009-10-02 15:16:56 -04005182 phba->cfg_aer_support = 0;
5183 }
5184 }
5185
James Smarted957682007-06-17 19:56:37 -05005186 if (phba->sli_rev == 3) {
5187 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5188 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005189 } else {
5190 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5191 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005192 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005193 }
5194
5195 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005196 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5197 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005198 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005199
5200 if (rc)
5201 goto lpfc_sli_hba_setup_error;
5202
James Smart6d368e52011-05-24 11:44:12 -04005203 /* Initialize VPIs. */
5204 if (phba->sli_rev == LPFC_SLI_REV3) {
5205 /*
5206 * The VPI bitmask and physical ID array are allocated
5207 * and initialized once only - at driver load. A port
5208 * reset doesn't need to reinitialize this memory.
5209 */
5210 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5211 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005212 phba->vpi_bmask = kcalloc(longs,
5213 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005214 GFP_KERNEL);
5215 if (!phba->vpi_bmask) {
5216 rc = -ENOMEM;
5217 goto lpfc_sli_hba_setup_error;
5218 }
5219
Kees Cook6396bb22018-06-12 14:03:40 -07005220 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5221 sizeof(uint16_t),
5222 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005223 if (!phba->vpi_ids) {
5224 kfree(phba->vpi_bmask);
5225 rc = -ENOMEM;
5226 goto lpfc_sli_hba_setup_error;
5227 }
5228 for (i = 0; i < phba->max_vpi; i++)
5229 phba->vpi_ids[i] = i;
5230 }
5231 }
5232
James Smart93996272008-08-24 21:50:30 -04005233 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005234 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5235 rc = lpfc_sli_hbq_setup(phba);
5236 if (rc)
5237 goto lpfc_sli_hba_setup_error;
5238 }
James Smart04c68492009-05-22 14:52:52 -04005239 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005240 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005241 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005242
5243 rc = lpfc_config_port_post(phba);
5244 if (rc)
5245 goto lpfc_sli_hba_setup_error;
5246
James Smarted957682007-06-17 19:56:37 -05005247 return rc;
5248
James Smart92d7f7b2007-06-17 19:56:38 -05005249lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005250 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005251 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005252 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005253 return rc;
5254}
5255
James Smartda0436e2009-05-22 14:51:39 -04005256/**
5257 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5258 * @phba: Pointer to HBA context object.
5259 * @mboxq: mailbox pointer.
5260 * This function issue a dump mailbox command to read config region
5261 * 23 and parse the records in the region and populate driver
5262 * data structure.
5263 **/
5264static int
James Smartff78d8f2011-12-13 13:21:35 -05005265lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005266{
James Smartff78d8f2011-12-13 13:21:35 -05005267 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005268 struct lpfc_dmabuf *mp;
5269 struct lpfc_mqe *mqe;
5270 uint32_t data_length;
5271 int rc;
5272
5273 /* Program the default value of vlan_id and fc_map */
5274 phba->valid_vlan = 0;
5275 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5276 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5277 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5278
James Smartff78d8f2011-12-13 13:21:35 -05005279 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5280 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005281 return -ENOMEM;
5282
James Smartff78d8f2011-12-13 13:21:35 -05005283 mqe = &mboxq->u.mqe;
5284 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5285 rc = -ENOMEM;
5286 goto out_free_mboxq;
5287 }
5288
James Smart3e1f0712018-11-29 16:09:29 -08005289 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04005290 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5291
5292 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5293 "(%d):2571 Mailbox cmd x%x Status x%x "
5294 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5295 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5296 "CQ: x%x x%x x%x x%x\n",
5297 mboxq->vport ? mboxq->vport->vpi : 0,
5298 bf_get(lpfc_mqe_command, mqe),
5299 bf_get(lpfc_mqe_status, mqe),
5300 mqe->un.mb_words[0], mqe->un.mb_words[1],
5301 mqe->un.mb_words[2], mqe->un.mb_words[3],
5302 mqe->un.mb_words[4], mqe->un.mb_words[5],
5303 mqe->un.mb_words[6], mqe->un.mb_words[7],
5304 mqe->un.mb_words[8], mqe->un.mb_words[9],
5305 mqe->un.mb_words[10], mqe->un.mb_words[11],
5306 mqe->un.mb_words[12], mqe->un.mb_words[13],
5307 mqe->un.mb_words[14], mqe->un.mb_words[15],
5308 mqe->un.mb_words[16], mqe->un.mb_words[50],
5309 mboxq->mcqe.word0,
5310 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5311 mboxq->mcqe.trailer);
5312
5313 if (rc) {
5314 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5315 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005316 rc = -EIO;
5317 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005318 }
5319 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005320 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005321 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5322 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005323 rc = -EIO;
5324 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005325 }
James Smartda0436e2009-05-22 14:51:39 -04005326
5327 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5328 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5329 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005330 rc = 0;
5331
5332out_free_mboxq:
5333 mempool_free(mboxq, phba->mbox_mem_pool);
5334 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005335}
5336
5337/**
5338 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5339 * @phba: pointer to lpfc hba data structure.
5340 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5341 * @vpd: pointer to the memory to hold resulting port vpd data.
5342 * @vpd_size: On input, the number of bytes allocated to @vpd.
5343 * On output, the number of data bytes in @vpd.
5344 *
5345 * This routine executes a READ_REV SLI4 mailbox command. In
5346 * addition, this routine gets the port vpd data.
5347 *
5348 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005349 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005350 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005351 **/
5352static int
5353lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5354 uint8_t *vpd, uint32_t *vpd_size)
5355{
5356 int rc = 0;
5357 uint32_t dma_size;
5358 struct lpfc_dmabuf *dmabuf;
5359 struct lpfc_mqe *mqe;
5360
5361 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5362 if (!dmabuf)
5363 return -ENOMEM;
5364
5365 /*
5366 * Get a DMA buffer for the vpd data resulting from the READ_REV
5367 * mailbox command.
5368 */
5369 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005370 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5371 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005372 if (!dmabuf->virt) {
5373 kfree(dmabuf);
5374 return -ENOMEM;
5375 }
James Smartda0436e2009-05-22 14:51:39 -04005376
5377 /*
5378 * The SLI4 implementation of READ_REV conflicts at word1,
5379 * bits 31:16 and SLI4 adds vpd functionality not present
5380 * in SLI3. This code corrects the conflicts.
5381 */
5382 lpfc_read_rev(phba, mboxq);
5383 mqe = &mboxq->u.mqe;
5384 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5385 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5386 mqe->un.read_rev.word1 &= 0x0000FFFF;
5387 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5388 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5389
5390 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5391 if (rc) {
5392 dma_free_coherent(&phba->pcidev->dev, dma_size,
5393 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005394 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005395 return -EIO;
5396 }
5397
James Smartda0436e2009-05-22 14:51:39 -04005398 /*
5399 * The available vpd length cannot be bigger than the
5400 * DMA buffer passed to the port. Catch the less than
5401 * case and update the caller's size.
5402 */
5403 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5404 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5405
James Smartd7c47992010-06-08 18:31:54 -04005406 memcpy(vpd, dmabuf->virt, *vpd_size);
5407
James Smartda0436e2009-05-22 14:51:39 -04005408 dma_free_coherent(&phba->pcidev->dev, dma_size,
5409 dmabuf->virt, dmabuf->phys);
5410 kfree(dmabuf);
5411 return 0;
5412}
5413
5414/**
James Smartcd1c8302011-10-10 21:33:25 -04005415 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5416 * @phba: pointer to lpfc hba data structure.
5417 *
5418 * This routine retrieves SLI4 device physical port name this PCI function
5419 * is attached to.
5420 *
5421 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005422 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005423 * otherwise - failed to retrieve physical port name
5424 **/
5425static int
5426lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5427{
5428 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005429 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5430 struct lpfc_controller_attribute *cntl_attr;
5431 struct lpfc_mbx_get_port_name *get_port_name;
5432 void *virtaddr = NULL;
5433 uint32_t alloclen, reqlen;
5434 uint32_t shdr_status, shdr_add_status;
5435 union lpfc_sli4_cfg_shdr *shdr;
5436 char cport_name = 0;
5437 int rc;
5438
5439 /* We assume nothing at this point */
5440 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5441 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5442
5443 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5444 if (!mboxq)
5445 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005446 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005447 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5448 lpfc_sli4_read_config(phba);
5449 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5450 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005451
5452 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5453 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5454 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5455 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5456 LPFC_SLI4_MBX_NEMBED);
5457 if (alloclen < reqlen) {
5458 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5459 "3084 Allocated DMA memory size (%d) is "
5460 "less than the requested DMA memory size "
5461 "(%d)\n", alloclen, reqlen);
5462 rc = -ENOMEM;
5463 goto out_free_mboxq;
5464 }
5465 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5466 virtaddr = mboxq->sge_array->addr[0];
5467 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5468 shdr = &mbx_cntl_attr->cfg_shdr;
5469 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5470 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5471 if (shdr_status || shdr_add_status || rc) {
5472 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5473 "3085 Mailbox x%x (x%x/x%x) failed, "
5474 "rc:x%x, status:x%x, add_status:x%x\n",
5475 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5476 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5477 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5478 rc, shdr_status, shdr_add_status);
5479 rc = -ENXIO;
5480 goto out_free_mboxq;
5481 }
5482 cntl_attr = &mbx_cntl_attr->cntl_attr;
5483 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5484 phba->sli4_hba.lnk_info.lnk_tp =
5485 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5486 phba->sli4_hba.lnk_info.lnk_no =
5487 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5488 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5489 "3086 lnk_type:%d, lnk_numb:%d\n",
5490 phba->sli4_hba.lnk_info.lnk_tp,
5491 phba->sli4_hba.lnk_info.lnk_no);
5492
5493retrieve_ppname:
5494 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5495 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5496 sizeof(struct lpfc_mbx_get_port_name) -
5497 sizeof(struct lpfc_sli4_cfg_mhdr),
5498 LPFC_SLI4_MBX_EMBED);
5499 get_port_name = &mboxq->u.mqe.un.get_port_name;
5500 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5501 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5502 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5503 phba->sli4_hba.lnk_info.lnk_tp);
5504 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5505 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5506 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5507 if (shdr_status || shdr_add_status || rc) {
5508 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5509 "3087 Mailbox x%x (x%x/x%x) failed: "
5510 "rc:x%x, status:x%x, add_status:x%x\n",
5511 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5512 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5513 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5514 rc, shdr_status, shdr_add_status);
5515 rc = -ENXIO;
5516 goto out_free_mboxq;
5517 }
5518 switch (phba->sli4_hba.lnk_info.lnk_no) {
5519 case LPFC_LINK_NUMBER_0:
5520 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5521 &get_port_name->u.response);
5522 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5523 break;
5524 case LPFC_LINK_NUMBER_1:
5525 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5526 &get_port_name->u.response);
5527 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5528 break;
5529 case LPFC_LINK_NUMBER_2:
5530 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5531 &get_port_name->u.response);
5532 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5533 break;
5534 case LPFC_LINK_NUMBER_3:
5535 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5536 &get_port_name->u.response);
5537 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5538 break;
5539 default:
5540 break;
5541 }
5542
5543 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5544 phba->Port[0] = cport_name;
5545 phba->Port[1] = '\0';
5546 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5547 "3091 SLI get port name: %s\n", phba->Port);
5548 }
5549
5550out_free_mboxq:
5551 if (rc != MBX_TIMEOUT) {
5552 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5553 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5554 else
5555 mempool_free(mboxq, phba->mbox_mem_pool);
5556 }
5557 return rc;
5558}
5559
5560/**
James Smartda0436e2009-05-22 14:51:39 -04005561 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5562 * @phba: pointer to lpfc hba data structure.
5563 *
5564 * This routine is called to explicitly arm the SLI4 device's completion and
5565 * event queues
5566 **/
5567static void
5568lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5569{
James Smart895427b2017-02-12 13:52:30 -08005570 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005571 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartcdb42be2019-01-28 11:14:21 -08005572 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -04005573
James Smartb71413d2018-02-22 08:18:40 -08005574 sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5575 sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5576 if (sli4_hba->nvmels_cq)
5577 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
James Smart895427b2017-02-12 13:52:30 -08005578 LPFC_QUEUE_REARM);
5579
James Smartcdb42be2019-01-28 11:14:21 -08005580 qp = sli4_hba->hdwq;
5581 if (sli4_hba->hdwq) {
5582 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
5583 sli4_hba->sli4_cq_release(qp[qidx].fcp_cq,
James Smart895427b2017-02-12 13:52:30 -08005584 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08005585 sli4_hba->sli4_cq_release(qp[qidx].nvme_cq,
James Smart895427b2017-02-12 13:52:30 -08005586 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08005587 }
James Smart1ba981f2014-02-20 09:56:45 -05005588
James Smart6a828b02019-01-28 11:14:31 -08005589 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++)
James Smartcdb42be2019-01-28 11:14:21 -08005590 sli4_hba->sli4_eq_release(qp[qidx].hba_eq,
5591 LPFC_QUEUE_REARM);
5592 }
James Smart1ba981f2014-02-20 09:56:45 -05005593
James Smart2d7dbc42017-02-12 13:52:35 -08005594 if (phba->nvmet_support) {
5595 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smartb71413d2018-02-22 08:18:40 -08005596 sli4_hba->sli4_cq_release(
5597 sli4_hba->nvmet_cqset[qidx],
James Smart2d7dbc42017-02-12 13:52:35 -08005598 LPFC_QUEUE_REARM);
5599 }
James Smart2e90f4b2011-12-13 13:22:37 -05005600 }
James Smartda0436e2009-05-22 14:51:39 -04005601}
5602
5603/**
James Smart6d368e52011-05-24 11:44:12 -04005604 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5605 * @phba: Pointer to HBA context object.
5606 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005607 * @extnt_count: buffer to hold port available extent count.
5608 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005609 *
James Smartb76f2dc2011-07-22 18:37:42 -04005610 * This function calls the port and retrievs the number of available
5611 * extents and their size for a particular extent type.
5612 *
5613 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005614 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005615int
James Smart6d368e52011-05-24 11:44:12 -04005616lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5617 uint16_t *extnt_count, uint16_t *extnt_size)
5618{
5619 int rc = 0;
5620 uint32_t length;
5621 uint32_t mbox_tmo;
5622 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5623 LPFC_MBOXQ_t *mbox;
5624
5625 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5626 if (!mbox)
5627 return -ENOMEM;
5628
5629 /* Find out how many extents are available for this resource type */
5630 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5631 sizeof(struct lpfc_sli4_cfg_mhdr));
5632 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5633 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5634 length, LPFC_SLI4_MBX_EMBED);
5635
5636 /* Send an extents count of 0 - the GET doesn't use it. */
5637 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5638 LPFC_SLI4_MBX_EMBED);
5639 if (unlikely(rc)) {
5640 rc = -EIO;
5641 goto err_exit;
5642 }
5643
5644 if (!phba->sli4_hba.intr_enable)
5645 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5646 else {
James Smarta183a152011-10-10 21:32:43 -04005647 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005648 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5649 }
5650 if (unlikely(rc)) {
5651 rc = -EIO;
5652 goto err_exit;
5653 }
5654
5655 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5656 if (bf_get(lpfc_mbox_hdr_status,
5657 &rsrc_info->header.cfg_shdr.response)) {
5658 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5659 "2930 Failed to get resource extents "
5660 "Status 0x%x Add'l Status 0x%x\n",
5661 bf_get(lpfc_mbox_hdr_status,
5662 &rsrc_info->header.cfg_shdr.response),
5663 bf_get(lpfc_mbox_hdr_add_status,
5664 &rsrc_info->header.cfg_shdr.response));
5665 rc = -EIO;
5666 goto err_exit;
5667 }
5668
5669 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5670 &rsrc_info->u.rsp);
5671 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5672 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005673
5674 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5675 "3162 Retrieved extents type-%d from port: count:%d, "
5676 "size:%d\n", type, *extnt_count, *extnt_size);
5677
5678err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005679 mempool_free(mbox, phba->mbox_mem_pool);
5680 return rc;
5681}
5682
5683/**
5684 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5685 * @phba: Pointer to HBA context object.
5686 * @type: The extent type to check.
5687 *
5688 * This function reads the current available extents from the port and checks
5689 * if the extent count or extent size has changed since the last access.
5690 * Callers use this routine post port reset to understand if there is a
5691 * extent reprovisioning requirement.
5692 *
5693 * Returns:
5694 * -Error: error indicates problem.
5695 * 1: Extent count or size has changed.
5696 * 0: No changes.
5697 **/
5698static int
5699lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5700{
5701 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5702 uint16_t size_diff, rsrc_ext_size;
5703 int rc = 0;
5704 struct lpfc_rsrc_blks *rsrc_entry;
5705 struct list_head *rsrc_blk_list = NULL;
5706
5707 size_diff = 0;
5708 curr_ext_cnt = 0;
5709 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5710 &rsrc_ext_cnt,
5711 &rsrc_ext_size);
5712 if (unlikely(rc))
5713 return -EIO;
5714
5715 switch (type) {
5716 case LPFC_RSC_TYPE_FCOE_RPI:
5717 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5718 break;
5719 case LPFC_RSC_TYPE_FCOE_VPI:
5720 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5721 break;
5722 case LPFC_RSC_TYPE_FCOE_XRI:
5723 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5724 break;
5725 case LPFC_RSC_TYPE_FCOE_VFI:
5726 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5727 break;
5728 default:
5729 break;
5730 }
5731
5732 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5733 curr_ext_cnt++;
5734 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5735 size_diff++;
5736 }
5737
5738 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5739 rc = 1;
5740
5741 return rc;
5742}
5743
5744/**
5745 * lpfc_sli4_cfg_post_extnts -
5746 * @phba: Pointer to HBA context object.
5747 * @extnt_cnt - number of available extents.
5748 * @type - the extent type (rpi, xri, vfi, vpi).
5749 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5750 * @mbox - pointer to the caller's allocated mailbox structure.
5751 *
5752 * This function executes the extents allocation request. It also
5753 * takes care of the amount of memory needed to allocate or get the
5754 * allocated extents. It is the caller's responsibility to evaluate
5755 * the response.
5756 *
5757 * Returns:
5758 * -Error: Error value describes the condition found.
5759 * 0: if successful
5760 **/
5761static int
James Smart8a9d2e82012-05-09 21:16:12 -04005762lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005763 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5764{
5765 int rc = 0;
5766 uint32_t req_len;
5767 uint32_t emb_len;
5768 uint32_t alloc_len, mbox_tmo;
5769
5770 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005771 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005772
5773 /*
5774 * Calculate the size of an embedded mailbox. The uint32_t
5775 * accounts for extents-specific word.
5776 */
5777 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5778 sizeof(uint32_t);
5779
5780 /*
5781 * Presume the allocation and response will fit into an embedded
5782 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5783 */
5784 *emb = LPFC_SLI4_MBX_EMBED;
5785 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005786 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005787 sizeof(union lpfc_sli4_cfg_shdr) +
5788 sizeof(uint32_t);
5789 *emb = LPFC_SLI4_MBX_NEMBED;
5790 }
5791
5792 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5793 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5794 req_len, *emb);
5795 if (alloc_len < req_len) {
5796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005797 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005798 "less than the requested DMA memory "
5799 "size (x%x)\n", alloc_len, req_len);
5800 return -ENOMEM;
5801 }
James Smart8a9d2e82012-05-09 21:16:12 -04005802 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005803 if (unlikely(rc))
5804 return -EIO;
5805
5806 if (!phba->sli4_hba.intr_enable)
5807 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5808 else {
James Smarta183a152011-10-10 21:32:43 -04005809 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005810 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5811 }
5812
5813 if (unlikely(rc))
5814 rc = -EIO;
5815 return rc;
5816}
5817
5818/**
5819 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5820 * @phba: Pointer to HBA context object.
5821 * @type: The resource extent type to allocate.
5822 *
5823 * This function allocates the number of elements for the specified
5824 * resource type.
5825 **/
5826static int
5827lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5828{
5829 bool emb = false;
5830 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5831 uint16_t rsrc_id, rsrc_start, j, k;
5832 uint16_t *ids;
5833 int i, rc;
5834 unsigned long longs;
5835 unsigned long *bmask;
5836 struct lpfc_rsrc_blks *rsrc_blks;
5837 LPFC_MBOXQ_t *mbox;
5838 uint32_t length;
5839 struct lpfc_id_range *id_array = NULL;
5840 void *virtaddr = NULL;
5841 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5842 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5843 struct list_head *ext_blk_list;
5844
5845 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5846 &rsrc_cnt,
5847 &rsrc_size);
5848 if (unlikely(rc))
5849 return -EIO;
5850
5851 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5852 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5853 "3009 No available Resource Extents "
5854 "for resource type 0x%x: Count: 0x%x, "
5855 "Size 0x%x\n", type, rsrc_cnt,
5856 rsrc_size);
5857 return -ENOMEM;
5858 }
5859
James Smart8a9d2e82012-05-09 21:16:12 -04005860 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5861 "2903 Post resource extents type-0x%x: "
5862 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005863
5864 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5865 if (!mbox)
5866 return -ENOMEM;
5867
James Smart8a9d2e82012-05-09 21:16:12 -04005868 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005869 if (unlikely(rc)) {
5870 rc = -EIO;
5871 goto err_exit;
5872 }
5873
5874 /*
5875 * Figure out where the response is located. Then get local pointers
5876 * to the response data. The port does not guarantee to respond to
5877 * all extents counts request so update the local variable with the
5878 * allocated count from the port.
5879 */
5880 if (emb == LPFC_SLI4_MBX_EMBED) {
5881 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5882 id_array = &rsrc_ext->u.rsp.id[0];
5883 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5884 } else {
5885 virtaddr = mbox->sge_array->addr[0];
5886 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5887 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5888 id_array = &n_rsrc->id;
5889 }
5890
5891 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5892 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5893
5894 /*
5895 * Based on the resource size and count, correct the base and max
5896 * resource values.
5897 */
5898 length = sizeof(struct lpfc_rsrc_blks);
5899 switch (type) {
5900 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005901 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005902 sizeof(unsigned long),
5903 GFP_KERNEL);
5904 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5905 rc = -ENOMEM;
5906 goto err_exit;
5907 }
Kees Cook6396bb22018-06-12 14:03:40 -07005908 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005909 sizeof(uint16_t),
5910 GFP_KERNEL);
5911 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5912 kfree(phba->sli4_hba.rpi_bmask);
5913 rc = -ENOMEM;
5914 goto err_exit;
5915 }
5916
5917 /*
5918 * The next_rpi was initialized with the maximum available
5919 * count but the port may allocate a smaller number. Catch
5920 * that case and update the next_rpi.
5921 */
5922 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5923
5924 /* Initialize local ptrs for common extent processing later. */
5925 bmask = phba->sli4_hba.rpi_bmask;
5926 ids = phba->sli4_hba.rpi_ids;
5927 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5928 break;
5929 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005930 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005931 GFP_KERNEL);
5932 if (unlikely(!phba->vpi_bmask)) {
5933 rc = -ENOMEM;
5934 goto err_exit;
5935 }
Kees Cook6396bb22018-06-12 14:03:40 -07005936 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04005937 GFP_KERNEL);
5938 if (unlikely(!phba->vpi_ids)) {
5939 kfree(phba->vpi_bmask);
5940 rc = -ENOMEM;
5941 goto err_exit;
5942 }
5943
5944 /* Initialize local ptrs for common extent processing later. */
5945 bmask = phba->vpi_bmask;
5946 ids = phba->vpi_ids;
5947 ext_blk_list = &phba->lpfc_vpi_blk_list;
5948 break;
5949 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07005950 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005951 sizeof(unsigned long),
5952 GFP_KERNEL);
5953 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5954 rc = -ENOMEM;
5955 goto err_exit;
5956 }
James Smart8a9d2e82012-05-09 21:16:12 -04005957 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07005958 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005959 sizeof(uint16_t),
5960 GFP_KERNEL);
5961 if (unlikely(!phba->sli4_hba.xri_ids)) {
5962 kfree(phba->sli4_hba.xri_bmask);
5963 rc = -ENOMEM;
5964 goto err_exit;
5965 }
5966
5967 /* Initialize local ptrs for common extent processing later. */
5968 bmask = phba->sli4_hba.xri_bmask;
5969 ids = phba->sli4_hba.xri_ids;
5970 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5971 break;
5972 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07005973 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005974 sizeof(unsigned long),
5975 GFP_KERNEL);
5976 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5977 rc = -ENOMEM;
5978 goto err_exit;
5979 }
Kees Cook6396bb22018-06-12 14:03:40 -07005980 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005981 sizeof(uint16_t),
5982 GFP_KERNEL);
5983 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5984 kfree(phba->sli4_hba.vfi_bmask);
5985 rc = -ENOMEM;
5986 goto err_exit;
5987 }
5988
5989 /* Initialize local ptrs for common extent processing later. */
5990 bmask = phba->sli4_hba.vfi_bmask;
5991 ids = phba->sli4_hba.vfi_ids;
5992 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5993 break;
5994 default:
5995 /* Unsupported Opcode. Fail call. */
5996 id_array = NULL;
5997 bmask = NULL;
5998 ids = NULL;
5999 ext_blk_list = NULL;
6000 goto err_exit;
6001 }
6002
6003 /*
6004 * Complete initializing the extent configuration with the
6005 * allocated ids assigned to this function. The bitmask serves
6006 * as an index into the array and manages the available ids. The
6007 * array just stores the ids communicated to the port via the wqes.
6008 */
6009 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6010 if ((i % 2) == 0)
6011 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6012 &id_array[k]);
6013 else
6014 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6015 &id_array[k]);
6016
6017 rsrc_blks = kzalloc(length, GFP_KERNEL);
6018 if (unlikely(!rsrc_blks)) {
6019 rc = -ENOMEM;
6020 kfree(bmask);
6021 kfree(ids);
6022 goto err_exit;
6023 }
6024 rsrc_blks->rsrc_start = rsrc_id;
6025 rsrc_blks->rsrc_size = rsrc_size;
6026 list_add_tail(&rsrc_blks->list, ext_blk_list);
6027 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08006028 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart5e5b5112019-01-28 11:14:22 -08006029 phba->sli4_hba.io_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08006030 lpfc_sli4_get_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08006031 }
James Smart6d368e52011-05-24 11:44:12 -04006032
6033 while (rsrc_id < (rsrc_start + rsrc_size)) {
6034 ids[j] = rsrc_id;
6035 rsrc_id++;
6036 j++;
6037 }
6038 /* Entire word processed. Get next word.*/
6039 if ((i % 2) == 1)
6040 k++;
6041 }
6042 err_exit:
6043 lpfc_sli4_mbox_cmd_free(phba, mbox);
6044 return rc;
6045}
6046
James Smart895427b2017-02-12 13:52:30 -08006047
6048
James Smart6d368e52011-05-24 11:44:12 -04006049/**
6050 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6051 * @phba: Pointer to HBA context object.
6052 * @type: the extent's type.
6053 *
6054 * This function deallocates all extents of a particular resource type.
6055 * SLI4 does not allow for deallocating a particular extent range. It
6056 * is the caller's responsibility to release all kernel memory resources.
6057 **/
6058static int
6059lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6060{
6061 int rc;
6062 uint32_t length, mbox_tmo = 0;
6063 LPFC_MBOXQ_t *mbox;
6064 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6065 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6066
6067 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6068 if (!mbox)
6069 return -ENOMEM;
6070
6071 /*
6072 * This function sends an embedded mailbox because it only sends the
6073 * the resource type. All extents of this type are released by the
6074 * port.
6075 */
6076 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6077 sizeof(struct lpfc_sli4_cfg_mhdr));
6078 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6079 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6080 length, LPFC_SLI4_MBX_EMBED);
6081
6082 /* Send an extents count of 0 - the dealloc doesn't use it. */
6083 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6084 LPFC_SLI4_MBX_EMBED);
6085 if (unlikely(rc)) {
6086 rc = -EIO;
6087 goto out_free_mbox;
6088 }
6089 if (!phba->sli4_hba.intr_enable)
6090 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6091 else {
James Smarta183a152011-10-10 21:32:43 -04006092 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006093 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6094 }
6095 if (unlikely(rc)) {
6096 rc = -EIO;
6097 goto out_free_mbox;
6098 }
6099
6100 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6101 if (bf_get(lpfc_mbox_hdr_status,
6102 &dealloc_rsrc->header.cfg_shdr.response)) {
6103 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6104 "2919 Failed to release resource extents "
6105 "for type %d - Status 0x%x Add'l Status 0x%x. "
6106 "Resource memory not released.\n",
6107 type,
6108 bf_get(lpfc_mbox_hdr_status,
6109 &dealloc_rsrc->header.cfg_shdr.response),
6110 bf_get(lpfc_mbox_hdr_add_status,
6111 &dealloc_rsrc->header.cfg_shdr.response));
6112 rc = -EIO;
6113 goto out_free_mbox;
6114 }
6115
6116 /* Release kernel memory resources for the specific type. */
6117 switch (type) {
6118 case LPFC_RSC_TYPE_FCOE_VPI:
6119 kfree(phba->vpi_bmask);
6120 kfree(phba->vpi_ids);
6121 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6122 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6123 &phba->lpfc_vpi_blk_list, list) {
6124 list_del_init(&rsrc_blk->list);
6125 kfree(rsrc_blk);
6126 }
James Smart16a3a202013-04-17 20:14:38 -04006127 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006128 break;
6129 case LPFC_RSC_TYPE_FCOE_XRI:
6130 kfree(phba->sli4_hba.xri_bmask);
6131 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006132 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6133 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6134 list_del_init(&rsrc_blk->list);
6135 kfree(rsrc_blk);
6136 }
6137 break;
6138 case LPFC_RSC_TYPE_FCOE_VFI:
6139 kfree(phba->sli4_hba.vfi_bmask);
6140 kfree(phba->sli4_hba.vfi_ids);
6141 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6142 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6143 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6144 list_del_init(&rsrc_blk->list);
6145 kfree(rsrc_blk);
6146 }
6147 break;
6148 case LPFC_RSC_TYPE_FCOE_RPI:
6149 /* RPI bitmask and physical id array are cleaned up earlier. */
6150 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6151 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6152 list_del_init(&rsrc_blk->list);
6153 kfree(rsrc_blk);
6154 }
6155 break;
6156 default:
6157 break;
6158 }
6159
6160 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6161
6162 out_free_mbox:
6163 mempool_free(mbox, phba->mbox_mem_pool);
6164 return rc;
6165}
6166
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006167static void
James Smart7bdedb32016-07-06 12:36:00 -07006168lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6169 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006170{
James Smart65791f12016-07-06 12:35:56 -07006171 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006172
James Smart65791f12016-07-06 12:35:56 -07006173 len = sizeof(struct lpfc_mbx_set_feature) -
6174 sizeof(struct lpfc_sli4_cfg_mhdr);
6175 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6176 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6177 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006178
James Smart7bdedb32016-07-06 12:36:00 -07006179 switch (feature) {
6180 case LPFC_SET_UE_RECOVERY:
6181 bf_set(lpfc_mbx_set_feature_UER,
6182 &mbox->u.mqe.un.set_feature, 1);
6183 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6184 mbox->u.mqe.un.set_feature.param_len = 8;
6185 break;
6186 case LPFC_SET_MDS_DIAGS:
6187 bf_set(lpfc_mbx_set_feature_mds,
6188 &mbox->u.mqe.un.set_feature, 1);
6189 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006190 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006191 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6192 mbox->u.mqe.un.set_feature.param_len = 8;
6193 break;
James Smart65791f12016-07-06 12:35:56 -07006194 }
James Smart7bdedb32016-07-06 12:36:00 -07006195
6196 return;
James Smart65791f12016-07-06 12:35:56 -07006197}
6198
James Smart6d368e52011-05-24 11:44:12 -04006199/**
James Smart1165a5c2018-11-29 16:09:39 -08006200 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6201 * @phba: Pointer to HBA context object.
6202 *
6203 * Disable FW logging into host memory on the adapter. To
6204 * be done before reading logs from the host memory.
6205 **/
6206void
6207lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6208{
6209 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6210
6211 ras_fwlog->ras_active = false;
6212
6213 /* Disable FW logging to host memory */
6214 writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6215 phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6216}
6217
6218/**
James Smartd2cc9bc2018-09-10 10:30:50 -07006219 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6220 * @phba: Pointer to HBA context object.
6221 *
6222 * This function is called to free memory allocated for RAS FW logging
6223 * support in the driver.
6224 **/
6225void
6226lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6227{
6228 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6229 struct lpfc_dmabuf *dmabuf, *next;
6230
6231 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6232 list_for_each_entry_safe(dmabuf, next,
6233 &ras_fwlog->fwlog_buff_list,
6234 list) {
6235 list_del(&dmabuf->list);
6236 dma_free_coherent(&phba->pcidev->dev,
6237 LPFC_RAS_MAX_ENTRY_SIZE,
6238 dmabuf->virt, dmabuf->phys);
6239 kfree(dmabuf);
6240 }
6241 }
6242
6243 if (ras_fwlog->lwpd.virt) {
6244 dma_free_coherent(&phba->pcidev->dev,
6245 sizeof(uint32_t) * 2,
6246 ras_fwlog->lwpd.virt,
6247 ras_fwlog->lwpd.phys);
6248 ras_fwlog->lwpd.virt = NULL;
6249 }
6250
6251 ras_fwlog->ras_active = false;
6252}
6253
6254/**
6255 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6256 * @phba: Pointer to HBA context object.
6257 * @fwlog_buff_count: Count of buffers to be created.
6258 *
6259 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6260 * to update FW log is posted to the adapter.
6261 * Buffer count is calculated based on module param ras_fwlog_buffsize
6262 * Size of each buffer posted to FW is 64K.
6263 **/
6264
6265static int
6266lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6267 uint32_t fwlog_buff_count)
6268{
6269 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6270 struct lpfc_dmabuf *dmabuf;
6271 int rc = 0, i = 0;
6272
6273 /* Initialize List */
6274 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6275
6276 /* Allocate memory for the LWPD */
6277 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6278 sizeof(uint32_t) * 2,
6279 &ras_fwlog->lwpd.phys,
6280 GFP_KERNEL);
6281 if (!ras_fwlog->lwpd.virt) {
James Smartcb349902018-11-29 16:09:27 -08006282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006283 "6185 LWPD Memory Alloc Failed\n");
6284
6285 return -ENOMEM;
6286 }
6287
6288 ras_fwlog->fw_buffcount = fwlog_buff_count;
6289 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6290 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6291 GFP_KERNEL);
6292 if (!dmabuf) {
6293 rc = -ENOMEM;
6294 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6295 "6186 Memory Alloc failed FW logging");
6296 goto free_mem;
6297 }
6298
Sabyasachi Gupta359d0ac2018-11-18 20:08:48 +05306299 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
James Smartd2cc9bc2018-09-10 10:30:50 -07006300 LPFC_RAS_MAX_ENTRY_SIZE,
6301 &dmabuf->phys,
6302 GFP_KERNEL);
6303 if (!dmabuf->virt) {
6304 kfree(dmabuf);
6305 rc = -ENOMEM;
6306 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6307 "6187 DMA Alloc Failed FW logging");
6308 goto free_mem;
6309 }
James Smartd2cc9bc2018-09-10 10:30:50 -07006310 dmabuf->buffer_tag = i;
6311 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6312 }
6313
6314free_mem:
6315 if (rc)
6316 lpfc_sli4_ras_dma_free(phba);
6317
6318 return rc;
6319}
6320
6321/**
6322 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6323 * @phba: pointer to lpfc hba data structure.
6324 * @pmboxq: pointer to the driver internal queue element for mailbox command.
6325 *
6326 * Completion handler for driver's RAS MBX command to the device.
6327 **/
6328static void
6329lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6330{
6331 MAILBOX_t *mb;
6332 union lpfc_sli4_cfg_shdr *shdr;
6333 uint32_t shdr_status, shdr_add_status;
6334 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6335
6336 mb = &pmb->u.mb;
6337
6338 shdr = (union lpfc_sli4_cfg_shdr *)
6339 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6340 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6341 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6342
6343 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
James Smartcb349902018-11-29 16:09:27 -08006344 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smartd2cc9bc2018-09-10 10:30:50 -07006345 "6188 FW LOG mailbox "
6346 "completed with status x%x add_status x%x,"
6347 " mbx status x%x\n",
6348 shdr_status, shdr_add_status, mb->mbxStatus);
James Smartcb349902018-11-29 16:09:27 -08006349
6350 ras_fwlog->ras_hwsupport = false;
James Smartd2cc9bc2018-09-10 10:30:50 -07006351 goto disable_ras;
6352 }
6353
6354 ras_fwlog->ras_active = true;
6355 mempool_free(pmb, phba->mbox_mem_pool);
6356
6357 return;
6358
6359disable_ras:
6360 /* Free RAS DMA memory */
6361 lpfc_sli4_ras_dma_free(phba);
6362 mempool_free(pmb, phba->mbox_mem_pool);
6363}
6364
6365/**
6366 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6367 * @phba: pointer to lpfc hba data structure.
6368 * @fwlog_level: Logging verbosity level.
6369 * @fwlog_enable: Enable/Disable logging.
6370 *
6371 * Initialize memory and post mailbox command to enable FW logging in host
6372 * memory.
6373 **/
6374int
6375lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6376 uint32_t fwlog_level,
6377 uint32_t fwlog_enable)
6378{
6379 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6380 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6381 struct lpfc_dmabuf *dmabuf;
6382 LPFC_MBOXQ_t *mbox;
6383 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6384 int rc = 0;
6385
6386 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6387 phba->cfg_ras_fwlog_buffsize);
6388 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6389
6390 /*
6391 * If re-enabling FW logging support use earlier allocated
6392 * DMA buffers while posting MBX command.
6393 **/
6394 if (!ras_fwlog->lwpd.virt) {
6395 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6396 if (rc) {
6397 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smartcb349902018-11-29 16:09:27 -08006398 "6189 FW Log Memory Allocation Failed");
James Smartd2cc9bc2018-09-10 10:30:50 -07006399 return rc;
6400 }
6401 }
6402
6403 /* Setup Mailbox command */
6404 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6405 if (!mbox) {
James Smartcb349902018-11-29 16:09:27 -08006406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006407 "6190 RAS MBX Alloc Failed");
6408 rc = -ENOMEM;
6409 goto mem_free;
6410 }
6411
6412 ras_fwlog->fw_loglevel = fwlog_level;
6413 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6414 sizeof(struct lpfc_sli4_cfg_mhdr));
6415
6416 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6417 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6418 len, LPFC_SLI4_MBX_EMBED);
6419
6420 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6421 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6422 fwlog_enable);
6423 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6424 ras_fwlog->fw_loglevel);
6425 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6426 ras_fwlog->fw_buffcount);
6427 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6428 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6429
6430 /* Update DMA buffer address */
6431 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6432 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6433
6434 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6435 putPaddrLow(dmabuf->phys);
6436
6437 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6438 putPaddrHigh(dmabuf->phys);
6439 }
6440
6441 /* Update LPWD address */
6442 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6443 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6444
6445 mbox->vport = phba->pport;
6446 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6447
6448 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6449
6450 if (rc == MBX_NOT_FINISHED) {
James Smartcb349902018-11-29 16:09:27 -08006451 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6452 "6191 FW-Log Mailbox failed. "
James Smartd2cc9bc2018-09-10 10:30:50 -07006453 "status %d mbxStatus : x%x", rc,
6454 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6455 mempool_free(mbox, phba->mbox_mem_pool);
6456 rc = -EIO;
6457 goto mem_free;
6458 } else
6459 rc = 0;
6460mem_free:
6461 if (rc)
6462 lpfc_sli4_ras_dma_free(phba);
6463
6464 return rc;
6465}
6466
6467/**
6468 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6469 * @phba: Pointer to HBA context object.
6470 *
6471 * Check if RAS is supported on the adapter and initialize it.
6472 **/
6473void
6474lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6475{
6476 /* Check RAS FW Log needs to be enabled or not */
6477 if (lpfc_check_fwlog_support(phba))
6478 return;
6479
6480 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6481 LPFC_RAS_ENABLE_LOGGING);
6482}
6483
6484/**
James Smart6d368e52011-05-24 11:44:12 -04006485 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6486 * @phba: Pointer to HBA context object.
6487 *
6488 * This function allocates all SLI4 resource identifiers.
6489 **/
6490int
6491lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6492{
6493 int i, rc, error = 0;
6494 uint16_t count, base;
6495 unsigned long longs;
6496
James Smartff78d8f2011-12-13 13:21:35 -05006497 if (!phba->sli4_hba.rpi_hdrs_in_use)
6498 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006499 if (phba->sli4_hba.extents_in_use) {
6500 /*
6501 * The port supports resource extents. The XRI, VPI, VFI, RPI
6502 * resource extent count must be read and allocated before
6503 * provisioning the resource id arrays.
6504 */
6505 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6506 LPFC_IDX_RSRC_RDY) {
6507 /*
6508 * Extent-based resources are set - the driver could
6509 * be in a port reset. Figure out if any corrective
6510 * actions need to be taken.
6511 */
6512 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6513 LPFC_RSC_TYPE_FCOE_VFI);
6514 if (rc != 0)
6515 error++;
6516 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6517 LPFC_RSC_TYPE_FCOE_VPI);
6518 if (rc != 0)
6519 error++;
6520 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6521 LPFC_RSC_TYPE_FCOE_XRI);
6522 if (rc != 0)
6523 error++;
6524 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6525 LPFC_RSC_TYPE_FCOE_RPI);
6526 if (rc != 0)
6527 error++;
6528
6529 /*
6530 * It's possible that the number of resources
6531 * provided to this port instance changed between
6532 * resets. Detect this condition and reallocate
6533 * resources. Otherwise, there is no action.
6534 */
6535 if (error) {
6536 lpfc_printf_log(phba, KERN_INFO,
6537 LOG_MBOX | LOG_INIT,
6538 "2931 Detected extent resource "
6539 "change. Reallocating all "
6540 "extents.\n");
6541 rc = lpfc_sli4_dealloc_extent(phba,
6542 LPFC_RSC_TYPE_FCOE_VFI);
6543 rc = lpfc_sli4_dealloc_extent(phba,
6544 LPFC_RSC_TYPE_FCOE_VPI);
6545 rc = lpfc_sli4_dealloc_extent(phba,
6546 LPFC_RSC_TYPE_FCOE_XRI);
6547 rc = lpfc_sli4_dealloc_extent(phba,
6548 LPFC_RSC_TYPE_FCOE_RPI);
6549 } else
6550 return 0;
6551 }
6552
6553 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6554 if (unlikely(rc))
6555 goto err_exit;
6556
6557 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6558 if (unlikely(rc))
6559 goto err_exit;
6560
6561 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6562 if (unlikely(rc))
6563 goto err_exit;
6564
6565 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6566 if (unlikely(rc))
6567 goto err_exit;
6568 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6569 LPFC_IDX_RSRC_RDY);
6570 return rc;
6571 } else {
6572 /*
6573 * The port does not support resource extents. The XRI, VPI,
6574 * VFI, RPI resource ids were determined from READ_CONFIG.
6575 * Just allocate the bitmasks and provision the resource id
6576 * arrays. If a port reset is active, the resources don't
6577 * need any action - just exit.
6578 */
6579 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006580 LPFC_IDX_RSRC_RDY) {
6581 lpfc_sli4_dealloc_resource_identifiers(phba);
6582 lpfc_sli4_remove_rpis(phba);
6583 }
James Smart6d368e52011-05-24 11:44:12 -04006584 /* RPIs. */
6585 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006586 if (count <= 0) {
6587 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6588 "3279 Invalid provisioning of "
6589 "rpi:%d\n", count);
6590 rc = -EINVAL;
6591 goto err_exit;
6592 }
James Smart6d368e52011-05-24 11:44:12 -04006593 base = phba->sli4_hba.max_cfg_param.rpi_base;
6594 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006595 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006596 sizeof(unsigned long),
6597 GFP_KERNEL);
6598 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6599 rc = -ENOMEM;
6600 goto err_exit;
6601 }
Kees Cook6396bb22018-06-12 14:03:40 -07006602 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006603 GFP_KERNEL);
6604 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6605 rc = -ENOMEM;
6606 goto free_rpi_bmask;
6607 }
6608
6609 for (i = 0; i < count; i++)
6610 phba->sli4_hba.rpi_ids[i] = base + i;
6611
6612 /* VPIs. */
6613 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006614 if (count <= 0) {
6615 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6616 "3280 Invalid provisioning of "
6617 "vpi:%d\n", count);
6618 rc = -EINVAL;
6619 goto free_rpi_ids;
6620 }
James Smart6d368e52011-05-24 11:44:12 -04006621 base = phba->sli4_hba.max_cfg_param.vpi_base;
6622 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006623 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006624 GFP_KERNEL);
6625 if (unlikely(!phba->vpi_bmask)) {
6626 rc = -ENOMEM;
6627 goto free_rpi_ids;
6628 }
Kees Cook6396bb22018-06-12 14:03:40 -07006629 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006630 GFP_KERNEL);
6631 if (unlikely(!phba->vpi_ids)) {
6632 rc = -ENOMEM;
6633 goto free_vpi_bmask;
6634 }
6635
6636 for (i = 0; i < count; i++)
6637 phba->vpi_ids[i] = base + i;
6638
6639 /* XRIs. */
6640 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006641 if (count <= 0) {
6642 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6643 "3281 Invalid provisioning of "
6644 "xri:%d\n", count);
6645 rc = -EINVAL;
6646 goto free_vpi_ids;
6647 }
James Smart6d368e52011-05-24 11:44:12 -04006648 base = phba->sli4_hba.max_cfg_param.xri_base;
6649 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006650 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006651 sizeof(unsigned long),
6652 GFP_KERNEL);
6653 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6654 rc = -ENOMEM;
6655 goto free_vpi_ids;
6656 }
James Smart41899be2012-03-01 22:34:19 -05006657 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006658 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006659 GFP_KERNEL);
6660 if (unlikely(!phba->sli4_hba.xri_ids)) {
6661 rc = -ENOMEM;
6662 goto free_xri_bmask;
6663 }
6664
6665 for (i = 0; i < count; i++)
6666 phba->sli4_hba.xri_ids[i] = base + i;
6667
6668 /* VFIs. */
6669 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006670 if (count <= 0) {
6671 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6672 "3282 Invalid provisioning of "
6673 "vfi:%d\n", count);
6674 rc = -EINVAL;
6675 goto free_xri_ids;
6676 }
James Smart6d368e52011-05-24 11:44:12 -04006677 base = phba->sli4_hba.max_cfg_param.vfi_base;
6678 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006679 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006680 sizeof(unsigned long),
6681 GFP_KERNEL);
6682 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6683 rc = -ENOMEM;
6684 goto free_xri_ids;
6685 }
Kees Cook6396bb22018-06-12 14:03:40 -07006686 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006687 GFP_KERNEL);
6688 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6689 rc = -ENOMEM;
6690 goto free_vfi_bmask;
6691 }
6692
6693 for (i = 0; i < count; i++)
6694 phba->sli4_hba.vfi_ids[i] = base + i;
6695
6696 /*
6697 * Mark all resources ready. An HBA reset doesn't need
6698 * to reset the initialization.
6699 */
6700 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6701 LPFC_IDX_RSRC_RDY);
6702 return 0;
6703 }
6704
6705 free_vfi_bmask:
6706 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006707 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006708 free_xri_ids:
6709 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006710 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006711 free_xri_bmask:
6712 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006713 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006714 free_vpi_ids:
6715 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006716 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006717 free_vpi_bmask:
6718 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006719 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006720 free_rpi_ids:
6721 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006722 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006723 free_rpi_bmask:
6724 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006725 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006726 err_exit:
6727 return rc;
6728}
6729
6730/**
6731 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6732 * @phba: Pointer to HBA context object.
6733 *
6734 * This function allocates the number of elements for the specified
6735 * resource type.
6736 **/
6737int
6738lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6739{
6740 if (phba->sli4_hba.extents_in_use) {
6741 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6742 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6743 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6744 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6745 } else {
6746 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006747 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006748 kfree(phba->vpi_ids);
6749 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6750 kfree(phba->sli4_hba.xri_bmask);
6751 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006752 kfree(phba->sli4_hba.vfi_bmask);
6753 kfree(phba->sli4_hba.vfi_ids);
6754 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6755 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6756 }
6757
6758 return 0;
6759}
6760
6761/**
James Smartb76f2dc2011-07-22 18:37:42 -04006762 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6763 * @phba: Pointer to HBA context object.
6764 * @type: The resource extent type.
6765 * @extnt_count: buffer to hold port extent count response
6766 * @extnt_size: buffer to hold port extent size response.
6767 *
6768 * This function calls the port to read the host allocated extents
6769 * for a particular type.
6770 **/
6771int
6772lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6773 uint16_t *extnt_cnt, uint16_t *extnt_size)
6774{
6775 bool emb;
6776 int rc = 0;
6777 uint16_t curr_blks = 0;
6778 uint32_t req_len, emb_len;
6779 uint32_t alloc_len, mbox_tmo;
6780 struct list_head *blk_list_head;
6781 struct lpfc_rsrc_blks *rsrc_blk;
6782 LPFC_MBOXQ_t *mbox;
6783 void *virtaddr = NULL;
6784 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6785 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6786 union lpfc_sli4_cfg_shdr *shdr;
6787
6788 switch (type) {
6789 case LPFC_RSC_TYPE_FCOE_VPI:
6790 blk_list_head = &phba->lpfc_vpi_blk_list;
6791 break;
6792 case LPFC_RSC_TYPE_FCOE_XRI:
6793 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6794 break;
6795 case LPFC_RSC_TYPE_FCOE_VFI:
6796 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6797 break;
6798 case LPFC_RSC_TYPE_FCOE_RPI:
6799 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6800 break;
6801 default:
6802 return -EIO;
6803 }
6804
6805 /* Count the number of extents currently allocatd for this type. */
6806 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6807 if (curr_blks == 0) {
6808 /*
6809 * The GET_ALLOCATED mailbox does not return the size,
6810 * just the count. The size should be just the size
6811 * stored in the current allocated block and all sizes
6812 * for an extent type are the same so set the return
6813 * value now.
6814 */
6815 *extnt_size = rsrc_blk->rsrc_size;
6816 }
6817 curr_blks++;
6818 }
6819
James Smartb76f2dc2011-07-22 18:37:42 -04006820 /*
6821 * Calculate the size of an embedded mailbox. The uint32_t
6822 * accounts for extents-specific word.
6823 */
6824 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6825 sizeof(uint32_t);
6826
6827 /*
6828 * Presume the allocation and response will fit into an embedded
6829 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6830 */
6831 emb = LPFC_SLI4_MBX_EMBED;
6832 req_len = emb_len;
6833 if (req_len > emb_len) {
6834 req_len = curr_blks * sizeof(uint16_t) +
6835 sizeof(union lpfc_sli4_cfg_shdr) +
6836 sizeof(uint32_t);
6837 emb = LPFC_SLI4_MBX_NEMBED;
6838 }
6839
6840 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6841 if (!mbox)
6842 return -ENOMEM;
6843 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6844
6845 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6846 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6847 req_len, emb);
6848 if (alloc_len < req_len) {
6849 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6850 "2983 Allocated DMA memory size (x%x) is "
6851 "less than the requested DMA memory "
6852 "size (x%x)\n", alloc_len, req_len);
6853 rc = -ENOMEM;
6854 goto err_exit;
6855 }
6856 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6857 if (unlikely(rc)) {
6858 rc = -EIO;
6859 goto err_exit;
6860 }
6861
6862 if (!phba->sli4_hba.intr_enable)
6863 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6864 else {
James Smarta183a152011-10-10 21:32:43 -04006865 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006866 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6867 }
6868
6869 if (unlikely(rc)) {
6870 rc = -EIO;
6871 goto err_exit;
6872 }
6873
6874 /*
6875 * Figure out where the response is located. Then get local pointers
6876 * to the response data. The port does not guarantee to respond to
6877 * all extents counts request so update the local variable with the
6878 * allocated count from the port.
6879 */
6880 if (emb == LPFC_SLI4_MBX_EMBED) {
6881 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6882 shdr = &rsrc_ext->header.cfg_shdr;
6883 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6884 } else {
6885 virtaddr = mbox->sge_array->addr[0];
6886 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6887 shdr = &n_rsrc->cfg_shdr;
6888 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6889 }
6890
6891 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6892 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6893 "2984 Failed to read allocated resources "
6894 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6895 type,
6896 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6897 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6898 rc = -EIO;
6899 goto err_exit;
6900 }
6901 err_exit:
6902 lpfc_sli4_mbox_cmd_free(phba, mbox);
6903 return rc;
6904}
6905
6906/**
James Smart0ef69962017-04-21 16:04:50 -07006907 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006908 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006909 * @pring: Pointer to driver SLI ring object.
6910 * @sgl_list: linked link of sgl buffers to post
6911 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006912 *
James Smart895427b2017-02-12 13:52:30 -08006913 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006914 * repost them to the port by using SGL block post. This is needed after a
6915 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006916 * of buffer sgls which contains contiguous xris and uses the non-embedded
6917 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006918 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6919 * mailbox command for posting.
6920 *
6921 * Returns: 0 = success, non-zero failure.
6922 **/
6923static int
James Smart895427b2017-02-12 13:52:30 -08006924lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6925 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006926{
6927 struct lpfc_sglq *sglq_entry = NULL;
6928 struct lpfc_sglq *sglq_entry_next = NULL;
6929 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006930 int status, total_cnt;
6931 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006932 int last_xritag = NO_XRI;
6933 LIST_HEAD(prep_sgl_list);
6934 LIST_HEAD(blck_sgl_list);
6935 LIST_HEAD(allc_sgl_list);
6936 LIST_HEAD(post_sgl_list);
6937 LIST_HEAD(free_sgl_list);
6938
James Smart38c20672013-03-01 16:37:44 -05006939 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006940 spin_lock(&phba->sli4_hba.sgl_list_lock);
6941 list_splice_init(sgl_list, &allc_sgl_list);
6942 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006943 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006944
James Smart895427b2017-02-12 13:52:30 -08006945 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006946 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6947 &allc_sgl_list, list) {
6948 list_del_init(&sglq_entry->list);
6949 block_cnt++;
6950 if ((last_xritag != NO_XRI) &&
6951 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6952 /* a hole in xri block, form a sgl posting block */
6953 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6954 post_cnt = block_cnt - 1;
6955 /* prepare list for next posting block */
6956 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6957 block_cnt = 1;
6958 } else {
6959 /* prepare list for next posting block */
6960 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6961 /* enough sgls for non-embed sgl mbox command */
6962 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6963 list_splice_init(&prep_sgl_list,
6964 &blck_sgl_list);
6965 post_cnt = block_cnt;
6966 block_cnt = 0;
6967 }
6968 }
6969 num_posted++;
6970
6971 /* keep track of last sgl's xritag */
6972 last_xritag = sglq_entry->sli4_xritag;
6973
James Smart895427b2017-02-12 13:52:30 -08006974 /* end of repost sgl list condition for buffers */
6975 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006976 if (post_cnt == 0) {
6977 list_splice_init(&prep_sgl_list,
6978 &blck_sgl_list);
6979 post_cnt = block_cnt;
6980 } else if (block_cnt == 1) {
6981 status = lpfc_sli4_post_sgl(phba,
6982 sglq_entry->phys, 0,
6983 sglq_entry->sli4_xritag);
6984 if (!status) {
6985 /* successful, put sgl to posted list */
6986 list_add_tail(&sglq_entry->list,
6987 &post_sgl_list);
6988 } else {
6989 /* Failure, put sgl to free list */
6990 lpfc_printf_log(phba, KERN_WARNING,
6991 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006992 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006993 "sgl, xritag:x%x\n",
6994 sglq_entry->sli4_xritag);
6995 list_add_tail(&sglq_entry->list,
6996 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006997 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006998 }
6999 }
7000 }
7001
7002 /* continue until a nembed page worth of sgls */
7003 if (post_cnt == 0)
7004 continue;
7005
James Smart895427b2017-02-12 13:52:30 -08007006 /* post the buffer list sgls as a block */
7007 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7008 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04007009
7010 if (!status) {
7011 /* success, put sgl list to posted sgl list */
7012 list_splice_init(&blck_sgl_list, &post_sgl_list);
7013 } else {
7014 /* Failure, put sgl list to free sgl list */
7015 sglq_entry_first = list_first_entry(&blck_sgl_list,
7016 struct lpfc_sglq,
7017 list);
7018 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007019 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04007020 "xritag:x%x-x%x\n",
7021 sglq_entry_first->sli4_xritag,
7022 (sglq_entry_first->sli4_xritag +
7023 post_cnt - 1));
7024 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007025 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007026 }
7027
7028 /* don't reset xirtag due to hole in xri block */
7029 if (block_cnt == 0)
7030 last_xritag = NO_XRI;
7031
James Smart895427b2017-02-12 13:52:30 -08007032 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04007033 post_cnt = 0;
7034 }
7035
James Smart895427b2017-02-12 13:52:30 -08007036 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04007037 lpfc_free_sgl_list(phba, &free_sgl_list);
7038
James Smart895427b2017-02-12 13:52:30 -08007039 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04007040 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05007041 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007042 spin_lock(&phba->sli4_hba.sgl_list_lock);
7043 list_splice_init(&post_sgl_list, sgl_list);
7044 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007045 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007046 } else {
7047 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007048 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04007049 return -EIO;
7050 }
James Smart895427b2017-02-12 13:52:30 -08007051
7052 /* return the number of XRIs actually posted */
7053 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007054}
7055
James Smart0794d602019-01-28 11:14:19 -08007056/**
James Smart5e5b5112019-01-28 11:14:22 -08007057 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
James Smart0794d602019-01-28 11:14:19 -08007058 * @phba: pointer to lpfc hba data structure.
7059 *
7060 * This routine walks the list of nvme buffers that have been allocated and
7061 * repost them to the port by using SGL block post. This is needed after a
7062 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7063 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
James Smart5e5b5112019-01-28 11:14:22 -08007064 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
James Smart0794d602019-01-28 11:14:19 -08007065 *
7066 * Returns: 0 = success, non-zero failure.
7067 **/
7068int
James Smart5e5b5112019-01-28 11:14:22 -08007069lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
James Smart0794d602019-01-28 11:14:19 -08007070{
7071 LIST_HEAD(post_nblist);
7072 int num_posted, rc = 0;
7073
7074 /* get all NVME buffers need to repost to a local list */
James Smart5e5b5112019-01-28 11:14:22 -08007075 lpfc_io_buf_flush(phba, &post_nblist);
James Smart0794d602019-01-28 11:14:19 -08007076
7077 /* post the list of nvme buffer sgls to port if available */
7078 if (!list_empty(&post_nblist)) {
James Smart5e5b5112019-01-28 11:14:22 -08007079 num_posted = lpfc_sli4_post_io_sgl_list(
7080 phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
James Smart0794d602019-01-28 11:14:19 -08007081 /* failed to post any nvme buffer, return error */
7082 if (num_posted == 0)
7083 rc = -EIO;
7084 }
7085 return rc;
7086}
7087
James Smart61bda8f2016-10-13 15:06:05 -07007088void
7089lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7090{
7091 uint32_t len;
7092
7093 len = sizeof(struct lpfc_mbx_set_host_data) -
7094 sizeof(struct lpfc_sli4_cfg_mhdr);
7095 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7096 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7097 LPFC_SLI4_MBX_EMBED);
7098
7099 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08007100 mbox->u.mqe.un.set_host_data.param_len =
7101 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07007102 snprintf(mbox->u.mqe.un.set_host_data.data,
7103 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7104 "Linux %s v"LPFC_DRIVER_VERSION,
7105 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7106}
7107
James Smarta8cf5df2017-05-15 15:20:46 -07007108int
James Smart6c621a22017-05-15 15:20:45 -07007109lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07007110 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07007111{
7112 int rc, i;
7113 struct lpfc_rqe hrqe;
7114 struct lpfc_rqe drqe;
7115 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08007116 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07007117 struct rqb_dmabuf *rqb_buffer;
7118 LIST_HEAD(rqb_buf_list);
7119
James Smart411de512018-01-30 15:58:52 -08007120 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007121 rqbp = hrq->rqbp;
7122 for (i = 0; i < count; i++) {
7123 /* IF RQ is already full, don't bother */
7124 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
7125 break;
7126 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7127 if (!rqb_buffer)
7128 break;
7129 rqb_buffer->hrq = hrq;
7130 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07007131 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07007132 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7133 }
7134 while (!list_empty(&rqb_buf_list)) {
7135 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7136 hbuf.list);
7137
7138 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7139 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7140 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7141 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7142 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7143 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08007144 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7145 "6421 Cannot post to HRQ %d: %x %x %x "
7146 "DRQ %x %x\n",
7147 hrq->queue_id,
7148 hrq->host_index,
7149 hrq->hba_index,
7150 hrq->entry_count,
7151 drq->host_index,
7152 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07007153 rqbp->rqb_free_buffer(phba, rqb_buffer);
7154 } else {
7155 list_add_tail(&rqb_buffer->hbuf.list,
7156 &rqbp->rqb_buffer_list);
7157 rqbp->buffer_count++;
7158 }
7159 }
James Smart411de512018-01-30 15:58:52 -08007160 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007161 return 1;
7162}
7163
James Smart8a9d2e82012-05-09 21:16:12 -04007164/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08007165 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04007166 * @phba: Pointer to HBA context object.
7167 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08007168 * This function is the main SLI4 device initialization PCI function. This
7169 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04007170 * HBA error attention handler code. Caller is not required to hold any
7171 * locks.
7172 **/
7173int
7174lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7175{
James Smartc4908502019-01-28 11:14:28 -08007176 int rc, i, cnt, len;
James Smartda0436e2009-05-22 14:51:39 -04007177 LPFC_MBOXQ_t *mboxq;
7178 struct lpfc_mqe *mqe;
7179 uint8_t *vpd;
7180 uint32_t vpd_size;
7181 uint32_t ftr_rsp = 0;
7182 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7183 struct lpfc_vport *vport = phba->pport;
7184 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08007185 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04007186
7187 /* Perform a PCI function reset to start from clean */
7188 rc = lpfc_pci_function_reset(phba);
7189 if (unlikely(rc))
7190 return -ENODEV;
7191
7192 /* Check the HBA Host Status Register for readyness */
7193 rc = lpfc_sli4_post_status_check(phba);
7194 if (unlikely(rc))
7195 return -ENODEV;
7196 else {
7197 spin_lock_irq(&phba->hbalock);
7198 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7199 spin_unlock_irq(&phba->hbalock);
7200 }
7201
7202 /*
7203 * Allocate a single mailbox container for initializing the
7204 * port.
7205 */
7206 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7207 if (!mboxq)
7208 return -ENOMEM;
7209
James Smartda0436e2009-05-22 14:51:39 -04007210 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04007211 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04007212 vpd = kzalloc(vpd_size, GFP_KERNEL);
7213 if (!vpd) {
7214 rc = -ENOMEM;
7215 goto out_free_mbox;
7216 }
7217
7218 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05007219 if (unlikely(rc)) {
7220 kfree(vpd);
7221 goto out_free_mbox;
7222 }
James Smart572709e2013-07-15 18:32:43 -04007223
James Smartda0436e2009-05-22 14:51:39 -04007224 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04007225 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07007226 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05007227 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007228 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7229 } else {
James Smart76a95d72010-11-20 23:11:48 -05007230 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007231 }
James Smart45ed1192009-10-02 15:17:02 -04007232
7233 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7234 LPFC_DCBX_CEE_MODE)
7235 phba->hba_flag |= HBA_FIP_SUPPORT;
7236 else
7237 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7238
James Smart4f2e66c2012-05-09 21:17:07 -04007239 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
7240
James Smartc31098c2011-04-16 11:03:33 -04007241 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007242 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7243 "0376 READ_REV Error. SLI Level %d "
7244 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05007245 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04007246 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007247 kfree(vpd);
7248 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007249 }
James Smartcd1c8302011-10-10 21:33:25 -04007250
7251 /*
James Smartff78d8f2011-12-13 13:21:35 -05007252 * Continue initialization with default values even if driver failed
7253 * to read FCoE param config regions, only read parameters if the
7254 * board is FCoE
7255 */
7256 if (phba->hba_flag & HBA_FCOE_MODE &&
7257 lpfc_sli4_read_fcoe_params(phba))
7258 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7259 "2570 Failed to read FCoE parameters\n");
7260
7261 /*
James Smartcd1c8302011-10-10 21:33:25 -04007262 * Retrieve sli4 device physical port name, failure of doing it
7263 * is considered as non-fatal.
7264 */
7265 rc = lpfc_sli4_retrieve_pport_name(phba);
7266 if (!rc)
7267 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7268 "3080 Successful retrieving SLI4 device "
7269 "physical port name: %s.\n", phba->Port);
7270
James Smartda0436e2009-05-22 14:51:39 -04007271 /*
7272 * Evaluate the read rev and vpd data. Populate the driver
7273 * state with the results. If this routine fails, the failure
7274 * is not fatal as the driver will use generic values.
7275 */
7276 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7277 if (unlikely(!rc)) {
7278 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7279 "0377 Error %d parsing vpd. "
7280 "Using defaults.\n", rc);
7281 rc = 0;
7282 }
James Smart76a95d72010-11-20 23:11:48 -05007283 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04007284
James Smartf1126682009-06-10 17:22:44 -04007285 /* Save information as VPD data */
7286 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7287 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08007288
7289 /*
7290 * This is because first G7 ASIC doesn't support the standard
7291 * 0x5a NVME cmd descriptor type/subtype
7292 */
7293 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7294 LPFC_SLI_INTF_IF_TYPE_6) &&
7295 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7296 (phba->vpd.rev.smRev == 0) &&
7297 (phba->cfg_nvme_embed_cmd == 1))
7298 phba->cfg_nvme_embed_cmd = 0;
7299
James Smartf1126682009-06-10 17:22:44 -04007300 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7301 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7302 &mqe->un.read_rev);
7303 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7304 &mqe->un.read_rev);
7305 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7306 &mqe->un.read_rev);
7307 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7308 &mqe->un.read_rev);
7309 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7310 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7311 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7312 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7313 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7314 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7315 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7316 "(%d):0380 READ_REV Status x%x "
7317 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7318 mboxq->vport ? mboxq->vport->vpi : 0,
7319 bf_get(lpfc_mqe_status, mqe),
7320 phba->vpd.rev.opFwName,
7321 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7322 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04007323
James Smart572709e2013-07-15 18:32:43 -04007324 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
7325 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
7326 if (phba->pport->cfg_lun_queue_depth > rc) {
7327 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7328 "3362 LUN queue depth changed from %d to %d\n",
7329 phba->pport->cfg_lun_queue_depth, rc);
7330 phba->pport->cfg_lun_queue_depth = rc;
7331 }
7332
James Smart65791f12016-07-06 12:35:56 -07007333 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07007334 LPFC_SLI_INTF_IF_TYPE_0) {
7335 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7336 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7337 if (rc == MBX_SUCCESS) {
7338 phba->hba_flag |= HBA_RECOVERABLE_UE;
7339 /* Set 1Sec interval to detect UE */
7340 phba->eratt_poll_interval = 1;
7341 phba->sli4_hba.ue_to_sr = bf_get(
7342 lpfc_mbx_set_feature_UESR,
7343 &mboxq->u.mqe.un.set_feature);
7344 phba->sli4_hba.ue_to_rp = bf_get(
7345 lpfc_mbx_set_feature_UERP,
7346 &mboxq->u.mqe.un.set_feature);
7347 }
7348 }
7349
7350 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7351 /* Enable MDS Diagnostics only if the SLI Port supports it */
7352 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7353 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7354 if (rc != MBX_SUCCESS)
7355 phba->mds_diags_support = 0;
7356 }
James Smart572709e2013-07-15 18:32:43 -04007357
James Smartda0436e2009-05-22 14:51:39 -04007358 /*
7359 * Discover the port's supported feature set and match it against the
7360 * hosts requests.
7361 */
7362 lpfc_request_features(phba, mboxq);
7363 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7364 if (unlikely(rc)) {
7365 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007366 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007367 }
7368
7369 /*
7370 * The port must support FCP initiator mode as this is the
7371 * only mode running in the host.
7372 */
7373 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7374 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7375 "0378 No support for fcpi mode.\n");
7376 ftr_rsp++;
7377 }
James Smart0bc2b7c2018-02-22 08:18:48 -08007378
7379 /* Performance Hints are ONLY for FCoE */
7380 if (phba->hba_flag & HBA_FCOE_MODE) {
7381 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7382 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7383 else
7384 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7385 }
7386
James Smartda0436e2009-05-22 14:51:39 -04007387 /*
7388 * If the port cannot support the host's requested features
7389 * then turn off the global config parameters to disable the
7390 * feature in the driver. This is not a fatal error.
7391 */
James Smartf44ac122018-03-05 12:04:08 -08007392 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7393 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7394 phba->cfg_enable_bg = 0;
7395 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04007396 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08007397 }
James Smartbf086112011-08-21 21:48:13 -04007398 }
James Smartda0436e2009-05-22 14:51:39 -04007399
7400 if (phba->max_vpi && phba->cfg_enable_npiv &&
7401 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7402 ftr_rsp++;
7403
7404 if (ftr_rsp) {
7405 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7406 "0379 Feature Mismatch Data: x%08x %08x "
7407 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7408 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7409 phba->cfg_enable_npiv, phba->max_vpi);
7410 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7411 phba->cfg_enable_bg = 0;
7412 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7413 phba->cfg_enable_npiv = 0;
7414 }
7415
7416 /* These SLI3 features are assumed in SLI4 */
7417 spin_lock_irq(&phba->hbalock);
7418 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7419 spin_unlock_irq(&phba->hbalock);
7420
James Smart6d368e52011-05-24 11:44:12 -04007421 /*
7422 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7423 * calls depends on these resources to complete port setup.
7424 */
7425 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7426 if (rc) {
7427 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7428 "2920 Failed to alloc Resource IDs "
7429 "rc = x%x\n", rc);
7430 goto out_free_mbox;
7431 }
7432
James Smart61bda8f2016-10-13 15:06:05 -07007433 lpfc_set_host_data(phba, mboxq);
7434
7435 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7436 if (rc) {
7437 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7438 "2134 Failed to set host os driver version %x",
7439 rc);
7440 }
7441
James Smartda0436e2009-05-22 14:51:39 -04007442 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007443 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7444 if (rc) {
7445 phba->link_state = LPFC_HBA_ERROR;
7446 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007447 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007448 }
7449
James Smartda0436e2009-05-22 14:51:39 -04007450 mboxq->vport = vport;
7451 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart3e1f0712018-11-29 16:09:29 -08007452 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04007453 if (rc == MBX_SUCCESS) {
7454 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7455 rc = 0;
7456 }
7457
7458 /*
7459 * This memory was allocated by the lpfc_read_sparam routine. Release
7460 * it to the mbuf pool.
7461 */
7462 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7463 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -08007464 mboxq->ctx_buf = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007465 if (unlikely(rc)) {
7466 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7467 "0382 READ_SPARAM command failed "
7468 "status %d, mbxStatus x%x\n",
7469 rc, bf_get(lpfc_mqe_status, mqe));
7470 phba->link_state = LPFC_HBA_ERROR;
7471 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007472 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007473 }
7474
James Smart05580562011-05-24 11:40:48 -04007475 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007476
7477 /* Update the fc_host data structures with new wwn. */
7478 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7479 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7480
James Smart895427b2017-02-12 13:52:30 -08007481 /* Create all the SLI4 queues */
7482 rc = lpfc_sli4_queue_create(phba);
7483 if (rc) {
7484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7485 "3089 Failed to allocate queues\n");
7486 rc = -ENODEV;
7487 goto out_free_mbox;
7488 }
7489 /* Set up all the queues to the device */
7490 rc = lpfc_sli4_queue_setup(phba);
7491 if (unlikely(rc)) {
7492 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7493 "0381 Error %d during queue setup.\n ", rc);
7494 goto out_stop_timers;
7495 }
7496 /* Initialize the driver internal SLI layer lists. */
7497 lpfc_sli4_setup(phba);
7498 lpfc_sli4_queue_init(phba);
7499
7500 /* update host els xri-sgl sizes and mappings */
7501 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007502 if (unlikely(rc)) {
7503 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7504 "1400 Failed to update xri-sgl size and "
7505 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007506 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007507 }
7508
James Smart8a9d2e82012-05-09 21:16:12 -04007509 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007510 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7511 phba->sli4_hba.els_xri_cnt);
7512 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007513 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7514 "0582 Error %d during els sgl post "
7515 "operation\n", rc);
7516 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007517 goto out_destroy_queue;
7518 }
7519 phba->sli4_hba.els_xri_cnt = rc;
7520
James Smartf358dd02017-02-12 13:52:34 -08007521 if (phba->nvmet_support) {
7522 /* update host nvmet xri-sgl sizes and mappings */
7523 rc = lpfc_sli4_nvmet_sgl_update(phba);
7524 if (unlikely(rc)) {
7525 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7526 "6308 Failed to update nvmet-sgl size "
7527 "and mapping: %d\n", rc);
7528 goto out_destroy_queue;
7529 }
7530
7531 /* register the nvmet sgl pool to the port */
7532 rc = lpfc_sli4_repost_sgl_list(
7533 phba,
7534 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7535 phba->sli4_hba.nvmet_xri_cnt);
7536 if (unlikely(rc < 0)) {
7537 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7538 "3117 Error %d during nvmet "
7539 "sgl post\n", rc);
7540 rc = -ENODEV;
7541 goto out_destroy_queue;
7542 }
7543 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007544
7545 cnt = phba->cfg_iocb_cnt * 1024;
7546 /* We need 1 iocbq for every SGL, for IO processing */
7547 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08007548 } else {
James Smart0794d602019-01-28 11:14:19 -08007549 /* update host common xri-sgl sizes and mappings */
James Smart5e5b5112019-01-28 11:14:22 -08007550 rc = lpfc_sli4_io_sgl_update(phba);
James Smart895427b2017-02-12 13:52:30 -08007551 if (unlikely(rc)) {
7552 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7553 "6082 Failed to update nvme-sgl size "
7554 "and mapping: %d\n", rc);
7555 goto out_destroy_queue;
7556 }
James Smart6c621a22017-05-15 15:20:45 -07007557
James Smart0794d602019-01-28 11:14:19 -08007558 /* register the allocated common sgl pool to the port */
James Smart5e5b5112019-01-28 11:14:22 -08007559 rc = lpfc_sli4_repost_io_sgl_list(phba);
James Smart0794d602019-01-28 11:14:19 -08007560 if (unlikely(rc)) {
7561 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7562 "6116 Error %d during nvme sgl post "
7563 "operation\n", rc);
7564 /* Some NVME buffers were moved to abort nvme list */
7565 /* A pci function reset will repost them */
7566 rc = -ENODEV;
7567 goto out_destroy_queue;
7568 }
James Smart6c621a22017-05-15 15:20:45 -07007569 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07007570 }
7571
7572 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007573 /* Initialize and populate the iocb list per host */
7574 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007575 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07007576 phba->cfg_iocb_cnt, cnt);
7577 rc = lpfc_init_iocb_list(phba, cnt);
7578 if (rc) {
7579 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007580 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007581 goto out_destroy_queue;
7582 }
James Smart8a9d2e82012-05-09 21:16:12 -04007583 }
7584
James Smart11e644e2017-06-15 22:56:48 -07007585 if (phba->nvmet_support)
7586 lpfc_nvmet_create_targetport(phba);
7587
James Smart2d7dbc42017-02-12 13:52:35 -08007588 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007589 /* Post initial buffers to all RQs created */
7590 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7591 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7592 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7593 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7594 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007595 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007596 rqbp->buffer_count = 0;
7597
James Smart2d7dbc42017-02-12 13:52:35 -08007598 lpfc_post_rq_buffer(
7599 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7600 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007601 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007602 }
7603 }
7604
James Smartda0436e2009-05-22 14:51:39 -04007605 /* Post the rpi header region to the device. */
7606 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7607 if (unlikely(rc)) {
7608 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7609 "0393 Error %d during rpi post operation\n",
7610 rc);
7611 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007612 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007613 }
James Smart97f2ecf2012-03-01 22:35:23 -05007614 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007615
James Smart895427b2017-02-12 13:52:30 -08007616 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007617 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007618 /*
7619 * The FC Port needs to register FCFI (index 0)
7620 */
7621 lpfc_reg_fcfi(phba, mboxq);
7622 mboxq->vport = phba->pport;
7623 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7624 if (rc != MBX_SUCCESS)
7625 goto out_unset_queue;
7626 rc = 0;
7627 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7628 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007629 } else {
7630 /* We are a NVME Target mode with MRQ > 1 */
7631
7632 /* First register the FCFI */
7633 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7634 mboxq->vport = phba->pport;
7635 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7636 if (rc != MBX_SUCCESS)
7637 goto out_unset_queue;
7638 rc = 0;
7639 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7640 &mboxq->u.mqe.un.reg_fcfi_mrq);
7641
7642 /* Next register the MRQs */
7643 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7644 mboxq->vport = phba->pport;
7645 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7646 if (rc != MBX_SUCCESS)
7647 goto out_unset_queue;
7648 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007649 }
7650 /* Check if the port is configured to be disabled */
7651 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007652 }
7653
James Smartc4908502019-01-28 11:14:28 -08007654 /* Don't post more new bufs if repost already recovered
7655 * the nvme sgls.
7656 */
7657 if (phba->nvmet_support == 0) {
7658 if (phba->sli4_hba.io_xri_cnt == 0) {
7659 len = lpfc_new_io_buf(
7660 phba, phba->sli4_hba.io_xri_max);
7661 if (len == 0) {
7662 rc = -ENOMEM;
7663 goto out_unset_queue;
7664 }
7665
7666 if (phba->cfg_xri_rebalancing)
7667 lpfc_create_multixri_pools(phba);
7668 }
7669 } else {
7670 phba->cfg_xri_rebalancing = 0;
7671 }
7672
James Smartda0436e2009-05-22 14:51:39 -04007673 /* Arm the CQs and then EQs on device */
7674 lpfc_sli4_arm_cqeq_intr(phba);
7675
7676 /* Indicate device interrupt mode */
7677 phba->sli4_hba.intr_enable = 1;
7678
7679 /* Allow asynchronous mailbox command to go through */
7680 spin_lock_irq(&phba->hbalock);
7681 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7682 spin_unlock_irq(&phba->hbalock);
7683
7684 /* Post receive buffers to the device */
7685 lpfc_sli4_rb_setup(phba);
7686
James Smartfc2b9892010-02-26 14:15:29 -05007687 /* Reset HBA FCF states after HBA reset */
7688 phba->fcf.fcf_flag = 0;
7689 phba->fcf.current_rec.flag = 0;
7690
James Smartda0436e2009-05-22 14:51:39 -04007691 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007692 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007693 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007694
7695 /* Start heart beat timer */
7696 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007697 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007698 phba->hb_outstanding = 0;
7699 phba->last_completion_time = jiffies;
7700
7701 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007702 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007703 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007704
James Smart75baf692010-06-08 18:31:21 -04007705 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7706 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7707 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7708 if (!rc) {
7709 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7710 "2829 This device supports "
7711 "Advanced Error Reporting (AER)\n");
7712 spin_lock_irq(&phba->hbalock);
7713 phba->hba_flag |= HBA_AER_ENABLED;
7714 spin_unlock_irq(&phba->hbalock);
7715 } else {
7716 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7717 "2830 This device does not support "
7718 "Advanced Error Reporting (AER)\n");
7719 phba->cfg_aer_support = 0;
7720 }
James Smart0a96e972011-07-22 18:37:28 -04007721 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007722 }
7723
James Smartda0436e2009-05-22 14:51:39 -04007724 /*
7725 * The port is ready, set the host's link state to LINK_DOWN
7726 * in preparation for link interrupts.
7727 */
James Smartda0436e2009-05-22 14:51:39 -04007728 spin_lock_irq(&phba->hbalock);
7729 phba->link_state = LPFC_LINK_DOWN;
James Smart1dc5ec22018-10-23 13:41:11 -07007730
7731 /* Check if physical ports are trunked */
7732 if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
7733 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
7734 if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
7735 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
7736 if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
7737 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
7738 if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
7739 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04007740 spin_unlock_irq(&phba->hbalock);
James Smart1dc5ec22018-10-23 13:41:11 -07007741
James Smart026abb82011-12-13 13:20:45 -05007742 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7743 (phba->hba_flag & LINK_DISABLED)) {
7744 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7745 "3103 Adapter Link is disabled.\n");
7746 lpfc_down_link(phba, mboxq);
7747 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7748 if (rc != MBX_SUCCESS) {
7749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7750 "3104 Adapter failed to issue "
7751 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
James Smartc4908502019-01-28 11:14:28 -08007752 goto out_io_buff_free;
James Smart026abb82011-12-13 13:20:45 -05007753 }
7754 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007755 /* don't perform init_link on SLI4 FC port loopback test */
7756 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7757 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7758 if (rc)
James Smartc4908502019-01-28 11:14:28 -08007759 goto out_io_buff_free;
James Smart1b511972011-12-13 13:23:09 -05007760 }
James Smart5350d872011-10-10 21:33:49 -04007761 }
7762 mempool_free(mboxq, phba->mbox_mem_pool);
7763 return rc;
James Smartc4908502019-01-28 11:14:28 -08007764out_io_buff_free:
7765 /* Free allocated IO Buffers */
7766 lpfc_io_free(phba);
James Smart76a95d72010-11-20 23:11:48 -05007767out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007768 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007769 lpfc_sli4_queue_unset(phba);
7770out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007771 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007772 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007773out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007774 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007775out_free_mbox:
7776 mempool_free(mboxq, phba->mbox_mem_pool);
7777 return rc;
7778}
James Smarte59058c2008-08-24 21:49:00 -04007779
7780/**
James Smart3621a712009-04-06 18:47:14 -04007781 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007782 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007783 *
James Smarte59058c2008-08-24 21:49:00 -04007784 * This is the callback function for mailbox timer. The mailbox
7785 * timer is armed when a new mailbox command is issued and the timer
7786 * is deleted when the mailbox complete. The function is called by
7787 * the kernel timer code when a mailbox does not complete within
7788 * expected time. This function wakes up the worker thread to
7789 * process the mailbox timeout and returns. All the processing is
7790 * done by the worker thread function lpfc_mbox_timeout_handler.
7791 **/
dea31012005-04-17 16:05:31 -05007792void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007793lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007794{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007795 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007796 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007797 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007798
James Smart2e0fef82007-06-17 19:56:36 -05007799 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007800 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007801 if (!tmo_posted)
7802 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7803 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7804
James Smart5e9d9b82008-06-14 22:52:53 -04007805 if (!tmo_posted)
7806 lpfc_worker_wake_up(phba);
7807 return;
dea31012005-04-17 16:05:31 -05007808}
7809
James Smarte8d3c3b2013-10-10 12:21:30 -04007810/**
7811 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7812 * are pending
7813 * @phba: Pointer to HBA context object.
7814 *
7815 * This function checks if any mailbox completions are present on the mailbox
7816 * completion queue.
7817 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007818static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007819lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7820{
7821
7822 uint32_t idx;
7823 struct lpfc_queue *mcq;
7824 struct lpfc_mcqe *mcqe;
7825 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08007826 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04007827
7828 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7829 return false;
7830
7831 /* Check for completions on mailbox completion queue */
7832
7833 mcq = phba->sli4_hba.mbx_cq;
7834 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08007835 qe_valid = mcq->qe_valid;
7836 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
James Smarte8d3c3b2013-10-10 12:21:30 -04007837 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7838 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7839 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7840 pending_completions = true;
7841 break;
7842 }
7843 idx = (idx + 1) % mcq->entry_count;
7844 if (mcq->hba_index == idx)
7845 break;
James Smart7365f6f2018-02-22 08:18:46 -08007846
7847 /* if the index wrapped around, toggle the valid bit */
7848 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7849 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04007850 }
7851 return pending_completions;
7852
7853}
7854
7855/**
7856 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7857 * that were missed.
7858 * @phba: Pointer to HBA context object.
7859 *
7860 * For sli4, it is possible to miss an interrupt. As such mbox completions
7861 * maybe missed causing erroneous mailbox timeouts to occur. This function
7862 * checks to see if mbox completions are on the mailbox completion queue
7863 * and will process all the completions associated with the eq for the
7864 * mailbox completion queue.
7865 **/
7866bool
7867lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7868{
James Smartb71413d2018-02-22 08:18:40 -08007869 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04007870 uint32_t eqidx;
7871 struct lpfc_queue *fpeq = NULL;
7872 struct lpfc_eqe *eqe;
7873 bool mbox_pending;
7874
7875 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7876 return false;
7877
7878 /* Find the eq associated with the mcq */
7879
James Smartcdb42be2019-01-28 11:14:21 -08007880 if (sli4_hba->hdwq)
James Smart6a828b02019-01-28 11:14:31 -08007881 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++)
James Smartcdb42be2019-01-28 11:14:21 -08007882 if (sli4_hba->hdwq[eqidx].hba_eq->queue_id ==
James Smartb71413d2018-02-22 08:18:40 -08007883 sli4_hba->mbx_cq->assoc_qid) {
James Smartcdb42be2019-01-28 11:14:21 -08007884 fpeq = sli4_hba->hdwq[eqidx].hba_eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04007885 break;
7886 }
7887 if (!fpeq)
7888 return false;
7889
7890 /* Turn off interrupts from this EQ */
7891
James Smartb71413d2018-02-22 08:18:40 -08007892 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04007893
7894 /* Check to see if a mbox completion is pending */
7895
7896 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7897
7898 /*
7899 * If a mbox completion is pending, process all the events on EQ
7900 * associated with the mbox completion queue (this could include
7901 * mailbox commands, async events, els commands, receive queue data
7902 * and fcp commands)
7903 */
7904
7905 if (mbox_pending)
7906 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7907 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7908 fpeq->EQ_processed++;
7909 }
7910
7911 /* Always clear and re-arm the EQ */
7912
James Smartb71413d2018-02-22 08:18:40 -08007913 sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04007914
7915 return mbox_pending;
7916
7917}
James Smarte59058c2008-08-24 21:49:00 -04007918
7919/**
James Smart3621a712009-04-06 18:47:14 -04007920 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007921 * @phba: Pointer to HBA context object.
7922 *
7923 * This function is called from worker thread when a mailbox command times out.
7924 * The caller is not required to hold any locks. This function will reset the
7925 * HBA and recover all the pending commands.
7926 **/
dea31012005-04-17 16:05:31 -05007927void
7928lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7929{
James Smart2e0fef82007-06-17 19:56:36 -05007930 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007931 MAILBOX_t *mb = NULL;
7932
James Smart1dcb58e2007-04-25 09:51:30 -04007933 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007934
James Smarte8d3c3b2013-10-10 12:21:30 -04007935 /* If the mailbox completed, process the completion and return */
7936 if (lpfc_sli4_process_missed_mbox_completions(phba))
7937 return;
7938
James Smarteb016562014-09-03 12:58:06 -04007939 if (pmbox != NULL)
7940 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007941 /* Check the pmbox pointer first. There is a race condition
7942 * between the mbox timeout handler getting executed in the
7943 * worklist and the mailbox actually completing. When this
7944 * race condition occurs, the mbox_active will be NULL.
7945 */
7946 spin_lock_irq(&phba->hbalock);
7947 if (pmbox == NULL) {
7948 lpfc_printf_log(phba, KERN_WARNING,
7949 LOG_MBOX | LOG_SLI,
7950 "0353 Active Mailbox cleared - mailbox timeout "
7951 "exiting\n");
7952 spin_unlock_irq(&phba->hbalock);
7953 return;
7954 }
7955
dea31012005-04-17 16:05:31 -05007956 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007957 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007958 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007959 mb->mbxCommand,
7960 phba->pport->port_state,
7961 phba->sli.sli_flag,
7962 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007963 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007964
James Smart1dcb58e2007-04-25 09:51:30 -04007965 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7966 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007967 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007968 */
James Smart2e0fef82007-06-17 19:56:36 -05007969 spin_lock_irq(&phba->pport->work_port_lock);
7970 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7971 spin_unlock_irq(&phba->pport->work_port_lock);
7972 spin_lock_irq(&phba->hbalock);
7973 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007974 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007975 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007976
James Smartdb55fba2014-04-04 13:52:02 -04007977 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007978
7979 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007980 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007981
7982 /* Reset the HBA device */
7983 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007984}
7985
James Smarte59058c2008-08-24 21:49:00 -04007986/**
James Smart3772a992009-05-22 14:50:54 -04007987 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007988 * @phba: Pointer to HBA context object.
7989 * @pmbox: Pointer to mailbox object.
7990 * @flag: Flag indicating how the mailbox need to be processed.
7991 *
7992 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007993 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7994 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007995 * The mailbox command can be submitted in polling mode, in which case
7996 * this function will wait in a polling loop for the completion of the
7997 * mailbox.
7998 * If the mailbox is submitted in no_wait mode (not polling) the
7999 * function will submit the command and returns immediately without waiting
8000 * for the mailbox completion. The no_wait is supported only when HBA
8001 * is in SLI2/SLI3 mode - interrupts are enabled.
8002 * The SLI interface allows only one mailbox pending at a time. If the
8003 * mailbox is issued in polling mode and there is already a mailbox
8004 * pending, then the function will return an error. If the mailbox is issued
8005 * in NO_WAIT mode and there is a mailbox pending already, the function
8006 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
8007 * The sli layer owns the mailbox object until the completion of mailbox
8008 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
8009 * return codes the caller owns the mailbox command after the return of
8010 * the function.
8011 **/
James Smart3772a992009-05-22 14:50:54 -04008012static int
8013lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
8014 uint32_t flag)
dea31012005-04-17 16:05:31 -05008015{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008016 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05008017 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008018 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05008019 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008020 int i;
James Smart09372822008-01-11 01:52:54 -05008021 unsigned long timeout;
dea31012005-04-17 16:05:31 -05008022 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04008023 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05008024 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04008025 int processing_queue = 0;
8026
8027 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8028 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04008029 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04008030 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04008031 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8032 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8033 return MBX_SUCCESS;
8034 }
James Smart58da1ff2008-04-07 10:15:56 -04008035 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04008036 pmbox = lpfc_mbox_get(phba);
8037 if (!pmbox) {
8038 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8039 return MBX_SUCCESS;
8040 }
8041 }
dea31012005-04-17 16:05:31 -05008042
James Smarted957682007-06-17 19:56:37 -05008043 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05008044 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05008045 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04008046 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05008047 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05008048 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008049 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04008050 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05008051 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04008052 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05008053 }
8054 }
8055
Linas Vepstas8d63f372007-02-14 14:28:36 -06008056 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04008057 if (unlikely(pci_channel_offline(phba->pcidev))) {
8058 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8059 goto out_not_finished;
8060 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008061
James Smarta257bf92009-04-06 18:48:10 -04008062 /* If HBA has a deferred error attention, fail the iocb. */
8063 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8064 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8065 goto out_not_finished;
8066 }
8067
dea31012005-04-17 16:05:31 -05008068 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05008069
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008070 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05008071 status = MBX_SUCCESS;
8072
James Smart2e0fef82007-06-17 19:56:36 -05008073 if (phba->link_state == LPFC_HBA_ERROR) {
8074 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05008075
8076 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008077 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8078 "(%d):0311 Mailbox command x%x cannot "
8079 "issue Data: x%x x%x\n",
8080 pmbox->vport ? pmbox->vport->vpi : 0,
8081 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008082 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05008083 }
8084
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008085 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05008086 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8087 !(hc_copy & HC_MBINT_ENA)) {
8088 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8089 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04008090 "(%d):2528 Mailbox command x%x cannot "
8091 "issue Data: x%x x%x\n",
8092 pmbox->vport ? pmbox->vport->vpi : 0,
8093 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05008094 goto out_not_finished;
8095 }
James Smart92908312006-03-07 15:04:13 -05008096 }
8097
dea31012005-04-17 16:05:31 -05008098 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8099 /* Polling for a mbox command when another one is already active
8100 * is not allowed in SLI. Also, the driver must have established
8101 * SLI2 mode to queue and process multiple mbox commands.
8102 */
8103
8104 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05008105 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008106
8107 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008108 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8109 "(%d):2529 Mailbox command x%x "
8110 "cannot issue Data: x%x x%x\n",
8111 pmbox->vport ? pmbox->vport->vpi : 0,
8112 pmbox->u.mb.mbxCommand,
8113 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008114 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008115 }
8116
James Smart3772a992009-05-22 14:50:54 -04008117 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05008118 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008119 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008120 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8121 "(%d):2530 Mailbox command x%x "
8122 "cannot issue Data: x%x x%x\n",
8123 pmbox->vport ? pmbox->vport->vpi : 0,
8124 pmbox->u.mb.mbxCommand,
8125 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008126 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008127 }
8128
dea31012005-04-17 16:05:31 -05008129 /* Another mailbox command is still being processed, queue this
8130 * command to be processed later.
8131 */
8132 lpfc_mbox_put(phba, pmbox);
8133
8134 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05008135 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008136 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008137 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008138 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07008139 mbx->mbxCommand,
8140 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008141 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008142
8143 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05008144 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008145
James Smart858c9f62007-06-17 19:56:39 -05008146 if (pmbox->vport) {
8147 lpfc_debugfs_disc_trc(pmbox->vport,
8148 LPFC_DISC_TRC_MBOX_VPORT,
8149 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008150 (uint32_t)mbx->mbxCommand,
8151 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008152 }
8153 else {
8154 lpfc_debugfs_disc_trc(phba->pport,
8155 LPFC_DISC_TRC_MBOX,
8156 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008157 (uint32_t)mbx->mbxCommand,
8158 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008159 }
8160
James Smart2e0fef82007-06-17 19:56:36 -05008161 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05008162 }
8163
dea31012005-04-17 16:05:31 -05008164 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8165
8166 /* If we are not polling, we MUST be in SLI2 mode */
8167 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04008168 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008169 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05008170 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008171 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008172 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008173 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8174 "(%d):2531 Mailbox command x%x "
8175 "cannot issue Data: x%x x%x\n",
8176 pmbox->vport ? pmbox->vport->vpi : 0,
8177 pmbox->u.mb.mbxCommand,
8178 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008179 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008180 }
8181 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04008182 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8183 1000);
8184 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05008185 }
8186
8187 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05008188 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008189 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05008190 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008191 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07008192 mbx->mbxCommand,
8193 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008194 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008195
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008196 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05008197 if (pmbox->vport) {
8198 lpfc_debugfs_disc_trc(pmbox->vport,
8199 LPFC_DISC_TRC_MBOX_VPORT,
8200 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008201 (uint32_t)mbx->mbxCommand,
8202 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008203 }
8204 else {
8205 lpfc_debugfs_disc_trc(phba->pport,
8206 LPFC_DISC_TRC_MBOX,
8207 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008208 (uint32_t)mbx->mbxCommand,
8209 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008210 }
8211 }
8212
dea31012005-04-17 16:05:31 -05008213 psli->slistat.mbox_cmd++;
8214 evtctr = psli->slistat.mbox_event;
8215
8216 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008217 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05008218
James Smart3772a992009-05-22 14:50:54 -04008219 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04008220 /* Populate mbox extension offset word. */
8221 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008222 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008223 = (uint8_t *)phba->mbox_ext
8224 - (uint8_t *)phba->mbox;
8225 }
8226
8227 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008228 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8229 lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8230 (uint8_t *)phba->mbox_ext,
8231 pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008232 }
8233 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008234 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008235 } else {
James Smart7a470272010-03-15 11:25:20 -04008236 /* Populate mbox extension offset word. */
8237 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008238 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008239 = MAILBOX_HBA_EXT_OFFSET;
8240
8241 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008242 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -04008243 lpfc_memcpy_to_slim(phba->MBslimaddr +
8244 MAILBOX_HBA_EXT_OFFSET,
James Smart3e1f0712018-11-29 16:09:29 -08008245 pmbox->ctx_buf, pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008246
James Smart895427b2017-02-12 13:52:30 -08008247 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008248 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08008249 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8250 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008251
8252 /* First copy mbox command data to HBA SLIM, skip past first
8253 word */
8254 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008255 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05008256 MAILBOX_CMD_SIZE - sizeof (uint32_t));
8257
8258 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008259 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05008260 to_slim = phba->MBslimaddr;
8261 writel(ldata, to_slim);
8262 readl(to_slim); /* flush */
8263
James Smart895427b2017-02-12 13:52:30 -08008264 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008265 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04008266 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008267 }
8268
8269 wmb();
dea31012005-04-17 16:05:31 -05008270
8271 switch (flag) {
8272 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05008273 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05008274 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05008275 /* Interrupt board to do it */
8276 writel(CA_MBATT, phba->CAregaddr);
8277 readl(phba->CAregaddr); /* flush */
8278 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05008279 break;
8280
8281 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05008282 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05008283 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05008284 /* Interrupt board to do it */
8285 writel(CA_MBATT, phba->CAregaddr);
8286 readl(phba->CAregaddr); /* flush */
8287
James Smart3772a992009-05-22 14:50:54 -04008288 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008289 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04008290 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008291 word0 = le32_to_cpu(word0);
8292 } else {
8293 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05008294 if (lpfc_readl(phba->MBslimaddr, &word0)) {
8295 spin_unlock_irqrestore(&phba->hbalock,
8296 drvr_flag);
8297 goto out_not_finished;
8298 }
dea31012005-04-17 16:05:31 -05008299 }
8300
8301 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008302 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8303 spin_unlock_irqrestore(&phba->hbalock,
8304 drvr_flag);
8305 goto out_not_finished;
8306 }
James Smarta183a152011-10-10 21:32:43 -04008307 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8308 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05008309 i = 0;
dea31012005-04-17 16:05:31 -05008310 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05008311 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8312 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05008313 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05008314 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05008315 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008316 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05008317 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04008318 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008319 }
8320
8321 /* Check if we took a mbox interrupt while we were
8322 polling */
8323 if (((word0 & OWN_CHIP) != OWN_CHIP)
8324 && (evtctr != psli->slistat.mbox_event))
8325 break;
8326
James Smart09372822008-01-11 01:52:54 -05008327 if (i++ > 10) {
8328 spin_unlock_irqrestore(&phba->hbalock,
8329 drvr_flag);
8330 msleep(1);
8331 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8332 }
dea31012005-04-17 16:05:31 -05008333
James Smart3772a992009-05-22 14:50:54 -04008334 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008335 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04008336 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008337 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008338 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05008339 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04008340 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05008341 /* Check real SLIM for any errors */
8342 slimword0 = readl(phba->MBslimaddr);
8343 slimmb = (MAILBOX_t *) & slimword0;
8344 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8345 && slimmb->mbxStatus) {
8346 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04008347 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008348 word0 = slimword0;
8349 }
8350 }
8351 } else {
8352 /* First copy command data */
8353 word0 = readl(phba->MBslimaddr);
8354 }
8355 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008356 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8357 spin_unlock_irqrestore(&phba->hbalock,
8358 drvr_flag);
8359 goto out_not_finished;
8360 }
dea31012005-04-17 16:05:31 -05008361 }
8362
James Smart3772a992009-05-22 14:50:54 -04008363 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008364 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08008365 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8366 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008367 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008368 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
James Smart7a470272010-03-15 11:25:20 -04008369 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -08008370 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008371 pmbox->out_ext_byte_len);
8372 }
dea31012005-04-17 16:05:31 -05008373 } else {
8374 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008375 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08008376 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008377 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008378 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8379 lpfc_memcpy_from_slim(
8380 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008381 phba->MBslimaddr +
8382 MAILBOX_HBA_EXT_OFFSET,
8383 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05008384 }
8385 }
8386
8387 writel(HA_MBATT, phba->HAregaddr);
8388 readl(phba->HAregaddr); /* flush */
8389
8390 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008391 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05008392 }
8393
James Smart2e0fef82007-06-17 19:56:36 -05008394 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8395 return status;
James Smart58da1ff2008-04-07 10:15:56 -04008396
8397out_not_finished:
8398 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04008399 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04008400 lpfc_mbox_cmpl_put(phba, pmbox);
8401 }
8402 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008403}
8404
James Smarte59058c2008-08-24 21:49:00 -04008405/**
James Smartf1126682009-06-10 17:22:44 -04008406 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8407 * @phba: Pointer to HBA context object.
8408 *
8409 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8410 * the driver internal pending mailbox queue. It will then try to wait out the
8411 * possible outstanding mailbox command before return.
8412 *
8413 * Returns:
8414 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8415 * the outstanding mailbox command timed out.
8416 **/
8417static int
8418lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8419{
8420 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008421 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008422 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008423
8424 /* Mark the asynchronous mailbox command posting as blocked */
8425 spin_lock_irq(&phba->hbalock);
8426 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008427 /* Determine how long we might wait for the active mailbox
8428 * command to be gracefully completed by firmware.
8429 */
James Smarta183a152011-10-10 21:32:43 -04008430 if (phba->sli.mbox_active)
8431 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8432 phba->sli.mbox_active) *
8433 1000) + jiffies;
8434 spin_unlock_irq(&phba->hbalock);
8435
James Smarte8d3c3b2013-10-10 12:21:30 -04008436 /* Make sure the mailbox is really active */
8437 if (timeout)
8438 lpfc_sli4_process_missed_mbox_completions(phba);
8439
James Smartf1126682009-06-10 17:22:44 -04008440 /* Wait for the outstnading mailbox command to complete */
8441 while (phba->sli.mbox_active) {
8442 /* Check active mailbox complete status every 2ms */
8443 msleep(2);
8444 if (time_after(jiffies, timeout)) {
8445 /* Timeout, marked the outstanding cmd not complete */
8446 rc = 1;
8447 break;
8448 }
8449 }
8450
8451 /* Can not cleanly block async mailbox command, fails it */
8452 if (rc) {
8453 spin_lock_irq(&phba->hbalock);
8454 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8455 spin_unlock_irq(&phba->hbalock);
8456 }
8457 return rc;
8458}
8459
8460/**
8461 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8462 * @phba: Pointer to HBA context object.
8463 *
8464 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8465 * commands from the driver internal pending mailbox queue. It makes sure
8466 * that there is no outstanding mailbox command before resuming posting
8467 * asynchronous mailbox commands. If, for any reason, there is outstanding
8468 * mailbox command, it will try to wait it out before resuming asynchronous
8469 * mailbox command posting.
8470 **/
8471static void
8472lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8473{
8474 struct lpfc_sli *psli = &phba->sli;
8475
8476 spin_lock_irq(&phba->hbalock);
8477 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8478 /* Asynchronous mailbox posting is not blocked, do nothing */
8479 spin_unlock_irq(&phba->hbalock);
8480 return;
8481 }
8482
8483 /* Outstanding synchronous mailbox command is guaranteed to be done,
8484 * successful or timeout, after timing-out the outstanding mailbox
8485 * command shall always be removed, so just unblock posting async
8486 * mailbox command and resume
8487 */
8488 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8489 spin_unlock_irq(&phba->hbalock);
8490
8491 /* wake up worker thread to post asynchronlous mailbox command */
8492 lpfc_worker_wake_up(phba);
8493}
8494
8495/**
James Smart2d843ed2012-09-29 11:29:06 -04008496 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8497 * @phba: Pointer to HBA context object.
8498 * @mboxq: Pointer to mailbox object.
8499 *
8500 * The function waits for the bootstrap mailbox register ready bit from
8501 * port for twice the regular mailbox command timeout value.
8502 *
8503 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8504 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8505 **/
8506static int
8507lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8508{
8509 uint32_t db_ready;
8510 unsigned long timeout;
8511 struct lpfc_register bmbx_reg;
8512
8513 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8514 * 1000) + jiffies;
8515
8516 do {
8517 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8518 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8519 if (!db_ready)
8520 msleep(2);
8521
8522 if (time_after(jiffies, timeout))
8523 return MBXERR_ERROR;
8524 } while (!db_ready);
8525
8526 return 0;
8527}
8528
8529/**
James Smartda0436e2009-05-22 14:51:39 -04008530 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8531 * @phba: Pointer to HBA context object.
8532 * @mboxq: Pointer to mailbox object.
8533 *
8534 * The function posts a mailbox to the port. The mailbox is expected
8535 * to be comletely filled in and ready for the port to operate on it.
8536 * This routine executes a synchronous completion operation on the
8537 * mailbox by polling for its completion.
8538 *
8539 * The caller must not be holding any locks when calling this routine.
8540 *
8541 * Returns:
8542 * MBX_SUCCESS - mailbox posted successfully
8543 * Any of the MBX error values.
8544 **/
8545static int
8546lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8547{
8548 int rc = MBX_SUCCESS;
8549 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008550 uint32_t mcqe_status;
8551 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008552 struct lpfc_sli *psli = &phba->sli;
8553 struct lpfc_mqe *mb = &mboxq->u.mqe;
8554 struct lpfc_bmbx_create *mbox_rgn;
8555 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008556
8557 /*
8558 * Only one mailbox can be active to the bootstrap mailbox region
8559 * at a time and there is no queueing provided.
8560 */
8561 spin_lock_irqsave(&phba->hbalock, iflag);
8562 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8563 spin_unlock_irqrestore(&phba->hbalock, iflag);
8564 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008565 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008566 "cannot issue Data: x%x x%x\n",
8567 mboxq->vport ? mboxq->vport->vpi : 0,
8568 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008569 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8570 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008571 psli->sli_flag, MBX_POLL);
8572 return MBXERR_ERROR;
8573 }
8574 /* The server grabs the token and owns it until release */
8575 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8576 phba->sli.mbox_active = mboxq;
8577 spin_unlock_irqrestore(&phba->hbalock, iflag);
8578
James Smart2d843ed2012-09-29 11:29:06 -04008579 /* wait for bootstrap mbox register for readyness */
8580 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8581 if (rc)
8582 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008583 /*
8584 * Initialize the bootstrap memory region to avoid stale data areas
8585 * in the mailbox post. Then copy the caller's mailbox contents to
8586 * the bmbx mailbox region.
8587 */
8588 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8589 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07008590 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8591 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008592
8593 /* Post the high mailbox dma address to the port and wait for ready. */
8594 dma_address = &phba->sli4_hba.bmbx.dma_address;
8595 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8596
James Smart2d843ed2012-09-29 11:29:06 -04008597 /* wait for bootstrap mbox register for hi-address write done */
8598 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8599 if (rc)
8600 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008601
8602 /* Post the low mailbox dma address to the port. */
8603 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008604
James Smart2d843ed2012-09-29 11:29:06 -04008605 /* wait for bootstrap mbox register for low address write done */
8606 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8607 if (rc)
8608 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008609
8610 /*
8611 * Read the CQ to ensure the mailbox has completed.
8612 * If so, update the mailbox status so that the upper layers
8613 * can complete the request normally.
8614 */
James Smart48f8fdb2018-05-04 20:37:51 -07008615 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8616 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008617 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07008618 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8619 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04008620 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008621 /*
8622 * When the CQE status indicates a failure and the mailbox status
8623 * indicates success then copy the CQE status into the mailbox status
8624 * (and prefix it with x4000).
8625 */
James Smartda0436e2009-05-22 14:51:39 -04008626 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008627 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8628 bf_set(lpfc_mqe_status, mb,
8629 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008630 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008631 } else
8632 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008633
8634 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008635 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008636 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8637 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008638 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8639 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8640 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008641 bf_get(lpfc_mqe_status, mb),
8642 mb->un.mb_words[0], mb->un.mb_words[1],
8643 mb->un.mb_words[2], mb->un.mb_words[3],
8644 mb->un.mb_words[4], mb->un.mb_words[5],
8645 mb->un.mb_words[6], mb->un.mb_words[7],
8646 mb->un.mb_words[8], mb->un.mb_words[9],
8647 mb->un.mb_words[10], mb->un.mb_words[11],
8648 mb->un.mb_words[12], mboxq->mcqe.word0,
8649 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8650 mboxq->mcqe.trailer);
8651exit:
8652 /* We are holding the token, no needed for lock when release */
8653 spin_lock_irqsave(&phba->hbalock, iflag);
8654 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8655 phba->sli.mbox_active = NULL;
8656 spin_unlock_irqrestore(&phba->hbalock, iflag);
8657 return rc;
8658}
8659
8660/**
8661 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8662 * @phba: Pointer to HBA context object.
8663 * @pmbox: Pointer to mailbox object.
8664 * @flag: Flag indicating how the mailbox need to be processed.
8665 *
8666 * This function is called by discovery code and HBA management code to submit
8667 * a mailbox command to firmware with SLI-4 interface spec.
8668 *
8669 * Return codes the caller owns the mailbox command after the return of the
8670 * function.
8671 **/
8672static int
8673lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8674 uint32_t flag)
8675{
8676 struct lpfc_sli *psli = &phba->sli;
8677 unsigned long iflags;
8678 int rc;
8679
James Smartb76f2dc2011-07-22 18:37:42 -04008680 /* dump from issue mailbox command if setup */
8681 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8682
James Smart8fa38512009-07-19 10:01:03 -04008683 rc = lpfc_mbox_dev_check(phba);
8684 if (unlikely(rc)) {
8685 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008686 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008687 "cannot issue Data: x%x x%x\n",
8688 mboxq->vport ? mboxq->vport->vpi : 0,
8689 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008690 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8691 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008692 psli->sli_flag, flag);
8693 goto out_not_finished;
8694 }
8695
James Smartda0436e2009-05-22 14:51:39 -04008696 /* Detect polling mode and jump to a handler */
8697 if (!phba->sli4_hba.intr_enable) {
8698 if (flag == MBX_POLL)
8699 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8700 else
8701 rc = -EIO;
8702 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008703 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008704 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008705 "(x%x/x%x) failure: "
8706 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8707 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008708 mboxq->vport ? mboxq->vport->vpi : 0,
8709 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008710 lpfc_sli_config_mbox_subsys_get(phba,
8711 mboxq),
8712 lpfc_sli_config_mbox_opcode_get(phba,
8713 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008714 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8715 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8716 bf_get(lpfc_mcqe_ext_status,
8717 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008718 psli->sli_flag, flag);
8719 return rc;
8720 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008721 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8722 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008723 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008724 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008725 mboxq->vport ? mboxq->vport->vpi : 0,
8726 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008727 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8728 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008729 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008730 /* Try to block the asynchronous mailbox posting */
8731 rc = lpfc_sli4_async_mbox_block(phba);
8732 if (!rc) {
8733 /* Successfully blocked, now issue sync mbox cmd */
8734 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8735 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008736 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008737 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008738 "(%d):2597 Sync Mailbox command "
8739 "x%x (x%x/x%x) failure: "
8740 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8741 "Data: x%x x%x\n,",
8742 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008743 mboxq->u.mb.mbxCommand,
8744 lpfc_sli_config_mbox_subsys_get(phba,
8745 mboxq),
8746 lpfc_sli_config_mbox_opcode_get(phba,
8747 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008748 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8749 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8750 bf_get(lpfc_mcqe_ext_status,
8751 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008752 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008753 /* Unblock the async mailbox posting afterward */
8754 lpfc_sli4_async_mbox_unblock(phba);
8755 }
8756 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008757 }
8758
8759 /* Now, interrupt mode asynchrous mailbox command */
8760 rc = lpfc_mbox_cmd_check(phba, mboxq);
8761 if (rc) {
8762 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008763 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008764 "cannot issue Data: x%x x%x\n",
8765 mboxq->vport ? mboxq->vport->vpi : 0,
8766 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008767 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8768 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008769 psli->sli_flag, flag);
8770 goto out_not_finished;
8771 }
James Smartda0436e2009-05-22 14:51:39 -04008772
8773 /* Put the mailbox command to the driver internal FIFO */
8774 psli->slistat.mbox_busy++;
8775 spin_lock_irqsave(&phba->hbalock, iflags);
8776 lpfc_mbox_put(phba, mboxq);
8777 spin_unlock_irqrestore(&phba->hbalock, iflags);
8778 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8779 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008780 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008781 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8782 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008783 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8784 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008785 phba->pport->port_state,
8786 psli->sli_flag, MBX_NOWAIT);
8787 /* Wake up worker thread to transport mailbox command from head */
8788 lpfc_worker_wake_up(phba);
8789
8790 return MBX_BUSY;
8791
8792out_not_finished:
8793 return MBX_NOT_FINISHED;
8794}
8795
8796/**
8797 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8798 * @phba: Pointer to HBA context object.
8799 *
8800 * This function is called by worker thread to send a mailbox command to
8801 * SLI4 HBA firmware.
8802 *
8803 **/
8804int
8805lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8806{
8807 struct lpfc_sli *psli = &phba->sli;
8808 LPFC_MBOXQ_t *mboxq;
8809 int rc = MBX_SUCCESS;
8810 unsigned long iflags;
8811 struct lpfc_mqe *mqe;
8812 uint32_t mbx_cmnd;
8813
8814 /* Check interrupt mode before post async mailbox command */
8815 if (unlikely(!phba->sli4_hba.intr_enable))
8816 return MBX_NOT_FINISHED;
8817
8818 /* Check for mailbox command service token */
8819 spin_lock_irqsave(&phba->hbalock, iflags);
8820 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8821 spin_unlock_irqrestore(&phba->hbalock, iflags);
8822 return MBX_NOT_FINISHED;
8823 }
8824 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8825 spin_unlock_irqrestore(&phba->hbalock, iflags);
8826 return MBX_NOT_FINISHED;
8827 }
8828 if (unlikely(phba->sli.mbox_active)) {
8829 spin_unlock_irqrestore(&phba->hbalock, iflags);
8830 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8831 "0384 There is pending active mailbox cmd\n");
8832 return MBX_NOT_FINISHED;
8833 }
8834 /* Take the mailbox command service token */
8835 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8836
8837 /* Get the next mailbox command from head of queue */
8838 mboxq = lpfc_mbox_get(phba);
8839
8840 /* If no more mailbox command waiting for post, we're done */
8841 if (!mboxq) {
8842 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8843 spin_unlock_irqrestore(&phba->hbalock, iflags);
8844 return MBX_SUCCESS;
8845 }
8846 phba->sli.mbox_active = mboxq;
8847 spin_unlock_irqrestore(&phba->hbalock, iflags);
8848
8849 /* Check device readiness for posting mailbox command */
8850 rc = lpfc_mbox_dev_check(phba);
8851 if (unlikely(rc))
8852 /* Driver clean routine will clean up pending mailbox */
8853 goto out_not_finished;
8854
8855 /* Prepare the mbox command to be posted */
8856 mqe = &mboxq->u.mqe;
8857 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8858
8859 /* Start timer for the mbox_tmo and log some mailbox post messages */
8860 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008861 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008862
8863 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008864 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008865 "x%x x%x\n",
8866 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008867 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8868 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008869 phba->pport->port_state, psli->sli_flag);
8870
8871 if (mbx_cmnd != MBX_HEARTBEAT) {
8872 if (mboxq->vport) {
8873 lpfc_debugfs_disc_trc(mboxq->vport,
8874 LPFC_DISC_TRC_MBOX_VPORT,
8875 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8876 mbx_cmnd, mqe->un.mb_words[0],
8877 mqe->un.mb_words[1]);
8878 } else {
8879 lpfc_debugfs_disc_trc(phba->pport,
8880 LPFC_DISC_TRC_MBOX,
8881 "MBOX Send: cmd:x%x mb:x%x x%x",
8882 mbx_cmnd, mqe->un.mb_words[0],
8883 mqe->un.mb_words[1]);
8884 }
8885 }
8886 psli->slistat.mbox_cmd++;
8887
8888 /* Post the mailbox command to the port */
8889 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8890 if (rc != MBX_SUCCESS) {
8891 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008892 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008893 "cannot issue Data: x%x x%x\n",
8894 mboxq->vport ? mboxq->vport->vpi : 0,
8895 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008896 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8897 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008898 psli->sli_flag, MBX_NOWAIT);
8899 goto out_not_finished;
8900 }
8901
8902 return rc;
8903
8904out_not_finished:
8905 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008906 if (phba->sli.mbox_active) {
8907 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8908 __lpfc_mbox_cmpl_put(phba, mboxq);
8909 /* Release the token */
8910 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8911 phba->sli.mbox_active = NULL;
8912 }
James Smartda0436e2009-05-22 14:51:39 -04008913 spin_unlock_irqrestore(&phba->hbalock, iflags);
8914
8915 return MBX_NOT_FINISHED;
8916}
8917
8918/**
8919 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8920 * @phba: Pointer to HBA context object.
8921 * @pmbox: Pointer to mailbox object.
8922 * @flag: Flag indicating how the mailbox need to be processed.
8923 *
8924 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8925 * the API jump table function pointer from the lpfc_hba struct.
8926 *
8927 * Return codes the caller owns the mailbox command after the return of the
8928 * function.
8929 **/
8930int
8931lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8932{
8933 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8934}
8935
8936/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008937 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008938 * @phba: The hba struct for which this call is being executed.
8939 * @dev_grp: The HBA PCI-Device group number.
8940 *
8941 * This routine sets up the mbox interface API function jump table in @phba
8942 * struct.
8943 * Returns: 0 - success, -ENODEV - failure.
8944 **/
8945int
8946lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8947{
8948
8949 switch (dev_grp) {
8950 case LPFC_PCI_DEV_LP:
8951 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8952 phba->lpfc_sli_handle_slow_ring_event =
8953 lpfc_sli_handle_slow_ring_event_s3;
8954 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8955 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8956 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8957 break;
8958 case LPFC_PCI_DEV_OC:
8959 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8960 phba->lpfc_sli_handle_slow_ring_event =
8961 lpfc_sli_handle_slow_ring_event_s4;
8962 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8963 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8964 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8965 break;
8966 default:
8967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8968 "1420 Invalid HBA PCI-device group: 0x%x\n",
8969 dev_grp);
8970 return -ENODEV;
8971 break;
8972 }
8973 return 0;
8974}
8975
8976/**
James Smart3621a712009-04-06 18:47:14 -04008977 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008978 * @phba: Pointer to HBA context object.
8979 * @pring: Pointer to driver SLI ring object.
8980 * @piocb: Pointer to address of newly added command iocb.
8981 *
8982 * This function is called with hbalock held to add a command
8983 * iocb to the txq when SLI layer cannot submit the command iocb
8984 * to the ring.
8985 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008986void
James Smart92d7f7b2007-06-17 19:56:38 -05008987__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008988 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008989{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008990 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008991 /* Insert the caller's iocb in the txq tail for later processing. */
8992 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008993}
8994
James Smarte59058c2008-08-24 21:49:00 -04008995/**
James Smart3621a712009-04-06 18:47:14 -04008996 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008997 * @phba: Pointer to HBA context object.
8998 * @pring: Pointer to driver SLI ring object.
8999 * @piocb: Pointer to address of newly added command iocb.
9000 *
9001 * This function is called with hbalock held before a new
9002 * iocb is submitted to the firmware. This function checks
9003 * txq to flush the iocbs in txq to Firmware before
9004 * submitting new iocbs to the Firmware.
9005 * If there are iocbs in the txq which need to be submitted
9006 * to firmware, lpfc_sli_next_iocb returns the first element
9007 * of the txq after dequeuing it from txq.
9008 * If there is no iocb in the txq then the function will return
9009 * *piocb and *piocb is set to NULL. Caller needs to check
9010 * *piocb to find if there are more commands in the txq.
9011 **/
dea31012005-04-17 16:05:31 -05009012static struct lpfc_iocbq *
9013lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009014 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05009015{
9016 struct lpfc_iocbq * nextiocb;
9017
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009018 lockdep_assert_held(&phba->hbalock);
9019
dea31012005-04-17 16:05:31 -05009020 nextiocb = lpfc_sli_ringtx_get(phba, pring);
9021 if (!nextiocb) {
9022 nextiocb = *piocb;
9023 *piocb = NULL;
9024 }
9025
9026 return nextiocb;
9027}
9028
James Smarte59058c2008-08-24 21:49:00 -04009029/**
James Smart3772a992009-05-22 14:50:54 -04009030 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009031 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04009032 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04009033 * @piocb: Pointer to command iocb.
9034 * @flag: Flag indicating if this command can be put into txq.
9035 *
James Smart3772a992009-05-22 14:50:54 -04009036 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
9037 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
9038 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
9039 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
9040 * this function allows only iocbs for posting buffers. This function finds
9041 * next available slot in the command ring and posts the command to the
9042 * available slot and writes the port attention register to request HBA start
9043 * processing new iocb. If there is no slot available in the ring and
9044 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9045 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04009046 *
James Smart3772a992009-05-22 14:50:54 -04009047 * This function is called with hbalock held. The function will return success
9048 * after it successfully submit the iocb to firmware or after adding to the
9049 * txq.
James Smarte59058c2008-08-24 21:49:00 -04009050 **/
James Smart98c9ea52007-10-27 13:37:33 -04009051static int
James Smart3772a992009-05-22 14:50:54 -04009052__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05009053 struct lpfc_iocbq *piocb, uint32_t flag)
9054{
9055 struct lpfc_iocbq *nextiocb;
9056 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08009057 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05009058
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009059 lockdep_assert_held(&phba->hbalock);
9060
James Smart92d7f7b2007-06-17 19:56:38 -05009061 if (piocb->iocb_cmpl && (!piocb->vport) &&
9062 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9063 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
9064 lpfc_printf_log(phba, KERN_ERR,
9065 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04009066 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05009067 piocb->iocb.ulpCommand);
9068 dump_stack();
9069 return IOCB_ERROR;
9070 }
9071
9072
Linas Vepstas8d63f372007-02-14 14:28:36 -06009073 /* If the PCI channel is in offline state, do not post iocbs. */
9074 if (unlikely(pci_channel_offline(phba->pcidev)))
9075 return IOCB_ERROR;
9076
James Smarta257bf92009-04-06 18:48:10 -04009077 /* If HBA has a deferred error attention, fail the iocb. */
9078 if (unlikely(phba->hba_flag & DEFER_ERATT))
9079 return IOCB_ERROR;
9080
dea31012005-04-17 16:05:31 -05009081 /*
9082 * We should never get an IOCB if we are in a < LINK_DOWN state
9083 */
James Smart2e0fef82007-06-17 19:56:36 -05009084 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05009085 return IOCB_ERROR;
9086
9087 /*
9088 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04009089 * outstanding event.
dea31012005-04-17 16:05:31 -05009090 */
James Smart0b727fe2007-10-27 13:37:25 -04009091 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05009092 goto iocb_busy;
9093
James Smart2e0fef82007-06-17 19:56:36 -05009094 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05009095 /*
James Smart2680eea2007-04-25 09:52:55 -04009096 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05009097 * can be issued if the link is not up.
9098 */
9099 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04009100 case CMD_GEN_REQUEST64_CR:
9101 case CMD_GEN_REQUEST64_CX:
9102 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9103 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04009104 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04009105 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9106 MENLO_TRANSPORT_TYPE))
9107
9108 goto iocb_busy;
9109 break;
dea31012005-04-17 16:05:31 -05009110 case CMD_QUE_RING_BUF_CN:
9111 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05009112 /*
9113 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9114 * completion, iocb_cmpl MUST be 0.
9115 */
9116 if (piocb->iocb_cmpl)
9117 piocb->iocb_cmpl = NULL;
9118 /*FALLTHROUGH*/
9119 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04009120 case CMD_CLOSE_XRI_CN:
9121 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05009122 break;
9123 default:
9124 goto iocb_busy;
9125 }
9126
9127 /*
9128 * For FCP commands, we must be in a state where we can process link
9129 * attention events.
9130 */
James Smart895427b2017-02-12 13:52:30 -08009131 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05009132 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05009133 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05009134 }
dea31012005-04-17 16:05:31 -05009135
dea31012005-04-17 16:05:31 -05009136 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9137 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9138 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9139
9140 if (iocb)
9141 lpfc_sli_update_ring(phba, pring);
9142 else
9143 lpfc_sli_update_full_ring(phba, pring);
9144
9145 if (!piocb)
9146 return IOCB_SUCCESS;
9147
9148 goto out_busy;
9149
9150 iocb_busy:
9151 pring->stats.iocb_cmd_delay++;
9152
9153 out_busy:
9154
9155 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009156 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05009157 return IOCB_SUCCESS;
9158 }
9159
9160 return IOCB_BUSY;
9161}
9162
James Smart3772a992009-05-22 14:50:54 -04009163/**
James Smart4f774512009-05-22 14:52:35 -04009164 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9165 * @phba: Pointer to HBA context object.
9166 * @piocb: Pointer to command iocb.
9167 * @sglq: Pointer to the scatter gather queue object.
9168 *
9169 * This routine converts the bpl or bde that is in the IOCB
9170 * to a sgl list for the sli4 hardware. The physical address
9171 * of the bpl/bde is converted back to a virtual address.
9172 * If the IOCB contains a BPL then the list of BDE's is
9173 * converted to sli4_sge's. If the IOCB contains a single
9174 * BDE then it is converted to a single sli_sge.
9175 * The IOCB is still in cpu endianess so the contents of
9176 * the bpl can be used without byte swapping.
9177 *
9178 * Returns valid XRI = Success, NO_XRI = Failure.
9179**/
9180static uint16_t
9181lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9182 struct lpfc_sglq *sglq)
9183{
9184 uint16_t xritag = NO_XRI;
9185 struct ulp_bde64 *bpl = NULL;
9186 struct ulp_bde64 bde;
9187 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05009188 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04009189 IOCB_t *icmd;
9190 int numBdes = 0;
9191 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05009192 uint32_t offset = 0; /* accumulated offset in the sg request list */
9193 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04009194
9195 if (!piocbq || !sglq)
9196 return xritag;
9197
9198 sgl = (struct sli4_sge *)sglq->sgl;
9199 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05009200 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9201 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009202 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9203 numBdes = icmd->un.genreq64.bdl.bdeSize /
9204 sizeof(struct ulp_bde64);
9205 /* The addrHigh and addrLow fields within the IOCB
9206 * have not been byteswapped yet so there is no
9207 * need to swap them back.
9208 */
James Smart1b511972011-12-13 13:23:09 -05009209 if (piocbq->context3)
9210 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9211 else
9212 return xritag;
James Smart4f774512009-05-22 14:52:35 -04009213
James Smart1b511972011-12-13 13:23:09 -05009214 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04009215 if (!bpl)
9216 return xritag;
9217
9218 for (i = 0; i < numBdes; i++) {
9219 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05009220 sgl->addr_hi = bpl->addrHigh;
9221 sgl->addr_lo = bpl->addrLow;
9222
James Smart05580562011-05-24 11:40:48 -04009223 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009224 if ((i+1) == numBdes)
9225 bf_set(lpfc_sli4_sge_last, sgl, 1);
9226 else
9227 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05009228 /* swap the size field back to the cpu so we
9229 * can assign it to the sgl.
9230 */
9231 bde.tus.w = le32_to_cpu(bpl->tus.w);
9232 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05009233 /* The offsets in the sgl need to be accumulated
9234 * separately for the request and reply lists.
9235 * The request is always first, the reply follows.
9236 */
9237 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9238 /* add up the reply sg entries */
9239 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9240 inbound++;
9241 /* first inbound? reset the offset */
9242 if (inbound == 1)
9243 offset = 0;
9244 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04009245 bf_set(lpfc_sli4_sge_type, sgl,
9246 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05009247 offset += bde.tus.f.bdeSize;
9248 }
James Smart546fc852011-03-11 16:06:29 -05009249 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009250 bpl++;
9251 sgl++;
9252 }
9253 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9254 /* The addrHigh and addrLow fields of the BDE have not
9255 * been byteswapped yet so they need to be swapped
9256 * before putting them in the sgl.
9257 */
9258 sgl->addr_hi =
9259 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9260 sgl->addr_lo =
9261 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04009262 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009263 bf_set(lpfc_sli4_sge_last, sgl, 1);
9264 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05009265 sgl->sge_len =
9266 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04009267 }
9268 return sglq->sli4_xritag;
9269}
9270
9271/**
James Smart4f774512009-05-22 14:52:35 -04009272 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9273 * @phba: Pointer to HBA context object.
9274 * @piocb: Pointer to command iocb.
9275 * @wqe: Pointer to the work queue entry.
9276 *
9277 * This routine converts the iocb command to its Work Queue Entry
9278 * equivalent. The wqe pointer should not have any fields set when
9279 * this routine is called because it will memcpy over them.
9280 * This routine does not set the CQ_ID or the WQEC bits in the
9281 * wqe.
9282 *
9283 * Returns: 0 = Success, IOCB_ERROR = Failure.
9284 **/
9285static int
9286lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08009287 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04009288{
James Smart5ffc2662009-11-18 15:39:44 -05009289 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04009290 uint8_t ct = 0;
9291 uint32_t fip;
9292 uint32_t abort_tag;
9293 uint8_t command_type = ELS_COMMAND_NON_FIP;
9294 uint8_t cmnd;
9295 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04009296 uint16_t abrt_iotag;
9297 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04009298 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04009299 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05009300 int numBdes, i;
9301 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04009302 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05009303 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05009304 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04009305
James Smart45ed1192009-10-02 15:17:02 -04009306 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04009307 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04009308 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04009309 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05009310 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04009311 command_type = ELS_COMMAND_FIP;
9312 else
9313 command_type = ELS_COMMAND_NON_FIP;
9314
James Smartb5c53952016-03-31 14:12:30 -07009315 if (phba->fcp_embed_io)
9316 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04009317 /* Some of the fields are in the right position already */
9318 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07009319 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
9320 /* The ct field has moved so reset */
9321 wqe->generic.wqe_com.word7 = 0;
9322 wqe->generic.wqe_com.word10 = 0;
9323 }
James Smartb5c53952016-03-31 14:12:30 -07009324
9325 abort_tag = (uint32_t) iocbq->iotag;
9326 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009327 /* words0-2 bpl convert bde */
9328 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05009329 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9330 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04009331 bpl = (struct ulp_bde64 *)
9332 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9333 if (!bpl)
9334 return IOCB_ERROR;
9335
9336 /* Should already be byte swapped. */
9337 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
9338 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
9339 /* swap the size field back to the cpu so we
9340 * can assign it to the sgl.
9341 */
9342 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05009343 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9344 total_len = 0;
9345 for (i = 0; i < numBdes; i++) {
9346 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9347 total_len += bde.tus.f.bdeSize;
9348 }
James Smart4f774512009-05-22 14:52:35 -04009349 } else
James Smart5ffc2662009-11-18 15:39:44 -05009350 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04009351
9352 iocbq->iocb.ulpIoTag = iocbq->iotag;
9353 cmnd = iocbq->iocb.ulpCommand;
9354
9355 switch (iocbq->iocb.ulpCommand) {
9356 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04009357 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9358 ndlp = iocbq->context_un.ndlp;
9359 else
9360 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009361 if (!iocbq->iocb.ulpLe) {
9362 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9363 "2007 Only Limited Edition cmd Format"
9364 " supported 0x%x\n",
9365 iocbq->iocb.ulpCommand);
9366 return IOCB_ERROR;
9367 }
James Smartff78d8f2011-12-13 13:21:35 -05009368
James Smart5ffc2662009-11-18 15:39:44 -05009369 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009370 /* Els_reguest64 has a TMO */
9371 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9372 iocbq->iocb.ulpTimeout);
9373 /* Need a VF for word 4 set the vf bit*/
9374 bf_set(els_req64_vf, &wqe->els_req, 0);
9375 /* And a VFID for word 12 */
9376 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04009377 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04009378 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9379 iocbq->iocb.ulpContext);
9380 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9381 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04009382 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05009383 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05009384 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9385 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05009386 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9387 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05009388 if_type = bf_get(lpfc_sli_intf_if_type,
9389 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009390 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05009391 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05009392 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05009393 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05009394 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05009395 *pcmd == ELS_CMD_PLOGI)) {
9396 bf_set(els_req64_sp, &wqe->els_req, 1);
9397 bf_set(els_req64_sid, &wqe->els_req,
9398 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009399 if ((*pcmd == ELS_CMD_FLOGI) &&
9400 !(phba->fc_topology ==
9401 LPFC_TOPOLOGY_LOOP))
9402 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009403 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9404 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009405 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009406 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009407 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9408 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9409 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9410 }
James Smartc8685952009-11-18 15:39:16 -05009411 }
James Smart6d368e52011-05-24 11:44:12 -04009412 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9413 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009414 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9415 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9416 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9417 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9418 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9419 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009420 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009421 break;
James Smart5ffc2662009-11-18 15:39:44 -05009422 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009423 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9424 iocbq->iocb.un.ulpWord[3]);
9425 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009426 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009427 /* The entire sequence is transmitted for this IOCB */
9428 xmit_len = total_len;
9429 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009430 if (phba->link_flag & LS_LOOPBACK_MODE)
9431 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04009432 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009433 /* word3 iocb=io_tag32 wqe=reserved */
9434 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009435 /* word4 relative_offset memcpy */
9436 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009437 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9438 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9439 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9440 LPFC_WQE_IOD_WRITE);
9441 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9442 LPFC_WQE_LENLOC_WORD12);
9443 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009444 wqe->xmit_sequence.xmit_len = xmit_len;
9445 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009446 break;
James Smart4f774512009-05-22 14:52:35 -04009447 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009448 /* word3 iocb=iotag32 wqe=seq_payload_len */
9449 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009450 /* word4 iocb=rsvd wqe=rsvd */
9451 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9452 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009453 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009454 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009455 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9456 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9457 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9458 LPFC_WQE_LENLOC_WORD3);
9459 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009460 break;
James Smart4f774512009-05-22 14:52:35 -04009461 case CMD_FCP_IWRITE64_CR:
9462 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009463 /* word3 iocb=iotag wqe=payload_offset_len */
9464 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009465 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9466 xmit_len + sizeof(struct fcp_rsp));
9467 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9468 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009469 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9470 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9471 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9472 iocbq->iocb.ulpFCP2Rcvy);
9473 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9474 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009475 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9476 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9477 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009478 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009479 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009480 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9481 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009482 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9483 if (iocbq->priority) {
9484 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9485 (iocbq->priority << 1));
9486 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009487 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9488 (phba->cfg_XLanePriority << 1));
9489 }
9490 }
James Smartb5c53952016-03-31 14:12:30 -07009491 /* Note, word 10 is already initialized to 0 */
9492
James Smart414abe02018-06-26 08:24:26 -07009493 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9494 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009495 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9496 else
9497 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9498
James Smartb5c53952016-03-31 14:12:30 -07009499 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009500 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009501 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009502 struct fcp_cmnd *fcp_cmnd;
9503 uint32_t *ptr;
9504
9505 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009506
9507 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009508 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009509 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9510
9511 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009512 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009513 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009514 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9515 wqe->generic.bde.addrHigh = 0;
9516 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009517
James Smart205e8242018-03-05 12:04:03 -08009518 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9519 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009520
9521 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009522 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009523 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9524 }
James Smart7851fe22011-07-22 18:36:52 -04009525 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009526 case CMD_FCP_IREAD64_CR:
9527 /* word3 iocb=iotag wqe=payload_offset_len */
9528 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009529 bf_set(payload_offset_len, &wqe->fcp_iread,
9530 xmit_len + sizeof(struct fcp_rsp));
9531 bf_set(cmd_buff_len, &wqe->fcp_iread,
9532 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009533 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9534 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9535 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9536 iocbq->iocb.ulpFCP2Rcvy);
9537 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009538 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009539 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9540 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9541 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009542 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009543 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009544 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9545 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009546 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9547 if (iocbq->priority) {
9548 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9549 (iocbq->priority << 1));
9550 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009551 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9552 (phba->cfg_XLanePriority << 1));
9553 }
9554 }
James Smartb5c53952016-03-31 14:12:30 -07009555 /* Note, word 10 is already initialized to 0 */
9556
James Smart414abe02018-06-26 08:24:26 -07009557 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9558 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009559 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9560 else
9561 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9562
James Smartb5c53952016-03-31 14:12:30 -07009563 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009564 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009565 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009566 struct fcp_cmnd *fcp_cmnd;
9567 uint32_t *ptr;
9568
9569 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009570
9571 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009572 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009573 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9574
9575 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009576 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009577 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009578 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9579 wqe->generic.bde.addrHigh = 0;
9580 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009581
James Smart205e8242018-03-05 12:04:03 -08009582 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9583 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009584
9585 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009586 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009587 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9588 }
James Smart7851fe22011-07-22 18:36:52 -04009589 break;
James Smartf1126682009-06-10 17:22:44 -04009590 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009591 /* word3 iocb=iotag wqe=payload_offset_len */
9592 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9593 bf_set(payload_offset_len, &wqe->fcp_icmd,
9594 xmit_len + sizeof(struct fcp_rsp));
9595 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9596 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009597 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009598 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009599 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009600 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9601 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9602 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9603 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9604 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009605 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9606 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009607 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9608 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009609 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9610 if (iocbq->priority) {
9611 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9612 (iocbq->priority << 1));
9613 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009614 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9615 (phba->cfg_XLanePriority << 1));
9616 }
9617 }
James Smartb5c53952016-03-31 14:12:30 -07009618 /* Note, word 10 is already initialized to 0 */
9619
9620 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009621 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009622 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009623 struct fcp_cmnd *fcp_cmnd;
9624 uint32_t *ptr;
9625
9626 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009627
9628 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009629 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009630 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9631
9632 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009633 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009634 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009635 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9636 wqe->generic.bde.addrHigh = 0;
9637 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009638
James Smart205e8242018-03-05 12:04:03 -08009639 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9640 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009641
9642 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009643 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009644 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9645 }
James Smart7851fe22011-07-22 18:36:52 -04009646 break;
James Smart4f774512009-05-22 14:52:35 -04009647 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009648 /* For this command calculate the xmit length of the
9649 * request bde.
9650 */
9651 xmit_len = 0;
9652 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9653 sizeof(struct ulp_bde64);
9654 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009655 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009656 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9657 break;
James Smart63e801c2010-11-20 23:14:19 -05009658 xmit_len += bde.tus.f.bdeSize;
9659 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009660 /* word3 iocb=IO_TAG wqe=request_payload_len */
9661 wqe->gen_req.request_payload_len = xmit_len;
9662 /* word4 iocb=parameter wqe=relative_offset memcpy */
9663 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009664 /* word6 context tag copied in memcpy */
9665 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9666 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9667 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9668 "2015 Invalid CT %x command 0x%x\n",
9669 ct, iocbq->iocb.ulpCommand);
9670 return IOCB_ERROR;
9671 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009672 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9673 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9674 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9675 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9676 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9677 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9678 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9679 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009680 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009681 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009682 break;
James Smart4f774512009-05-22 14:52:35 -04009683 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009684 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009685 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009686 /* word3 iocb=iotag32 wqe=response_payload_len */
9687 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009688 /* word4 */
9689 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009690 /* word5 iocb=rsvd wge=did */
9691 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009692 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9693
9694 if_type = bf_get(lpfc_sli_intf_if_type,
9695 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009696 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009697 if (iocbq->vport->fc_flag & FC_PT2PT) {
9698 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9699 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9700 iocbq->vport->fc_myDID);
9701 if (iocbq->vport->fc_myDID == Fabric_DID) {
9702 bf_set(wqe_els_did,
9703 &wqe->xmit_els_rsp.wqe_dest, 0);
9704 }
9705 }
9706 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009707 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9708 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9709 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9710 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009711 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009712 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009713 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009714 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009715 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9716 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9717 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9718 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9719 LPFC_WQE_LENLOC_WORD3);
9720 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009721 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9722 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009723 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9724 iocbq->context2)->virt);
9725 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009726 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9727 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009728 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009729 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9730 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009731 phba->vpi_ids[phba->pport->vpi]);
9732 }
James Smart4f774512009-05-22 14:52:35 -04009733 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009734 break;
James Smart4f774512009-05-22 14:52:35 -04009735 case CMD_CLOSE_XRI_CN:
9736 case CMD_ABORT_XRI_CN:
9737 case CMD_ABORT_XRI_CX:
9738 /* words 0-2 memcpy should be 0 rserved */
9739 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009740 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9741 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9742 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9743 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9744 } else
9745 fip = 0;
9746
9747 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009748 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009749 * The link is down, or the command was ELS_FIP
9750 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009751 * on the wire.
9752 */
9753 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9754 else
9755 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9756 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009757 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9758 wqe->abort_cmd.rsrvd5 = 0;
9759 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009760 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9761 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009762 /*
9763 * The abort handler will send us CMD_ABORT_XRI_CN or
9764 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9765 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009766 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9767 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9768 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9769 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009770 cmnd = CMD_ABORT_XRI_CX;
9771 command_type = OTHER_COMMAND;
9772 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009773 break;
James Smart6669f9b2009-10-02 15:16:45 -04009774 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009775 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009776 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009777 * we re-construct this WQE here based on information in
9778 * iocbq from scratch.
9779 */
9780 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009781 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009782 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009783 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9784 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009785 LPFC_ABTS_UNSOL_INT) {
9786 /* ABTS sent by initiator to CT exchange, the
9787 * RX_ID field will be filled with the newly
9788 * allocated responder XRI.
9789 */
9790 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9791 iocbq->sli4_xritag);
9792 } else {
9793 /* ABTS sent by responder to CT exchange, the
9794 * RX_ID field will be filled with the responder
9795 * RX_ID from ABTS.
9796 */
9797 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009798 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009799 }
James Smart6669f9b2009-10-02 15:16:45 -04009800 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9801 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009802
9803 /* Use CT=VPI */
9804 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9805 ndlp->nlp_DID);
9806 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9807 iocbq->iocb.ulpContext);
9808 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009809 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009810 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009811 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9812 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9813 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009814 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9815 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009816 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9817 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9818 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9819 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9820 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9821 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9822 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9823 }
9824
James Smart7851fe22011-07-22 18:36:52 -04009825 break;
James Smartae9e28f2017-05-15 15:20:51 -07009826 case CMD_SEND_FRAME:
9827 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9828 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9829 return 0;
James Smart4f774512009-05-22 14:52:35 -04009830 case CMD_XRI_ABORTED_CX:
9831 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009832 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9833 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9834 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9835 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9836 default:
9837 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9838 "2014 Invalid command 0x%x\n",
9839 iocbq->iocb.ulpCommand);
9840 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009841 break;
James Smart4f774512009-05-22 14:52:35 -04009842 }
James Smart6d368e52011-05-24 11:44:12 -04009843
James Smart8012cc32012-10-31 14:44:49 -04009844 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9845 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9846 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9847 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9848 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9849 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9850 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9851 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009852 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9853 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9854 wqe->generic.wqe_com.abort_tag = abort_tag;
9855 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9856 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9857 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9858 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009859 return 0;
9860}
9861
9862/**
9863 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9864 * @phba: Pointer to HBA context object.
9865 * @ring_number: SLI ring number to issue iocb on.
9866 * @piocb: Pointer to command iocb.
9867 * @flag: Flag indicating if this command can be put into txq.
9868 *
9869 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9870 * an iocb command to an HBA with SLI-4 interface spec.
9871 *
9872 * This function is called with hbalock held. The function will return success
9873 * after it successfully submit the iocb to firmware or after adding to the
9874 * txq.
9875 **/
9876static int
9877__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9878 struct lpfc_iocbq *piocb, uint32_t flag)
9879{
9880 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -08009881 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -05009882 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009883 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009884
James Smart895427b2017-02-12 13:52:30 -08009885 /* Get the WQ */
9886 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9887 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -08009888 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].fcp_wq;
James Smart895427b2017-02-12 13:52:30 -08009889 } else {
9890 wq = phba->sli4_hba.els_wq;
9891 }
9892
9893 /* Get corresponding ring */
9894 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009895
James Smartb5c53952016-03-31 14:12:30 -07009896 /*
9897 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -07009898 */
James Smartb5c53952016-03-31 14:12:30 -07009899
James Smart895427b2017-02-12 13:52:30 -08009900 lockdep_assert_held(&phba->hbalock);
9901
James Smart4f774512009-05-22 14:52:35 -04009902 if (piocb->sli4_xritag == NO_XRI) {
9903 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009904 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009905 sglq = NULL;
9906 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009907 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009908 if (!(flag & SLI_IOCB_RET_IOCB)) {
9909 __lpfc_sli_ringtx_put(phba,
9910 pring, piocb);
9911 return IOCB_SUCCESS;
9912 } else {
9913 return IOCB_BUSY;
9914 }
9915 } else {
James Smart895427b2017-02-12 13:52:30 -08009916 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009917 if (!sglq) {
9918 if (!(flag & SLI_IOCB_RET_IOCB)) {
9919 __lpfc_sli_ringtx_put(phba,
9920 pring,
9921 piocb);
9922 return IOCB_SUCCESS;
9923 } else
9924 return IOCB_BUSY;
9925 }
9926 }
James Smart4f774512009-05-22 14:52:35 -04009927 }
James Smart2ea259e2017-02-12 13:52:27 -08009928 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009929 /* These IO's already have an XRI and a mapped sgl. */
9930 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009931 else {
James Smart6d368e52011-05-24 11:44:12 -04009932 /*
9933 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009934 * sglq is on the active list
9935 */
James Smartedccdc12013-01-03 15:43:45 -05009936 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009937 if (!sglq)
9938 return IOCB_ERROR;
9939 }
9940
9941 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009942 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009943 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009944 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009945 return IOCB_ERROR;
9946 }
9947
James Smart205e8242018-03-05 12:04:03 -08009948 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -04009949 return IOCB_ERROR;
9950
James Smart205e8242018-03-05 12:04:03 -08009951 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -08009952 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009953 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9954
9955 return 0;
9956}
9957
9958/**
James Smart3772a992009-05-22 14:50:54 -04009959 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9960 *
9961 * This routine wraps the actual lockless version for issusing IOCB function
9962 * pointer from the lpfc_hba struct.
9963 *
9964 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009965 * IOCB_ERROR - Error
9966 * IOCB_SUCCESS - Success
9967 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009968 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009969int
James Smart3772a992009-05-22 14:50:54 -04009970__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9971 struct lpfc_iocbq *piocb, uint32_t flag)
9972{
9973 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9974}
9975
9976/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009977 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009978 * @phba: The hba struct for which this call is being executed.
9979 * @dev_grp: The HBA PCI-Device group number.
9980 *
9981 * This routine sets up the SLI interface API function jump table in @phba
9982 * struct.
9983 * Returns: 0 - success, -ENODEV - failure.
9984 **/
9985int
9986lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9987{
9988
9989 switch (dev_grp) {
9990 case LPFC_PCI_DEV_LP:
9991 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9992 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9993 break;
James Smart4f774512009-05-22 14:52:35 -04009994 case LPFC_PCI_DEV_OC:
9995 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9996 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9997 break;
James Smart3772a992009-05-22 14:50:54 -04009998 default:
9999 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10000 "1419 Invalid HBA PCI-device group: 0x%x\n",
10001 dev_grp);
10002 return -ENODEV;
10003 break;
10004 }
10005 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
10006 return 0;
10007}
James Smart92d7f7b2007-06-17 19:56:38 -050010008
James Smarta1efe162015-05-21 13:55:20 -040010009/**
James Smart895427b2017-02-12 13:52:30 -080010010 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -040010011 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -040010012 * @piocb: Pointer to command iocb.
10013 *
James Smart895427b2017-02-12 13:52:30 -080010014 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
10015 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -040010016 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -080010017 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -040010018 */
James Smart895427b2017-02-12 13:52:30 -080010019struct lpfc_sli_ring *
10020lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -040010021{
James Smartc4908502019-01-28 11:14:28 -080010022 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -080010023
James Smart895427b2017-02-12 13:52:30 -080010024 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -080010025 if (unlikely(!phba->sli4_hba.hdwq))
James Smart7370d102019-01-28 11:14:20 -080010026 return NULL;
10027 /*
10028 * for abort iocb hba_wqidx should already
10029 * be setup based on what work queue we used.
10030 */
10031 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc4908502019-01-28 11:14:28 -080010032 lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
James Smart1fbf9742019-01-28 11:14:26 -080010033 piocb->hba_wqidx = lpfc_cmd->hdwq_no;
James Smart9bd2bff52014-09-03 12:57:30 -040010034 }
James Smartcdb42be2019-01-28 11:14:21 -080010035 return phba->sli4_hba.hdwq[piocb->hba_wqidx].fcp_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010036 } else {
10037 if (unlikely(!phba->sli4_hba.els_wq))
10038 return NULL;
10039 piocb->hba_wqidx = 0;
10040 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -040010041 }
James Smart9bd2bff52014-09-03 12:57:30 -040010042}
10043
James Smarte59058c2008-08-24 21:49:00 -040010044/**
James Smart3621a712009-04-06 18:47:14 -040010045 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040010046 * @phba: Pointer to HBA context object.
10047 * @pring: Pointer to driver SLI ring object.
10048 * @piocb: Pointer to command iocb.
10049 * @flag: Flag indicating if this command can be put into txq.
10050 *
10051 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10052 * function. This function gets the hbalock and calls
10053 * __lpfc_sli_issue_iocb function and will return the error returned
10054 * by __lpfc_sli_issue_iocb function. This wrapper is used by
10055 * functions which do not hold hbalock.
10056 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010057int
James Smart3772a992009-05-22 14:50:54 -040010058lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -050010059 struct lpfc_iocbq *piocb, uint32_t flag)
10060{
James Smart2a76a282012-08-03 12:35:54 -040010061 struct lpfc_sli_ring *pring;
James Smart92d7f7b2007-06-17 19:56:38 -050010062 unsigned long iflags;
James Smart6a828b02019-01-28 11:14:31 -080010063 int rc;
James Smart92d7f7b2007-06-17 19:56:38 -050010064
James Smart7e56aa22012-08-03 12:35:34 -040010065 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010066 pring = lpfc_sli4_calc_ring(phba, piocb);
10067 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010068 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -040010069
James Smart9bd2bff52014-09-03 12:57:30 -040010070 spin_lock_irqsave(&pring->ring_lock, iflags);
10071 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10072 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart7e56aa22012-08-03 12:35:34 -040010073 } else {
10074 /* For now, SLI2/3 will still use hbalock */
10075 spin_lock_irqsave(&phba->hbalock, iflags);
10076 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10077 spin_unlock_irqrestore(&phba->hbalock, iflags);
10078 }
James Smart92d7f7b2007-06-17 19:56:38 -050010079 return rc;
10080}
10081
James Smarte59058c2008-08-24 21:49:00 -040010082/**
James Smart3621a712009-04-06 18:47:14 -040010083 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010084 * @phba: Pointer to HBA context object.
10085 *
10086 * This function is called while driver attaches with the
10087 * HBA to setup the extra ring. The extra ring is used
10088 * only when driver needs to support target mode functionality
10089 * or IP over FC functionalities.
10090 *
James Smart895427b2017-02-12 13:52:30 -080010091 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -040010092 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010093static int
10094lpfc_extra_ring_setup( struct lpfc_hba *phba)
10095{
10096 struct lpfc_sli *psli;
10097 struct lpfc_sli_ring *pring;
10098
10099 psli = &phba->sli;
10100
10101 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -050010102
10103 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -080010104 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -040010105 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10106 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10107 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10108 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010109
James Smarta4bc3372006-12-02 13:34:16 -050010110 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -080010111 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -050010112
James Smart7e56aa22012-08-03 12:35:34 -040010113 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10114 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10115 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10116 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010117
10118 /* Setup default profile for this ring */
10119 pring->iotag_max = 4096;
10120 pring->num_mask = 1;
10121 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -050010122 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10123 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010124 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10125 return 0;
10126}
10127
James Smartcb69f7d2011-12-13 13:21:57 -050010128/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10129 * @phba: Pointer to HBA context object.
10130 * @iocbq: Pointer to iocb object.
10131 *
10132 * The async_event handler calls this routine when it receives
10133 * an ASYNC_STATUS_CN event from the port. The port generates
10134 * this event when an Abort Sequence request to an rport fails
10135 * twice in succession. The abort could be originated by the
10136 * driver or by the port. The ABTS could have been for an ELS
10137 * or FCP IO. The port only generates this event when an ABTS
10138 * fails to complete after one retry.
10139 */
10140static void
10141lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10142 struct lpfc_iocbq *iocbq)
10143{
10144 struct lpfc_nodelist *ndlp = NULL;
10145 uint16_t rpi = 0, vpi = 0;
10146 struct lpfc_vport *vport = NULL;
10147
10148 /* The rpi in the ulpContext is vport-sensitive. */
10149 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10150 rpi = iocbq->iocb.ulpContext;
10151
10152 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10153 "3092 Port generated ABTS async event "
10154 "on vpi %d rpi %d status 0x%x\n",
10155 vpi, rpi, iocbq->iocb.ulpStatus);
10156
10157 vport = lpfc_find_vport_by_vpid(phba, vpi);
10158 if (!vport)
10159 goto err_exit;
10160 ndlp = lpfc_findnode_rpi(vport, rpi);
10161 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
10162 goto err_exit;
10163
10164 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10165 lpfc_sli_abts_recover_port(vport, ndlp);
10166 return;
10167
10168 err_exit:
10169 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10170 "3095 Event Context not found, no "
10171 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10172 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10173 vpi, rpi);
10174}
10175
10176/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10177 * @phba: pointer to HBA context object.
10178 * @ndlp: nodelist pointer for the impacted rport.
10179 * @axri: pointer to the wcqe containing the failed exchange.
10180 *
10181 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10182 * port. The port generates this event when an abort exchange request to an
10183 * rport fails twice in succession with no reply. The abort could be originated
10184 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
10185 */
10186void
10187lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10188 struct lpfc_nodelist *ndlp,
10189 struct sli4_wcqe_xri_aborted *axri)
10190{
10191 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -050010192 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -050010193
James Smart6b5151f2012-01-18 16:24:06 -050010194 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -050010195 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10196 "3115 Node Context not found, driver "
10197 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -050010198 return;
10199 }
10200
James Smartcb69f7d2011-12-13 13:21:57 -050010201 vport = ndlp->vport;
10202 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10203 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -050010204 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -040010205 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -050010206 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -050010207 bf_get(lpfc_wcqe_xa_status, axri),
10208 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -050010209
James Smart5c1db2a2012-03-01 22:34:36 -050010210 /*
10211 * Catch the ABTS protocol failure case. Older OCe FW releases returned
10212 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10213 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10214 */
James Smarte3d2b802012-08-14 14:25:43 -040010215 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -050010216 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10217 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -050010218 lpfc_sli_abts_recover_port(vport, ndlp);
10219}
10220
James Smarte59058c2008-08-24 21:49:00 -040010221/**
James Smart3621a712009-04-06 18:47:14 -040010222 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -040010223 * @phba: Pointer to HBA context object.
10224 * @pring: Pointer to driver SLI ring object.
10225 * @iocbq: Pointer to iocb object.
10226 *
10227 * This function is called by the slow ring event handler
10228 * function when there is an ASYNC event iocb in the ring.
10229 * This function is called with no lock held.
10230 * Currently this function handles only temperature related
10231 * ASYNC events. The function decodes the temperature sensor
10232 * event message and posts events for the management applications.
10233 **/
James Smart98c9ea52007-10-27 13:37:33 -040010234static void
James Smart57127f12007-10-27 13:37:05 -040010235lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10236 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10237{
10238 IOCB_t *icmd;
10239 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -040010240 struct temp_event temp_event_data;
10241 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -040010242 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -040010243
10244 icmd = &iocbq->iocb;
10245 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -040010246
James Smartcb69f7d2011-12-13 13:21:57 -050010247 switch (evt_code) {
10248 case ASYNC_TEMP_WARN:
10249 case ASYNC_TEMP_SAFE:
10250 temp_event_data.data = (uint32_t) icmd->ulpContext;
10251 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10252 if (evt_code == ASYNC_TEMP_WARN) {
10253 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10254 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10255 "0347 Adapter is very hot, please take "
10256 "corrective action. temperature : %d Celsius\n",
10257 (uint32_t) icmd->ulpContext);
10258 } else {
10259 temp_event_data.event_code = LPFC_NORMAL_TEMP;
10260 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10261 "0340 Adapter temperature is OK now. "
10262 "temperature : %d Celsius\n",
10263 (uint32_t) icmd->ulpContext);
10264 }
10265
10266 /* Send temperature change event to applications */
10267 shost = lpfc_shost_from_vport(phba->pport);
10268 fc_host_post_vendor_event(shost, fc_get_event_number(),
10269 sizeof(temp_event_data), (char *) &temp_event_data,
10270 LPFC_NL_VENDOR_ID);
10271 break;
10272 case ASYNC_STATUS_CN:
10273 lpfc_sli_abts_err_handler(phba, iocbq);
10274 break;
10275 default:
James Smarta257bf92009-04-06 18:48:10 -040010276 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -050010277 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -040010278 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -040010279 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -040010280 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
10281 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
10282 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
10283 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -050010284 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -040010285 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10286 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10287 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10288 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10289
James Smartcb69f7d2011-12-13 13:21:57 -050010290 break;
James Smart57127f12007-10-27 13:37:05 -040010291 }
James Smart57127f12007-10-27 13:37:05 -040010292}
10293
10294
James Smarte59058c2008-08-24 21:49:00 -040010295/**
James Smart895427b2017-02-12 13:52:30 -080010296 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010297 * @phba: Pointer to HBA context object.
10298 *
10299 * lpfc_sli_setup sets up rings of the SLI interface with
10300 * number of iocbs per ring and iotags. This function is
10301 * called while driver attach to the HBA and before the
10302 * interrupts are enabled. So there is no need for locking.
10303 *
10304 * This function always returns 0.
10305 **/
dea31012005-04-17 16:05:31 -050010306int
James Smart895427b2017-02-12 13:52:30 -080010307lpfc_sli4_setup(struct lpfc_hba *phba)
10308{
10309 struct lpfc_sli_ring *pring;
10310
10311 pring = phba->sli4_hba.els_wq->pring;
10312 pring->num_mask = LPFC_MAX_RING_MASK;
10313 pring->prt[0].profile = 0; /* Mask 0 */
10314 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10315 pring->prt[0].type = FC_TYPE_ELS;
10316 pring->prt[0].lpfc_sli_rcv_unsol_event =
10317 lpfc_els_unsol_event;
10318 pring->prt[1].profile = 0; /* Mask 1 */
10319 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10320 pring->prt[1].type = FC_TYPE_ELS;
10321 pring->prt[1].lpfc_sli_rcv_unsol_event =
10322 lpfc_els_unsol_event;
10323 pring->prt[2].profile = 0; /* Mask 2 */
10324 /* NameServer Inquiry */
10325 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10326 /* NameServer */
10327 pring->prt[2].type = FC_TYPE_CT;
10328 pring->prt[2].lpfc_sli_rcv_unsol_event =
10329 lpfc_ct_unsol_event;
10330 pring->prt[3].profile = 0; /* Mask 3 */
10331 /* NameServer response */
10332 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10333 /* NameServer */
10334 pring->prt[3].type = FC_TYPE_CT;
10335 pring->prt[3].lpfc_sli_rcv_unsol_event =
10336 lpfc_ct_unsol_event;
10337 return 0;
10338}
10339
10340/**
10341 * lpfc_sli_setup - SLI ring setup function
10342 * @phba: Pointer to HBA context object.
10343 *
10344 * lpfc_sli_setup sets up rings of the SLI interface with
10345 * number of iocbs per ring and iotags. This function is
10346 * called while driver attach to the HBA and before the
10347 * interrupts are enabled. So there is no need for locking.
10348 *
10349 * This function always returns 0. SLI3 only.
10350 **/
10351int
dea31012005-04-17 16:05:31 -050010352lpfc_sli_setup(struct lpfc_hba *phba)
10353{
James Smarted957682007-06-17 19:56:37 -050010354 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050010355 struct lpfc_sli *psli = &phba->sli;
10356 struct lpfc_sli_ring *pring;
10357
James Smart2a76a282012-08-03 12:35:54 -040010358 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010359 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010360
James Bottomley604a3e32005-10-29 10:28:33 -050010361 psli->iocbq_lookup = NULL;
10362 psli->iocbq_lookup_len = 0;
10363 psli->last_iotag = 0;
10364
dea31012005-04-17 16:05:31 -050010365 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010366 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010367 switch (i) {
10368 case LPFC_FCP_RING: /* ring 0 - FCP */
10369 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010370 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10371 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10372 pring->sli.sli3.numCiocb +=
10373 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10374 pring->sli.sli3.numRiocb +=
10375 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10376 pring->sli.sli3.numCiocb +=
10377 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10378 pring->sli.sli3.numRiocb +=
10379 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10380 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010381 SLI3_IOCB_CMD_SIZE :
10382 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010383 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010384 SLI3_IOCB_RSP_SIZE :
10385 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010386 pring->iotag_ctr = 0;
10387 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010388 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010389 pring->fast_iotag = pring->iotag_max;
10390 pring->num_mask = 0;
10391 break;
James Smarta4bc3372006-12-02 13:34:16 -050010392 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010393 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010394 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10395 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10396 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010397 SLI3_IOCB_CMD_SIZE :
10398 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010399 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010400 SLI3_IOCB_RSP_SIZE :
10401 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010402 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010403 pring->num_mask = 0;
10404 break;
10405 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10406 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010407 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10408 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10409 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010410 SLI3_IOCB_CMD_SIZE :
10411 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010412 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010413 SLI3_IOCB_RSP_SIZE :
10414 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010415 pring->fast_iotag = 0;
10416 pring->iotag_ctr = 0;
10417 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010418 pring->lpfc_sli_rcv_async_status =
10419 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010420 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010421 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010422 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10423 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010424 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010425 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010426 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010427 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10428 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010429 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010430 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010431 pring->prt[2].profile = 0; /* Mask 2 */
10432 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010433 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010434 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010435 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010436 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010437 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010438 pring->prt[3].profile = 0; /* Mask 3 */
10439 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010440 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010441 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010442 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010443 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010444 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010445 break;
10446 }
James Smart7e56aa22012-08-03 12:35:34 -040010447 totiocbsize += (pring->sli.sli3.numCiocb *
10448 pring->sli.sli3.sizeCiocb) +
10449 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010450 }
James Smarted957682007-06-17 19:56:37 -050010451 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010452 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010453 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10454 "SLI2 SLIM Data: x%x x%lx\n",
10455 phba->brd_no, totiocbsize,
10456 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010457 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010458 if (phba->cfg_multi_ring_support == 2)
10459 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010460
10461 return 0;
10462}
10463
James Smarte59058c2008-08-24 21:49:00 -040010464/**
James Smart895427b2017-02-12 13:52:30 -080010465 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010466 * @phba: Pointer to HBA context object.
10467 *
James Smart895427b2017-02-12 13:52:30 -080010468 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010469 * ring. This function also initializes ring indices of each ring.
10470 * This function is called during the initialization of the SLI
10471 * interface of an HBA.
10472 * This function is called with no lock held and always returns
10473 * 1.
10474 **/
James Smart895427b2017-02-12 13:52:30 -080010475void
10476lpfc_sli4_queue_init(struct lpfc_hba *phba)
10477{
10478 struct lpfc_sli *psli;
10479 struct lpfc_sli_ring *pring;
10480 int i;
10481
10482 psli = &phba->sli;
10483 spin_lock_irq(&phba->hbalock);
10484 INIT_LIST_HEAD(&psli->mboxq);
10485 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10486 /* Initialize list headers for txq and txcmplq as double linked lists */
James Smartcdb42be2019-01-28 11:14:21 -080010487 for (i = 0; i < phba->cfg_hdw_queue; i++) {
10488 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010489 pring->flag = 0;
10490 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080010491 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010492 INIT_LIST_HEAD(&pring->txq);
10493 INIT_LIST_HEAD(&pring->txcmplq);
10494 INIT_LIST_HEAD(&pring->iocb_continueq);
10495 spin_lock_init(&pring->ring_lock);
10496 }
10497 pring = phba->sli4_hba.els_wq->pring;
10498 pring->flag = 0;
10499 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010500 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010501 INIT_LIST_HEAD(&pring->txq);
10502 INIT_LIST_HEAD(&pring->txcmplq);
10503 INIT_LIST_HEAD(&pring->iocb_continueq);
10504 spin_lock_init(&pring->ring_lock);
10505
James Smartcdb42be2019-01-28 11:14:21 -080010506 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10507 for (i = 0; i < phba->cfg_hdw_queue; i++) {
10508 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
James Smartc4908502019-01-28 11:14:28 -080010509 pring->flag = 0;
James Smartcdb42be2019-01-28 11:14:21 -080010510 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080010511 pring->txcmplq_cnt = 0;
James Smartcdb42be2019-01-28 11:14:21 -080010512 INIT_LIST_HEAD(&pring->txq);
10513 INIT_LIST_HEAD(&pring->txcmplq);
10514 INIT_LIST_HEAD(&pring->iocb_continueq);
10515 spin_lock_init(&pring->ring_lock);
10516 }
James Smart895427b2017-02-12 13:52:30 -080010517 pring = phba->sli4_hba.nvmels_wq->pring;
10518 pring->flag = 0;
10519 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010520 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010521 INIT_LIST_HEAD(&pring->txq);
10522 INIT_LIST_HEAD(&pring->txcmplq);
10523 INIT_LIST_HEAD(&pring->iocb_continueq);
10524 spin_lock_init(&pring->ring_lock);
10525 }
10526
James Smart895427b2017-02-12 13:52:30 -080010527 spin_unlock_irq(&phba->hbalock);
10528}
10529
10530/**
10531 * lpfc_sli_queue_init - Queue initialization function
10532 * @phba: Pointer to HBA context object.
10533 *
10534 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10535 * ring. This function also initializes ring indices of each ring.
10536 * This function is called during the initialization of the SLI
10537 * interface of an HBA.
10538 * This function is called with no lock held and always returns
10539 * 1.
10540 **/
10541void
10542lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010543{
10544 struct lpfc_sli *psli;
10545 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010546 int i;
dea31012005-04-17 16:05:31 -050010547
10548 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010549 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010550 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010551 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010552 /* Initialize list headers for txq and txcmplq as double linked lists */
10553 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010554 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010555 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010556 pring->sli.sli3.next_cmdidx = 0;
10557 pring->sli.sli3.local_getidx = 0;
10558 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010559 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010560 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010561 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010562 pring->flag = 0;
10563 INIT_LIST_HEAD(&pring->txq);
10564 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010565 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010566 }
James Smart2e0fef82007-06-17 19:56:36 -050010567 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010568}
10569
James Smarte59058c2008-08-24 21:49:00 -040010570/**
James Smart04c68492009-05-22 14:52:52 -040010571 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10572 * @phba: Pointer to HBA context object.
10573 *
10574 * This routine flushes the mailbox command subsystem. It will unconditionally
10575 * flush all the mailbox commands in the three possible stages in the mailbox
10576 * command sub-system: pending mailbox command queue; the outstanding mailbox
10577 * command; and completed mailbox command queue. It is caller's responsibility
10578 * to make sure that the driver is in the proper state to flush the mailbox
10579 * command sub-system. Namely, the posting of mailbox commands into the
10580 * pending mailbox command queue from the various clients must be stopped;
10581 * either the HBA is in a state that it will never works on the outstanding
10582 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10583 * mailbox command has been completed.
10584 **/
10585static void
10586lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10587{
10588 LIST_HEAD(completions);
10589 struct lpfc_sli *psli = &phba->sli;
10590 LPFC_MBOXQ_t *pmb;
10591 unsigned long iflag;
10592
James Smart523128e2018-09-10 10:30:46 -070010593 /* Disable softirqs, including timers from obtaining phba->hbalock */
10594 local_bh_disable();
10595
James Smart04c68492009-05-22 14:52:52 -040010596 /* Flush all the mailbox commands in the mbox system */
10597 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart523128e2018-09-10 10:30:46 -070010598
James Smart04c68492009-05-22 14:52:52 -040010599 /* The pending mailbox command queue */
10600 list_splice_init(&phba->sli.mboxq, &completions);
10601 /* The outstanding active mailbox command */
10602 if (psli->mbox_active) {
10603 list_add_tail(&psli->mbox_active->list, &completions);
10604 psli->mbox_active = NULL;
10605 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10606 }
10607 /* The completed mailbox command queue */
10608 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10609 spin_unlock_irqrestore(&phba->hbalock, iflag);
10610
James Smart523128e2018-09-10 10:30:46 -070010611 /* Enable softirqs again, done with phba->hbalock */
10612 local_bh_enable();
10613
James Smart04c68492009-05-22 14:52:52 -040010614 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10615 while (!list_empty(&completions)) {
10616 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10617 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10618 if (pmb->mbox_cmpl)
10619 pmb->mbox_cmpl(phba, pmb);
10620 }
10621}
10622
10623/**
James Smart3621a712009-04-06 18:47:14 -040010624 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010625 * @vport: Pointer to virtual port object.
10626 *
10627 * lpfc_sli_host_down is called to clean up the resources
10628 * associated with a vport before destroying virtual
10629 * port data structures.
10630 * This function does following operations:
10631 * - Free discovery resources associated with this virtual
10632 * port.
10633 * - Free iocbs associated with this virtual port in
10634 * the txq.
10635 * - Send abort for all iocb commands associated with this
10636 * vport in txcmplq.
10637 *
10638 * This function is called with no lock held and always returns 1.
10639 **/
dea31012005-04-17 16:05:31 -050010640int
James Smart92d7f7b2007-06-17 19:56:38 -050010641lpfc_sli_host_down(struct lpfc_vport *vport)
10642{
James Smart858c9f62007-06-17 19:56:39 -050010643 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010644 struct lpfc_hba *phba = vport->phba;
10645 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010646 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010647 struct lpfc_sli_ring *pring;
10648 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010649 int i;
10650 unsigned long flags = 0;
10651 uint16_t prev_pring_flag;
10652
10653 lpfc_cleanup_discovery_resources(vport);
10654
10655 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010656
James Smart895427b2017-02-12 13:52:30 -080010657 /*
10658 * Error everything on the txq since these iocbs
10659 * have not been given to the FW yet.
10660 * Also issue ABTS for everything on the txcmplq
10661 */
10662 if (phba->sli_rev != LPFC_SLI_REV4) {
10663 for (i = 0; i < psli->num_rings; i++) {
10664 pring = &psli->sli3_ring[i];
10665 prev_pring_flag = pring->flag;
10666 /* Only slow rings */
10667 if (pring->ringno == LPFC_ELS_RING) {
10668 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10669 /* Set the lpfc data pending flag */
10670 set_bit(LPFC_DATA_READY, &phba->data_flags);
10671 }
10672 list_for_each_entry_safe(iocb, next_iocb,
10673 &pring->txq, list) {
10674 if (iocb->vport != vport)
10675 continue;
10676 list_move_tail(&iocb->list, &completions);
10677 }
10678 list_for_each_entry_safe(iocb, next_iocb,
10679 &pring->txcmplq, list) {
10680 if (iocb->vport != vport)
10681 continue;
10682 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10683 }
10684 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010685 }
James Smart895427b2017-02-12 13:52:30 -080010686 } else {
10687 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10688 pring = qp->pring;
10689 if (!pring)
10690 continue;
10691 if (pring == phba->sli4_hba.els_wq->pring) {
10692 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10693 /* Set the lpfc data pending flag */
10694 set_bit(LPFC_DATA_READY, &phba->data_flags);
10695 }
10696 prev_pring_flag = pring->flag;
10697 spin_lock_irq(&pring->ring_lock);
10698 list_for_each_entry_safe(iocb, next_iocb,
10699 &pring->txq, list) {
10700 if (iocb->vport != vport)
10701 continue;
10702 list_move_tail(&iocb->list, &completions);
10703 }
10704 spin_unlock_irq(&pring->ring_lock);
10705 list_for_each_entry_safe(iocb, next_iocb,
10706 &pring->txcmplq, list) {
10707 if (iocb->vport != vport)
10708 continue;
10709 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10710 }
10711 pring->flag = prev_pring_flag;
10712 }
James Smart92d7f7b2007-06-17 19:56:38 -050010713 }
James Smart92d7f7b2007-06-17 19:56:38 -050010714 spin_unlock_irqrestore(&phba->hbalock, flags);
10715
James Smarta257bf92009-04-06 18:48:10 -040010716 /* Cancel all the IOCBs from the completions list */
10717 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10718 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010719 return 1;
10720}
10721
James Smarte59058c2008-08-24 21:49:00 -040010722/**
James Smart3621a712009-04-06 18:47:14 -040010723 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010724 * @phba: Pointer to HBA context object.
10725 *
10726 * This function cleans up all iocb, buffers, mailbox commands
10727 * while shutting down the HBA. This function is called with no
10728 * lock held and always returns 1.
10729 * This function does the following to cleanup driver resources:
10730 * - Free discovery resources for each virtual port
10731 * - Cleanup any pending fabric iocbs
10732 * - Iterate through the iocb txq and free each entry
10733 * in the list.
10734 * - Free up any buffer posted to the HBA
10735 * - Free mailbox commands in the mailbox queue.
10736 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010737int
James Smart2e0fef82007-06-17 19:56:36 -050010738lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010739{
James Smart2534ba72007-04-25 09:52:20 -040010740 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010741 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010742 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010743 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010744 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010745 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010746 int i;
10747
10748 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010749 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010750
dea31012005-04-17 16:05:31 -050010751 lpfc_hba_down_prep(phba);
10752
James Smart523128e2018-09-10 10:30:46 -070010753 /* Disable softirqs, including timers from obtaining phba->hbalock */
10754 local_bh_disable();
10755
James Smart92d7f7b2007-06-17 19:56:38 -050010756 lpfc_fabric_abort_hba(phba);
10757
James Smart2e0fef82007-06-17 19:56:36 -050010758 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010759
James Smart895427b2017-02-12 13:52:30 -080010760 /*
10761 * Error everything on the txq since these iocbs
10762 * have not been given to the FW yet.
10763 */
10764 if (phba->sli_rev != LPFC_SLI_REV4) {
10765 for (i = 0; i < psli->num_rings; i++) {
10766 pring = &psli->sli3_ring[i];
10767 /* Only slow rings */
10768 if (pring->ringno == LPFC_ELS_RING) {
10769 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10770 /* Set the lpfc data pending flag */
10771 set_bit(LPFC_DATA_READY, &phba->data_flags);
10772 }
10773 list_splice_init(&pring->txq, &completions);
10774 }
10775 } else {
10776 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10777 pring = qp->pring;
10778 if (!pring)
10779 continue;
10780 spin_lock_irq(&pring->ring_lock);
10781 list_splice_init(&pring->txq, &completions);
10782 spin_unlock_irq(&pring->ring_lock);
10783 if (pring == phba->sli4_hba.els_wq->pring) {
10784 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10785 /* Set the lpfc data pending flag */
10786 set_bit(LPFC_DATA_READY, &phba->data_flags);
10787 }
10788 }
dea31012005-04-17 16:05:31 -050010789 }
James Smart2e0fef82007-06-17 19:56:36 -050010790 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010791
James Smarta257bf92009-04-06 18:48:10 -040010792 /* Cancel all the IOCBs from the completions list */
10793 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10794 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010795
James Smart0ff10d42008-01-11 01:52:36 -050010796 spin_lock_irqsave(&phba->hbalock, flags);
10797 list_splice_init(&phba->elsbuf, &completions);
10798 phba->elsbuf_cnt = 0;
10799 phba->elsbuf_prev_cnt = 0;
10800 spin_unlock_irqrestore(&phba->hbalock, flags);
10801
10802 while (!list_empty(&completions)) {
10803 list_remove_head(&completions, buf_ptr,
10804 struct lpfc_dmabuf, list);
10805 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10806 kfree(buf_ptr);
10807 }
10808
James Smart523128e2018-09-10 10:30:46 -070010809 /* Enable softirqs again, done with phba->hbalock */
10810 local_bh_enable();
10811
dea31012005-04-17 16:05:31 -050010812 /* Return any active mbox cmds */
10813 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010814
James Smartda0436e2009-05-22 14:51:39 -040010815 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010816 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010817 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010818
James Smartda0436e2009-05-22 14:51:39 -040010819 return 1;
10820}
James Smart92d7f7b2007-06-17 19:56:38 -050010821
James Smartda0436e2009-05-22 14:51:39 -040010822/**
James Smart3621a712009-04-06 18:47:14 -040010823 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010824 * @srcp: Source memory pointer.
10825 * @destp: Destination memory pointer.
10826 * @cnt: Number of words required to be copied.
10827 *
10828 * This function is used for copying data between driver memory
10829 * and the SLI memory. This function also changes the endianness
10830 * of each word if native endianness is different from SLI
10831 * endianness. This function can be called with or without
10832 * lock.
10833 **/
dea31012005-04-17 16:05:31 -050010834void
10835lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10836{
10837 uint32_t *src = srcp;
10838 uint32_t *dest = destp;
10839 uint32_t ldata;
10840 int i;
10841
10842 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10843 ldata = *src;
10844 ldata = le32_to_cpu(ldata);
10845 *dest = ldata;
10846 src++;
10847 dest++;
10848 }
10849}
10850
James Smarte59058c2008-08-24 21:49:00 -040010851
10852/**
James Smarta0c87cb2009-07-19 10:01:10 -040010853 * lpfc_sli_bemem_bcopy - SLI memory copy function
10854 * @srcp: Source memory pointer.
10855 * @destp: Destination memory pointer.
10856 * @cnt: Number of words required to be copied.
10857 *
10858 * This function is used for copying data between a data structure
10859 * with big endian representation to local endianness.
10860 * This function can be called with or without lock.
10861 **/
10862void
10863lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10864{
10865 uint32_t *src = srcp;
10866 uint32_t *dest = destp;
10867 uint32_t ldata;
10868 int i;
10869
10870 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10871 ldata = *src;
10872 ldata = be32_to_cpu(ldata);
10873 *dest = ldata;
10874 src++;
10875 dest++;
10876 }
10877}
10878
10879/**
James Smart3621a712009-04-06 18:47:14 -040010880 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010881 * @phba: Pointer to HBA context object.
10882 * @pring: Pointer to driver SLI ring object.
10883 * @mp: Pointer to driver buffer object.
10884 *
10885 * This function is called with no lock held.
10886 * It always return zero after adding the buffer to the postbufq
10887 * buffer list.
10888 **/
dea31012005-04-17 16:05:31 -050010889int
James Smart2e0fef82007-06-17 19:56:36 -050010890lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10891 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010892{
10893 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10894 later */
James Smart2e0fef82007-06-17 19:56:36 -050010895 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010896 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010897 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010898 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010899 return 0;
10900}
10901
James Smarte59058c2008-08-24 21:49:00 -040010902/**
James Smart3621a712009-04-06 18:47:14 -040010903 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010904 * @phba: Pointer to HBA context object.
10905 *
10906 * When HBQ is enabled, buffers are searched based on tags. This function
10907 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10908 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10909 * does not conflict with tags of buffer posted for unsolicited events.
10910 * The function returns the allocated tag. The function is called with
10911 * no locks held.
10912 **/
James Smart76bb24e2007-10-27 13:38:00 -040010913uint32_t
10914lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10915{
10916 spin_lock_irq(&phba->hbalock);
10917 phba->buffer_tag_count++;
10918 /*
10919 * Always set the QUE_BUFTAG_BIT to distiguish between
10920 * a tag assigned by HBQ.
10921 */
10922 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10923 spin_unlock_irq(&phba->hbalock);
10924 return phba->buffer_tag_count;
10925}
10926
James Smarte59058c2008-08-24 21:49:00 -040010927/**
James Smart3621a712009-04-06 18:47:14 -040010928 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010929 * @phba: Pointer to HBA context object.
10930 * @pring: Pointer to driver SLI ring object.
10931 * @tag: Buffer tag.
10932 *
10933 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10934 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10935 * iocb is posted to the response ring with the tag of the buffer.
10936 * This function searches the pring->postbufq list using the tag
10937 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10938 * iocb. If the buffer is found then lpfc_dmabuf object of the
10939 * buffer is returned to the caller else NULL is returned.
10940 * This function is called with no lock held.
10941 **/
James Smart76bb24e2007-10-27 13:38:00 -040010942struct lpfc_dmabuf *
10943lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10944 uint32_t tag)
10945{
10946 struct lpfc_dmabuf *mp, *next_mp;
10947 struct list_head *slp = &pring->postbufq;
10948
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010949 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010950 spin_lock_irq(&phba->hbalock);
10951 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10952 if (mp->buffer_tag == tag) {
10953 list_del_init(&mp->list);
10954 pring->postbufq_cnt--;
10955 spin_unlock_irq(&phba->hbalock);
10956 return mp;
10957 }
10958 }
10959
10960 spin_unlock_irq(&phba->hbalock);
10961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010962 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010963 "ring %d Data x%lx x%p x%p x%x\n",
10964 pring->ringno, (unsigned long) tag,
10965 slp->next, slp->prev, pring->postbufq_cnt);
10966
10967 return NULL;
10968}
dea31012005-04-17 16:05:31 -050010969
James Smarte59058c2008-08-24 21:49:00 -040010970/**
James Smart3621a712009-04-06 18:47:14 -040010971 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010972 * @phba: Pointer to HBA context object.
10973 * @pring: Pointer to driver SLI ring object.
10974 * @phys: DMA address of the buffer.
10975 *
10976 * This function searches the buffer list using the dma_address
10977 * of unsolicited event to find the driver's lpfc_dmabuf object
10978 * corresponding to the dma_address. The function returns the
10979 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10980 * This function is called by the ct and els unsolicited event
10981 * handlers to get the buffer associated with the unsolicited
10982 * event.
10983 *
10984 * This function is called with no lock held.
10985 **/
dea31012005-04-17 16:05:31 -050010986struct lpfc_dmabuf *
10987lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10988 dma_addr_t phys)
10989{
10990 struct lpfc_dmabuf *mp, *next_mp;
10991 struct list_head *slp = &pring->postbufq;
10992
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010993 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010994 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010995 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10996 if (mp->phys == phys) {
10997 list_del_init(&mp->list);
10998 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010999 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011000 return mp;
11001 }
11002 }
11003
James Smart2e0fef82007-06-17 19:56:36 -050011004 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040011006 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050011007 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011008 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050011009 slp->next, slp->prev, pring->postbufq_cnt);
11010 return NULL;
11011}
11012
James Smarte59058c2008-08-24 21:49:00 -040011013/**
James Smart3621a712009-04-06 18:47:14 -040011014 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040011015 * @phba: Pointer to HBA context object.
11016 * @cmdiocb: Pointer to driver command iocb object.
11017 * @rspiocb: Pointer to driver response iocb object.
11018 *
11019 * This function is the completion handler for the abort iocbs for
11020 * ELS commands. This function is called from the ELS ring event
11021 * handler with no lock held. This function frees memory resources
11022 * associated with the abort iocb.
11023 **/
dea31012005-04-17 16:05:31 -050011024static void
James Smart2e0fef82007-06-17 19:56:36 -050011025lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11026 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050011027{
James Smart2e0fef82007-06-17 19:56:36 -050011028 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040011029 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050011030 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040011031
11032 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050011033
11034 /*
11035 * Assume that the port already completed and returned, or
11036 * will return the iocb. Just Log the message.
11037 */
James Smart2680eea2007-04-25 09:52:55 -040011038 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11039 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11040
James Smart2e0fef82007-06-17 19:56:36 -050011041 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040011042 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smartfaa832e2018-07-31 17:23:18 -070011043 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11044 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11045 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11046 spin_unlock_irq(&phba->hbalock);
11047 goto release_iocb;
11048 }
James Smart45ed1192009-10-02 15:17:02 -040011049 if (abort_iotag != 0 &&
11050 abort_iotag <= phba->sli.last_iotag)
11051 abort_iocb =
11052 phba->sli.iocbq_lookup[abort_iotag];
11053 } else
11054 /* For sli4 the abort_tag is the XRI,
11055 * so the abort routine puts the iotag of the iocb
11056 * being aborted in the context field of the abort
11057 * IOCB.
11058 */
11059 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040011060
James Smart2a9bf3d2010-06-07 15:24:45 -040011061 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
11062 "0327 Cannot abort els iocb %p "
11063 "with tag %x context %x, abort status %x, "
11064 "abort code %x\n",
11065 abort_iocb, abort_iotag, abort_context,
11066 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040011067
James Smartff78d8f2011-12-13 13:21:35 -050011068 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040011069 }
James Smartfaa832e2018-07-31 17:23:18 -070011070release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050011071 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050011072 return;
11073}
11074
James Smarte59058c2008-08-24 21:49:00 -040011075/**
James Smart3621a712009-04-06 18:47:14 -040011076 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040011077 * @phba: Pointer to HBA context object.
11078 * @cmdiocb: Pointer to driver command iocb object.
11079 * @rspiocb: Pointer to driver response iocb object.
11080 *
11081 * The function is called from SLI ring event handler with no
11082 * lock held. This function is the completion handler for ELS commands
11083 * which are aborted. The function frees memory resources used for
11084 * the aborted ELS commands.
11085 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011086static void
11087lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11088 struct lpfc_iocbq *rspiocb)
11089{
11090 IOCB_t *irsp = &rspiocb->iocb;
11091
11092 /* ELS cmd tag <ulpIoTag> completes */
11093 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040011094 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050011095 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011096 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050011097 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050011098 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11099 lpfc_ct_free_iocb(phba, cmdiocb);
11100 else
11101 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050011102 return;
11103}
11104
James Smarte59058c2008-08-24 21:49:00 -040011105/**
James Smart5af5eee2010-10-22 11:06:38 -040011106 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040011107 * @phba: Pointer to HBA context object.
11108 * @pring: Pointer to driver SLI ring object.
11109 * @cmdiocb: Pointer to driver command iocb object.
11110 *
James Smart5af5eee2010-10-22 11:06:38 -040011111 * This function issues an abort iocb for the provided command iocb down to
11112 * the port. Other than the case the outstanding command iocb is an abort
11113 * request, this function issues abort out unconditionally. This function is
11114 * called with hbalock held. The function returns 0 when it fails due to
11115 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040011116 **/
James Smart5af5eee2010-10-22 11:06:38 -040011117static int
11118lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050011119 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050011120{
James Smart2e0fef82007-06-17 19:56:36 -050011121 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011122 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050011123 IOCB_t *icmd = NULL;
11124 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011125 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040011126 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070011127 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040011128
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011129 lockdep_assert_held(&phba->hbalock);
11130
James Smart92d7f7b2007-06-17 19:56:38 -050011131 /*
11132 * There are certain command types we don't want to abort. And we
11133 * don't want to abort commands that are already in the process of
11134 * being aborted.
James Smart07951072007-04-25 09:51:38 -040011135 */
11136 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050011137 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050011138 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11139 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040011140 return 0;
11141
dea31012005-04-17 16:05:31 -050011142 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050011143 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011144 if (abtsiocbp == NULL)
11145 return 0;
dea31012005-04-17 16:05:31 -050011146
James Smart07951072007-04-25 09:51:38 -040011147 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050011148 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040011149 */
11150 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11151
dea31012005-04-17 16:05:31 -050011152 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040011153 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11154 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040011155 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040011156 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040011157 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070011158 } else {
James Smartda0436e2009-05-22 14:51:39 -040011159 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070011160 if (pring->ringno == LPFC_ELS_RING) {
11161 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11162 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11163 }
11164 }
dea31012005-04-17 16:05:31 -050011165 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040011166 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050011167
James Smart5ffc2662009-11-18 15:39:44 -050011168 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011169 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011170 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
11171 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011172 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11173 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011174
James Smart2e0fef82007-06-17 19:56:36 -050011175 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040011176 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11177 else
11178 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11179
11180 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080011181 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040011182
James Smarte8b62012007-08-02 11:10:09 -040011183 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11184 "0339 Abort xri x%x, original iotag x%x, "
11185 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040011186 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040011187 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040011188 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040011189
11190 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011191 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11192 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040011193 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040011194 /* Note: both hbalock and ring_lock need to be set here */
11195 spin_lock_irqsave(&pring->ring_lock, iflags);
11196 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11197 abtsiocbp, 0);
11198 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11199 } else {
11200 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11201 abtsiocbp, 0);
11202 }
James Smart07951072007-04-25 09:51:38 -040011203
James Smartd7c255b2008-08-24 21:50:00 -040011204 if (retval)
11205 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040011206
11207 /*
11208 * Caller to this routine should check for IOCB_ERROR
11209 * and handle it properly. This routine no longer removes
11210 * iocb off txcmplq and call compl in case of IOCB_ERROR.
11211 */
11212 return retval;
11213}
11214
11215/**
11216 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11217 * @phba: Pointer to HBA context object.
11218 * @pring: Pointer to driver SLI ring object.
11219 * @cmdiocb: Pointer to driver command iocb object.
11220 *
11221 * This function issues an abort iocb for the provided command iocb. In case
11222 * of unloading, the abort iocb will not be issued to commands on the ELS
11223 * ring. Instead, the callback function shall be changed to those commands
11224 * so that nothing happens when them finishes. This function is called with
11225 * hbalock held. The function returns 0 when the command iocb is an abort
11226 * request.
11227 **/
11228int
11229lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11230 struct lpfc_iocbq *cmdiocb)
11231{
11232 struct lpfc_vport *vport = cmdiocb->vport;
11233 int retval = IOCB_ERROR;
11234 IOCB_t *icmd = NULL;
11235
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011236 lockdep_assert_held(&phba->hbalock);
11237
James Smart5af5eee2010-10-22 11:06:38 -040011238 /*
11239 * There are certain command types we don't want to abort. And we
11240 * don't want to abort commands that are already in the process of
11241 * being aborted.
11242 */
11243 icmd = &cmdiocb->iocb;
11244 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11245 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11246 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11247 return 0;
11248
Dick Kennedy1234a6d2017-09-29 17:34:29 -070011249 if (!pring) {
11250 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11251 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11252 else
11253 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11254 goto abort_iotag_exit;
11255 }
11256
James Smart5af5eee2010-10-22 11:06:38 -040011257 /*
11258 * If we're unloading, don't abort iocb on the ELS ring, but change
11259 * the callback so that nothing happens when it finishes.
11260 */
11261 if ((vport->load_flag & FC_UNLOADING) &&
11262 (pring->ringno == LPFC_ELS_RING)) {
11263 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11264 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11265 else
11266 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11267 goto abort_iotag_exit;
11268 }
11269
11270 /* Now, we try to issue the abort to the cmdiocb out */
11271 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
11272
James Smart07951072007-04-25 09:51:38 -040011273abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050011274 /*
11275 * Caller to this routine should check for IOCB_ERROR
11276 * and handle it properly. This routine no longer removes
11277 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040011278 */
James Smart2e0fef82007-06-17 19:56:36 -050011279 return retval;
dea31012005-04-17 16:05:31 -050011280}
11281
James Smarte59058c2008-08-24 21:49:00 -040011282/**
James Smart895427b2017-02-12 13:52:30 -080011283 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
11284 * @phba: Pointer to HBA context object.
11285 * @pring: Pointer to driver SLI ring object.
11286 * @cmdiocb: Pointer to driver command iocb object.
11287 *
11288 * This function issues an abort iocb for the provided command iocb down to
11289 * the port. Other than the case the outstanding command iocb is an abort
11290 * request, this function issues abort out unconditionally. This function is
11291 * called with hbalock held. The function returns 0 when it fails due to
11292 * memory allocation failure or when the command iocb is an abort request.
11293 **/
11294static int
11295lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11296 struct lpfc_iocbq *cmdiocb)
11297{
11298 struct lpfc_vport *vport = cmdiocb->vport;
11299 struct lpfc_iocbq *abtsiocbp;
James Smart205e8242018-03-05 12:04:03 -080011300 union lpfc_wqe128 *abts_wqe;
James Smart895427b2017-02-12 13:52:30 -080011301 int retval;
James Smart1fbf9742019-01-28 11:14:26 -080011302 int idx = cmdiocb->hba_wqidx;
James Smart895427b2017-02-12 13:52:30 -080011303
11304 /*
11305 * There are certain command types we don't want to abort. And we
11306 * don't want to abort commands that are already in the process of
11307 * being aborted.
11308 */
11309 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
11310 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
11311 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11312 return 0;
11313
11314 /* issue ABTS for this io based on iotag */
11315 abtsiocbp = __lpfc_sli_get_iocbq(phba);
11316 if (abtsiocbp == NULL)
11317 return 0;
11318
11319 /* This signals the response to set the correct status
11320 * before calling the completion handler
11321 */
11322 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11323
11324 /* Complete prepping the abort wqe and issue to the FW. */
11325 abts_wqe = &abtsiocbp->wqe;
James Smart1c368332018-11-29 16:09:37 -080011326
11327 /* Clear any stale WQE contents */
11328 memset(abts_wqe, 0, sizeof(union lpfc_wqe));
James Smart895427b2017-02-12 13:52:30 -080011329 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
11330
James Smart895427b2017-02-12 13:52:30 -080011331 /* word 7 */
James Smart895427b2017-02-12 13:52:30 -080011332 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
11333 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
11334 cmdiocb->iocb.ulpClass);
11335
11336 /* word 8 - tell the FW to abort the IO associated with this
11337 * outstanding exchange ID.
11338 */
11339 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
11340
11341 /* word 9 - this is the iotag for the abts_wqe completion. */
11342 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
11343 abtsiocbp->iotag);
11344
11345 /* word 10 */
James Smart895427b2017-02-12 13:52:30 -080011346 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
11347 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
11348
11349 /* word 11 */
11350 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11351 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
11352 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
11353
11354 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11355 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
11356 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080011357 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart1fbf9742019-01-28 11:14:26 -080011358 retval = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[idx],
11359 abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070011360 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080011361 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11362 "6147 Failed abts issue_wqe with status x%x "
11363 "for oxid x%x\n",
11364 retval, cmdiocb->sli4_xritag);
11365 lpfc_sli_release_iocbq(phba, abtsiocbp);
11366 return retval;
11367 }
11368
11369 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11370 "6148 Drv Abort NVME Request Issued for "
11371 "ox_id x%x on reqtag x%x\n",
11372 cmdiocb->sli4_xritag,
11373 abtsiocbp->iotag);
11374
11375 return retval;
11376}
11377
11378/**
James Smart5af5eee2010-10-22 11:06:38 -040011379 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11380 * @phba: pointer to lpfc HBA data structure.
11381 *
11382 * This routine will abort all pending and outstanding iocbs to an HBA.
11383 **/
11384void
11385lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11386{
11387 struct lpfc_sli *psli = &phba->sli;
11388 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011389 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011390 int i;
11391
James Smart895427b2017-02-12 13:52:30 -080011392 if (phba->sli_rev != LPFC_SLI_REV4) {
11393 for (i = 0; i < psli->num_rings; i++) {
11394 pring = &psli->sli3_ring[i];
11395 lpfc_sli_abort_iocb_ring(phba, pring);
11396 }
11397 return;
11398 }
11399 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11400 pring = qp->pring;
11401 if (!pring)
11402 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011403 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011404 }
11405}
11406
11407/**
James Smart3621a712009-04-06 18:47:14 -040011408 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011409 * @iocbq: Pointer to driver iocb object.
11410 * @vport: Pointer to driver virtual port object.
11411 * @tgt_id: SCSI ID of the target.
11412 * @lun_id: LUN ID of the scsi device.
11413 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11414 *
James Smart3621a712009-04-06 18:47:14 -040011415 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011416 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11417 * 0 if the filtering criteria is met for the given iocb and will return
11418 * 1 if the filtering criteria is not met.
11419 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11420 * given iocb is for the SCSI device specified by vport, tgt_id and
11421 * lun_id parameter.
11422 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11423 * given iocb is for the SCSI target specified by vport and tgt_id
11424 * parameters.
11425 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11426 * given iocb is for the SCSI host associated with the given vport.
11427 * This function is called with no locks held.
11428 **/
dea31012005-04-17 16:05:31 -050011429static int
James Smart51ef4c22007-08-02 11:10:31 -040011430lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11431 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011432 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011433{
James Smartc4908502019-01-28 11:14:28 -080011434 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011435 int rc = 1;
11436
James Smartb0e83012018-06-26 08:24:29 -070011437 if (iocbq->vport != vport)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011438 return rc;
11439
James Smartb0e83012018-06-26 08:24:29 -070011440 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
11441 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
James Smart51ef4c22007-08-02 11:10:31 -040011442 return rc;
11443
James Smartc4908502019-01-28 11:14:28 -080011444 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011445
James Smart495a7142008-06-14 22:52:59 -040011446 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011447 return rc;
11448
11449 switch (ctx_cmd) {
11450 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070011451 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011452 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11453 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011454 rc = 0;
11455 break;
11456 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070011457 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011458 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011459 rc = 0;
11460 break;
dea31012005-04-17 16:05:31 -050011461 case LPFC_CTX_HOST:
11462 rc = 0;
11463 break;
11464 default:
11465 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011466 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011467 break;
11468 }
11469
11470 return rc;
11471}
11472
James Smarte59058c2008-08-24 21:49:00 -040011473/**
James Smart3621a712009-04-06 18:47:14 -040011474 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011475 * @vport: Pointer to virtual port.
11476 * @tgt_id: SCSI ID of the target.
11477 * @lun_id: LUN ID of the scsi device.
11478 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11479 *
11480 * This function returns number of FCP commands pending for the vport.
11481 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11482 * commands pending on the vport associated with SCSI device specified
11483 * by tgt_id and lun_id parameters.
11484 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11485 * commands pending on the vport associated with SCSI target specified
11486 * by tgt_id parameter.
11487 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11488 * commands pending on the vport.
11489 * This function returns the number of iocbs which satisfy the filter.
11490 * This function is called without any lock held.
11491 **/
dea31012005-04-17 16:05:31 -050011492int
James Smart51ef4c22007-08-02 11:10:31 -040011493lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11494 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011495{
James Smart51ef4c22007-08-02 11:10:31 -040011496 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011497 struct lpfc_iocbq *iocbq;
11498 int sum, i;
dea31012005-04-17 16:05:31 -050011499
Johannes Thumshirn31979002016-07-18 16:06:03 +020011500 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011501 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11502 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011503
James Smart51ef4c22007-08-02 11:10:31 -040011504 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11505 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011506 sum++;
dea31012005-04-17 16:05:31 -050011507 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011508 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011509
dea31012005-04-17 16:05:31 -050011510 return sum;
11511}
11512
James Smarte59058c2008-08-24 21:49:00 -040011513/**
James Smart3621a712009-04-06 18:47:14 -040011514 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011515 * @phba: Pointer to HBA context object
11516 * @cmdiocb: Pointer to command iocb object.
11517 * @rspiocb: Pointer to response iocb object.
11518 *
11519 * This function is called when an aborted FCP iocb completes. This
11520 * function is called by the ring event handler with no lock held.
11521 * This function frees the iocb.
11522 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011523void
James Smart2e0fef82007-06-17 19:56:36 -050011524lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11525 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011526{
James Smartcb69f7d2011-12-13 13:21:57 -050011527 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011528 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011529 "original iotag x%x, abort cmd iotag x%x "
11530 "status 0x%x, reason 0x%x\n",
11531 cmdiocb->iocb.un.acxri.abortContextTag,
11532 cmdiocb->iocb.un.acxri.abortIoTag,
11533 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11534 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011535 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011536 return;
11537}
11538
James Smarte59058c2008-08-24 21:49:00 -040011539/**
James Smart3621a712009-04-06 18:47:14 -040011540 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011541 * @vport: Pointer to virtual port.
11542 * @pring: Pointer to driver SLI ring object.
11543 * @tgt_id: SCSI ID of the target.
11544 * @lun_id: LUN ID of the scsi device.
11545 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11546 *
11547 * This function sends an abort command for every SCSI command
11548 * associated with the given virtual port pending on the ring
11549 * filtered by lpfc_sli_validate_fcp_iocb function.
11550 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11551 * FCP iocbs associated with lun specified by tgt_id and lun_id
11552 * parameters
11553 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11554 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11555 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11556 * FCP iocbs associated with virtual port.
11557 * This function returns number of iocbs it failed to abort.
11558 * This function is called with no locks held.
11559 **/
dea31012005-04-17 16:05:31 -050011560int
James Smart51ef4c22007-08-02 11:10:31 -040011561lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11562 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011563{
James Smart51ef4c22007-08-02 11:10:31 -040011564 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011565 struct lpfc_iocbq *iocbq;
11566 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011567 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011568 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011569 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011570 int i;
dea31012005-04-17 16:05:31 -050011571
James Smartb0e83012018-06-26 08:24:29 -070011572 /* all I/Os are in process of being flushed */
11573 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH)
11574 return errcnt;
11575
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011576 for (i = 1; i <= phba->sli.last_iotag; i++) {
11577 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011578
James Smart51ef4c22007-08-02 11:10:31 -040011579 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011580 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011581 continue;
11582
James Smartafbd8d82013-09-06 12:22:13 -040011583 /*
11584 * If the iocbq is already being aborted, don't take a second
11585 * action, but do count it.
11586 */
11587 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11588 continue;
11589
dea31012005-04-17 16:05:31 -050011590 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011591 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011592 if (abtsiocb == NULL) {
11593 errcnt++;
11594 continue;
11595 }
dea31012005-04-17 16:05:31 -050011596
James Smartafbd8d82013-09-06 12:22:13 -040011597 /* indicate the IO is being aborted by the driver. */
11598 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11599
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011600 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011601 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11602 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011603 if (phba->sli_rev == LPFC_SLI_REV4)
11604 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11605 else
11606 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011607 abtsiocb->iocb.ulpLe = 1;
11608 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011609 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011610
James Smart5ffc2662009-11-18 15:39:44 -050011611 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011612 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011613 if (iocbq->iocb_flag & LPFC_IO_FCP)
11614 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011615 if (iocbq->iocb_flag & LPFC_IO_FOF)
11616 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011617
James Smart2e0fef82007-06-17 19:56:36 -050011618 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011619 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11620 else
11621 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11622
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011623 /* Setup callback routine and issue the command. */
11624 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011625 if (phba->sli_rev == LPFC_SLI_REV4) {
11626 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11627 if (!pring_s4)
11628 continue;
11629 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11630 abtsiocb, 0);
11631 } else
11632 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11633 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011634 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011635 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011636 errcnt++;
11637 continue;
11638 }
11639 }
11640
11641 return errcnt;
11642}
11643
James Smarte59058c2008-08-24 21:49:00 -040011644/**
James Smart98912dda2014-04-04 13:52:31 -040011645 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11646 * @vport: Pointer to virtual port.
11647 * @pring: Pointer to driver SLI ring object.
11648 * @tgt_id: SCSI ID of the target.
11649 * @lun_id: LUN ID of the scsi device.
11650 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11651 *
11652 * This function sends an abort command for every SCSI command
11653 * associated with the given virtual port pending on the ring
11654 * filtered by lpfc_sli_validate_fcp_iocb function.
11655 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11656 * FCP iocbs associated with lun specified by tgt_id and lun_id
11657 * parameters
11658 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11659 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11660 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11661 * FCP iocbs associated with virtual port.
11662 * This function returns number of iocbs it aborted .
11663 * This function is called with no locks held right after a taskmgmt
11664 * command is sent.
11665 **/
11666int
11667lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11668 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11669{
11670 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -080011671 struct lpfc_io_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011672 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011673 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011674 struct lpfc_iocbq *iocbq;
11675 IOCB_t *icmd;
11676 int sum, i, ret_val;
11677 unsigned long iflags;
11678 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011679
James Smart59c68ea2018-04-09 14:24:25 -070011680 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011681
11682 /* all I/Os are in process of being flushed */
11683 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070011684 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011685 return 0;
11686 }
11687 sum = 0;
11688
11689 for (i = 1; i <= phba->sli.last_iotag; i++) {
11690 iocbq = phba->sli.iocbq_lookup[i];
11691
11692 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11693 cmd) != 0)
11694 continue;
11695
11696 /*
11697 * If the iocbq is already being aborted, don't take a second
11698 * action, but do count it.
11699 */
11700 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11701 continue;
11702
11703 /* issue ABTS for this IOCB based on iotag */
11704 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11705 if (abtsiocbq == NULL)
11706 continue;
11707
11708 icmd = &iocbq->iocb;
11709 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11710 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11711 if (phba->sli_rev == LPFC_SLI_REV4)
11712 abtsiocbq->iocb.un.acxri.abortIoTag =
11713 iocbq->sli4_xritag;
11714 else
11715 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11716 abtsiocbq->iocb.ulpLe = 1;
11717 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11718 abtsiocbq->vport = vport;
11719
11720 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011721 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011722 if (iocbq->iocb_flag & LPFC_IO_FCP)
11723 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011724 if (iocbq->iocb_flag & LPFC_IO_FOF)
11725 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011726
James Smartc4908502019-01-28 11:14:28 -080011727 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James Smart8c50d252014-09-03 12:58:16 -040011728 ndlp = lpfc_cmd->rdata->pnode;
11729
11730 if (lpfc_is_link_up(phba) &&
11731 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011732 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11733 else
11734 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11735
11736 /* Setup callback routine and issue the command. */
11737 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11738
11739 /*
11740 * Indicate the IO is being aborted by the driver and set
11741 * the caller's flag into the aborted IO.
11742 */
11743 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11744
11745 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart59c68ea2018-04-09 14:24:25 -070011746 pring_s4 = lpfc_sli4_calc_ring(phba, abtsiocbq);
11747 if (!pring_s4)
James Smart895427b2017-02-12 13:52:30 -080011748 continue;
James Smart98912dda2014-04-04 13:52:31 -040011749 /* Note: both hbalock and ring_lock must be set here */
James Smart59c68ea2018-04-09 14:24:25 -070011750 spin_lock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011751 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11752 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070011753 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011754 } else {
11755 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11756 abtsiocbq, 0);
11757 }
11758
11759
11760 if (ret_val == IOCB_ERROR)
11761 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11762 else
11763 sum++;
11764 }
James Smart59c68ea2018-04-09 14:24:25 -070011765 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011766 return sum;
11767}
11768
11769/**
James Smart3621a712009-04-06 18:47:14 -040011770 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011771 * @phba: Pointer to HBA context object.
11772 * @cmdiocbq: Pointer to command iocb.
11773 * @rspiocbq: Pointer to response iocb.
11774 *
11775 * This function is the completion handler for iocbs issued using
11776 * lpfc_sli_issue_iocb_wait function. This function is called by the
11777 * ring event handler function without any lock held. This function
11778 * can be called from both worker thread context and interrupt
11779 * context. This function also can be called from other thread which
11780 * cleans up the SLI layer objects.
11781 * This function copy the contents of the response iocb to the
11782 * response iocb memory object provided by the caller of
11783 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11784 * sleeps for the iocb completion.
11785 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011786static void
11787lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11788 struct lpfc_iocbq *cmdiocbq,
11789 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011790{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011791 wait_queue_head_t *pdone_q;
11792 unsigned long iflags;
James Smartc4908502019-01-28 11:14:28 -080011793 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011794
James Smart2e0fef82007-06-17 19:56:36 -050011795 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011796 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11797
11798 /*
11799 * A time out has occurred for the iocb. If a time out
11800 * completion handler has been supplied, call it. Otherwise,
11801 * just free the iocbq.
11802 */
11803
11804 spin_unlock_irqrestore(&phba->hbalock, iflags);
11805 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11806 cmdiocbq->wait_iocb_cmpl = NULL;
11807 if (cmdiocbq->iocb_cmpl)
11808 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11809 else
11810 lpfc_sli_release_iocbq(phba, cmdiocbq);
11811 return;
11812 }
11813
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011814 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11815 if (cmdiocbq->context2 && rspiocbq)
11816 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11817 &rspiocbq->iocb, sizeof(IOCB_t));
11818
James Smart0f65ff62010-02-26 14:14:23 -050011819 /* Set the exchange busy flag for task management commands */
11820 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11821 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smartc4908502019-01-28 11:14:28 -080011822 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
James Smart0f65ff62010-02-26 14:14:23 -050011823 cur_iocbq);
11824 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11825 }
11826
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011827 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011828 if (pdone_q)
11829 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011830 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011831 return;
11832}
11833
James Smarte59058c2008-08-24 21:49:00 -040011834/**
James Smartd11e31d2009-06-10 17:23:06 -040011835 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11836 * @phba: Pointer to HBA context object..
11837 * @piocbq: Pointer to command iocb.
11838 * @flag: Flag to test.
11839 *
11840 * This routine grabs the hbalock and then test the iocb_flag to
11841 * see if the passed in flag is set.
11842 * Returns:
11843 * 1 if flag is set.
11844 * 0 if flag is not set.
11845 **/
11846static int
11847lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11848 struct lpfc_iocbq *piocbq, uint32_t flag)
11849{
11850 unsigned long iflags;
11851 int ret;
11852
11853 spin_lock_irqsave(&phba->hbalock, iflags);
11854 ret = piocbq->iocb_flag & flag;
11855 spin_unlock_irqrestore(&phba->hbalock, iflags);
11856 return ret;
11857
11858}
11859
11860/**
James Smart3621a712009-04-06 18:47:14 -040011861 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011862 * @phba: Pointer to HBA context object..
11863 * @pring: Pointer to sli ring.
11864 * @piocb: Pointer to command iocb.
11865 * @prspiocbq: Pointer to response iocb.
11866 * @timeout: Timeout in number of seconds.
11867 *
11868 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011869 * iocb to complete. The iocb_cmpl field of the shall be used
11870 * to handle iocbs which time out. If the field is NULL, the
11871 * function shall free the iocbq structure. If more clean up is
11872 * needed, the caller is expected to provide a completion function
11873 * that will provide the needed clean up. If the iocb command is
11874 * not completed within timeout seconds, the function will either
11875 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11876 * completion function set in the iocb_cmpl field and then return
11877 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11878 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011879 * The function waits for the iocb completion using an
11880 * non-interruptible wait.
11881 * This function will sleep while waiting for iocb completion.
11882 * So, this function should not be called from any context which
11883 * does not allow sleeping. Due to the same reason, this function
11884 * cannot be called with interrupt disabled.
11885 * This function assumes that the iocb completions occur while
11886 * this function sleep. So, this function cannot be called from
11887 * the thread which process iocb completion for this ring.
11888 * This function clears the iocb_flag of the iocb object before
11889 * issuing the iocb and the iocb completion handler sets this
11890 * flag and wakes this thread when the iocb completes.
11891 * The contents of the response iocb will be copied to prspiocbq
11892 * by the completion handler when the command completes.
11893 * This function returns IOCB_SUCCESS when success.
11894 * This function is called with no lock held.
11895 **/
dea31012005-04-17 16:05:31 -050011896int
James Smart2e0fef82007-06-17 19:56:36 -050011897lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011898 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011899 struct lpfc_iocbq *piocb,
11900 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011901 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011902{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011903 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011904 long timeleft, timeout_req = 0;
11905 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011906 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011907 struct lpfc_iocbq *iocb;
11908 int txq_cnt = 0;
11909 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011910 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011911 unsigned long iflags;
11912 bool iocb_completed = true;
11913
James Smart895427b2017-02-12 13:52:30 -080011914 if (phba->sli_rev >= LPFC_SLI_REV4)
11915 pring = lpfc_sli4_calc_ring(phba, piocb);
11916 else
11917 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011918 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011919 * If the caller has provided a response iocbq buffer, then context2
11920 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011921 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011922 if (prspiocbq) {
11923 if (piocb->context2)
11924 return IOCB_ERROR;
11925 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011926 }
11927
James Smart5a0916b2013-07-15 18:31:42 -040011928 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011929 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11930 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011931 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011932
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011933 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011934 if (lpfc_readl(phba->HCregaddr, &creg_val))
11935 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011936 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11937 writel(creg_val, phba->HCregaddr);
11938 readl(phba->HCregaddr); /* flush */
11939 }
11940
James Smart2a9bf3d2010-06-07 15:24:45 -040011941 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11942 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011943 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011944 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011945 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011946 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011947 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011948 spin_lock_irqsave(&phba->hbalock, iflags);
11949 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011950
James Smart5a0916b2013-07-15 18:31:42 -040011951 /*
11952 * IOCB timed out. Inform the wake iocb wait
11953 * completion function and set local status
11954 */
11955
11956 iocb_completed = false;
11957 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11958 }
11959 spin_unlock_irqrestore(&phba->hbalock, iflags);
11960 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011961 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011962 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011963 /* Note: we are not indicating if the IOCB has a success
11964 * status or not - that's for the caller to check.
11965 * IOCB_SUCCESS means just that the command was sent and
11966 * completed. Not that it completed successfully.
11967 * */
James Smart7054a602007-04-25 09:52:34 -040011968 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011969 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011970 "0338 IOCB wait timeout error - no "
11971 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011972 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011973 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011974 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011975 "0330 IOCB wake NOT set, "
11976 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011977 timeout, (timeleft / jiffies));
11978 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011979 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011980 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011981 if (phba->cfg_log_verbose & LOG_SLI) {
11982 list_for_each_entry(iocb, &pring->txq, list) {
11983 txq_cnt++;
11984 }
11985 list_for_each_entry(iocb, &pring->txcmplq, list) {
11986 txcmplq_cnt++;
11987 }
11988 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11989 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11990 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11991 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011992 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011993 } else {
11994 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011995 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011996 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011997 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011998 }
11999
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012000 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050012001 if (lpfc_readl(phba->HCregaddr, &creg_val))
12002 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012003 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
12004 writel(creg_val, phba->HCregaddr);
12005 readl(phba->HCregaddr); /* flush */
12006 }
12007
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012008 if (prspiocbq)
12009 piocb->context2 = NULL;
12010
12011 piocb->context_un.wait_queue = NULL;
12012 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050012013 return retval;
12014}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012015
James Smarte59058c2008-08-24 21:49:00 -040012016/**
James Smart3621a712009-04-06 18:47:14 -040012017 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040012018 * @phba: Pointer to HBA context object.
12019 * @pmboxq: Pointer to driver mailbox object.
12020 * @timeout: Timeout in number of seconds.
12021 *
12022 * This function issues the mailbox to firmware and waits for the
12023 * mailbox command to complete. If the mailbox command is not
12024 * completed within timeout seconds, it returns MBX_TIMEOUT.
12025 * The function waits for the mailbox completion using an
12026 * interruptible wait. If the thread is woken up due to a
12027 * signal, MBX_TIMEOUT error is returned to the caller. Caller
12028 * should not free the mailbox resources, if this function returns
12029 * MBX_TIMEOUT.
12030 * This function will sleep while waiting for mailbox completion.
12031 * So, this function should not be called from any context which
12032 * does not allow sleeping. Due to the same reason, this function
12033 * cannot be called with interrupt disabled.
12034 * This function assumes that the mailbox completion occurs while
12035 * this function sleep. So, this function cannot be called from
12036 * the worker thread which processes mailbox completion.
12037 * This function is called in the context of HBA management
12038 * applications.
12039 * This function returns MBX_SUCCESS when successful.
12040 * This function is called with no lock held.
12041 **/
dea31012005-04-17 16:05:31 -050012042int
James Smart2e0fef82007-06-17 19:56:36 -050012043lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050012044 uint32_t timeout)
12045{
James Smarte29d74f2018-03-05 12:04:07 -080012046 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050012047 int retval;
James Smart858c9f62007-06-17 19:56:39 -050012048 unsigned long flag;
dea31012005-04-17 16:05:31 -050012049
James Smart495a7142008-06-14 22:52:59 -040012050 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050012051 /* setup wake call as IOCB callback */
12052 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050012053
James Smarte29d74f2018-03-05 12:04:07 -080012054 /* setup context3 field to pass wait_queue pointer to wake function */
12055 init_completion(&mbox_done);
12056 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050012057 /* now issue the command */
12058 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050012059 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080012060 wait_for_completion_timeout(&mbox_done,
12061 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040012062
James Smart858c9f62007-06-17 19:56:39 -050012063 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080012064 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040012065 /*
12066 * if LPFC_MBX_WAKE flag is set the mailbox is completed
12067 * else do not free the resources.
12068 */
James Smartd7c47992010-06-08 18:31:54 -040012069 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050012070 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040012071 } else {
James Smart7054a602007-04-25 09:52:34 -040012072 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050012073 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12074 }
12075 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050012076 }
dea31012005-04-17 16:05:31 -050012077 return retval;
12078}
12079
James Smarte59058c2008-08-24 21:49:00 -040012080/**
James Smart3772a992009-05-22 14:50:54 -040012081 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040012082 * @phba: Pointer to HBA context.
12083 *
James Smart3772a992009-05-22 14:50:54 -040012084 * This function is called to shutdown the driver's mailbox sub-system.
12085 * It first marks the mailbox sub-system is in a block state to prevent
12086 * the asynchronous mailbox command from issued off the pending mailbox
12087 * command queue. If the mailbox command sub-system shutdown is due to
12088 * HBA error conditions such as EEH or ERATT, this routine shall invoke
12089 * the mailbox sub-system flush routine to forcefully bring down the
12090 * mailbox sub-system. Otherwise, if it is due to normal condition (such
12091 * as with offline or HBA function reset), this routine will wait for the
12092 * outstanding mailbox command to complete before invoking the mailbox
12093 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040012094 **/
James Smart3772a992009-05-22 14:50:54 -040012095void
James Smart618a5232012-06-12 13:54:36 -040012096lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040012097{
James Smart3772a992009-05-22 14:50:54 -040012098 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040012099 unsigned long timeout;
12100
James Smart618a5232012-06-12 13:54:36 -040012101 if (mbx_action == LPFC_MBX_NO_WAIT) {
12102 /* delay 100ms for port state */
12103 msleep(100);
12104 lpfc_sli_mbox_sys_flush(phba);
12105 return;
12106 }
James Smarta183a152011-10-10 21:32:43 -040012107 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050012108
James Smart523128e2018-09-10 10:30:46 -070012109 /* Disable softirqs, including timers from obtaining phba->hbalock */
12110 local_bh_disable();
12111
James Smart3772a992009-05-22 14:50:54 -040012112 spin_lock_irq(&phba->hbalock);
12113 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040012114
12115 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040012116 /* Determine how long we might wait for the active mailbox
12117 * command to be gracefully completed by firmware.
12118 */
James Smarta183a152011-10-10 21:32:43 -040012119 if (phba->sli.mbox_active)
12120 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12121 phba->sli.mbox_active) *
12122 1000) + jiffies;
12123 spin_unlock_irq(&phba->hbalock);
12124
James Smart523128e2018-09-10 10:30:46 -070012125 /* Enable softirqs again, done with phba->hbalock */
12126 local_bh_enable();
12127
James Smart3772a992009-05-22 14:50:54 -040012128 while (phba->sli.mbox_active) {
12129 /* Check active mailbox complete status every 2ms */
12130 msleep(2);
12131 if (time_after(jiffies, timeout))
12132 /* Timeout, let the mailbox flush routine to
12133 * forcefully release active mailbox command
12134 */
12135 break;
12136 }
James Smart523128e2018-09-10 10:30:46 -070012137 } else {
James Smartd7069f02012-03-01 22:36:29 -050012138 spin_unlock_irq(&phba->hbalock);
12139
James Smart523128e2018-09-10 10:30:46 -070012140 /* Enable softirqs again, done with phba->hbalock */
12141 local_bh_enable();
12142 }
12143
James Smart3772a992009-05-22 14:50:54 -040012144 lpfc_sli_mbox_sys_flush(phba);
12145}
12146
12147/**
12148 * lpfc_sli_eratt_read - read sli-3 error attention events
12149 * @phba: Pointer to HBA context.
12150 *
12151 * This function is called to read the SLI3 device error attention registers
12152 * for possible error attention events. The caller must hold the hostlock
12153 * with spin_lock_irq().
12154 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012155 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040012156 * Register and returns 0 otherwise.
12157 **/
12158static int
12159lpfc_sli_eratt_read(struct lpfc_hba *phba)
12160{
James Smarted957682007-06-17 19:56:37 -050012161 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040012162
James Smart3772a992009-05-22 14:50:54 -040012163 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050012164 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12165 goto unplug_err;
12166
James Smart3772a992009-05-22 14:50:54 -040012167 if (ha_copy & HA_ERATT) {
12168 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050012169 if (lpfc_sli_read_hs(phba))
12170 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040012171
James Smart3772a992009-05-22 14:50:54 -040012172 /* Check if there is a deferred error condition is active */
12173 if ((HS_FFER1 & phba->work_hs) &&
12174 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012175 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040012176 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040012177 /* Clear all interrupt enable conditions */
12178 writel(0, phba->HCregaddr);
12179 readl(phba->HCregaddr);
12180 }
12181
12182 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040012183 phba->work_ha |= HA_ERATT;
12184 /* Indicate polling handles this ERATT */
12185 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012186 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012187 }
James Smart3772a992009-05-22 14:50:54 -040012188 return 0;
James Smart9940b972011-03-11 16:06:12 -050012189
12190unplug_err:
12191 /* Set the driver HS work bitmap */
12192 phba->work_hs |= UNPLUG_ERR;
12193 /* Set the driver HA work bitmap */
12194 phba->work_ha |= HA_ERATT;
12195 /* Indicate polling handles this ERATT */
12196 phba->hba_flag |= HBA_ERATT_HANDLED;
12197 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012198}
12199
James Smarte59058c2008-08-24 21:49:00 -040012200/**
James Smartda0436e2009-05-22 14:51:39 -040012201 * lpfc_sli4_eratt_read - read sli-4 error attention events
12202 * @phba: Pointer to HBA context.
12203 *
12204 * This function is called to read the SLI4 device error attention registers
12205 * for possible error attention events. The caller must hold the hostlock
12206 * with spin_lock_irq().
12207 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012208 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040012209 * Register and returns 0 otherwise.
12210 **/
12211static int
12212lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12213{
12214 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050012215 uint32_t if_type, portsmphr;
12216 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040012217
James Smart2fcee4b2010-12-15 17:57:46 -050012218 /*
12219 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040012220 * registers for error attention. This can be changed later.
12221 */
James Smart2fcee4b2010-12-15 17:57:46 -050012222 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12223 switch (if_type) {
12224 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050012225 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12226 &uerr_sta_lo) ||
12227 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12228 &uerr_sta_hi)) {
12229 phba->work_hs |= UNPLUG_ERR;
12230 phba->work_ha |= HA_ERATT;
12231 phba->hba_flag |= HBA_ERATT_HANDLED;
12232 return 1;
12233 }
James Smart2fcee4b2010-12-15 17:57:46 -050012234 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12235 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12236 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12237 "1423 HBA Unrecoverable error: "
12238 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12239 "ue_mask_lo_reg=0x%x, "
12240 "ue_mask_hi_reg=0x%x\n",
12241 uerr_sta_lo, uerr_sta_hi,
12242 phba->sli4_hba.ue_mask_lo,
12243 phba->sli4_hba.ue_mask_hi);
12244 phba->work_status[0] = uerr_sta_lo;
12245 phba->work_status[1] = uerr_sta_hi;
12246 phba->work_ha |= HA_ERATT;
12247 phba->hba_flag |= HBA_ERATT_HANDLED;
12248 return 1;
12249 }
12250 break;
12251 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080012252 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050012253 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12254 &portstat_reg.word0) ||
12255 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12256 &portsmphr)){
12257 phba->work_hs |= UNPLUG_ERR;
12258 phba->work_ha |= HA_ERATT;
12259 phba->hba_flag |= HBA_ERATT_HANDLED;
12260 return 1;
12261 }
James Smart2fcee4b2010-12-15 17:57:46 -050012262 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12263 phba->work_status[0] =
12264 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12265 phba->work_status[1] =
12266 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050012268 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050012269 "port status reg 0x%x, "
12270 "port smphr reg 0x%x, "
12271 "error 1=0x%x, error 2=0x%x\n",
12272 portstat_reg.word0,
12273 portsmphr,
12274 phba->work_status[0],
12275 phba->work_status[1]);
12276 phba->work_ha |= HA_ERATT;
12277 phba->hba_flag |= HBA_ERATT_HANDLED;
12278 return 1;
12279 }
12280 break;
12281 case LPFC_SLI_INTF_IF_TYPE_1:
12282 default:
James Smarta747c9c2009-11-18 15:41:10 -050012283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050012284 "2886 HBA Error Attention on unsupported "
12285 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050012286 return 1;
James Smartda0436e2009-05-22 14:51:39 -040012287 }
James Smart2fcee4b2010-12-15 17:57:46 -050012288
James Smartda0436e2009-05-22 14:51:39 -040012289 return 0;
12290}
12291
12292/**
James Smart3621a712009-04-06 18:47:14 -040012293 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040012294 * @phba: Pointer to HBA context.
12295 *
James Smart3772a992009-05-22 14:50:54 -040012296 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040012297 * error attention register bit for error attention events.
12298 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012299 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040012300 * Register and returns 0 otherwise.
12301 **/
12302int
12303lpfc_sli_check_eratt(struct lpfc_hba *phba)
12304{
12305 uint32_t ha_copy;
12306
12307 /* If somebody is waiting to handle an eratt, don't process it
12308 * here. The brdkill function will do this.
12309 */
12310 if (phba->link_flag & LS_IGNORE_ERATT)
12311 return 0;
12312
12313 /* Check if interrupt handler handles this ERATT */
12314 spin_lock_irq(&phba->hbalock);
12315 if (phba->hba_flag & HBA_ERATT_HANDLED) {
12316 /* Interrupt handler has handled ERATT */
12317 spin_unlock_irq(&phba->hbalock);
12318 return 0;
12319 }
12320
James Smarta257bf92009-04-06 18:48:10 -040012321 /*
12322 * If there is deferred error attention, do not check for error
12323 * attention
12324 */
12325 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12326 spin_unlock_irq(&phba->hbalock);
12327 return 0;
12328 }
12329
James Smart3772a992009-05-22 14:50:54 -040012330 /* If PCI channel is offline, don't process it */
12331 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040012332 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012333 return 0;
12334 }
12335
12336 switch (phba->sli_rev) {
12337 case LPFC_SLI_REV2:
12338 case LPFC_SLI_REV3:
12339 /* Read chip Host Attention (HA) register */
12340 ha_copy = lpfc_sli_eratt_read(phba);
12341 break;
James Smartda0436e2009-05-22 14:51:39 -040012342 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050012343 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040012344 ha_copy = lpfc_sli4_eratt_read(phba);
12345 break;
James Smart3772a992009-05-22 14:50:54 -040012346 default:
12347 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12348 "0299 Invalid SLI revision (%d)\n",
12349 phba->sli_rev);
12350 ha_copy = 0;
12351 break;
James Smart93996272008-08-24 21:50:30 -040012352 }
12353 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012354
12355 return ha_copy;
12356}
12357
12358/**
12359 * lpfc_intr_state_check - Check device state for interrupt handling
12360 * @phba: Pointer to HBA context.
12361 *
12362 * This inline routine checks whether a device or its PCI slot is in a state
12363 * that the interrupt should be handled.
12364 *
12365 * This function returns 0 if the device or the PCI slot is in a state that
12366 * interrupt should be handled, otherwise -EIO.
12367 */
12368static inline int
12369lpfc_intr_state_check(struct lpfc_hba *phba)
12370{
12371 /* If the pci channel is offline, ignore all the interrupts */
12372 if (unlikely(pci_channel_offline(phba->pcidev)))
12373 return -EIO;
12374
12375 /* Update device level interrupt statistics */
12376 phba->sli.slistat.sli_intr++;
12377
12378 /* Ignore all interrupts during initialization. */
12379 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12380 return -EIO;
12381
James Smart93996272008-08-24 21:50:30 -040012382 return 0;
12383}
12384
12385/**
James Smart3772a992009-05-22 14:50:54 -040012386 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012387 * @irq: Interrupt number.
12388 * @dev_id: The device context pointer.
12389 *
James Smart93996272008-08-24 21:50:30 -040012390 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012391 * service routine when device with SLI-3 interface spec is enabled with
12392 * MSI-X multi-message interrupt mode and there are slow-path events in
12393 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12394 * interrupt mode, this function is called as part of the device-level
12395 * interrupt handler. When the PCI slot is in error recovery or the HBA
12396 * is undergoing initialization, the interrupt handler will not process
12397 * the interrupt. The link attention and ELS ring attention events are
12398 * handled by the worker thread. The interrupt handler signals the worker
12399 * thread and returns for these events. This function is called without
12400 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012401 * structures.
12402 *
12403 * This function returns IRQ_HANDLED when interrupt is handled else it
12404 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012405 **/
dea31012005-04-17 16:05:31 -050012406irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012407lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012408{
James Smart2e0fef82007-06-17 19:56:36 -050012409 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012410 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012411 uint32_t work_ha_copy;
12412 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012413 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012414 uint32_t control;
12415
James Smart92d7f7b2007-06-17 19:56:38 -050012416 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012417 struct lpfc_vport *vport;
12418 struct lpfc_nodelist *ndlp;
12419 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012420 LPFC_MBOXQ_t *pmb;
12421 int rc;
12422
dea31012005-04-17 16:05:31 -050012423 /*
12424 * Get the driver's phba structure from the dev_id and
12425 * assume the HBA is not interrupting.
12426 */
James Smart93996272008-08-24 21:50:30 -040012427 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012428
12429 if (unlikely(!phba))
12430 return IRQ_NONE;
12431
dea31012005-04-17 16:05:31 -050012432 /*
James Smart93996272008-08-24 21:50:30 -040012433 * Stuff needs to be attented to when this function is invoked as an
12434 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012435 */
James Smart93996272008-08-24 21:50:30 -040012436 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012437 /* Check device state for handling interrupt */
12438 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012439 return IRQ_NONE;
12440 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012441 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012442 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12443 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012444 /* If somebody is waiting to handle an eratt don't process it
12445 * here. The brdkill function will do this.
12446 */
12447 if (phba->link_flag & LS_IGNORE_ERATT)
12448 ha_copy &= ~HA_ERATT;
12449 /* Check the need for handling ERATT in interrupt handler */
12450 if (ha_copy & HA_ERATT) {
12451 if (phba->hba_flag & HBA_ERATT_HANDLED)
12452 /* ERATT polling has handled ERATT */
12453 ha_copy &= ~HA_ERATT;
12454 else
12455 /* Indicate interrupt handler handles ERATT */
12456 phba->hba_flag |= HBA_ERATT_HANDLED;
12457 }
James Smarta257bf92009-04-06 18:48:10 -040012458
12459 /*
12460 * If there is deferred error attention, do not check for any
12461 * interrupt.
12462 */
12463 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012464 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012465 return IRQ_NONE;
12466 }
12467
James Smart93996272008-08-24 21:50:30 -040012468 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012469 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12470 goto unplug_error;
12471
James Smarta747c9c2009-11-18 15:41:10 -050012472 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12473 HC_LAINT_ENA | HC_ERINT_ENA),
12474 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012475 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12476 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012477 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012478 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012479 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012480 } else
12481 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012482
dea31012005-04-17 16:05:31 -050012483 work_ha_copy = ha_copy & phba->work_ha_mask;
12484
James Smart93996272008-08-24 21:50:30 -040012485 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012486 if (work_ha_copy & HA_LATT) {
12487 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12488 /*
12489 * Turn off Link Attention interrupts
12490 * until CLEAR_LA done
12491 */
James Smart5b75da22008-12-04 22:39:35 -050012492 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012493 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012494 if (lpfc_readl(phba->HCregaddr, &control))
12495 goto unplug_error;
dea31012005-04-17 16:05:31 -050012496 control &= ~HC_LAINT_ENA;
12497 writel(control, phba->HCregaddr);
12498 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012499 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012500 }
12501 else
12502 work_ha_copy &= ~HA_LATT;
12503 }
12504
James Smart93996272008-08-24 21:50:30 -040012505 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012506 /*
12507 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12508 * the only slow ring.
12509 */
12510 status = (work_ha_copy &
12511 (HA_RXMASK << (4*LPFC_ELS_RING)));
12512 status >>= (4*LPFC_ELS_RING);
12513 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012514 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012515 if (lpfc_readl(phba->HCregaddr, &control))
12516 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012517
12518 lpfc_debugfs_slow_ring_trc(phba,
12519 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12520 control, status,
12521 (uint32_t)phba->sli.slistat.sli_intr);
12522
James Smart858c9f62007-06-17 19:56:39 -050012523 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012524 lpfc_debugfs_slow_ring_trc(phba,
12525 "ISR Disable ring:"
12526 "pwork:x%x hawork:x%x wait:x%x",
12527 phba->work_ha, work_ha_copy,
12528 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012529 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012530
James Smart858c9f62007-06-17 19:56:39 -050012531 control &=
12532 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012533 writel(control, phba->HCregaddr);
12534 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012535 }
James Smarta58cbd52007-08-02 11:09:43 -040012536 else {
12537 lpfc_debugfs_slow_ring_trc(phba,
12538 "ISR slow ring: pwork:"
12539 "x%x hawork:x%x wait:x%x",
12540 phba->work_ha, work_ha_copy,
12541 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012542 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012543 }
James Smart5b75da22008-12-04 22:39:35 -050012544 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012545 }
12546 }
James Smart5b75da22008-12-04 22:39:35 -050012547 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012548 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012549 if (lpfc_sli_read_hs(phba))
12550 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012551 /*
12552 * Check if there is a deferred error condition
12553 * is active
12554 */
12555 if ((HS_FFER1 & phba->work_hs) &&
12556 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012557 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12558 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012559 phba->hba_flag |= DEFER_ERATT;
12560 /* Clear all interrupt enable conditions */
12561 writel(0, phba->HCregaddr);
12562 readl(phba->HCregaddr);
12563 }
12564 }
12565
James Smart93996272008-08-24 21:50:30 -040012566 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012567 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012568 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012569 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012570 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012571
12572 /* First check out the status word */
12573 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12574 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012575 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012576 /*
12577 * Stray Mailbox Interrupt, mbxCommand <cmd>
12578 * mbxStatus <status>
12579 */
James Smart09372822008-01-11 01:52:54 -050012580 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012581 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012582 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012583 "Interrupt mbxCommand x%x "
12584 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012585 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012586 pmbox->mbxCommand,
12587 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012588 /* clear mailbox attention bit */
12589 work_ha_copy &= ~HA_MBATT;
12590 } else {
James Smart97eab632008-04-07 10:16:05 -040012591 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012592 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012593 phba->last_completion_time = jiffies;
12594 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012595 if (pmb->mbox_cmpl) {
12596 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12597 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012598 if (pmb->out_ext_byte_len &&
James Smart3e1f0712018-11-29 16:09:29 -080012599 pmb->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -040012600 lpfc_sli_pcimem_bcopy(
12601 phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -080012602 pmb->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -040012603 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012604 }
James Smart09372822008-01-11 01:52:54 -050012605 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12606 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12607
12608 lpfc_debugfs_disc_trc(vport,
12609 LPFC_DISC_TRC_MBOX_VPORT,
12610 "MBOX dflt rpi: : "
12611 "status:x%x rpi:x%x",
12612 (uint32_t)pmbox->mbxStatus,
12613 pmbox->un.varWords[0], 0);
12614
12615 if (!pmbox->mbxStatus) {
12616 mp = (struct lpfc_dmabuf *)
James Smart3e1f0712018-11-29 16:09:29 -080012617 (pmb->ctx_buf);
James Smart09372822008-01-11 01:52:54 -050012618 ndlp = (struct lpfc_nodelist *)
James Smart3e1f0712018-11-29 16:09:29 -080012619 pmb->ctx_ndlp;
James Smart09372822008-01-11 01:52:54 -050012620
12621 /* Reg_LOGIN of dflt RPI was
12622 * successful. new lets get
12623 * rid of the RPI using the
12624 * same mbox buffer.
12625 */
12626 lpfc_unreg_login(phba,
12627 vport->vpi,
12628 pmbox->un.varWords[0],
12629 pmb);
12630 pmb->mbox_cmpl =
12631 lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080012632 pmb->ctx_buf = mp;
12633 pmb->ctx_ndlp = ndlp;
James Smart09372822008-01-11 01:52:54 -050012634 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012635 rc = lpfc_sli_issue_mbox(phba,
12636 pmb,
12637 MBX_NOWAIT);
12638 if (rc != MBX_BUSY)
12639 lpfc_printf_log(phba,
12640 KERN_ERR,
12641 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012642 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012643 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012644 if (rc != MBX_NOT_FINISHED)
12645 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012646 }
12647 }
James Smart5b75da22008-12-04 22:39:35 -050012648 spin_lock_irqsave(
12649 &phba->pport->work_port_lock,
12650 iflag);
James Smart09372822008-01-11 01:52:54 -050012651 phba->pport->work_port_events &=
12652 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012653 spin_unlock_irqrestore(
12654 &phba->pport->work_port_lock,
12655 iflag);
James Smart09372822008-01-11 01:52:54 -050012656 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012657 }
James Smart97eab632008-04-07 10:16:05 -040012658 } else
James Smart5b75da22008-12-04 22:39:35 -050012659 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012660
James Smart92d7f7b2007-06-17 19:56:38 -050012661 if ((work_ha_copy & HA_MBATT) &&
12662 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012663send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012664 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012665 do {
12666 rc = lpfc_sli_issue_mbox(phba, NULL,
12667 MBX_NOWAIT);
12668 } while (rc == MBX_NOT_FINISHED);
12669 if (rc != MBX_SUCCESS)
12670 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12671 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012672 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012673 }
12674
James Smart5b75da22008-12-04 22:39:35 -050012675 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012676 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012677 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012678 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012679 }
James Smart93996272008-08-24 21:50:30 -040012680 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012681unplug_error:
12682 spin_unlock_irqrestore(&phba->hbalock, iflag);
12683 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012684
James Smart3772a992009-05-22 14:50:54 -040012685} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012686
12687/**
James Smart3772a992009-05-22 14:50:54 -040012688 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012689 * @irq: Interrupt number.
12690 * @dev_id: The device context pointer.
12691 *
12692 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012693 * service routine when device with SLI-3 interface spec is enabled with
12694 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12695 * ring event in the HBA. However, when the device is enabled with either
12696 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12697 * device-level interrupt handler. When the PCI slot is in error recovery
12698 * or the HBA is undergoing initialization, the interrupt handler will not
12699 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12700 * the intrrupt context. This function is called without any lock held.
12701 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012702 *
12703 * This function returns IRQ_HANDLED when interrupt is handled else it
12704 * returns IRQ_NONE.
12705 **/
12706irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012707lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012708{
12709 struct lpfc_hba *phba;
12710 uint32_t ha_copy;
12711 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012712 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012713 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012714
12715 /* Get the driver's phba structure from the dev_id and
12716 * assume the HBA is not interrupting.
12717 */
12718 phba = (struct lpfc_hba *) dev_id;
12719
12720 if (unlikely(!phba))
12721 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012722
12723 /*
James Smart93996272008-08-24 21:50:30 -040012724 * Stuff needs to be attented to when this function is invoked as an
12725 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012726 */
James Smart93996272008-08-24 21:50:30 -040012727 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012728 /* Check device state for handling interrupt */
12729 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012730 return IRQ_NONE;
12731 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012732 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12733 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012734 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012735 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012736 /*
12737 * If there is deferred error attention, do not check for
12738 * any interrupt.
12739 */
12740 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012741 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012742 return IRQ_NONE;
12743 }
James Smart93996272008-08-24 21:50:30 -040012744 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12745 phba->HAregaddr);
12746 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012747 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012748 } else
12749 ha_copy = phba->ha_copy;
12750
12751 /*
12752 * Process all events on FCP ring. Take the optimized path for FCP IO.
12753 */
12754 ha_copy &= ~(phba->work_ha_mask);
12755
12756 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012757 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012758 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012759 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012760 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012761
12762 if (phba->cfg_multi_ring_support == 2) {
12763 /*
James Smart93996272008-08-24 21:50:30 -040012764 * Process all events on extra ring. Take the optimized path
12765 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012766 */
James Smart93996272008-08-24 21:50:30 -040012767 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012768 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012769 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012770 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012771 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012772 status);
12773 }
12774 }
dea31012005-04-17 16:05:31 -050012775 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012776} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012777
James Smart93996272008-08-24 21:50:30 -040012778/**
James Smart3772a992009-05-22 14:50:54 -040012779 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012780 * @irq: Interrupt number.
12781 * @dev_id: The device context pointer.
12782 *
James Smart3772a992009-05-22 14:50:54 -040012783 * This function is the HBA device-level interrupt handler to device with
12784 * SLI-3 interface spec, called from the PCI layer when either MSI or
12785 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12786 * requires driver attention. This function invokes the slow-path interrupt
12787 * attention handling function and fast-path interrupt attention handling
12788 * function in turn to process the relevant HBA attention events. This
12789 * function is called without any lock held. It gets the hbalock to access
12790 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012791 *
12792 * This function returns IRQ_HANDLED when interrupt is handled, else it
12793 * returns IRQ_NONE.
12794 **/
12795irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012796lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012797{
12798 struct lpfc_hba *phba;
12799 irqreturn_t sp_irq_rc, fp_irq_rc;
12800 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012801 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012802
12803 /*
12804 * Get the driver's phba structure from the dev_id and
12805 * assume the HBA is not interrupting.
12806 */
12807 phba = (struct lpfc_hba *) dev_id;
12808
12809 if (unlikely(!phba))
12810 return IRQ_NONE;
12811
James Smart3772a992009-05-22 14:50:54 -040012812 /* Check device state for handling interrupt */
12813 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012814 return IRQ_NONE;
12815
12816 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012817 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12818 spin_unlock(&phba->hbalock);
12819 return IRQ_HANDLED;
12820 }
12821
James Smart93996272008-08-24 21:50:30 -040012822 if (unlikely(!phba->ha_copy)) {
12823 spin_unlock(&phba->hbalock);
12824 return IRQ_NONE;
12825 } else if (phba->ha_copy & HA_ERATT) {
12826 if (phba->hba_flag & HBA_ERATT_HANDLED)
12827 /* ERATT polling has handled ERATT */
12828 phba->ha_copy &= ~HA_ERATT;
12829 else
12830 /* Indicate interrupt handler handles ERATT */
12831 phba->hba_flag |= HBA_ERATT_HANDLED;
12832 }
12833
James Smarta257bf92009-04-06 18:48:10 -040012834 /*
12835 * If there is deferred error attention, do not check for any interrupt.
12836 */
12837 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012838 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012839 return IRQ_NONE;
12840 }
12841
James Smart93996272008-08-24 21:50:30 -040012842 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012843 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12844 spin_unlock(&phba->hbalock);
12845 return IRQ_HANDLED;
12846 }
James Smarta747c9c2009-11-18 15:41:10 -050012847 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12848 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12849 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012850 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012851 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012852 readl(phba->HAregaddr); /* flush */
12853 spin_unlock(&phba->hbalock);
12854
12855 /*
12856 * Invokes slow-path host attention interrupt handling as appropriate.
12857 */
12858
12859 /* status of events with mailbox and link attention */
12860 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12861
12862 /* status of events with ELS ring */
12863 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12864 status2 >>= (4*LPFC_ELS_RING);
12865
12866 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012867 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012868 else
12869 sp_irq_rc = IRQ_NONE;
12870
12871 /*
12872 * Invoke fast-path host attention interrupt handling as appropriate.
12873 */
12874
12875 /* status of events with FCP ring */
12876 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12877 status1 >>= (4*LPFC_FCP_RING);
12878
12879 /* status of events with extra ring */
12880 if (phba->cfg_multi_ring_support == 2) {
12881 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12882 status2 >>= (4*LPFC_EXTRA_RING);
12883 } else
12884 status2 = 0;
12885
12886 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012887 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012888 else
12889 fp_irq_rc = IRQ_NONE;
12890
12891 /* Return device-level interrupt handling status */
12892 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012893} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012894
12895/**
James Smart4f774512009-05-22 14:52:35 -040012896 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12897 * @phba: pointer to lpfc hba data structure.
12898 *
12899 * This routine is invoked by the worker thread to process all the pending
12900 * SLI4 els abort xri events.
12901 **/
12902void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12903{
12904 struct lpfc_cq_event *cq_event;
12905
12906 /* First, declare the els xri abort event has been handled */
12907 spin_lock_irq(&phba->hbalock);
12908 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12909 spin_unlock_irq(&phba->hbalock);
12910 /* Now, handle all the els xri abort events */
12911 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12912 /* Get the first event from the head of the event queue */
12913 spin_lock_irq(&phba->hbalock);
12914 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12915 cq_event, struct lpfc_cq_event, list);
12916 spin_unlock_irq(&phba->hbalock);
12917 /* Notify aborted XRI for ELS work queue */
12918 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12919 /* Free the event processed back to the free pool */
12920 lpfc_sli4_cq_event_release(phba, cq_event);
12921 }
12922}
12923
James Smart341af102010-01-26 23:07:37 -050012924/**
12925 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12926 * @phba: pointer to lpfc hba data structure
12927 * @pIocbIn: pointer to the rspiocbq
12928 * @pIocbOut: pointer to the cmdiocbq
12929 * @wcqe: pointer to the complete wcqe
12930 *
12931 * This routine transfers the fields of a command iocbq to a response iocbq
12932 * by copying all the IOCB fields from command iocbq and transferring the
12933 * completion status information from the complete wcqe.
12934 **/
James Smart4f774512009-05-22 14:52:35 -040012935static void
James Smart341af102010-01-26 23:07:37 -050012936lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12937 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012938 struct lpfc_iocbq *pIocbOut,
12939 struct lpfc_wcqe_complete *wcqe)
12940{
James Smartaf227412013-10-10 12:23:10 -040012941 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012942 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012943 uint32_t status, max_response;
12944 struct lpfc_dmabuf *dmabuf;
12945 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012946 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12947
12948 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12949 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012950 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012951 status = bf_get(lpfc_wcqe_c_status, wcqe);
12952 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012953 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12954 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12955 pIocbIn->iocb.un.fcpi.fcpi_parm =
12956 pIocbOut->iocb.un.fcpi.fcpi_parm -
12957 wcqe->total_data_placed;
12958 else
12959 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012960 else {
James Smart4f774512009-05-22 14:52:35 -040012961 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012962 switch (pIocbOut->iocb.ulpCommand) {
12963 case CMD_ELS_REQUEST64_CR:
12964 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12965 bpl = (struct ulp_bde64 *)dmabuf->virt;
12966 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12967 max_response = bde.tus.f.bdeSize;
12968 break;
12969 case CMD_GEN_REQUEST64_CR:
12970 max_response = 0;
12971 if (!pIocbOut->context3)
12972 break;
12973 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12974 sizeof(struct ulp_bde64);
12975 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12976 bpl = (struct ulp_bde64 *)dmabuf->virt;
12977 for (i = 0; i < numBdes; i++) {
12978 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12979 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12980 max_response += bde.tus.f.bdeSize;
12981 }
12982 break;
12983 default:
12984 max_response = wcqe->total_data_placed;
12985 break;
12986 }
12987 if (max_response < wcqe->total_data_placed)
12988 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12989 else
12990 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12991 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012992 }
James Smart341af102010-01-26 23:07:37 -050012993
James Smartacd68592012-01-18 16:25:09 -050012994 /* Convert BG errors for completion status */
12995 if (status == CQE_STATUS_DI_ERROR) {
12996 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12997
12998 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12999 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
13000 else
13001 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
13002
13003 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
13004 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
13005 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13006 BGS_GUARD_ERR_MASK;
13007 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
13008 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13009 BGS_APPTAG_ERR_MASK;
13010 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
13011 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13012 BGS_REFTAG_ERR_MASK;
13013
13014 /* Check to see if there was any good data before the error */
13015 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
13016 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13017 BGS_HI_WATER_MARK_PRESENT_MASK;
13018 pIocbIn->iocb.unsli3.sli3_bg.bghm =
13019 wcqe->total_data_placed;
13020 }
13021
13022 /*
13023 * Set ALL the error bits to indicate we don't know what
13024 * type of error it is.
13025 */
13026 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
13027 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13028 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
13029 BGS_GUARD_ERR_MASK);
13030 }
13031
James Smart341af102010-01-26 23:07:37 -050013032 /* Pick up HBA exchange busy condition */
13033 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
13034 spin_lock_irqsave(&phba->hbalock, iflags);
13035 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
13036 spin_unlock_irqrestore(&phba->hbalock, iflags);
13037 }
James Smart4f774512009-05-22 14:52:35 -040013038}
13039
13040/**
James Smart45ed1192009-10-02 15:17:02 -040013041 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
13042 * @phba: Pointer to HBA context object.
13043 * @wcqe: Pointer to work-queue completion queue entry.
13044 *
13045 * This routine handles an ELS work-queue completion event and construct
13046 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
13047 * discovery engine to handle.
13048 *
13049 * Return: Pointer to the receive IOCBQ, NULL otherwise.
13050 **/
13051static struct lpfc_iocbq *
13052lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
13053 struct lpfc_iocbq *irspiocbq)
13054{
James Smart895427b2017-02-12 13:52:30 -080013055 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040013056 struct lpfc_iocbq *cmdiocbq;
13057 struct lpfc_wcqe_complete *wcqe;
13058 unsigned long iflags;
13059
James Smart895427b2017-02-12 13:52:30 -080013060 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070013061 if (unlikely(!pring))
13062 return NULL;
James Smart895427b2017-02-12 13:52:30 -080013063
James Smart45ed1192009-10-02 15:17:02 -040013064 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040013065 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040013066 pring->stats.iocb_event++;
13067 /* Look up the ELS command IOCB and create pseudo response IOCB */
13068 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13069 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040013070 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070013071 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040013072 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13073 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070013074 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
13075 wcqe->word0, wcqe->total_data_placed,
13076 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040013077 lpfc_sli_release_iocbq(phba, irspiocbq);
13078 return NULL;
13079 }
13080
Dick Kennedy401bb412017-09-29 17:34:28 -070013081 /* Put the iocb back on the txcmplq */
13082 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13083 spin_unlock_irqrestore(&pring->ring_lock, iflags);
13084
James Smart45ed1192009-10-02 15:17:02 -040013085 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013086 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040013087
13088 return irspiocbq;
13089}
13090
James Smart8a5ca102017-11-20 16:00:30 -080013091inline struct lpfc_cq_event *
13092lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13093{
13094 struct lpfc_cq_event *cq_event;
13095
13096 /* Allocate a new internal CQ_EVENT entry */
13097 cq_event = lpfc_sli4_cq_event_alloc(phba);
13098 if (!cq_event) {
13099 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13100 "0602 Failed to alloc CQ_EVENT entry\n");
13101 return NULL;
13102 }
13103
13104 /* Move the CQE into the event */
13105 memcpy(&cq_event->cqe, entry, size);
13106 return cq_event;
13107}
13108
James Smart45ed1192009-10-02 15:17:02 -040013109/**
James Smart04c68492009-05-22 14:52:52 -040013110 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
13111 * @phba: Pointer to HBA context object.
13112 * @cqe: Pointer to mailbox completion queue entry.
13113 *
13114 * This routine process a mailbox completion queue entry with asynchrous
13115 * event.
13116 *
13117 * Return: true if work posted to worker thread, otherwise false.
13118 **/
13119static bool
13120lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13121{
13122 struct lpfc_cq_event *cq_event;
13123 unsigned long iflags;
13124
13125 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13126 "0392 Async Event: word0:x%x, word1:x%x, "
13127 "word2:x%x, word3:x%x\n", mcqe->word0,
13128 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13129
James Smart8a5ca102017-11-20 16:00:30 -080013130 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13131 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040013132 return false;
James Smart04c68492009-05-22 14:52:52 -040013133 spin_lock_irqsave(&phba->hbalock, iflags);
13134 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13135 /* Set the async event flag */
13136 phba->hba_flag |= ASYNC_EVENT;
13137 spin_unlock_irqrestore(&phba->hbalock, iflags);
13138
13139 return true;
13140}
13141
13142/**
13143 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13144 * @phba: Pointer to HBA context object.
13145 * @cqe: Pointer to mailbox completion queue entry.
13146 *
13147 * This routine process a mailbox completion queue entry with mailbox
13148 * completion event.
13149 *
13150 * Return: true if work posted to worker thread, otherwise false.
13151 **/
13152static bool
13153lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13154{
13155 uint32_t mcqe_status;
13156 MAILBOX_t *mbox, *pmbox;
13157 struct lpfc_mqe *mqe;
13158 struct lpfc_vport *vport;
13159 struct lpfc_nodelist *ndlp;
13160 struct lpfc_dmabuf *mp;
13161 unsigned long iflags;
13162 LPFC_MBOXQ_t *pmb;
13163 bool workposted = false;
13164 int rc;
13165
13166 /* If not a mailbox complete MCQE, out by checking mailbox consume */
13167 if (!bf_get(lpfc_trailer_completed, mcqe))
13168 goto out_no_mqe_complete;
13169
13170 /* Get the reference to the active mbox command */
13171 spin_lock_irqsave(&phba->hbalock, iflags);
13172 pmb = phba->sli.mbox_active;
13173 if (unlikely(!pmb)) {
13174 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13175 "1832 No pending MBOX command to handle\n");
13176 spin_unlock_irqrestore(&phba->hbalock, iflags);
13177 goto out_no_mqe_complete;
13178 }
13179 spin_unlock_irqrestore(&phba->hbalock, iflags);
13180 mqe = &pmb->u.mqe;
13181 pmbox = (MAILBOX_t *)&pmb->u.mqe;
13182 mbox = phba->mbox;
13183 vport = pmb->vport;
13184
13185 /* Reset heartbeat timer */
13186 phba->last_completion_time = jiffies;
13187 del_timer(&phba->sli.mbox_tmo);
13188
13189 /* Move mbox data to caller's mailbox region, do endian swapping */
13190 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070013191 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040013192
James Smart73d91e52011-10-10 21:32:10 -040013193 /*
13194 * For mcqe errors, conditionally move a modified error code to
13195 * the mbox so that the error will not be missed.
13196 */
13197 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13198 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13199 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13200 bf_set(lpfc_mqe_status, mqe,
13201 (LPFC_MBX_ERROR_RANGE | mcqe_status));
13202 }
James Smart04c68492009-05-22 14:52:52 -040013203 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13204 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13205 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13206 "MBOX dflt rpi: status:x%x rpi:x%x",
13207 mcqe_status,
13208 pmbox->un.varWords[0], 0);
13209 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
James Smart3e1f0712018-11-29 16:09:29 -080013210 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13211 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart04c68492009-05-22 14:52:52 -040013212 /* Reg_LOGIN of dflt RPI was successful. Now lets get
13213 * RID of the PPI using the same mbox buffer.
13214 */
13215 lpfc_unreg_login(phba, vport->vpi,
13216 pmbox->un.varWords[0], pmb);
13217 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080013218 pmb->ctx_buf = mp;
13219 pmb->ctx_ndlp = ndlp;
James Smart04c68492009-05-22 14:52:52 -040013220 pmb->vport = vport;
13221 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13222 if (rc != MBX_BUSY)
13223 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
13224 LOG_SLI, "0385 rc should "
13225 "have been MBX_BUSY\n");
13226 if (rc != MBX_NOT_FINISHED)
13227 goto send_current_mbox;
13228 }
13229 }
13230 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13231 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13232 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13233
13234 /* There is mailbox completion work to do */
13235 spin_lock_irqsave(&phba->hbalock, iflags);
13236 __lpfc_mbox_cmpl_put(phba, pmb);
13237 phba->work_ha |= HA_MBATT;
13238 spin_unlock_irqrestore(&phba->hbalock, iflags);
13239 workposted = true;
13240
13241send_current_mbox:
13242 spin_lock_irqsave(&phba->hbalock, iflags);
13243 /* Release the mailbox command posting token */
13244 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13245 /* Setting active mailbox pointer need to be in sync to flag clear */
13246 phba->sli.mbox_active = NULL;
13247 spin_unlock_irqrestore(&phba->hbalock, iflags);
13248 /* Wake up worker thread to post the next pending mailbox command */
13249 lpfc_worker_wake_up(phba);
13250out_no_mqe_complete:
13251 if (bf_get(lpfc_trailer_consumed, mcqe))
13252 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13253 return workposted;
13254}
13255
13256/**
13257 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13258 * @phba: Pointer to HBA context object.
13259 * @cqe: Pointer to mailbox completion queue entry.
13260 *
13261 * This routine process a mailbox completion queue entry, it invokes the
13262 * proper mailbox complete handling or asynchrous event handling routine
13263 * according to the MCQE's async bit.
13264 *
13265 * Return: true if work posted to worker thread, otherwise false.
13266 **/
13267static bool
13268lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
13269{
13270 struct lpfc_mcqe mcqe;
13271 bool workposted;
13272
13273 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013274 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040013275
13276 /* Invoke the proper event handling routine */
13277 if (!bf_get(lpfc_trailer_async, &mcqe))
13278 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13279 else
13280 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13281 return workposted;
13282}
13283
13284/**
James Smart4f774512009-05-22 14:52:35 -040013285 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13286 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040013287 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040013288 * @wcqe: Pointer to work-queue completion queue entry.
13289 *
13290 * This routine handles an ELS work-queue completion event.
13291 *
13292 * Return: true if work posted to worker thread, otherwise false.
13293 **/
13294static bool
James Smart2a76a282012-08-03 12:35:54 -040013295lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013296 struct lpfc_wcqe_complete *wcqe)
13297{
James Smart4f774512009-05-22 14:52:35 -040013298 struct lpfc_iocbq *irspiocbq;
13299 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040013300 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050013301 int txq_cnt = 0;
13302 int txcmplq_cnt = 0;
13303 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040013304
James Smart11f0e342018-05-04 20:37:57 -070013305 /* Check for response status */
13306 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13307 /* Log the error status */
13308 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13309 "0357 ELS CQE error: status=x%x: "
13310 "CQE: %08x %08x %08x %08x\n",
13311 bf_get(lpfc_wcqe_c_status, wcqe),
13312 wcqe->word0, wcqe->total_data_placed,
13313 wcqe->parameter, wcqe->word3);
13314 }
13315
James Smart45ed1192009-10-02 15:17:02 -040013316 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040013317 irspiocbq = lpfc_sli_get_iocbq(phba);
13318 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013319 if (!list_empty(&pring->txq))
13320 txq_cnt++;
13321 if (!list_empty(&pring->txcmplq))
13322 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040013323 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040013324 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13325 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050013326 txq_cnt, phba->iocb_cnt,
13327 fcp_txcmplq_cnt,
13328 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040013329 return false;
James Smart4f774512009-05-22 14:52:35 -040013330 }
James Smart4f774512009-05-22 14:52:35 -040013331
James Smart45ed1192009-10-02 15:17:02 -040013332 /* Save off the slow-path queue event for work thread to process */
13333 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040013334 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040013335 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013336 &phba->sli4_hba.sp_queue_event);
13337 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013338 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013339
James Smart45ed1192009-10-02 15:17:02 -040013340 return true;
James Smart4f774512009-05-22 14:52:35 -040013341}
13342
13343/**
13344 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13345 * @phba: Pointer to HBA context object.
13346 * @wcqe: Pointer to work-queue completion queue entry.
13347 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013348 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013349 * proper WQ release routine to the slow-path WQ.
13350 **/
13351static void
13352lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13353 struct lpfc_wcqe_release *wcqe)
13354{
James Smart2e90f4b2011-12-13 13:22:37 -050013355 /* sanity check on queue memory */
13356 if (unlikely(!phba->sli4_hba.els_wq))
13357 return;
James Smart4f774512009-05-22 14:52:35 -040013358 /* Check for the slow-path ELS work queue */
13359 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13360 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13361 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13362 else
13363 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13364 "2579 Slow-path wqe consume event carries "
13365 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13366 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13367 phba->sli4_hba.els_wq->queue_id);
13368}
13369
13370/**
13371 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13372 * @phba: Pointer to HBA context object.
13373 * @cq: Pointer to a WQ completion queue.
13374 * @wcqe: Pointer to work-queue completion queue entry.
13375 *
13376 * This routine handles an XRI abort event.
13377 *
13378 * Return: true if work posted to worker thread, otherwise false.
13379 **/
13380static bool
13381lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13382 struct lpfc_queue *cq,
13383 struct sli4_wcqe_xri_aborted *wcqe)
13384{
13385 bool workposted = false;
13386 struct lpfc_cq_event *cq_event;
13387 unsigned long iflags;
13388
James Smart4f774512009-05-22 14:52:35 -040013389 switch (cq->subtype) {
13390 case LPFC_FCP:
James Smart5e5b5112019-01-28 11:14:22 -080013391 lpfc_sli4_fcp_xri_aborted(phba, wcqe, cq->hdwq);
13392 workposted = false;
James Smart4f774512009-05-22 14:52:35 -040013393 break;
James Smart422c4cb2017-11-20 16:00:32 -080013394 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013395 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013396 cq_event = lpfc_cq_event_setup(
13397 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13398 if (!cq_event)
13399 return false;
James Smart5e5b5112019-01-28 11:14:22 -080013400 cq_event->hdwq = cq->hdwq;
James Smart4f774512009-05-22 14:52:35 -040013401 spin_lock_irqsave(&phba->hbalock, iflags);
13402 list_add_tail(&cq_event->list,
13403 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13404 /* Set the els xri abort event flag */
13405 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13406 spin_unlock_irqrestore(&phba->hbalock, iflags);
13407 workposted = true;
13408 break;
James Smart318083a2017-03-04 09:30:30 -080013409 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080013410 /* Notify aborted XRI for NVME work queue */
13411 if (phba->nvmet_support)
13412 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13413 else
James Smart5e5b5112019-01-28 11:14:22 -080013414 lpfc_sli4_nvme_xri_aborted(phba, wcqe, cq->hdwq);
James Smart8a5ca102017-11-20 16:00:30 -080013415
13416 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080013417 break;
James Smart4f774512009-05-22 14:52:35 -040013418 default:
13419 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013420 "0603 Invalid CQ subtype %d: "
13421 "%08x %08x %08x %08x\n",
13422 cq->subtype, wcqe->word0, wcqe->parameter,
13423 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013424 workposted = false;
13425 break;
13426 }
13427 return workposted;
13428}
13429
James Smarte817e5d2018-12-13 15:17:53 -080013430#define FC_RCTL_MDS_DIAGS 0xF4
13431
James Smart4f774512009-05-22 14:52:35 -040013432/**
James Smart4d9ab992009-10-02 15:16:39 -040013433 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013434 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013435 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013436 *
James Smart4d9ab992009-10-02 15:16:39 -040013437 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013438 *
13439 * Return: true if work posted to worker thread, otherwise false.
13440 **/
13441static bool
James Smart4d9ab992009-10-02 15:16:39 -040013442lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13443{
13444 bool workposted = false;
James Smarte817e5d2018-12-13 15:17:53 -080013445 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013446 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13447 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013448 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013449 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013450 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013451 unsigned long iflags;
13452
James Smart2e90f4b2011-12-13 13:22:37 -050013453 /* sanity check on queue memory */
13454 if (unlikely(!hrq) || unlikely(!drq))
13455 return workposted;
13456
James Smart7851fe22011-07-22 18:36:52 -040013457 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13458 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13459 else
13460 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13461 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013462 goto out;
13463
13464 status = bf_get(lpfc_rcqe_status, rcqe);
13465 switch (status) {
13466 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13467 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13468 "2537 Receive Frame Truncated!!\n");
13469 case FC_STATUS_RQ_SUCCESS:
13470 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013471 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013472 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13473 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013474 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013475 spin_unlock_irqrestore(&phba->hbalock, iflags);
13476 goto out;
13477 }
James Smartb84daac2012-08-03 12:35:13 -040013478 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013479 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013480 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013481
James Smarte817e5d2018-12-13 15:17:53 -080013482 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13483
13484 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
13485 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
13486 spin_unlock_irqrestore(&phba->hbalock, iflags);
13487 /* Handle MDS Loopback frames */
13488 lpfc_sli4_handle_mds_loopback(phba->pport, dma_buf);
13489 break;
13490 }
13491
13492 /* save off the frame for the work thread to process */
James Smart4d9ab992009-10-02 15:16:39 -040013493 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013494 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013495 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013496 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013497 spin_unlock_irqrestore(&phba->hbalock, iflags);
13498 workposted = true;
13499 break;
James Smart4d9ab992009-10-02 15:16:39 -040013500 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013501 if (phba->nvmet_support) {
13502 tgtp = phba->targetport->private;
13503 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13504 "6402 RQE Error x%x, posted %d err_cnt "
13505 "%d: %x %x %x\n",
13506 status, hrq->RQ_buf_posted,
13507 hrq->RQ_no_posted_buf,
13508 atomic_read(&tgtp->rcv_fcp_cmd_in),
13509 atomic_read(&tgtp->rcv_fcp_cmd_out),
13510 atomic_read(&tgtp->xmt_fcp_release));
13511 }
13512 /* fallthrough */
13513
13514 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013515 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013516 /* Post more buffers if possible */
13517 spin_lock_irqsave(&phba->hbalock, iflags);
13518 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13519 spin_unlock_irqrestore(&phba->hbalock, iflags);
13520 workposted = true;
13521 break;
13522 }
13523out:
13524 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013525}
13526
13527/**
13528 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13529 * @phba: Pointer to HBA context object.
13530 * @cq: Pointer to the completion queue.
13531 * @wcqe: Pointer to a completion queue entry.
13532 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013533 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013534 * entry.
13535 *
13536 * Return: true if work posted to worker thread, otherwise false.
13537 **/
13538static bool
13539lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013540 struct lpfc_cqe *cqe)
13541{
James Smart45ed1192009-10-02 15:17:02 -040013542 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013543 bool workposted = false;
13544
13545 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013546 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013547
13548 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013549 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013550 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013551 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013552 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013553 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013554 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013555 break;
13556 case CQE_CODE_RELEASE_WQE:
13557 /* Process the WQ release event */
13558 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013559 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013560 break;
13561 case CQE_CODE_XRI_ABORTED:
13562 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013563 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013564 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013565 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013566 break;
James Smart4d9ab992009-10-02 15:16:39 -040013567 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013568 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013569 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013570 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013571 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013572 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013573 break;
James Smart4f774512009-05-22 14:52:35 -040013574 default:
13575 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13576 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013577 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013578 break;
13579 }
13580 return workposted;
13581}
13582
13583/**
James Smart4f774512009-05-22 14:52:35 -040013584 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13585 * @phba: Pointer to HBA context object.
13586 * @eqe: Pointer to fast-path event queue entry.
13587 *
13588 * This routine process a event queue entry from the slow-path event queue.
13589 * It will check the MajorCode and MinorCode to determine this is for a
13590 * completion event on a completion queue, if not, an error shall be logged
13591 * and just return. Otherwise, it will get to the corresponding completion
13592 * queue and process all the entries on that completion queue, rearm the
13593 * completion queue, and then return.
13594 *
13595 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013596static void
James Smart67d12732012-08-03 12:36:13 -040013597lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13598 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013599{
James Smart67d12732012-08-03 12:36:13 -040013600 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013601 uint16_t cqid;
13602
James Smart4f774512009-05-22 14:52:35 -040013603 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013604 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013605
James Smart4f774512009-05-22 14:52:35 -040013606 list_for_each_entry(childq, &speq->child_list, list) {
13607 if (childq->queue_id == cqid) {
13608 cq = childq;
13609 break;
13610 }
13611 }
13612 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013613 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13614 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13615 "0365 Slow-path CQ identifier "
13616 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013617 return;
James Smart4f774512009-05-22 14:52:35 -040013618 }
13619
James Smart895427b2017-02-12 13:52:30 -080013620 /* Save EQ associated with this CQ */
13621 cq->assoc_qp = speq;
13622
James Smart6a828b02019-01-28 11:14:31 -080013623 if (!queue_work_on(cq->chann, phba->wq, &cq->spwork))
Dick Kennedyf485c182017-09-29 17:34:34 -070013624 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13625 "0390 Cannot schedule soft IRQ "
13626 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13627 cqid, cq->queue_id, smp_processor_id());
13628}
13629
13630/**
13631 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13632 * @phba: Pointer to HBA context object.
13633 *
13634 * This routine process a event queue entry from the slow-path event queue.
13635 * It will check the MajorCode and MinorCode to determine this is for a
13636 * completion event on a completion queue, if not, an error shall be logged
13637 * and just return. Otherwise, it will get to the corresponding completion
13638 * queue and process all the entries on that completion queue, rearm the
13639 * completion queue, and then return.
13640 *
13641 **/
13642static void
13643lpfc_sli4_sp_process_cq(struct work_struct *work)
13644{
13645 struct lpfc_queue *cq =
13646 container_of(work, struct lpfc_queue, spwork);
13647 struct lpfc_hba *phba = cq->phba;
13648 struct lpfc_cqe *cqe;
13649 bool workposted = false;
13650 int ccount = 0;
13651
James Smart4f774512009-05-22 14:52:35 -040013652 /* Process all the entries to the CQ */
13653 switch (cq->type) {
13654 case LPFC_MCQ:
13655 while ((cqe = lpfc_sli4_cq_get(cq))) {
13656 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013657 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013658 break;
James Smartb84daac2012-08-03 12:35:13 -040013659 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013660 }
13661 break;
13662 case LPFC_WCQ:
13663 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013664 if (cq->subtype == LPFC_FCP ||
13665 cq->subtype == LPFC_NVME) {
13666#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13667 if (phba->ktime_on)
13668 cq->isr_timestamp = ktime_get_ns();
13669 else
13670 cq->isr_timestamp = 0;
13671#endif
James Smart895427b2017-02-12 13:52:30 -080013672 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013673 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013674 } else {
James Smart05580562011-05-24 11:40:48 -040013675 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13676 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013677 }
Dick Kennedyf485c182017-09-29 17:34:34 -070013678 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013679 break;
James Smart4f774512009-05-22 14:52:35 -040013680 }
James Smartb84daac2012-08-03 12:35:13 -040013681
13682 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013683 if (ccount > cq->CQ_max_cqe)
13684 cq->CQ_max_cqe = ccount;
James Smart4f774512009-05-22 14:52:35 -040013685 break;
13686 default:
13687 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13688 "0370 Invalid completion queue type (%d)\n",
13689 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013690 return;
James Smart4f774512009-05-22 14:52:35 -040013691 }
13692
13693 /* Catch the no cq entry condition, log an error */
Dick Kennedyf485c182017-09-29 17:34:34 -070013694 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013695 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13696 "0371 No entry from the CQ: identifier "
13697 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13698
13699 /* In any case, flash and re-arm the RCQ */
James Smartb71413d2018-02-22 08:18:40 -080013700 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013701
13702 /* wake up worker thread if there are works to be done */
13703 if (workposted)
13704 lpfc_worker_wake_up(phba);
13705}
13706
13707/**
13708 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013709 * @phba: Pointer to HBA context object.
13710 * @cq: Pointer to associated CQ
13711 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013712 *
13713 * This routine process a fast-path work queue completion entry from fast-path
13714 * event queue for FCP command response completion.
13715 **/
13716static void
James Smart2a76a282012-08-03 12:35:54 -040013717lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013718 struct lpfc_wcqe_complete *wcqe)
13719{
James Smart2a76a282012-08-03 12:35:54 -040013720 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013721 struct lpfc_iocbq *cmdiocbq;
13722 struct lpfc_iocbq irspiocbq;
13723 unsigned long iflags;
13724
James Smart4f774512009-05-22 14:52:35 -040013725 /* Check for response status */
13726 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13727 /* If resource errors reported from HBA, reduce queue
13728 * depth of the SCSI device.
13729 */
James Smarte3d2b802012-08-14 14:25:43 -040013730 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13731 IOSTAT_LOCAL_REJECT)) &&
13732 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13733 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013734 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013735
James Smart4f774512009-05-22 14:52:35 -040013736 /* Log the error status */
James Smart11f0e342018-05-04 20:37:57 -070013737 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13738 "0373 FCP CQE error: status=x%x: "
13739 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013740 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070013741 wcqe->word0, wcqe->total_data_placed,
13742 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013743 }
13744
13745 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013746 spin_lock_irqsave(&pring->ring_lock, iflags);
13747 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013748 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13749 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013750 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013751 if (unlikely(!cmdiocbq)) {
13752 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13753 "0374 FCP complete with no corresponding "
13754 "cmdiocb: iotag (%d)\n",
13755 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13756 return;
13757 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013758#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13759 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13760#endif
James Smart895427b2017-02-12 13:52:30 -080013761 if (cmdiocbq->iocb_cmpl == NULL) {
13762 if (cmdiocbq->wqe_cmpl) {
13763 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13764 spin_lock_irqsave(&phba->hbalock, iflags);
13765 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13766 spin_unlock_irqrestore(&phba->hbalock, iflags);
13767 }
13768
13769 /* Pass the cmd_iocb and the wcqe to the upper layer */
13770 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13771 return;
13772 }
James Smart4f774512009-05-22 14:52:35 -040013773 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13774 "0375 FCP cmdiocb not callback function "
13775 "iotag: (%d)\n",
13776 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13777 return;
13778 }
13779
13780 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013781 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013782
James Smart0f65ff62010-02-26 14:14:23 -050013783 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13784 spin_lock_irqsave(&phba->hbalock, iflags);
13785 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13786 spin_unlock_irqrestore(&phba->hbalock, iflags);
13787 }
13788
James Smart4f774512009-05-22 14:52:35 -040013789 /* Pass the cmd_iocb and the rsp state to the upper layer */
13790 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13791}
13792
13793/**
13794 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13795 * @phba: Pointer to HBA context object.
13796 * @cq: Pointer to completion queue.
13797 * @wcqe: Pointer to work-queue completion queue entry.
13798 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013799 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013800 * proper WQ release routine to the slow-path WQ.
13801 **/
13802static void
13803lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13804 struct lpfc_wcqe_release *wcqe)
13805{
13806 struct lpfc_queue *childwq;
13807 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013808 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013809
13810 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013811 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013812 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013813 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013814 lpfc_sli4_wq_release(childwq,
13815 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013816 if (childwq->q_flag & HBA_NVMET_WQFULL)
13817 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013818 wqid_matched = true;
13819 break;
13820 }
13821 }
13822 /* Report warning log message if no match found */
13823 if (wqid_matched != true)
13824 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13825 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013826 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013827}
13828
13829/**
James Smart2d7dbc42017-02-12 13:52:35 -080013830 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13831 * @phba: Pointer to HBA context object.
13832 * @rcqe: Pointer to receive-queue completion queue entry.
13833 *
13834 * This routine process a receive-queue completion queue entry.
13835 *
13836 * Return: true if work posted to worker thread, otherwise false.
13837 **/
13838static bool
13839lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13840 struct lpfc_rcqe *rcqe)
13841{
13842 bool workposted = false;
13843 struct lpfc_queue *hrq;
13844 struct lpfc_queue *drq;
13845 struct rqb_dmabuf *dma_buf;
13846 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013847 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013848 uint32_t status, rq_id;
13849 unsigned long iflags;
13850 uint32_t fctl, idx;
13851
13852 if ((phba->nvmet_support == 0) ||
13853 (phba->sli4_hba.nvmet_cqset == NULL))
13854 return workposted;
13855
13856 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13857 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13858 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13859
13860 /* sanity check on queue memory */
13861 if (unlikely(!hrq) || unlikely(!drq))
13862 return workposted;
13863
13864 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13865 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13866 else
13867 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13868
13869 if ((phba->nvmet_support == 0) ||
13870 (rq_id != hrq->queue_id))
13871 return workposted;
13872
13873 status = bf_get(lpfc_rcqe_status, rcqe);
13874 switch (status) {
13875 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13876 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13877 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013878 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013879 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013880 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013881 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013882 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13883 if (!dma_buf) {
13884 hrq->RQ_no_buf_found++;
13885 spin_unlock_irqrestore(&phba->hbalock, iflags);
13886 goto out;
13887 }
13888 spin_unlock_irqrestore(&phba->hbalock, iflags);
13889 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013890 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013891 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13892
13893 /* Just some basic sanity checks on FCP Command frame */
13894 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13895 fc_hdr->fh_f_ctl[1] << 8 |
13896 fc_hdr->fh_f_ctl[2]);
13897 if (((fctl &
13898 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13899 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13900 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13901 goto drop;
13902
13903 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13904 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013905 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070013906 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013907 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013908 return false;
13909 }
13910drop:
13911 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13912 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013913 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013914 if (phba->nvmet_support) {
13915 tgtp = phba->targetport->private;
13916 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13917 "6401 RQE Error x%x, posted %d err_cnt "
13918 "%d: %x %x %x\n",
13919 status, hrq->RQ_buf_posted,
13920 hrq->RQ_no_posted_buf,
13921 atomic_read(&tgtp->rcv_fcp_cmd_in),
13922 atomic_read(&tgtp->rcv_fcp_cmd_out),
13923 atomic_read(&tgtp->xmt_fcp_release));
13924 }
13925 /* fallthrough */
13926
13927 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013928 hrq->RQ_no_posted_buf++;
13929 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013930 break;
13931 }
13932out:
13933 return workposted;
13934}
13935
13936/**
James Smart895427b2017-02-12 13:52:30 -080013937 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013938 * @cq: Pointer to the completion queue.
13939 * @eqe: Pointer to fast-path completion queue entry.
13940 *
13941 * This routine process a fast-path work queue completion entry from fast-path
13942 * event queue for FCP command response completion.
13943 **/
13944static int
James Smart895427b2017-02-12 13:52:30 -080013945lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013946 struct lpfc_cqe *cqe)
13947{
13948 struct lpfc_wcqe_release wcqe;
13949 bool workposted = false;
13950
13951 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013952 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013953
13954 /* Check and process for different type of WCQE and dispatch */
13955 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13956 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013957 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013958 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013959 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013960 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013961 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13962 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13963 (struct lpfc_wcqe_complete *)&wcqe);
13964 if (cq->subtype == LPFC_NVME_LS)
13965 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013966 (struct lpfc_wcqe_complete *)&wcqe);
13967 break;
13968 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013969 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013970 /* Process the WQ release event */
13971 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13972 (struct lpfc_wcqe_release *)&wcqe);
13973 break;
13974 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013975 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013976 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013977 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013978 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13979 (struct sli4_wcqe_xri_aborted *)&wcqe);
13980 break;
James Smart895427b2017-02-12 13:52:30 -080013981 case CQE_CODE_RECEIVE_V1:
13982 case CQE_CODE_RECEIVE:
13983 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013984 if (cq->subtype == LPFC_NVMET) {
13985 workposted = lpfc_sli4_nvmet_handle_rcqe(
13986 phba, cq, (struct lpfc_rcqe *)&wcqe);
13987 }
James Smart895427b2017-02-12 13:52:30 -080013988 break;
James Smart4f774512009-05-22 14:52:35 -040013989 default:
13990 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013991 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013992 bf_get(lpfc_wcqe_c_code, &wcqe));
13993 break;
13994 }
13995 return workposted;
13996}
13997
13998/**
James Smart67d12732012-08-03 12:36:13 -040013999 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040014000 * @phba: Pointer to HBA context object.
14001 * @eqe: Pointer to fast-path event queue entry.
14002 *
14003 * This routine process a event queue entry from the fast-path event queue.
14004 * It will check the MajorCode and MinorCode to determine this is for a
14005 * completion event on a completion queue, if not, an error shall be logged
14006 * and just return. Otherwise, it will get to the corresponding completion
14007 * queue and process all the entries on the completion queue, rearm the
14008 * completion queue, and then return.
14009 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070014010static void
James Smart67d12732012-08-03 12:36:13 -040014011lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14012 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040014013{
James Smart895427b2017-02-12 13:52:30 -080014014 struct lpfc_queue *cq = NULL;
James Smart2d7dbc42017-02-12 13:52:35 -080014015 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040014016
James Smartcb5172e2010-03-15 11:25:07 -040014017 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040014018 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040014019 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040014020 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040014021 bf_get_le32(lpfc_eqe_major_code, eqe),
14022 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070014023 return;
James Smart4f774512009-05-22 14:52:35 -040014024 }
14025
James Smart67d12732012-08-03 12:36:13 -040014026 /* Get the reference to the corresponding CQ */
14027 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14028
James Smart6a828b02019-01-28 11:14:31 -080014029 /* Use the fast lookup method first */
14030 if (cqid <= phba->sli4_hba.cq_max) {
14031 cq = phba->sli4_hba.cq_lookup[cqid];
14032 if (cq)
14033 goto work_cq;
James Smartcdb42be2019-01-28 11:14:21 -080014034 }
14035
14036 /* Next check for NVMET completion */
James Smart2d7dbc42017-02-12 13:52:35 -080014037 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14038 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14039 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14040 /* Process NVMET unsol rcv */
14041 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14042 goto process_cq;
14043 }
14044 }
14045
James Smart895427b2017-02-12 13:52:30 -080014046 if (phba->sli4_hba.nvmels_cq &&
14047 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14048 /* Process NVME unsol rcv */
14049 cq = phba->sli4_hba.nvmels_cq;
14050 }
14051
14052 /* Otherwise this is a Slow path event */
14053 if (cq == NULL) {
James Smartcdb42be2019-01-28 11:14:21 -080014054 lpfc_sli4_sp_handle_eqe(phba, eqe,
14055 phba->sli4_hba.hdwq[qidx].hba_eq);
Dick Kennedyf485c182017-09-29 17:34:34 -070014056 return;
James Smart67d12732012-08-03 12:36:13 -040014057 }
14058
James Smart895427b2017-02-12 13:52:30 -080014059process_cq:
James Smart4f774512009-05-22 14:52:35 -040014060 if (unlikely(cqid != cq->queue_id)) {
14061 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14062 "0368 Miss-matched fast-path completion "
14063 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14064 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070014065 return;
James Smart4f774512009-05-22 14:52:35 -040014066 }
14067
James Smart6a828b02019-01-28 11:14:31 -080014068work_cq:
James Smart45aa3122019-01-28 11:14:29 -080014069 if (!queue_work_on(cq->chann, phba->wq, &cq->irqwork))
Dick Kennedyf485c182017-09-29 17:34:34 -070014070 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14071 "0363 Cannot schedule soft IRQ "
14072 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14073 cqid, cq->queue_id, smp_processor_id());
14074}
14075
14076/**
14077 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14078 * @phba: Pointer to HBA context object.
14079 * @eqe: Pointer to fast-path event queue entry.
14080 *
14081 * This routine process a event queue entry from the fast-path event queue.
14082 * It will check the MajorCode and MinorCode to determine this is for a
14083 * completion event on a completion queue, if not, an error shall be logged
14084 * and just return. Otherwise, it will get to the corresponding completion
14085 * queue and process all the entries on the completion queue, rearm the
14086 * completion queue, and then return.
14087 **/
14088static void
14089lpfc_sli4_hba_process_cq(struct work_struct *work)
14090{
14091 struct lpfc_queue *cq =
14092 container_of(work, struct lpfc_queue, irqwork);
14093 struct lpfc_hba *phba = cq->phba;
14094 struct lpfc_cqe *cqe;
14095 bool workposted = false;
14096 int ccount = 0;
14097
James Smart4f774512009-05-22 14:52:35 -040014098 /* Process all the entries to the CQ */
14099 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070014100#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
14101 if (phba->ktime_on)
14102 cq->isr_timestamp = ktime_get_ns();
14103 else
14104 cq->isr_timestamp = 0;
14105#endif
James Smart895427b2017-02-12 13:52:30 -080014106 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070014107 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070014108 break;
James Smart4f774512009-05-22 14:52:35 -040014109 }
14110
James Smartb84daac2012-08-03 12:35:13 -040014111 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070014112 if (ccount > cq->CQ_max_cqe)
14113 cq->CQ_max_cqe = ccount;
14114 cq->assoc_qp->EQ_cqe_cnt += ccount;
James Smartb84daac2012-08-03 12:35:13 -040014115
James Smart4f774512009-05-22 14:52:35 -040014116 /* Catch the no cq entry condition */
Dick Kennedyf485c182017-09-29 17:34:34 -070014117 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040014118 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14119 "0369 No entry from fast-path completion "
14120 "queue fcpcqid=%d\n", cq->queue_id);
14121
14122 /* In any case, flash and re-arm the CQ */
James Smartb71413d2018-02-22 08:18:40 -080014123 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014124
14125 /* wake up worker thread if there are works to be done */
14126 if (workposted)
14127 lpfc_worker_wake_up(phba);
14128}
14129
14130static void
14131lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
14132{
14133 struct lpfc_eqe *eqe;
14134
14135 /* walk all the EQ entries and drop on the floor */
14136 while ((eqe = lpfc_sli4_eq_get(eq)))
14137 ;
14138
14139 /* Clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -080014140 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014141}
14142
James Smart1ba981f2014-02-20 09:56:45 -050014143
14144/**
James Smart67d12732012-08-03 12:36:13 -040014145 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040014146 * @irq: Interrupt number.
14147 * @dev_id: The device context pointer.
14148 *
14149 * This function is directly called from the PCI layer as an interrupt
14150 * service routine when device with SLI-4 interface spec is enabled with
14151 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14152 * ring event in the HBA. However, when the device is enabled with either
14153 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14154 * device-level interrupt handler. When the PCI slot is in error recovery
14155 * or the HBA is undergoing initialization, the interrupt handler will not
14156 * process the interrupt. The SCSI FCP fast-path ring event are handled in
14157 * the intrrupt context. This function is called without any lock held.
14158 * It gets the hbalock to access and update SLI data structures. Note that,
14159 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14160 * equal to that of FCP CQ index.
14161 *
James Smart67d12732012-08-03 12:36:13 -040014162 * The link attention and ELS ring attention events are handled
14163 * by the worker thread. The interrupt handler signals the worker thread
14164 * and returns for these events. This function is called without any lock
14165 * held. It gets the hbalock to access and update SLI data structures.
14166 *
James Smart4f774512009-05-22 14:52:35 -040014167 * This function returns IRQ_HANDLED when interrupt is handled else it
14168 * returns IRQ_NONE.
14169 **/
14170irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040014171lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040014172{
14173 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080014174 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040014175 struct lpfc_queue *fpeq;
14176 struct lpfc_eqe *eqe;
14177 unsigned long iflag;
14178 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080014179 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040014180
14181 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014182 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14183 phba = hba_eq_hdl->phba;
14184 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014185
14186 if (unlikely(!phba))
14187 return IRQ_NONE;
James Smartcdb42be2019-01-28 11:14:21 -080014188 if (unlikely(!phba->sli4_hba.hdwq))
James Smart5350d872011-10-10 21:33:49 -040014189 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014190
14191 /* Get to the EQ struct associated with this vector */
James Smartcdb42be2019-01-28 11:14:21 -080014192 fpeq = phba->sli4_hba.hdwq[hba_eqidx].hba_eq;
James Smart2e90f4b2011-12-13 13:22:37 -050014193 if (unlikely(!fpeq))
14194 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014195
14196 /* Check device state for handling interrupt */
14197 if (unlikely(lpfc_intr_state_check(phba))) {
14198 /* Check again for link_state with lock held */
14199 spin_lock_irqsave(&phba->hbalock, iflag);
14200 if (phba->link_state < LPFC_LINK_DOWN)
14201 /* Flush, clear interrupt, and rearm the EQ */
14202 lpfc_sli4_eq_flush(phba, fpeq);
14203 spin_unlock_irqrestore(&phba->hbalock, iflag);
14204 return IRQ_NONE;
14205 }
14206
14207 /*
14208 * Process all the event on FCP fast-path EQ
14209 */
14210 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
Dick Kennedyf485c182017-09-29 17:34:34 -070014211 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14212 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070014213 break;
James Smartb84daac2012-08-03 12:35:13 -040014214 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040014215 }
14216
James Smartb84daac2012-08-03 12:35:13 -040014217 /* Track the max number of EQEs processed in 1 intr */
14218 if (ecount > fpeq->EQ_max_eqe)
14219 fpeq->EQ_max_eqe = ecount;
14220
James Smart4f774512009-05-22 14:52:35 -040014221 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080014222 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014223
14224 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014225 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040014226 if (phba->intr_type == MSIX)
14227 /* MSI-X treated interrupt served as no EQ share INT */
14228 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14229 "0358 MSI-X interrupt with no EQE\n");
14230 else
14231 /* Non MSI-X treated on interrupt as EQ share INT */
14232 return IRQ_NONE;
14233 }
14234
14235 return IRQ_HANDLED;
14236} /* lpfc_sli4_fp_intr_handler */
14237
14238/**
14239 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14240 * @irq: Interrupt number.
14241 * @dev_id: The device context pointer.
14242 *
14243 * This function is the device-level interrupt handler to device with SLI-4
14244 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14245 * interrupt mode is enabled and there is an event in the HBA which requires
14246 * driver attention. This function invokes the slow-path interrupt attention
14247 * handling function and fast-path interrupt attention handling function in
14248 * turn to process the relevant HBA attention events. This function is called
14249 * without any lock held. It gets the hbalock to access and update SLI data
14250 * structures.
14251 *
14252 * This function returns IRQ_HANDLED when interrupt is handled, else it
14253 * returns IRQ_NONE.
14254 **/
14255irqreturn_t
14256lpfc_sli4_intr_handler(int irq, void *dev_id)
14257{
14258 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014259 irqreturn_t hba_irq_rc;
14260 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014261 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014262
14263 /* Get the driver's phba structure from the dev_id */
14264 phba = (struct lpfc_hba *)dev_id;
14265
14266 if (unlikely(!phba))
14267 return IRQ_NONE;
14268
14269 /*
James Smart4f774512009-05-22 14:52:35 -040014270 * Invoke fast-path host attention interrupt handling as appropriate.
14271 */
James Smart6a828b02019-01-28 11:14:31 -080014272 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014273 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014274 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014275 if (hba_irq_rc == IRQ_HANDLED)
14276 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014277 }
14278
James Smart67d12732012-08-03 12:36:13 -040014279 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014280} /* lpfc_sli4_intr_handler */
14281
14282/**
14283 * lpfc_sli4_queue_free - free a queue structure and associated memory
14284 * @queue: The queue structure to free.
14285 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014286 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014287 * the host resident queue. This function must be called after destroying the
14288 * queue on the HBA.
14289 **/
14290void
14291lpfc_sli4_queue_free(struct lpfc_queue *queue)
14292{
14293 struct lpfc_dmabuf *dmabuf;
14294
14295 if (!queue)
14296 return;
14297
14298 while (!list_empty(&queue->page_list)) {
14299 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14300 list);
James Smart81b96ed2017-11-20 16:00:29 -080014301 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014302 dmabuf->virt, dmabuf->phys);
14303 kfree(dmabuf);
14304 }
James Smart895427b2017-02-12 13:52:30 -080014305 if (queue->rqbp) {
14306 lpfc_free_rq_buffer(queue->phba, queue);
14307 kfree(queue->rqbp);
14308 }
James Smartd1f525a2017-04-21 16:04:55 -070014309
14310 if (!list_empty(&queue->wq_list))
14311 list_del(&queue->wq_list);
14312
James Smart4f774512009-05-22 14:52:35 -040014313 kfree(queue);
14314 return;
14315}
14316
14317/**
14318 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14319 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014320 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014321 * @entry_size: The size of each queue entry for this queue.
14322 * @entry count: The number of entries that this queue will handle.
14323 *
14324 * This function allocates a queue structure and the DMAable memory used for
14325 * the host resident queue. This function must be called before creating the
14326 * queue on the HBA.
14327 **/
14328struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014329lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14330 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040014331{
14332 struct lpfc_queue *queue;
14333 struct lpfc_dmabuf *dmabuf;
14334 int x, total_qe_count;
14335 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040014336 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040014337
James Smartcb5172e2010-03-15 11:25:07 -040014338 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014339 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014340
James Smart4f774512009-05-22 14:52:35 -040014341 queue = kzalloc(sizeof(struct lpfc_queue) +
14342 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14343 if (!queue)
14344 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040014345 queue->page_count = (ALIGN(entry_size * entry_count,
14346 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014347
14348 /* If needed, Adjust page count to match the max the adapter supports */
Ewan D. Milne4e87eb22018-12-13 15:25:16 -050014349 if (phba->sli4_hba.pc_sli4_params.wqpcnt &&
14350 (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt))
James Smart895427b2017-02-12 13:52:30 -080014351 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14352
James Smart4f774512009-05-22 14:52:35 -040014353 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014354 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014355 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014356 INIT_LIST_HEAD(&queue->page_list);
14357 INIT_LIST_HEAD(&queue->child_list);
James Smart81b96ed2017-11-20 16:00:29 -080014358
14359 /* Set queue parameters now. If the system cannot provide memory
14360 * resources, the free routine needs to know what was allocated.
14361 */
14362 queue->entry_size = entry_size;
14363 queue->entry_count = entry_count;
14364 queue->page_size = hw_page_size;
14365 queue->phba = phba;
14366
James Smart4f774512009-05-22 14:52:35 -040014367 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14368 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14369 if (!dmabuf)
14370 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040014371 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14372 hw_page_size, &dmabuf->phys,
14373 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014374 if (!dmabuf->virt) {
14375 kfree(dmabuf);
14376 goto out_fail;
14377 }
14378 dmabuf->buffer_tag = x;
14379 list_add_tail(&dmabuf->list, &queue->page_list);
14380 /* initialize queue's entry array */
14381 dma_pointer = dmabuf->virt;
14382 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040014383 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040014384 total_qe_count++, dma_pointer += entry_size) {
14385 queue->qe[total_qe_count].address = dma_pointer;
14386 }
14387 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014388 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14389 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014390
James Smart64eb4dc2017-05-15 15:20:49 -070014391 /* entry_repost will be set during q creation */
14392
James Smart4f774512009-05-22 14:52:35 -040014393 return queue;
14394out_fail:
14395 lpfc_sli4_queue_free(queue);
14396 return NULL;
14397}
14398
14399/**
James Smart962bc512013-01-03 15:44:00 -050014400 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14401 * @phba: HBA structure that indicates port to create a queue on.
14402 * @pci_barset: PCI BAR set flag.
14403 *
14404 * This function shall perform iomap of the specified PCI BAR address to host
14405 * memory address if not already done so and return it. The returned host
14406 * memory address can be NULL.
14407 */
14408static void __iomem *
14409lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14410{
James Smart962bc512013-01-03 15:44:00 -050014411 if (!phba->pcidev)
14412 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014413
14414 switch (pci_barset) {
14415 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014416 return phba->pci_bar0_memmap_p;
14417 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014418 return phba->pci_bar2_memmap_p;
14419 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014420 return phba->pci_bar4_memmap_p;
14421 default:
14422 break;
14423 }
14424 return NULL;
14425}
14426
14427/**
James Smart895427b2017-02-12 13:52:30 -080014428 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040014429 * @phba: HBA structure that indicates port to create a queue on.
14430 * @startq: The starting FCP EQ to modify
14431 *
14432 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080014433 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14434 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040014435 *
14436 * The @phba struct is used to send mailbox command to HBA. The @startq
14437 * is used to get the starting FCP EQ to change.
14438 * This function is asynchronous and will wait for the mailbox
14439 * command to finish before continuing.
14440 *
14441 * On success this function will return a zero. If unable to allocate enough
14442 * memory this function will return -ENOMEM. If the queue create mailbox command
14443 * fails this function will return -ENXIO.
14444 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014445int
James Smart0cf07f842017-06-01 21:07:10 -070014446lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14447 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014448{
14449 struct lpfc_mbx_modify_eq_delay *eq_delay;
14450 LPFC_MBOXQ_t *mbox;
14451 struct lpfc_queue *eq;
14452 int cnt, rc, length, status = 0;
14453 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014454 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014455 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014456 union lpfc_sli4_cfg_shdr *shdr;
14457 uint16_t dmult;
14458
James Smart6a828b02019-01-28 11:14:31 -080014459 if (startq >= phba->cfg_irq_chann)
James Smart173edbb2012-06-12 13:54:50 -040014460 return 0;
14461
14462 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14463 if (!mbox)
14464 return -ENOMEM;
14465 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14466 sizeof(struct lpfc_sli4_cfg_mhdr));
14467 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14468 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14469 length, LPFC_SLI4_MBX_EMBED);
14470 eq_delay = &mbox->u.mqe.un.eq_delay;
14471
14472 /* Calculate delay multiper from maximum interrupt per second */
James Smart6a828b02019-01-28 11:14:31 -080014473 result = imax / phba->cfg_irq_chann;
James Smart895427b2017-02-12 13:52:30 -080014474 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014475 dmult = 0;
14476 else
14477 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014478 if (dmult > LPFC_DMULT_MAX)
14479 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014480
14481 cnt = 0;
James Smart6a828b02019-01-28 11:14:31 -080014482 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smartcdb42be2019-01-28 11:14:21 -080014483 eq = phba->sli4_hba.hdwq[qidx].hba_eq;
James Smart173edbb2012-06-12 13:54:50 -040014484 if (!eq)
14485 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014486 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014487 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14488 eq_delay->u.request.eq[cnt].phase = 0;
14489 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14490 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014491
14492 /* q_mode is only used for auto_imax */
14493 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14494 /* Use EQ Delay Register method for q_mode */
14495
14496 /* Convert for EQ Delay register */
14497 val = phba->cfg_fcp_imax;
14498 if (val) {
14499 /* First, interrupts per sec per EQ */
James Smart6a828b02019-01-28 11:14:31 -080014500 val = phba->cfg_fcp_imax / phba->cfg_irq_chann;
James Smart0cf07f842017-06-01 21:07:10 -070014501
14502 /* us delay between each interrupt */
14503 val = LPFC_SEC_TO_USEC / val;
14504 }
14505 eq->q_mode = val;
14506 } else {
14507 eq->q_mode = imax;
14508 }
14509
14510 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014511 break;
14512 }
14513 eq_delay->u.request.num_eq = cnt;
14514
14515 mbox->vport = phba->pport;
14516 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014517 mbox->ctx_buf = NULL;
14518 mbox->ctx_ndlp = NULL;
James Smart173edbb2012-06-12 13:54:50 -040014519 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14520 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14521 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14522 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14523 if (shdr_status || shdr_add_status || rc) {
14524 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14525 "2512 MODIFY_EQ_DELAY mailbox failed with "
14526 "status x%x add_status x%x, mbx status x%x\n",
14527 shdr_status, shdr_add_status, rc);
14528 status = -ENXIO;
14529 }
14530 mempool_free(mbox, phba->mbox_mem_pool);
14531 return status;
14532}
14533
14534/**
James Smart4f774512009-05-22 14:52:35 -040014535 * lpfc_eq_create - Create an Event Queue on the HBA
14536 * @phba: HBA structure that indicates port to create a queue on.
14537 * @eq: The queue structure to use to create the event queue.
14538 * @imax: The maximum interrupt per second limit.
14539 *
14540 * This function creates an event queue, as detailed in @eq, on a port,
14541 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14542 *
14543 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14544 * is used to get the entry count and entry size that are necessary to
14545 * determine the number of pages to allocate and use for this queue. This
14546 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14547 * event queue. This function is asynchronous and will wait for the mailbox
14548 * command to finish before continuing.
14549 *
14550 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014551 * memory this function will return -ENOMEM. If the queue create mailbox command
14552 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014553 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014554int
James Smartee020062012-09-29 11:28:52 -040014555lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014556{
14557 struct lpfc_mbx_eq_create *eq_create;
14558 LPFC_MBOXQ_t *mbox;
14559 int rc, length, status = 0;
14560 struct lpfc_dmabuf *dmabuf;
14561 uint32_t shdr_status, shdr_add_status;
14562 union lpfc_sli4_cfg_shdr *shdr;
14563 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014564 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14565
James Smart2e90f4b2011-12-13 13:22:37 -050014566 /* sanity check on queue memory */
14567 if (!eq)
14568 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014569 if (!phba->sli4_hba.pc_sli4_params.supported)
14570 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014571
14572 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14573 if (!mbox)
14574 return -ENOMEM;
14575 length = (sizeof(struct lpfc_mbx_eq_create) -
14576 sizeof(struct lpfc_sli4_cfg_mhdr));
14577 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14578 LPFC_MBOX_OPCODE_EQ_CREATE,
14579 length, LPFC_SLI4_MBX_EMBED);
14580 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014581 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014582 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14583 eq->page_count);
14584 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14585 LPFC_EQE_SIZE);
14586 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014587
14588 /* Use version 2 of CREATE_EQ if eqav is set */
14589 if (phba->sli4_hba.pc_sli4_params.eqav) {
14590 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14591 LPFC_Q_CREATE_VERSION_2);
14592 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14593 phba->sli4_hba.pc_sli4_params.eqav);
14594 }
14595
James Smart2c9c5a02015-04-07 15:07:15 -040014596 /* don't setup delay multiplier using EQ_CREATE */
14597 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014598 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14599 dmult);
14600 switch (eq->entry_count) {
14601 default:
14602 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14603 "0360 Unsupported EQ count. (%d)\n",
14604 eq->entry_count);
14605 if (eq->entry_count < 256)
14606 return -EINVAL;
14607 /* otherwise default to smallest count (drop through) */
14608 case 256:
14609 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14610 LPFC_EQ_CNT_256);
14611 break;
14612 case 512:
14613 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14614 LPFC_EQ_CNT_512);
14615 break;
14616 case 1024:
14617 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14618 LPFC_EQ_CNT_1024);
14619 break;
14620 case 2048:
14621 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14622 LPFC_EQ_CNT_2048);
14623 break;
14624 case 4096:
14625 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14626 LPFC_EQ_CNT_4096);
14627 break;
14628 }
14629 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014630 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014631 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14632 putPaddrLow(dmabuf->phys);
14633 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14634 putPaddrHigh(dmabuf->phys);
14635 }
14636 mbox->vport = phba->pport;
14637 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014638 mbox->ctx_buf = NULL;
14639 mbox->ctx_ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -040014640 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040014641 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14642 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14643 if (shdr_status || shdr_add_status || rc) {
14644 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14645 "2500 EQ_CREATE mailbox failed with "
14646 "status x%x add_status x%x, mbx status x%x\n",
14647 shdr_status, shdr_add_status, rc);
14648 status = -ENXIO;
14649 }
14650 eq->type = LPFC_EQ;
14651 eq->subtype = LPFC_NONE;
14652 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14653 if (eq->queue_id == 0xFFFF)
14654 status = -ENXIO;
14655 eq->host_index = 0;
14656 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014657 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014658
James Smart8fa38512009-07-19 10:01:03 -040014659 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014660 return status;
14661}
14662
14663/**
14664 * lpfc_cq_create - Create a Completion Queue on the HBA
14665 * @phba: HBA structure that indicates port to create a queue on.
14666 * @cq: The queue structure to use to create the completion queue.
14667 * @eq: The event queue to bind this completion queue to.
14668 *
14669 * This function creates a completion queue, as detailed in @wq, on a port,
14670 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14671 *
14672 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14673 * is used to get the entry count and entry size that are necessary to
14674 * determine the number of pages to allocate and use for this queue. The @eq
14675 * is used to indicate which event queue to bind this completion queue to. This
14676 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14677 * completion queue. This function is asynchronous and will wait for the mailbox
14678 * command to finish before continuing.
14679 *
14680 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014681 * memory this function will return -ENOMEM. If the queue create mailbox command
14682 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014683 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014684int
James Smart4f774512009-05-22 14:52:35 -040014685lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14686 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14687{
14688 struct lpfc_mbx_cq_create *cq_create;
14689 struct lpfc_dmabuf *dmabuf;
14690 LPFC_MBOXQ_t *mbox;
14691 int rc, length, status = 0;
14692 uint32_t shdr_status, shdr_add_status;
14693 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014694
James Smart2e90f4b2011-12-13 13:22:37 -050014695 /* sanity check on queue memory */
14696 if (!cq || !eq)
14697 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014698
James Smart4f774512009-05-22 14:52:35 -040014699 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14700 if (!mbox)
14701 return -ENOMEM;
14702 length = (sizeof(struct lpfc_mbx_cq_create) -
14703 sizeof(struct lpfc_sli4_cfg_mhdr));
14704 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14705 LPFC_MBOX_OPCODE_CQ_CREATE,
14706 length, LPFC_SLI4_MBX_EMBED);
14707 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014708 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014709 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14710 cq->page_count);
14711 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14712 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014713 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14714 phba->sli4_hba.pc_sli4_params.cqv);
14715 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014716 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14717 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014718 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14719 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080014720 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14721 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050014722 } else {
14723 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14724 eq->queue_id);
14725 }
James Smart4f774512009-05-22 14:52:35 -040014726 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014727 case 2048:
14728 case 4096:
14729 if (phba->sli4_hba.pc_sli4_params.cqv ==
14730 LPFC_Q_CREATE_VERSION_2) {
14731 cq_create->u.request.context.lpfc_cq_context_count =
14732 cq->entry_count;
14733 bf_set(lpfc_cq_context_count,
14734 &cq_create->u.request.context,
14735 LPFC_CQ_CNT_WORD7);
14736 break;
14737 }
14738 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014739 default:
14740 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014741 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014742 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014743 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014744 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014745 if (cq->entry_count < 256) {
14746 status = -EINVAL;
14747 goto out;
14748 }
James Smart4f774512009-05-22 14:52:35 -040014749 /* otherwise default to smallest count (drop through) */
14750 case 256:
14751 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14752 LPFC_CQ_CNT_256);
14753 break;
14754 case 512:
14755 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14756 LPFC_CQ_CNT_512);
14757 break;
14758 case 1024:
14759 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14760 LPFC_CQ_CNT_1024);
14761 break;
14762 }
14763 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014764 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014765 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14766 putPaddrLow(dmabuf->phys);
14767 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14768 putPaddrHigh(dmabuf->phys);
14769 }
14770 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14771
14772 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014773 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14774 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14775 if (shdr_status || shdr_add_status || rc) {
14776 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14777 "2501 CQ_CREATE mailbox failed with "
14778 "status x%x add_status x%x, mbx status x%x\n",
14779 shdr_status, shdr_add_status, rc);
14780 status = -ENXIO;
14781 goto out;
14782 }
14783 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14784 if (cq->queue_id == 0xFFFF) {
14785 status = -ENXIO;
14786 goto out;
14787 }
14788 /* link the cq onto the parent eq child list */
14789 list_add_tail(&cq->list, &eq->child_list);
14790 /* Set up completion queue's type and subtype */
14791 cq->type = type;
14792 cq->subtype = subtype;
14793 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014794 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080014795 cq->assoc_qp = eq;
James Smart4f774512009-05-22 14:52:35 -040014796 cq->host_index = 0;
14797 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014798 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014799
James Smart6a828b02019-01-28 11:14:31 -080014800 if (cq->queue_id > phba->sli4_hba.cq_max)
14801 phba->sli4_hba.cq_max = cq->queue_id;
James Smart8fa38512009-07-19 10:01:03 -040014802out:
14803 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014804 return status;
14805}
14806
14807/**
James Smart2d7dbc42017-02-12 13:52:35 -080014808 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14809 * @phba: HBA structure that indicates port to create a queue on.
14810 * @cqp: The queue structure array to use to create the completion queues.
James Smartcdb42be2019-01-28 11:14:21 -080014811 * @hdwq: The hardware queue array with the EQ to bind completion queues to.
James Smart2d7dbc42017-02-12 13:52:35 -080014812 *
14813 * This function creates a set of completion queue, s to support MRQ
14814 * as detailed in @cqp, on a port,
14815 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14816 *
14817 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14818 * is used to get the entry count and entry size that are necessary to
14819 * determine the number of pages to allocate and use for this queue. The @eq
14820 * is used to indicate which event queue to bind this completion queue to. This
14821 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14822 * completion queue. This function is asynchronous and will wait for the mailbox
14823 * command to finish before continuing.
14824 *
14825 * On success this function will return a zero. If unable to allocate enough
14826 * memory this function will return -ENOMEM. If the queue create mailbox command
14827 * fails this function will return -ENXIO.
14828 **/
14829int
14830lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
James Smartcdb42be2019-01-28 11:14:21 -080014831 struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
14832 uint32_t subtype)
James Smart2d7dbc42017-02-12 13:52:35 -080014833{
14834 struct lpfc_queue *cq;
14835 struct lpfc_queue *eq;
14836 struct lpfc_mbx_cq_create_set *cq_set;
14837 struct lpfc_dmabuf *dmabuf;
14838 LPFC_MBOXQ_t *mbox;
14839 int rc, length, alloclen, status = 0;
14840 int cnt, idx, numcq, page_idx = 0;
14841 uint32_t shdr_status, shdr_add_status;
14842 union lpfc_sli4_cfg_shdr *shdr;
14843 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14844
14845 /* sanity check on queue memory */
14846 numcq = phba->cfg_nvmet_mrq;
James Smartcdb42be2019-01-28 11:14:21 -080014847 if (!cqp || !hdwq || !numcq)
James Smart2d7dbc42017-02-12 13:52:35 -080014848 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014849
14850 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14851 if (!mbox)
14852 return -ENOMEM;
14853
14854 length = sizeof(struct lpfc_mbx_cq_create_set);
14855 length += ((numcq * cqp[0]->page_count) *
14856 sizeof(struct dma_address));
14857 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14858 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14859 LPFC_SLI4_MBX_NEMBED);
14860 if (alloclen < length) {
14861 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14862 "3098 Allocated DMA memory size (%d) is "
14863 "less than the requested DMA memory size "
14864 "(%d)\n", alloclen, length);
14865 status = -ENOMEM;
14866 goto out;
14867 }
14868 cq_set = mbox->sge_array->addr[0];
14869 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14870 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14871
14872 for (idx = 0; idx < numcq; idx++) {
14873 cq = cqp[idx];
James Smartcdb42be2019-01-28 11:14:21 -080014874 eq = hdwq[idx].hba_eq;
James Smart2d7dbc42017-02-12 13:52:35 -080014875 if (!cq || !eq) {
14876 status = -ENOMEM;
14877 goto out;
14878 }
James Smart81b96ed2017-11-20 16:00:29 -080014879 if (!phba->sli4_hba.pc_sli4_params.supported)
14880 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014881
14882 switch (idx) {
14883 case 0:
14884 bf_set(lpfc_mbx_cq_create_set_page_size,
14885 &cq_set->u.request,
14886 (hw_page_size / SLI4_PAGE_SIZE));
14887 bf_set(lpfc_mbx_cq_create_set_num_pages,
14888 &cq_set->u.request, cq->page_count);
14889 bf_set(lpfc_mbx_cq_create_set_evt,
14890 &cq_set->u.request, 1);
14891 bf_set(lpfc_mbx_cq_create_set_valid,
14892 &cq_set->u.request, 1);
14893 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14894 &cq_set->u.request, 0);
14895 bf_set(lpfc_mbx_cq_create_set_num_cq,
14896 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080014897 bf_set(lpfc_mbx_cq_create_set_autovalid,
14898 &cq_set->u.request,
14899 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080014900 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014901 case 2048:
14902 case 4096:
14903 if (phba->sli4_hba.pc_sli4_params.cqv ==
14904 LPFC_Q_CREATE_VERSION_2) {
14905 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14906 &cq_set->u.request,
14907 cq->entry_count);
14908 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14909 &cq_set->u.request,
14910 LPFC_CQ_CNT_WORD7);
14911 break;
14912 }
14913 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080014914 default:
14915 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14916 "3118 Bad CQ count. (%d)\n",
14917 cq->entry_count);
14918 if (cq->entry_count < 256) {
14919 status = -EINVAL;
14920 goto out;
14921 }
14922 /* otherwise default to smallest (drop thru) */
14923 case 256:
14924 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14925 &cq_set->u.request, LPFC_CQ_CNT_256);
14926 break;
14927 case 512:
14928 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14929 &cq_set->u.request, LPFC_CQ_CNT_512);
14930 break;
14931 case 1024:
14932 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14933 &cq_set->u.request, LPFC_CQ_CNT_1024);
14934 break;
14935 }
14936 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14937 &cq_set->u.request, eq->queue_id);
14938 break;
14939 case 1:
14940 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14941 &cq_set->u.request, eq->queue_id);
14942 break;
14943 case 2:
14944 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14945 &cq_set->u.request, eq->queue_id);
14946 break;
14947 case 3:
14948 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14949 &cq_set->u.request, eq->queue_id);
14950 break;
14951 case 4:
14952 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14953 &cq_set->u.request, eq->queue_id);
14954 break;
14955 case 5:
14956 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14957 &cq_set->u.request, eq->queue_id);
14958 break;
14959 case 6:
14960 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14961 &cq_set->u.request, eq->queue_id);
14962 break;
14963 case 7:
14964 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14965 &cq_set->u.request, eq->queue_id);
14966 break;
14967 case 8:
14968 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14969 &cq_set->u.request, eq->queue_id);
14970 break;
14971 case 9:
14972 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14973 &cq_set->u.request, eq->queue_id);
14974 break;
14975 case 10:
14976 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14977 &cq_set->u.request, eq->queue_id);
14978 break;
14979 case 11:
14980 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14981 &cq_set->u.request, eq->queue_id);
14982 break;
14983 case 12:
14984 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14985 &cq_set->u.request, eq->queue_id);
14986 break;
14987 case 13:
14988 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14989 &cq_set->u.request, eq->queue_id);
14990 break;
14991 case 14:
14992 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14993 &cq_set->u.request, eq->queue_id);
14994 break;
14995 case 15:
14996 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14997 &cq_set->u.request, eq->queue_id);
14998 break;
14999 }
15000
15001 /* link the cq onto the parent eq child list */
15002 list_add_tail(&cq->list, &eq->child_list);
15003 /* Set up completion queue's type and subtype */
15004 cq->type = type;
15005 cq->subtype = subtype;
15006 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080015007 cq->assoc_qp = eq;
James Smart2d7dbc42017-02-12 13:52:35 -080015008 cq->host_index = 0;
15009 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070015010 cq->entry_repost = LPFC_CQ_REPOST;
James Smart81b96ed2017-11-20 16:00:29 -080015011 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080015012
15013 rc = 0;
15014 list_for_each_entry(dmabuf, &cq->page_list, list) {
15015 memset(dmabuf->virt, 0, hw_page_size);
15016 cnt = page_idx + dmabuf->buffer_tag;
15017 cq_set->u.request.page[cnt].addr_lo =
15018 putPaddrLow(dmabuf->phys);
15019 cq_set->u.request.page[cnt].addr_hi =
15020 putPaddrHigh(dmabuf->phys);
15021 rc++;
15022 }
15023 page_idx += rc;
15024 }
15025
15026 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15027
15028 /* The IOCTL status is embedded in the mailbox subheader. */
15029 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15030 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15031 if (shdr_status || shdr_add_status || rc) {
15032 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15033 "3119 CQ_CREATE_SET mailbox failed with "
15034 "status x%x add_status x%x, mbx status x%x\n",
15035 shdr_status, shdr_add_status, rc);
15036 status = -ENXIO;
15037 goto out;
15038 }
15039 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15040 if (rc == 0xFFFF) {
15041 status = -ENXIO;
15042 goto out;
15043 }
15044
15045 for (idx = 0; idx < numcq; idx++) {
15046 cq = cqp[idx];
15047 cq->queue_id = rc + idx;
James Smart6a828b02019-01-28 11:14:31 -080015048 if (cq->queue_id > phba->sli4_hba.cq_max)
15049 phba->sli4_hba.cq_max = cq->queue_id;
James Smart2d7dbc42017-02-12 13:52:35 -080015050 }
15051
15052out:
15053 lpfc_sli4_mbox_cmd_free(phba, mbox);
15054 return status;
15055}
15056
15057/**
James Smartb19a0612010-04-06 14:48:51 -040015058 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040015059 * @phba: HBA structure that indicates port to create a queue on.
15060 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040015061 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15062 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040015063 *
James Smartb19a0612010-04-06 14:48:51 -040015064 * This function provides failback (fb) functionality when the
15065 * mq_create_ext fails on older FW generations. It's purpose is identical
15066 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040015067 *
James Smartb19a0612010-04-06 14:48:51 -040015068 * This routine cannot fail as all attributes were previously accessed and
15069 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040015070 **/
James Smartb19a0612010-04-06 14:48:51 -040015071static void
15072lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15073 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040015074{
15075 struct lpfc_mbx_mq_create *mq_create;
15076 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040015077 int length;
James Smart04c68492009-05-22 14:52:52 -040015078
James Smart04c68492009-05-22 14:52:52 -040015079 length = (sizeof(struct lpfc_mbx_mq_create) -
15080 sizeof(struct lpfc_sli4_cfg_mhdr));
15081 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15082 LPFC_MBOX_OPCODE_MQ_CREATE,
15083 length, LPFC_SLI4_MBX_EMBED);
15084 mq_create = &mbox->u.mqe.un.mq_create;
15085 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040015086 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040015087 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040015088 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040015089 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15090 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040015091 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015092 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15093 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040015094 break;
15095 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015096 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15097 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040015098 break;
15099 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015100 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15101 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040015102 break;
15103 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015104 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15105 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040015106 break;
15107 }
15108 list_for_each_entry(dmabuf, &mq->page_list, list) {
15109 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040015110 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040015111 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040015112 putPaddrHigh(dmabuf->phys);
15113 }
15114}
15115
15116/**
15117 * lpfc_mq_create - Create a mailbox Queue on the HBA
15118 * @phba: HBA structure that indicates port to create a queue on.
15119 * @mq: The queue structure to use to create the mailbox queue.
15120 * @cq: The completion queue to associate with this cq.
15121 * @subtype: The queue's subtype.
15122 *
15123 * This function creates a mailbox queue, as detailed in @mq, on a port,
15124 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15125 *
15126 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15127 * is used to get the entry count and entry size that are necessary to
15128 * determine the number of pages to allocate and use for this queue. This
15129 * function will send the MQ_CREATE mailbox command to the HBA to setup the
15130 * mailbox queue. This function is asynchronous and will wait for the mailbox
15131 * command to finish before continuing.
15132 *
15133 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015134 * memory this function will return -ENOMEM. If the queue create mailbox command
15135 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040015136 **/
15137int32_t
15138lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15139 struct lpfc_queue *cq, uint32_t subtype)
15140{
15141 struct lpfc_mbx_mq_create *mq_create;
15142 struct lpfc_mbx_mq_create_ext *mq_create_ext;
15143 struct lpfc_dmabuf *dmabuf;
15144 LPFC_MBOXQ_t *mbox;
15145 int rc, length, status = 0;
15146 uint32_t shdr_status, shdr_add_status;
15147 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015148 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040015149
James Smart2e90f4b2011-12-13 13:22:37 -050015150 /* sanity check on queue memory */
15151 if (!mq || !cq)
15152 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015153 if (!phba->sli4_hba.pc_sli4_params.supported)
15154 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040015155
15156 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15157 if (!mbox)
15158 return -ENOMEM;
15159 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15160 sizeof(struct lpfc_sli4_cfg_mhdr));
15161 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15162 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15163 length, LPFC_SLI4_MBX_EMBED);
15164
15165 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015166 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015167 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15168 &mq_create_ext->u.request, mq->page_count);
15169 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15170 &mq_create_ext->u.request, 1);
15171 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015172 &mq_create_ext->u.request, 1);
15173 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15174 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015175 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15176 &mq_create_ext->u.request, 1);
15177 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15178 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015179 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015180 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15181 phba->sli4_hba.pc_sli4_params.mqv);
15182 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15183 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15184 cq->queue_id);
15185 else
15186 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15187 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015188 switch (mq->entry_count) {
15189 default:
15190 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15191 "0362 Unsupported MQ count. (%d)\n",
15192 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015193 if (mq->entry_count < 16) {
15194 status = -EINVAL;
15195 goto out;
15196 }
James Smartb19a0612010-04-06 14:48:51 -040015197 /* otherwise default to smallest count (drop through) */
15198 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015199 bf_set(lpfc_mq_context_ring_size,
15200 &mq_create_ext->u.request.context,
15201 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015202 break;
15203 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015204 bf_set(lpfc_mq_context_ring_size,
15205 &mq_create_ext->u.request.context,
15206 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015207 break;
15208 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015209 bf_set(lpfc_mq_context_ring_size,
15210 &mq_create_ext->u.request.context,
15211 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015212 break;
15213 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015214 bf_set(lpfc_mq_context_ring_size,
15215 &mq_create_ext->u.request.context,
15216 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015217 break;
15218 }
15219 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015220 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015221 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15222 putPaddrLow(dmabuf->phys);
15223 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015224 putPaddrHigh(dmabuf->phys);
15225 }
15226 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015227 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15228 &mq_create_ext->u.response);
15229 if (rc != MBX_SUCCESS) {
15230 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15231 "2795 MQ_CREATE_EXT failed with "
15232 "status x%x. Failback to MQ_CREATE.\n",
15233 rc);
15234 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15235 mq_create = &mbox->u.mqe.un.mq_create;
15236 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15237 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15238 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15239 &mq_create->u.response);
15240 }
15241
James Smart04c68492009-05-22 14:52:52 -040015242 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015243 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15244 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15245 if (shdr_status || shdr_add_status || rc) {
15246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15247 "2502 MQ_CREATE mailbox failed with "
15248 "status x%x add_status x%x, mbx status x%x\n",
15249 shdr_status, shdr_add_status, rc);
15250 status = -ENXIO;
15251 goto out;
15252 }
James Smart04c68492009-05-22 14:52:52 -040015253 if (mq->queue_id == 0xFFFF) {
15254 status = -ENXIO;
15255 goto out;
15256 }
15257 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015258 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015259 mq->subtype = subtype;
15260 mq->host_index = 0;
15261 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070015262 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040015263
15264 /* link the mq onto the parent cq child list */
15265 list_add_tail(&mq->list, &cq->child_list);
15266out:
James Smart8fa38512009-07-19 10:01:03 -040015267 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015268 return status;
15269}
15270
15271/**
James Smart4f774512009-05-22 14:52:35 -040015272 * lpfc_wq_create - Create a Work Queue on the HBA
15273 * @phba: HBA structure that indicates port to create a queue on.
15274 * @wq: The queue structure to use to create the work queue.
15275 * @cq: The completion queue to bind this work queue to.
15276 * @subtype: The subtype of the work queue indicating its functionality.
15277 *
15278 * This function creates a work queue, as detailed in @wq, on a port, described
15279 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15280 *
15281 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15282 * is used to get the entry count and entry size that are necessary to
15283 * determine the number of pages to allocate and use for this queue. The @cq
15284 * is used to indicate which completion queue to bind this work queue to. This
15285 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15286 * work queue. This function is asynchronous and will wait for the mailbox
15287 * command to finish before continuing.
15288 *
15289 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015290 * memory this function will return -ENOMEM. If the queue create mailbox command
15291 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015292 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015293int
James Smart4f774512009-05-22 14:52:35 -040015294lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15295 struct lpfc_queue *cq, uint32_t subtype)
15296{
15297 struct lpfc_mbx_wq_create *wq_create;
15298 struct lpfc_dmabuf *dmabuf;
15299 LPFC_MBOXQ_t *mbox;
15300 int rc, length, status = 0;
15301 uint32_t shdr_status, shdr_add_status;
15302 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015303 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015304 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015305 void __iomem *bar_memmap_p;
15306 uint32_t db_offset;
15307 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015308 uint8_t dpp_barset;
15309 uint32_t dpp_offset;
15310 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015311 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015312
James Smart2e90f4b2011-12-13 13:22:37 -050015313 /* sanity check on queue memory */
15314 if (!wq || !cq)
15315 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015316 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015317 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015318
15319 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15320 if (!mbox)
15321 return -ENOMEM;
15322 length = (sizeof(struct lpfc_mbx_wq_create) -
15323 sizeof(struct lpfc_sli4_cfg_mhdr));
15324 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15325 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15326 length, LPFC_SLI4_MBX_EMBED);
15327 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015328 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015329 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15330 wq->page_count);
15331 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15332 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015333
15334 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015335 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15336 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015337
James Smartc176ffa2018-01-30 15:58:46 -080015338 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15339 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015340 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15341 else
15342 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15343
James Smart0c651872013-07-15 18:33:23 -040015344
James Smart1351e692018-02-22 08:18:43 -080015345 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15346 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15347 else
15348 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15349
15350 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015351 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015352 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15353 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015354 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15355 LPFC_Q_CREATE_VERSION_1);
15356
James Smart5a6f1332011-03-11 16:05:35 -050015357 switch (wq->entry_size) {
15358 default:
15359 case 64:
15360 bf_set(lpfc_mbx_wq_create_wqe_size,
15361 &wq_create->u.request_1,
15362 LPFC_WQ_WQE_SIZE_64);
15363 break;
15364 case 128:
15365 bf_set(lpfc_mbx_wq_create_wqe_size,
15366 &wq_create->u.request_1,
15367 LPFC_WQ_WQE_SIZE_128);
15368 break;
15369 }
James Smart1351e692018-02-22 08:18:43 -080015370 /* Request DPP by default */
15371 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015372 bf_set(lpfc_mbx_wq_create_page_size,
15373 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015374 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015375 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015376 break;
15377 default:
James Smart1351e692018-02-22 08:18:43 -080015378 page = wq_create->u.request.page;
15379 break;
James Smart5a6f1332011-03-11 16:05:35 -050015380 }
James Smart0c651872013-07-15 18:33:23 -040015381
James Smart4f774512009-05-22 14:52:35 -040015382 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015383 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015384 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15385 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015386 }
James Smart962bc512013-01-03 15:44:00 -050015387
15388 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15389 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15390
James Smart4f774512009-05-22 14:52:35 -040015391 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15392 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015393 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15394 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15395 if (shdr_status || shdr_add_status || rc) {
15396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15397 "2503 WQ_CREATE mailbox failed with "
15398 "status x%x add_status x%x, mbx status x%x\n",
15399 shdr_status, shdr_add_status, rc);
15400 status = -ENXIO;
15401 goto out;
15402 }
James Smart1351e692018-02-22 08:18:43 -080015403
15404 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15405 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15406 &wq_create->u.response);
15407 else
15408 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15409 &wq_create->u.response_1);
15410
James Smart4f774512009-05-22 14:52:35 -040015411 if (wq->queue_id == 0xFFFF) {
15412 status = -ENXIO;
15413 goto out;
15414 }
James Smart1351e692018-02-22 08:18:43 -080015415
15416 wq->db_format = LPFC_DB_LIST_FORMAT;
15417 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15418 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15419 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15420 &wq_create->u.response);
15421 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15422 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15423 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15424 "3265 WQ[%d] doorbell format "
15425 "not supported: x%x\n",
15426 wq->queue_id, wq->db_format);
15427 status = -EINVAL;
15428 goto out;
15429 }
15430 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15431 &wq_create->u.response);
15432 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15433 pci_barset);
15434 if (!bar_memmap_p) {
15435 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15436 "3263 WQ[%d] failed to memmap "
15437 "pci barset:x%x\n",
15438 wq->queue_id, pci_barset);
15439 status = -ENOMEM;
15440 goto out;
15441 }
15442 db_offset = wq_create->u.response.doorbell_offset;
15443 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15444 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15446 "3252 WQ[%d] doorbell offset "
15447 "not supported: x%x\n",
15448 wq->queue_id, db_offset);
15449 status = -EINVAL;
15450 goto out;
15451 }
15452 wq->db_regaddr = bar_memmap_p + db_offset;
15453 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15454 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15455 "format:x%x\n", wq->queue_id,
15456 pci_barset, db_offset, wq->db_format);
15457 } else
15458 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015459 } else {
James Smart1351e692018-02-22 08:18:43 -080015460 /* Check if DPP was honored by the firmware */
15461 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15462 &wq_create->u.response_1);
15463 if (wq->dpp_enable) {
15464 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15465 &wq_create->u.response_1);
15466 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15467 pci_barset);
15468 if (!bar_memmap_p) {
15469 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15470 "3267 WQ[%d] failed to memmap "
15471 "pci barset:x%x\n",
15472 wq->queue_id, pci_barset);
15473 status = -ENOMEM;
15474 goto out;
15475 }
15476 db_offset = wq_create->u.response_1.doorbell_offset;
15477 wq->db_regaddr = bar_memmap_p + db_offset;
15478 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15479 &wq_create->u.response_1);
15480 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15481 &wq_create->u.response_1);
15482 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15483 dpp_barset);
15484 if (!bar_memmap_p) {
15485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15486 "3268 WQ[%d] failed to memmap "
15487 "pci barset:x%x\n",
15488 wq->queue_id, dpp_barset);
15489 status = -ENOMEM;
15490 goto out;
15491 }
15492 dpp_offset = wq_create->u.response_1.dpp_offset;
15493 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15494 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15495 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15496 "dpp_id:x%x dpp_barset:x%x "
15497 "dpp_offset:x%x\n",
15498 wq->queue_id, pci_barset, db_offset,
15499 wq->dpp_id, dpp_barset, dpp_offset);
15500
15501 /* Enable combined writes for DPP aperture */
15502 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15503#ifdef CONFIG_X86
15504 rc = set_memory_wc(pg_addr, 1);
15505 if (rc) {
15506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15507 "3272 Cannot setup Combined "
15508 "Write on WQ[%d] - disable DPP\n",
15509 wq->queue_id);
15510 phba->cfg_enable_dpp = 0;
15511 }
15512#else
15513 phba->cfg_enable_dpp = 0;
15514#endif
15515 } else
15516 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015517 }
James Smart895427b2017-02-12 13:52:30 -080015518 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15519 if (wq->pring == NULL) {
15520 status = -ENOMEM;
15521 goto out;
15522 }
James Smart4f774512009-05-22 14:52:35 -040015523 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015524 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015525 wq->subtype = subtype;
15526 wq->host_index = 0;
15527 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050015528 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040015529
15530 /* link the wq onto the parent cq child list */
15531 list_add_tail(&wq->list, &cq->child_list);
15532out:
James Smart8fa38512009-07-19 10:01:03 -040015533 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015534 return status;
15535}
15536
15537/**
15538 * lpfc_rq_create - Create a Receive Queue on the HBA
15539 * @phba: HBA structure that indicates port to create a queue on.
15540 * @hrq: The queue structure to use to create the header receive queue.
15541 * @drq: The queue structure to use to create the data receive queue.
15542 * @cq: The completion queue to bind this work queue to.
15543 *
15544 * This function creates a receive buffer queue pair , as detailed in @hrq and
15545 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15546 * to the HBA.
15547 *
15548 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15549 * struct is used to get the entry count that is necessary to determine the
15550 * number of pages to use for this queue. The @cq is used to indicate which
15551 * completion queue to bind received buffers that are posted to these queues to.
15552 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15553 * receive queue pair. This function is asynchronous and will wait for the
15554 * mailbox command to finish before continuing.
15555 *
15556 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015557 * memory this function will return -ENOMEM. If the queue create mailbox command
15558 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015559 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015560int
James Smart4f774512009-05-22 14:52:35 -040015561lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15562 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15563{
15564 struct lpfc_mbx_rq_create *rq_create;
15565 struct lpfc_dmabuf *dmabuf;
15566 LPFC_MBOXQ_t *mbox;
15567 int rc, length, status = 0;
15568 uint32_t shdr_status, shdr_add_status;
15569 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015570 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015571 void __iomem *bar_memmap_p;
15572 uint32_t db_offset;
15573 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015574
James Smart2e90f4b2011-12-13 13:22:37 -050015575 /* sanity check on queue memory */
15576 if (!hrq || !drq || !cq)
15577 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015578 if (!phba->sli4_hba.pc_sli4_params.supported)
15579 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015580
15581 if (hrq->entry_count != drq->entry_count)
15582 return -EINVAL;
15583 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15584 if (!mbox)
15585 return -ENOMEM;
15586 length = (sizeof(struct lpfc_mbx_rq_create) -
15587 sizeof(struct lpfc_sli4_cfg_mhdr));
15588 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15589 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15590 length, LPFC_SLI4_MBX_EMBED);
15591 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015592 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15593 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15594 phba->sli4_hba.pc_sli4_params.rqv);
15595 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15596 bf_set(lpfc_rq_context_rqe_count_1,
15597 &rq_create->u.request.context,
15598 hrq->entry_count);
15599 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015600 bf_set(lpfc_rq_context_rqe_size,
15601 &rq_create->u.request.context,
15602 LPFC_RQE_SIZE_8);
15603 bf_set(lpfc_rq_context_page_size,
15604 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015605 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015606 } else {
15607 switch (hrq->entry_count) {
15608 default:
15609 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15610 "2535 Unsupported RQ count. (%d)\n",
15611 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015612 if (hrq->entry_count < 512) {
15613 status = -EINVAL;
15614 goto out;
15615 }
James Smart5a6f1332011-03-11 16:05:35 -050015616 /* otherwise default to smallest count (drop through) */
15617 case 512:
15618 bf_set(lpfc_rq_context_rqe_count,
15619 &rq_create->u.request.context,
15620 LPFC_RQ_RING_SIZE_512);
15621 break;
15622 case 1024:
15623 bf_set(lpfc_rq_context_rqe_count,
15624 &rq_create->u.request.context,
15625 LPFC_RQ_RING_SIZE_1024);
15626 break;
15627 case 2048:
15628 bf_set(lpfc_rq_context_rqe_count,
15629 &rq_create->u.request.context,
15630 LPFC_RQ_RING_SIZE_2048);
15631 break;
15632 case 4096:
15633 bf_set(lpfc_rq_context_rqe_count,
15634 &rq_create->u.request.context,
15635 LPFC_RQ_RING_SIZE_4096);
15636 break;
15637 }
15638 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15639 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015640 }
15641 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15642 cq->queue_id);
15643 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15644 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015645 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015646 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015647 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15648 putPaddrLow(dmabuf->phys);
15649 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15650 putPaddrHigh(dmabuf->phys);
15651 }
James Smart962bc512013-01-03 15:44:00 -050015652 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15653 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15654
James Smart4f774512009-05-22 14:52:35 -040015655 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15656 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015657 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15658 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15659 if (shdr_status || shdr_add_status || rc) {
15660 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15661 "2504 RQ_CREATE mailbox failed with "
15662 "status x%x add_status x%x, mbx status x%x\n",
15663 shdr_status, shdr_add_status, rc);
15664 status = -ENXIO;
15665 goto out;
15666 }
15667 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15668 if (hrq->queue_id == 0xFFFF) {
15669 status = -ENXIO;
15670 goto out;
15671 }
James Smart962bc512013-01-03 15:44:00 -050015672
15673 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15674 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15675 &rq_create->u.response);
15676 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15677 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15679 "3262 RQ [%d] doorbell format not "
15680 "supported: x%x\n", hrq->queue_id,
15681 hrq->db_format);
15682 status = -EINVAL;
15683 goto out;
15684 }
15685
15686 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15687 &rq_create->u.response);
15688 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15689 if (!bar_memmap_p) {
15690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15691 "3269 RQ[%d] failed to memmap pci "
15692 "barset:x%x\n", hrq->queue_id,
15693 pci_barset);
15694 status = -ENOMEM;
15695 goto out;
15696 }
15697
15698 db_offset = rq_create->u.response.doorbell_offset;
15699 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15700 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15702 "3270 RQ[%d] doorbell offset not "
15703 "supported: x%x\n", hrq->queue_id,
15704 db_offset);
15705 status = -EINVAL;
15706 goto out;
15707 }
15708 hrq->db_regaddr = bar_memmap_p + db_offset;
15709 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015710 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15711 "format:x%x\n", hrq->queue_id, pci_barset,
15712 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015713 } else {
15714 hrq->db_format = LPFC_DB_RING_FORMAT;
15715 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15716 }
James Smart4f774512009-05-22 14:52:35 -040015717 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015718 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015719 hrq->subtype = subtype;
15720 hrq->host_index = 0;
15721 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015722 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015723
15724 /* now create the data queue */
15725 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15726 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15727 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015728 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15729 phba->sli4_hba.pc_sli4_params.rqv);
15730 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15731 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015732 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015733 if (subtype == LPFC_NVMET)
15734 rq_create->u.request.context.buffer_size =
15735 LPFC_NVMET_DATA_BUF_SIZE;
15736 else
15737 rq_create->u.request.context.buffer_size =
15738 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015739 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15740 LPFC_RQE_SIZE_8);
15741 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15742 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015743 } else {
15744 switch (drq->entry_count) {
15745 default:
15746 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15747 "2536 Unsupported RQ count. (%d)\n",
15748 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015749 if (drq->entry_count < 512) {
15750 status = -EINVAL;
15751 goto out;
15752 }
James Smart5a6f1332011-03-11 16:05:35 -050015753 /* otherwise default to smallest count (drop through) */
15754 case 512:
15755 bf_set(lpfc_rq_context_rqe_count,
15756 &rq_create->u.request.context,
15757 LPFC_RQ_RING_SIZE_512);
15758 break;
15759 case 1024:
15760 bf_set(lpfc_rq_context_rqe_count,
15761 &rq_create->u.request.context,
15762 LPFC_RQ_RING_SIZE_1024);
15763 break;
15764 case 2048:
15765 bf_set(lpfc_rq_context_rqe_count,
15766 &rq_create->u.request.context,
15767 LPFC_RQ_RING_SIZE_2048);
15768 break;
15769 case 4096:
15770 bf_set(lpfc_rq_context_rqe_count,
15771 &rq_create->u.request.context,
15772 LPFC_RQ_RING_SIZE_4096);
15773 break;
15774 }
James Smart3c603be2017-05-15 15:20:44 -070015775 if (subtype == LPFC_NVMET)
15776 bf_set(lpfc_rq_context_buf_size,
15777 &rq_create->u.request.context,
15778 LPFC_NVMET_DATA_BUF_SIZE);
15779 else
15780 bf_set(lpfc_rq_context_buf_size,
15781 &rq_create->u.request.context,
15782 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015783 }
15784 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15785 cq->queue_id);
15786 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15787 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015788 list_for_each_entry(dmabuf, &drq->page_list, list) {
15789 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15790 putPaddrLow(dmabuf->phys);
15791 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15792 putPaddrHigh(dmabuf->phys);
15793 }
James Smart962bc512013-01-03 15:44:00 -050015794 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15795 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015796 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15797 /* The IOCTL status is embedded in the mailbox subheader. */
15798 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15799 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15800 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15801 if (shdr_status || shdr_add_status || rc) {
15802 status = -ENXIO;
15803 goto out;
15804 }
15805 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15806 if (drq->queue_id == 0xFFFF) {
15807 status = -ENXIO;
15808 goto out;
15809 }
15810 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015811 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015812 drq->subtype = subtype;
15813 drq->host_index = 0;
15814 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015815 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015816
15817 /* link the header and data RQs onto the parent cq child list */
15818 list_add_tail(&hrq->list, &cq->child_list);
15819 list_add_tail(&drq->list, &cq->child_list);
15820
15821out:
James Smart8fa38512009-07-19 10:01:03 -040015822 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015823 return status;
15824}
15825
15826/**
James Smart2d7dbc42017-02-12 13:52:35 -080015827 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15828 * @phba: HBA structure that indicates port to create a queue on.
15829 * @hrqp: The queue structure array to use to create the header receive queues.
15830 * @drqp: The queue structure array to use to create the data receive queues.
15831 * @cqp: The completion queue array to bind these receive queues to.
15832 *
15833 * This function creates a receive buffer queue pair , as detailed in @hrq and
15834 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15835 * to the HBA.
15836 *
15837 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15838 * struct is used to get the entry count that is necessary to determine the
15839 * number of pages to use for this queue. The @cq is used to indicate which
15840 * completion queue to bind received buffers that are posted to these queues to.
15841 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15842 * receive queue pair. This function is asynchronous and will wait for the
15843 * mailbox command to finish before continuing.
15844 *
15845 * On success this function will return a zero. If unable to allocate enough
15846 * memory this function will return -ENOMEM. If the queue create mailbox command
15847 * fails this function will return -ENXIO.
15848 **/
15849int
15850lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15851 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15852 uint32_t subtype)
15853{
15854 struct lpfc_queue *hrq, *drq, *cq;
15855 struct lpfc_mbx_rq_create_v2 *rq_create;
15856 struct lpfc_dmabuf *dmabuf;
15857 LPFC_MBOXQ_t *mbox;
15858 int rc, length, alloclen, status = 0;
15859 int cnt, idx, numrq, page_idx = 0;
15860 uint32_t shdr_status, shdr_add_status;
15861 union lpfc_sli4_cfg_shdr *shdr;
15862 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15863
15864 numrq = phba->cfg_nvmet_mrq;
15865 /* sanity check on array memory */
15866 if (!hrqp || !drqp || !cqp || !numrq)
15867 return -ENODEV;
15868 if (!phba->sli4_hba.pc_sli4_params.supported)
15869 hw_page_size = SLI4_PAGE_SIZE;
15870
15871 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15872 if (!mbox)
15873 return -ENOMEM;
15874
15875 length = sizeof(struct lpfc_mbx_rq_create_v2);
15876 length += ((2 * numrq * hrqp[0]->page_count) *
15877 sizeof(struct dma_address));
15878
15879 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15880 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15881 LPFC_SLI4_MBX_NEMBED);
15882 if (alloclen < length) {
15883 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15884 "3099 Allocated DMA memory size (%d) is "
15885 "less than the requested DMA memory size "
15886 "(%d)\n", alloclen, length);
15887 status = -ENOMEM;
15888 goto out;
15889 }
15890
15891
15892
15893 rq_create = mbox->sge_array->addr[0];
15894 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15895
15896 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15897 cnt = 0;
15898
15899 for (idx = 0; idx < numrq; idx++) {
15900 hrq = hrqp[idx];
15901 drq = drqp[idx];
15902 cq = cqp[idx];
15903
James Smart2d7dbc42017-02-12 13:52:35 -080015904 /* sanity check on queue memory */
15905 if (!hrq || !drq || !cq) {
15906 status = -ENODEV;
15907 goto out;
15908 }
15909
James Smart7aabe842017-03-04 09:30:22 -080015910 if (hrq->entry_count != drq->entry_count) {
15911 status = -EINVAL;
15912 goto out;
15913 }
15914
James Smart2d7dbc42017-02-12 13:52:35 -080015915 if (idx == 0) {
15916 bf_set(lpfc_mbx_rq_create_num_pages,
15917 &rq_create->u.request,
15918 hrq->page_count);
15919 bf_set(lpfc_mbx_rq_create_rq_cnt,
15920 &rq_create->u.request, (numrq * 2));
15921 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15922 1);
15923 bf_set(lpfc_rq_context_base_cq,
15924 &rq_create->u.request.context,
15925 cq->queue_id);
15926 bf_set(lpfc_rq_context_data_size,
15927 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015928 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015929 bf_set(lpfc_rq_context_hdr_size,
15930 &rq_create->u.request.context,
15931 LPFC_HDR_BUF_SIZE);
15932 bf_set(lpfc_rq_context_rqe_count_1,
15933 &rq_create->u.request.context,
15934 hrq->entry_count);
15935 bf_set(lpfc_rq_context_rqe_size,
15936 &rq_create->u.request.context,
15937 LPFC_RQE_SIZE_8);
15938 bf_set(lpfc_rq_context_page_size,
15939 &rq_create->u.request.context,
15940 (PAGE_SIZE/SLI4_PAGE_SIZE));
15941 }
15942 rc = 0;
15943 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15944 memset(dmabuf->virt, 0, hw_page_size);
15945 cnt = page_idx + dmabuf->buffer_tag;
15946 rq_create->u.request.page[cnt].addr_lo =
15947 putPaddrLow(dmabuf->phys);
15948 rq_create->u.request.page[cnt].addr_hi =
15949 putPaddrHigh(dmabuf->phys);
15950 rc++;
15951 }
15952 page_idx += rc;
15953
15954 rc = 0;
15955 list_for_each_entry(dmabuf, &drq->page_list, list) {
15956 memset(dmabuf->virt, 0, hw_page_size);
15957 cnt = page_idx + dmabuf->buffer_tag;
15958 rq_create->u.request.page[cnt].addr_lo =
15959 putPaddrLow(dmabuf->phys);
15960 rq_create->u.request.page[cnt].addr_hi =
15961 putPaddrHigh(dmabuf->phys);
15962 rc++;
15963 }
15964 page_idx += rc;
15965
15966 hrq->db_format = LPFC_DB_RING_FORMAT;
15967 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15968 hrq->type = LPFC_HRQ;
15969 hrq->assoc_qid = cq->queue_id;
15970 hrq->subtype = subtype;
15971 hrq->host_index = 0;
15972 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015973 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015974
15975 drq->db_format = LPFC_DB_RING_FORMAT;
15976 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15977 drq->type = LPFC_DRQ;
15978 drq->assoc_qid = cq->queue_id;
15979 drq->subtype = subtype;
15980 drq->host_index = 0;
15981 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015982 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015983
15984 list_add_tail(&hrq->list, &cq->child_list);
15985 list_add_tail(&drq->list, &cq->child_list);
15986 }
15987
15988 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15989 /* The IOCTL status is embedded in the mailbox subheader. */
15990 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15991 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15992 if (shdr_status || shdr_add_status || rc) {
15993 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15994 "3120 RQ_CREATE mailbox failed with "
15995 "status x%x add_status x%x, mbx status x%x\n",
15996 shdr_status, shdr_add_status, rc);
15997 status = -ENXIO;
15998 goto out;
15999 }
16000 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16001 if (rc == 0xFFFF) {
16002 status = -ENXIO;
16003 goto out;
16004 }
16005
16006 /* Initialize all RQs with associated queue id */
16007 for (idx = 0; idx < numrq; idx++) {
16008 hrq = hrqp[idx];
16009 hrq->queue_id = rc + (2 * idx);
16010 drq = drqp[idx];
16011 drq->queue_id = rc + (2 * idx) + 1;
16012 }
16013
16014out:
16015 lpfc_sli4_mbox_cmd_free(phba, mbox);
16016 return status;
16017}
16018
16019/**
James Smart4f774512009-05-22 14:52:35 -040016020 * lpfc_eq_destroy - Destroy an event Queue on the HBA
16021 * @eq: The queue structure associated with the queue to destroy.
16022 *
16023 * This function destroys a queue, as detailed in @eq by sending an mailbox
16024 * command, specific to the type of queue, to the HBA.
16025 *
16026 * The @eq struct is used to get the queue ID of the queue to destroy.
16027 *
16028 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016029 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016030 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016031int
James Smart4f774512009-05-22 14:52:35 -040016032lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16033{
16034 LPFC_MBOXQ_t *mbox;
16035 int rc, length, status = 0;
16036 uint32_t shdr_status, shdr_add_status;
16037 union lpfc_sli4_cfg_shdr *shdr;
16038
James Smart2e90f4b2011-12-13 13:22:37 -050016039 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016040 if (!eq)
16041 return -ENODEV;
16042 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16043 if (!mbox)
16044 return -ENOMEM;
16045 length = (sizeof(struct lpfc_mbx_eq_destroy) -
16046 sizeof(struct lpfc_sli4_cfg_mhdr));
16047 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16048 LPFC_MBOX_OPCODE_EQ_DESTROY,
16049 length, LPFC_SLI4_MBX_EMBED);
16050 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16051 eq->queue_id);
16052 mbox->vport = eq->phba->pport;
16053 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16054
16055 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16056 /* The IOCTL status is embedded in the mailbox subheader. */
16057 shdr = (union lpfc_sli4_cfg_shdr *)
16058 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16059 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16060 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16061 if (shdr_status || shdr_add_status || rc) {
16062 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16063 "2505 EQ_DESTROY mailbox failed with "
16064 "status x%x add_status x%x, mbx status x%x\n",
16065 shdr_status, shdr_add_status, rc);
16066 status = -ENXIO;
16067 }
16068
16069 /* Remove eq from any list */
16070 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040016071 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016072 return status;
16073}
16074
16075/**
16076 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16077 * @cq: The queue structure associated with the queue to destroy.
16078 *
16079 * This function destroys a queue, as detailed in @cq by sending an mailbox
16080 * command, specific to the type of queue, to the HBA.
16081 *
16082 * The @cq struct is used to get the queue ID of the queue to destroy.
16083 *
16084 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016085 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016086 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016087int
James Smart4f774512009-05-22 14:52:35 -040016088lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16089{
16090 LPFC_MBOXQ_t *mbox;
16091 int rc, length, status = 0;
16092 uint32_t shdr_status, shdr_add_status;
16093 union lpfc_sli4_cfg_shdr *shdr;
16094
James Smart2e90f4b2011-12-13 13:22:37 -050016095 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016096 if (!cq)
16097 return -ENODEV;
16098 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16099 if (!mbox)
16100 return -ENOMEM;
16101 length = (sizeof(struct lpfc_mbx_cq_destroy) -
16102 sizeof(struct lpfc_sli4_cfg_mhdr));
16103 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16104 LPFC_MBOX_OPCODE_CQ_DESTROY,
16105 length, LPFC_SLI4_MBX_EMBED);
16106 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16107 cq->queue_id);
16108 mbox->vport = cq->phba->pport;
16109 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16110 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16111 /* The IOCTL status is embedded in the mailbox subheader. */
16112 shdr = (union lpfc_sli4_cfg_shdr *)
16113 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16114 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16115 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16116 if (shdr_status || shdr_add_status || rc) {
16117 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16118 "2506 CQ_DESTROY mailbox failed with "
16119 "status x%x add_status x%x, mbx status x%x\n",
16120 shdr_status, shdr_add_status, rc);
16121 status = -ENXIO;
16122 }
16123 /* Remove cq from any list */
16124 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040016125 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016126 return status;
16127}
16128
16129/**
James Smart04c68492009-05-22 14:52:52 -040016130 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16131 * @qm: The queue structure associated with the queue to destroy.
16132 *
16133 * This function destroys a queue, as detailed in @mq by sending an mailbox
16134 * command, specific to the type of queue, to the HBA.
16135 *
16136 * The @mq struct is used to get the queue ID of the queue to destroy.
16137 *
16138 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016139 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040016140 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016141int
James Smart04c68492009-05-22 14:52:52 -040016142lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16143{
16144 LPFC_MBOXQ_t *mbox;
16145 int rc, length, status = 0;
16146 uint32_t shdr_status, shdr_add_status;
16147 union lpfc_sli4_cfg_shdr *shdr;
16148
James Smart2e90f4b2011-12-13 13:22:37 -050016149 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040016150 if (!mq)
16151 return -ENODEV;
16152 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16153 if (!mbox)
16154 return -ENOMEM;
16155 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16156 sizeof(struct lpfc_sli4_cfg_mhdr));
16157 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16158 LPFC_MBOX_OPCODE_MQ_DESTROY,
16159 length, LPFC_SLI4_MBX_EMBED);
16160 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16161 mq->queue_id);
16162 mbox->vport = mq->phba->pport;
16163 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16164 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16165 /* The IOCTL status is embedded in the mailbox subheader. */
16166 shdr = (union lpfc_sli4_cfg_shdr *)
16167 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16168 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16169 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16170 if (shdr_status || shdr_add_status || rc) {
16171 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16172 "2507 MQ_DESTROY mailbox failed with "
16173 "status x%x add_status x%x, mbx status x%x\n",
16174 shdr_status, shdr_add_status, rc);
16175 status = -ENXIO;
16176 }
16177 /* Remove mq from any list */
16178 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016179 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016180 return status;
16181}
16182
16183/**
James Smart4f774512009-05-22 14:52:35 -040016184 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16185 * @wq: The queue structure associated with the queue to destroy.
16186 *
16187 * This function destroys a queue, as detailed in @wq by sending an mailbox
16188 * command, specific to the type of queue, to the HBA.
16189 *
16190 * The @wq struct is used to get the queue ID of the queue to destroy.
16191 *
16192 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016193 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016194 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016195int
James Smart4f774512009-05-22 14:52:35 -040016196lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16197{
16198 LPFC_MBOXQ_t *mbox;
16199 int rc, length, status = 0;
16200 uint32_t shdr_status, shdr_add_status;
16201 union lpfc_sli4_cfg_shdr *shdr;
16202
James Smart2e90f4b2011-12-13 13:22:37 -050016203 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016204 if (!wq)
16205 return -ENODEV;
16206 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16207 if (!mbox)
16208 return -ENOMEM;
16209 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16210 sizeof(struct lpfc_sli4_cfg_mhdr));
16211 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16212 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16213 length, LPFC_SLI4_MBX_EMBED);
16214 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16215 wq->queue_id);
16216 mbox->vport = wq->phba->pport;
16217 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16218 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16219 shdr = (union lpfc_sli4_cfg_shdr *)
16220 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16221 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16222 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16223 if (shdr_status || shdr_add_status || rc) {
16224 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16225 "2508 WQ_DESTROY mailbox failed with "
16226 "status x%x add_status x%x, mbx status x%x\n",
16227 shdr_status, shdr_add_status, rc);
16228 status = -ENXIO;
16229 }
16230 /* Remove wq from any list */
16231 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016232 kfree(wq->pring);
16233 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016234 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016235 return status;
16236}
16237
16238/**
16239 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16240 * @rq: The queue structure associated with the queue to destroy.
16241 *
16242 * This function destroys a queue, as detailed in @rq by sending an mailbox
16243 * command, specific to the type of queue, to the HBA.
16244 *
16245 * The @rq struct is used to get the queue ID of the queue to destroy.
16246 *
16247 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016248 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016249 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016250int
James Smart4f774512009-05-22 14:52:35 -040016251lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16252 struct lpfc_queue *drq)
16253{
16254 LPFC_MBOXQ_t *mbox;
16255 int rc, length, status = 0;
16256 uint32_t shdr_status, shdr_add_status;
16257 union lpfc_sli4_cfg_shdr *shdr;
16258
James Smart2e90f4b2011-12-13 13:22:37 -050016259 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016260 if (!hrq || !drq)
16261 return -ENODEV;
16262 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16263 if (!mbox)
16264 return -ENOMEM;
16265 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016266 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016267 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16268 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16269 length, LPFC_SLI4_MBX_EMBED);
16270 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16271 hrq->queue_id);
16272 mbox->vport = hrq->phba->pport;
16273 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16274 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16275 /* The IOCTL status is embedded in the mailbox subheader. */
16276 shdr = (union lpfc_sli4_cfg_shdr *)
16277 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16278 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16279 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16280 if (shdr_status || shdr_add_status || rc) {
16281 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16282 "2509 RQ_DESTROY mailbox failed with "
16283 "status x%x add_status x%x, mbx status x%x\n",
16284 shdr_status, shdr_add_status, rc);
16285 if (rc != MBX_TIMEOUT)
16286 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16287 return -ENXIO;
16288 }
16289 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16290 drq->queue_id);
16291 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16292 shdr = (union lpfc_sli4_cfg_shdr *)
16293 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16294 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16295 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16296 if (shdr_status || shdr_add_status || rc) {
16297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16298 "2510 RQ_DESTROY mailbox failed with "
16299 "status x%x add_status x%x, mbx status x%x\n",
16300 shdr_status, shdr_add_status, rc);
16301 status = -ENXIO;
16302 }
16303 list_del_init(&hrq->list);
16304 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016305 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016306 return status;
16307}
16308
16309/**
16310 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16311 * @phba: The virtual port for which this call being executed.
16312 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16313 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16314 * @xritag: the xritag that ties this io to the SGL pages.
16315 *
16316 * This routine will post the sgl pages for the IO that has the xritag
16317 * that is in the iocbq structure. The xritag is assigned during iocbq
16318 * creation and persists for as long as the driver is loaded.
16319 * if the caller has fewer than 256 scatter gather segments to map then
16320 * pdma_phys_addr1 should be 0.
16321 * If the caller needs to map more than 256 scatter gather segment then
16322 * pdma_phys_addr1 should be a valid physical address.
16323 * physical address for SGLs must be 64 byte aligned.
16324 * If you are going to map 2 SGL's then the first one must have 256 entries
16325 * the second sgl can have between 1 and 256 entries.
16326 *
16327 * Return codes:
16328 * 0 - Success
16329 * -ENXIO, -ENOMEM - Failure
16330 **/
16331int
16332lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16333 dma_addr_t pdma_phys_addr0,
16334 dma_addr_t pdma_phys_addr1,
16335 uint16_t xritag)
16336{
16337 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16338 LPFC_MBOXQ_t *mbox;
16339 int rc;
16340 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016341 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016342 union lpfc_sli4_cfg_shdr *shdr;
16343
16344 if (xritag == NO_XRI) {
16345 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16346 "0364 Invalid param:\n");
16347 return -EINVAL;
16348 }
16349
16350 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16351 if (!mbox)
16352 return -ENOMEM;
16353
16354 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16355 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16356 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016357 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016358
16359 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16360 &mbox->u.mqe.un.post_sgl_pages;
16361 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16362 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16363
16364 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16365 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16366 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16367 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16368
16369 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16370 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16371 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16372 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16373 if (!phba->sli4_hba.intr_enable)
16374 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016375 else {
James Smarta183a152011-10-10 21:32:43 -040016376 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016377 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16378 }
James Smart4f774512009-05-22 14:52:35 -040016379 /* The IOCTL status is embedded in the mailbox subheader. */
16380 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16381 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16382 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16383 if (rc != MBX_TIMEOUT)
16384 mempool_free(mbox, phba->mbox_mem_pool);
16385 if (shdr_status || shdr_add_status || rc) {
16386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16387 "2511 POST_SGL mailbox failed with "
16388 "status x%x add_status x%x, mbx status x%x\n",
16389 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016390 }
16391 return 0;
16392}
James Smart4f774512009-05-22 14:52:35 -040016393
16394/**
James Smart88a2cfb2011-07-22 18:36:33 -040016395 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016396 * @phba: pointer to lpfc hba data structure.
16397 *
16398 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016399 * HBA consistent with the SLI-4 interface spec. This routine
16400 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16401 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016402 *
James Smart88a2cfb2011-07-22 18:36:33 -040016403 * Returns
16404 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16405 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16406 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016407static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016408lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16409{
16410 unsigned long xri;
16411
16412 /*
16413 * Fetch the next logical xri. Because this index is logical,
16414 * the driver starts at 0 each time.
16415 */
16416 spin_lock_irq(&phba->hbalock);
16417 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16418 phba->sli4_hba.max_cfg_param.max_xri, 0);
16419 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16420 spin_unlock_irq(&phba->hbalock);
16421 return NO_XRI;
16422 } else {
16423 set_bit(xri, phba->sli4_hba.xri_bmask);
16424 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016425 }
James Smart6d368e52011-05-24 11:44:12 -040016426 spin_unlock_irq(&phba->hbalock);
16427 return xri;
16428}
16429
16430/**
16431 * lpfc_sli4_free_xri - Release an xri for reuse.
16432 * @phba: pointer to lpfc hba data structure.
16433 *
16434 * This routine is invoked to release an xri to the pool of
16435 * available rpis maintained by the driver.
16436 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016437static void
James Smart6d368e52011-05-24 11:44:12 -040016438__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16439{
16440 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016441 phba->sli4_hba.max_cfg_param.xri_used--;
16442 }
16443}
16444
16445/**
16446 * lpfc_sli4_free_xri - Release an xri for reuse.
16447 * @phba: pointer to lpfc hba data structure.
16448 *
16449 * This routine is invoked to release an xri to the pool of
16450 * available rpis maintained by the driver.
16451 **/
16452void
16453lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16454{
16455 spin_lock_irq(&phba->hbalock);
16456 __lpfc_sli4_free_xri(phba, xri);
16457 spin_unlock_irq(&phba->hbalock);
16458}
16459
16460/**
James Smart4f774512009-05-22 14:52:35 -040016461 * lpfc_sli4_next_xritag - Get an xritag for the io
16462 * @phba: Pointer to HBA context object.
16463 *
16464 * This function gets an xritag for the iocb. If there is no unused xritag
16465 * it will return 0xffff.
16466 * The function returns the allocated xritag if successful, else returns zero.
16467 * Zero is not a valid xritag.
16468 * The caller is not required to hold any lock.
16469 **/
16470uint16_t
16471lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16472{
James Smart6d368e52011-05-24 11:44:12 -040016473 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016474
James Smart6d368e52011-05-24 11:44:12 -040016475 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016476 if (xri_index == NO_XRI)
16477 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16478 "2004 Failed to allocate XRI.last XRITAG is %d"
16479 " Max XRI is %d, Used XRI is %d\n",
16480 xri_index,
16481 phba->sli4_hba.max_cfg_param.max_xri,
16482 phba->sli4_hba.max_cfg_param.xri_used);
16483 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016484}
16485
16486/**
James Smart895427b2017-02-12 13:52:30 -080016487 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016488 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016489 * @post_sgl_list: pointer to els sgl entry list.
16490 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016491 *
16492 * This routine is invoked to post a block of driver's sgl pages to the
16493 * HBA using non-embedded mailbox command. No Lock is held. This routine
16494 * is only called when the driver is loading and after all IO has been
16495 * stopped.
16496 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016497static int
James Smart895427b2017-02-12 13:52:30 -080016498lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016499 struct list_head *post_sgl_list,
16500 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016501{
James Smart8a9d2e82012-05-09 21:16:12 -040016502 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016503 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16504 struct sgl_page_pairs *sgl_pg_pairs;
16505 void *viraddr;
16506 LPFC_MBOXQ_t *mbox;
16507 uint32_t reqlen, alloclen, pg_pairs;
16508 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016509 uint16_t xritag_start = 0;
16510 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016511 uint32_t shdr_status, shdr_add_status;
16512 union lpfc_sli4_cfg_shdr *shdr;
16513
James Smart895427b2017-02-12 13:52:30 -080016514 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016515 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016516 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016518 "2559 Block sgl registration required DMA "
16519 "size (%d) great than a page\n", reqlen);
16520 return -ENOMEM;
16521 }
James Smart895427b2017-02-12 13:52:30 -080016522
James Smart4f774512009-05-22 14:52:35 -040016523 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016524 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016525 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016526
16527 /* Allocate DMA memory and set up the non-embedded mailbox command */
16528 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16529 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16530 LPFC_SLI4_MBX_NEMBED);
16531
16532 if (alloclen < reqlen) {
16533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16534 "0285 Allocated DMA memory size (%d) is "
16535 "less than the requested DMA memory "
16536 "size (%d)\n", alloclen, reqlen);
16537 lpfc_sli4_mbox_cmd_free(phba, mbox);
16538 return -ENOMEM;
16539 }
James Smart4f774512009-05-22 14:52:35 -040016540 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016541 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016542 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16543 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16544
James Smart8a9d2e82012-05-09 21:16:12 -040016545 pg_pairs = 0;
16546 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016547 /* Set up the sge entry */
16548 sgl_pg_pairs->sgl_pg0_addr_lo =
16549 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16550 sgl_pg_pairs->sgl_pg0_addr_hi =
16551 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16552 sgl_pg_pairs->sgl_pg1_addr_lo =
16553 cpu_to_le32(putPaddrLow(0));
16554 sgl_pg_pairs->sgl_pg1_addr_hi =
16555 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016556
James Smart4f774512009-05-22 14:52:35 -040016557 /* Keep the first xritag on the list */
16558 if (pg_pairs == 0)
16559 xritag_start = sglq_entry->sli4_xritag;
16560 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016561 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016562 }
James Smart6d368e52011-05-24 11:44:12 -040016563
16564 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016565 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016566 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016567 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016568
James Smart4f774512009-05-22 14:52:35 -040016569 if (!phba->sli4_hba.intr_enable)
16570 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16571 else {
James Smarta183a152011-10-10 21:32:43 -040016572 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016573 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16574 }
16575 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16576 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16577 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16578 if (rc != MBX_TIMEOUT)
16579 lpfc_sli4_mbox_cmd_free(phba, mbox);
16580 if (shdr_status || shdr_add_status || rc) {
16581 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16582 "2513 POST_SGL_BLOCK mailbox command failed "
16583 "status x%x add_status x%x mbx status x%x\n",
16584 shdr_status, shdr_add_status, rc);
16585 rc = -ENXIO;
16586 }
16587 return rc;
16588}
16589
16590/**
James Smart5e5b5112019-01-28 11:14:22 -080016591 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
James Smart4f774512009-05-22 14:52:35 -040016592 * @phba: pointer to lpfc hba data structure.
James Smart0794d602019-01-28 11:14:19 -080016593 * @nblist: pointer to nvme buffer list.
James Smart4f774512009-05-22 14:52:35 -040016594 * @count: number of scsi buffers on the list.
16595 *
16596 * This routine is invoked to post a block of @count scsi sgl pages from a
James Smart0794d602019-01-28 11:14:19 -080016597 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
James Smart4f774512009-05-22 14:52:35 -040016598 * No Lock is held.
16599 *
16600 **/
James Smart0794d602019-01-28 11:14:19 -080016601static int
James Smart5e5b5112019-01-28 11:14:22 -080016602lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
16603 int count)
James Smart4f774512009-05-22 14:52:35 -040016604{
James Smartc4908502019-01-28 11:14:28 -080016605 struct lpfc_io_buf *lpfc_ncmd;
James Smart4f774512009-05-22 14:52:35 -040016606 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16607 struct sgl_page_pairs *sgl_pg_pairs;
16608 void *viraddr;
16609 LPFC_MBOXQ_t *mbox;
16610 uint32_t reqlen, alloclen, pg_pairs;
16611 uint32_t mbox_tmo;
16612 uint16_t xritag_start = 0;
16613 int rc = 0;
16614 uint32_t shdr_status, shdr_add_status;
16615 dma_addr_t pdma_phys_bpl1;
16616 union lpfc_sli4_cfg_shdr *shdr;
16617
16618 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016619 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016620 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016621 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016622 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016623 "6118 Block sgl registration required DMA "
James Smart4f774512009-05-22 14:52:35 -040016624 "size (%d) great than a page\n", reqlen);
16625 return -ENOMEM;
16626 }
16627 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16628 if (!mbox) {
16629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016630 "6119 Failed to allocate mbox cmd memory\n");
James Smart4f774512009-05-22 14:52:35 -040016631 return -ENOMEM;
16632 }
16633
16634 /* Allocate DMA memory and set up the non-embedded mailbox command */
16635 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
James Smart0794d602019-01-28 11:14:19 -080016636 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16637 reqlen, LPFC_SLI4_MBX_NEMBED);
James Smart4f774512009-05-22 14:52:35 -040016638
16639 if (alloclen < reqlen) {
16640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016641 "6120 Allocated DMA memory size (%d) is "
James Smart4f774512009-05-22 14:52:35 -040016642 "less than the requested DMA memory "
16643 "size (%d)\n", alloclen, reqlen);
16644 lpfc_sli4_mbox_cmd_free(phba, mbox);
16645 return -ENOMEM;
16646 }
James Smart6d368e52011-05-24 11:44:12 -040016647
James Smart4f774512009-05-22 14:52:35 -040016648 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016649 viraddr = mbox->sge_array->addr[0];
16650
16651 /* Set up the SGL pages in the non-embedded DMA pages */
16652 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16653 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16654
16655 pg_pairs = 0;
James Smart0794d602019-01-28 11:14:19 -080016656 list_for_each_entry(lpfc_ncmd, nblist, list) {
James Smart4f774512009-05-22 14:52:35 -040016657 /* Set up the sge entry */
16658 sgl_pg_pairs->sgl_pg0_addr_lo =
James Smart0794d602019-01-28 11:14:19 -080016659 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040016660 sgl_pg_pairs->sgl_pg0_addr_hi =
James Smart0794d602019-01-28 11:14:19 -080016661 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040016662 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
James Smart0794d602019-01-28 11:14:19 -080016663 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
16664 SGL_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040016665 else
16666 pdma_phys_bpl1 = 0;
16667 sgl_pg_pairs->sgl_pg1_addr_lo =
16668 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16669 sgl_pg_pairs->sgl_pg1_addr_hi =
16670 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16671 /* Keep the first xritag on the list */
16672 if (pg_pairs == 0)
James Smart0794d602019-01-28 11:14:19 -080016673 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040016674 sgl_pg_pairs++;
16675 pg_pairs++;
16676 }
16677 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16678 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16679 /* Perform endian conversion if necessary */
16680 sgl->word0 = cpu_to_le32(sgl->word0);
16681
James Smart0794d602019-01-28 11:14:19 -080016682 if (!phba->sli4_hba.intr_enable) {
James Smart4f774512009-05-22 14:52:35 -040016683 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart0794d602019-01-28 11:14:19 -080016684 } else {
James Smarta183a152011-10-10 21:32:43 -040016685 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016686 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16687 }
James Smart0794d602019-01-28 11:14:19 -080016688 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016689 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16690 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16691 if (rc != MBX_TIMEOUT)
16692 lpfc_sli4_mbox_cmd_free(phba, mbox);
16693 if (shdr_status || shdr_add_status || rc) {
16694 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -080016695 "6125 POST_SGL_BLOCK mailbox command failed "
James Smart4f774512009-05-22 14:52:35 -040016696 "status x%x add_status x%x mbx status x%x\n",
16697 shdr_status, shdr_add_status, rc);
16698 rc = -ENXIO;
16699 }
16700 return rc;
16701}
16702
16703/**
James Smart5e5b5112019-01-28 11:14:22 -080016704 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
James Smart0794d602019-01-28 11:14:19 -080016705 * @phba: pointer to lpfc hba data structure.
16706 * @post_nblist: pointer to the nvme buffer list.
16707 *
16708 * This routine walks a list of nvme buffers that was passed in. It attempts
16709 * to construct blocks of nvme buffer sgls which contains contiguous xris and
16710 * uses the non-embedded SGL block post mailbox commands to post to the port.
16711 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
16712 * embedded SGL post mailbox command for posting. The @post_nblist passed in
16713 * must be local list, thus no lock is needed when manipulate the list.
16714 *
16715 * Returns: 0 = failure, non-zero number of successfully posted buffers.
16716 **/
16717int
James Smart5e5b5112019-01-28 11:14:22 -080016718lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
16719 struct list_head *post_nblist, int sb_count)
James Smart0794d602019-01-28 11:14:19 -080016720{
James Smartc4908502019-01-28 11:14:28 -080016721 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart0794d602019-01-28 11:14:19 -080016722 int status, sgl_size;
16723 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
16724 dma_addr_t pdma_phys_sgl1;
16725 int last_xritag = NO_XRI;
16726 int cur_xritag;
James Smart0794d602019-01-28 11:14:19 -080016727 LIST_HEAD(prep_nblist);
16728 LIST_HEAD(blck_nblist);
16729 LIST_HEAD(nvme_nblist);
16730
16731 /* sanity check */
16732 if (sb_count <= 0)
16733 return -EINVAL;
16734
16735 sgl_size = phba->cfg_sg_dma_buf_size;
16736 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
16737 list_del_init(&lpfc_ncmd->list);
16738 block_cnt++;
16739 if ((last_xritag != NO_XRI) &&
16740 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
16741 /* a hole in xri block, form a sgl posting block */
16742 list_splice_init(&prep_nblist, &blck_nblist);
16743 post_cnt = block_cnt - 1;
16744 /* prepare list for next posting block */
16745 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16746 block_cnt = 1;
16747 } else {
16748 /* prepare list for next posting block */
16749 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16750 /* enough sgls for non-embed sgl mbox command */
16751 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
16752 list_splice_init(&prep_nblist, &blck_nblist);
16753 post_cnt = block_cnt;
16754 block_cnt = 0;
16755 }
16756 }
16757 num_posting++;
16758 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16759
16760 /* end of repost sgl list condition for NVME buffers */
16761 if (num_posting == sb_count) {
16762 if (post_cnt == 0) {
16763 /* last sgl posting block */
16764 list_splice_init(&prep_nblist, &blck_nblist);
16765 post_cnt = block_cnt;
16766 } else if (block_cnt == 1) {
16767 /* last single sgl with non-contiguous xri */
16768 if (sgl_size > SGL_PAGE_SIZE)
16769 pdma_phys_sgl1 =
16770 lpfc_ncmd->dma_phys_sgl +
16771 SGL_PAGE_SIZE;
16772 else
16773 pdma_phys_sgl1 = 0;
16774 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16775 status = lpfc_sli4_post_sgl(
16776 phba, lpfc_ncmd->dma_phys_sgl,
16777 pdma_phys_sgl1, cur_xritag);
16778 if (status) {
James Smartc4908502019-01-28 11:14:28 -080016779 /* Post error. Buffer unavailable. */
16780 lpfc_ncmd->flags |=
16781 LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016782 } else {
James Smartc4908502019-01-28 11:14:28 -080016783 /* Post success. Bffer available. */
16784 lpfc_ncmd->flags &=
16785 ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016786 lpfc_ncmd->status = IOSTAT_SUCCESS;
16787 num_posted++;
16788 }
16789 /* success, put on NVME buffer sgl list */
16790 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16791 }
16792 }
16793
16794 /* continue until a nembed page worth of sgls */
16795 if (post_cnt == 0)
16796 continue;
16797
16798 /* post block of NVME buffer list sgls */
James Smart5e5b5112019-01-28 11:14:22 -080016799 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
16800 post_cnt);
James Smart0794d602019-01-28 11:14:19 -080016801
16802 /* don't reset xirtag due to hole in xri block */
16803 if (block_cnt == 0)
16804 last_xritag = NO_XRI;
16805
16806 /* reset NVME buffer post count for next round of posting */
16807 post_cnt = 0;
16808
16809 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
16810 while (!list_empty(&blck_nblist)) {
16811 list_remove_head(&blck_nblist, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -080016812 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -080016813 if (status) {
James Smartc4908502019-01-28 11:14:28 -080016814 /* Post error. Mark buffer unavailable. */
16815 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016816 } else {
James Smartc4908502019-01-28 11:14:28 -080016817 /* Post success, Mark buffer available. */
16818 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080016819 lpfc_ncmd->status = IOSTAT_SUCCESS;
16820 num_posted++;
16821 }
16822 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16823 }
16824 }
16825 /* Push NVME buffers with sgl posted to the available list */
James Smart5e5b5112019-01-28 11:14:22 -080016826 lpfc_io_buf_replenish(phba, &nvme_nblist);
16827
James Smart0794d602019-01-28 11:14:19 -080016828 return num_posted;
16829}
16830
16831/**
James Smart4f774512009-05-22 14:52:35 -040016832 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16833 * @phba: pointer to lpfc_hba struct that the frame was received on
16834 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16835 *
16836 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16837 * valid type of frame that the LPFC driver will handle. This function will
16838 * return a zero if the frame is a valid frame or a non zero value when the
16839 * frame does not pass the check.
16840 **/
16841static int
16842lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16843{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016844 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016845 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016846 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016847
16848 switch (fc_hdr->fh_r_ctl) {
16849 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16850 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16851 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16852 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16853 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16854 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16855 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16856 case FC_RCTL_DD_CMD_STATUS: /* command status */
16857 case FC_RCTL_ELS_REQ: /* extended link services request */
16858 case FC_RCTL_ELS_REP: /* extended link services reply */
16859 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16860 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16861 case FC_RCTL_BA_NOP: /* basic link service NOP */
16862 case FC_RCTL_BA_ABTS: /* basic link service abort */
16863 case FC_RCTL_BA_RMC: /* remove connection */
16864 case FC_RCTL_BA_ACC: /* basic accept */
16865 case FC_RCTL_BA_RJT: /* basic reject */
16866 case FC_RCTL_BA_PRMT:
16867 case FC_RCTL_ACK_1: /* acknowledge_1 */
16868 case FC_RCTL_ACK_0: /* acknowledge_0 */
16869 case FC_RCTL_P_RJT: /* port reject */
16870 case FC_RCTL_F_RJT: /* fabric reject */
16871 case FC_RCTL_P_BSY: /* port busy */
16872 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16873 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16874 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016875 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016876 case FC_RCTL_END: /* end */
16877 break;
16878 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16879 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16880 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16881 return lpfc_fc_frame_check(phba, fc_hdr);
16882 default:
16883 goto drop;
16884 }
James Smartae9e28f2017-05-15 15:20:51 -070016885
James Smart4f774512009-05-22 14:52:35 -040016886 switch (fc_hdr->fh_type) {
16887 case FC_TYPE_BLS:
16888 case FC_TYPE_ELS:
16889 case FC_TYPE_FCP:
16890 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016891 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040016892 break;
16893 case FC_TYPE_IP:
16894 case FC_TYPE_ILS:
16895 default:
16896 goto drop;
16897 }
James Smart546fc852011-03-11 16:06:29 -050016898
James Smart4f774512009-05-22 14:52:35 -040016899 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016900 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016901 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016902 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16903 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16904 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16905 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16906 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016907 return 0;
16908drop:
16909 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016910 "2539 Dropped frame rctl:x%x type:x%x\n",
16911 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016912 return 1;
16913}
16914
16915/**
16916 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16917 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16918 *
16919 * This function processes the FC header to retrieve the VFI from the VF
16920 * header, if one exists. This function will return the VFI if one exists
16921 * or 0 if no VSAN Header exists.
16922 **/
16923static uint32_t
16924lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16925{
16926 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16927
16928 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16929 return 0;
16930 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16931}
16932
16933/**
16934 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16935 * @phba: Pointer to the HBA structure to search for the vport on
16936 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16937 * @fcfi: The FC Fabric ID that the frame came from
16938 *
16939 * This function searches the @phba for a vport that matches the content of the
16940 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16941 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16942 * returns the matching vport pointer or NULL if unable to match frame to a
16943 * vport.
16944 **/
16945static struct lpfc_vport *
16946lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016947 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016948{
16949 struct lpfc_vport **vports;
16950 struct lpfc_vport *vport = NULL;
16951 int i;
James Smart939723a2012-05-09 21:19:03 -040016952
James Smartbf086112011-08-21 21:48:13 -040016953 if (did == Fabric_DID)
16954 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016955 if ((phba->pport->fc_flag & FC_PT2PT) &&
16956 !(phba->link_state == LPFC_HBA_READY))
16957 return phba->pport;
16958
James Smart4f774512009-05-22 14:52:35 -040016959 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016960 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016961 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16962 if (phba->fcf.fcfi == fcfi &&
16963 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16964 vports[i]->fc_myDID == did) {
16965 vport = vports[i];
16966 break;
16967 }
16968 }
James Smart895427b2017-02-12 13:52:30 -080016969 }
James Smart4f774512009-05-22 14:52:35 -040016970 lpfc_destroy_vport_work_array(phba, vports);
16971 return vport;
16972}
16973
16974/**
James Smart45ed1192009-10-02 15:17:02 -040016975 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16976 * @vport: The vport to work on.
16977 *
16978 * This function updates the receive sequence time stamp for this vport. The
16979 * receive sequence time stamp indicates the time that the last frame of the
16980 * the sequence that has been idle for the longest amount of time was received.
16981 * the driver uses this time stamp to indicate if any received sequences have
16982 * timed out.
16983 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016984static void
James Smart45ed1192009-10-02 15:17:02 -040016985lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16986{
16987 struct lpfc_dmabuf *h_buf;
16988 struct hbq_dmabuf *dmabuf = NULL;
16989
16990 /* get the oldest sequence on the rcv list */
16991 h_buf = list_get_first(&vport->rcv_buffer_list,
16992 struct lpfc_dmabuf, list);
16993 if (!h_buf)
16994 return;
16995 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16996 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16997}
16998
16999/**
17000 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17001 * @vport: The vport that the received sequences were sent to.
17002 *
17003 * This function cleans up all outstanding received sequences. This is called
17004 * by the driver when a link event or user action invalidates all the received
17005 * sequences.
17006 **/
17007void
17008lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17009{
17010 struct lpfc_dmabuf *h_buf, *hnext;
17011 struct lpfc_dmabuf *d_buf, *dnext;
17012 struct hbq_dmabuf *dmabuf = NULL;
17013
17014 /* start with the oldest sequence on the rcv list */
17015 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17016 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17017 list_del_init(&dmabuf->hbuf.list);
17018 list_for_each_entry_safe(d_buf, dnext,
17019 &dmabuf->dbuf.list, list) {
17020 list_del_init(&d_buf->list);
17021 lpfc_in_buf_free(vport->phba, d_buf);
17022 }
17023 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17024 }
17025}
17026
17027/**
17028 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17029 * @vport: The vport that the received sequences were sent to.
17030 *
17031 * This function determines whether any received sequences have timed out by
17032 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17033 * indicates that there is at least one timed out sequence this routine will
17034 * go through the received sequences one at a time from most inactive to most
17035 * active to determine which ones need to be cleaned up. Once it has determined
17036 * that a sequence needs to be cleaned up it will simply free up the resources
17037 * without sending an abort.
17038 **/
17039void
17040lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17041{
17042 struct lpfc_dmabuf *h_buf, *hnext;
17043 struct lpfc_dmabuf *d_buf, *dnext;
17044 struct hbq_dmabuf *dmabuf = NULL;
17045 unsigned long timeout;
17046 int abort_count = 0;
17047
17048 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17049 vport->rcv_buffer_time_stamp);
17050 if (list_empty(&vport->rcv_buffer_list) ||
17051 time_before(jiffies, timeout))
17052 return;
17053 /* start with the oldest sequence on the rcv list */
17054 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17055 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17056 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17057 dmabuf->time_stamp);
17058 if (time_before(jiffies, timeout))
17059 break;
17060 abort_count++;
17061 list_del_init(&dmabuf->hbuf.list);
17062 list_for_each_entry_safe(d_buf, dnext,
17063 &dmabuf->dbuf.list, list) {
17064 list_del_init(&d_buf->list);
17065 lpfc_in_buf_free(vport->phba, d_buf);
17066 }
17067 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17068 }
17069 if (abort_count)
17070 lpfc_update_rcv_time_stamp(vport);
17071}
17072
17073/**
James Smart4f774512009-05-22 14:52:35 -040017074 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17075 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17076 *
17077 * This function searches through the existing incomplete sequences that have
17078 * been sent to this @vport. If the frame matches one of the incomplete
17079 * sequences then the dbuf in the @dmabuf is added to the list of frames that
17080 * make up that sequence. If no sequence is found that matches this frame then
17081 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17082 * This function returns a pointer to the first dmabuf in the sequence list that
17083 * the frame was linked to.
17084 **/
17085static struct hbq_dmabuf *
17086lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17087{
17088 struct fc_frame_header *new_hdr;
17089 struct fc_frame_header *temp_hdr;
17090 struct lpfc_dmabuf *d_buf;
17091 struct lpfc_dmabuf *h_buf;
17092 struct hbq_dmabuf *seq_dmabuf = NULL;
17093 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050017094 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040017095
James Smart4d9ab992009-10-02 15:16:39 -040017096 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017097 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040017098 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050017099
James Smart4f774512009-05-22 14:52:35 -040017100 /* Use the hdr_buf to find the sequence that this frame belongs to */
17101 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17102 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17103 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17104 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17105 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17106 continue;
17107 /* found a pending sequence that matches this frame */
17108 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17109 break;
17110 }
17111 if (!seq_dmabuf) {
17112 /*
17113 * This indicates first frame received for this sequence.
17114 * Queue the buffer on the vport's rcv_buffer_list.
17115 */
17116 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040017117 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017118 return dmabuf;
17119 }
17120 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050017121 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17122 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040017123 list_del_init(&seq_dmabuf->hbuf.list);
17124 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17125 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017126 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017127 return dmabuf;
17128 }
James Smart45ed1192009-10-02 15:17:02 -040017129 /* move this sequence to the tail to indicate a young sequence */
17130 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17131 seq_dmabuf->time_stamp = jiffies;
17132 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050017133 if (list_empty(&seq_dmabuf->dbuf.list)) {
17134 temp_hdr = dmabuf->hbuf.virt;
17135 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17136 return seq_dmabuf;
17137 }
James Smart4f774512009-05-22 14:52:35 -040017138 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050017139 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17140 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040017141 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17142 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17143 /*
17144 * If the frame's sequence count is greater than the frame on
17145 * the list then insert the frame right after this frame
17146 */
James Smarteeead812009-12-21 17:01:23 -050017147 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17148 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040017149 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050017150 found = 1;
17151 break;
James Smart4f774512009-05-22 14:52:35 -040017152 }
James Smart4360ca92015-12-16 18:12:04 -050017153
17154 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17155 break;
17156 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040017157 }
James Smart4360ca92015-12-16 18:12:04 -050017158
17159 if (found)
17160 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017161 return NULL;
17162}
17163
17164/**
James Smart6669f9b2009-10-02 15:16:45 -040017165 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17166 * @vport: pointer to a vitural port
17167 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17168 *
17169 * This function tries to abort from the partially assembed sequence, described
17170 * by the information from basic abbort @dmabuf. It checks to see whether such
17171 * partially assembled sequence held by the driver. If so, it shall free up all
17172 * the frames from the partially assembled sequence.
17173 *
17174 * Return
17175 * true -- if there is matching partially assembled sequence present and all
17176 * the frames freed with the sequence;
17177 * false -- if there is no matching partially assembled sequence present so
17178 * nothing got aborted in the lower layer driver
17179 **/
17180static bool
17181lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17182 struct hbq_dmabuf *dmabuf)
17183{
17184 struct fc_frame_header *new_hdr;
17185 struct fc_frame_header *temp_hdr;
17186 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17187 struct hbq_dmabuf *seq_dmabuf = NULL;
17188
17189 /* Use the hdr_buf to find the sequence that matches this frame */
17190 INIT_LIST_HEAD(&dmabuf->dbuf.list);
17191 INIT_LIST_HEAD(&dmabuf->hbuf.list);
17192 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17193 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17194 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17195 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17196 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17197 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17198 continue;
17199 /* found a pending sequence that matches this frame */
17200 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17201 break;
17202 }
17203
17204 /* Free up all the frames from the partially assembled sequence */
17205 if (seq_dmabuf) {
17206 list_for_each_entry_safe(d_buf, n_buf,
17207 &seq_dmabuf->dbuf.list, list) {
17208 list_del_init(&d_buf->list);
17209 lpfc_in_buf_free(vport->phba, d_buf);
17210 }
17211 return true;
17212 }
17213 return false;
17214}
17215
17216/**
James Smart6dd9e312013-01-03 15:43:37 -050017217 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
17218 * @vport: pointer to a vitural port
17219 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17220 *
17221 * This function tries to abort from the assembed sequence from upper level
17222 * protocol, described by the information from basic abbort @dmabuf. It
17223 * checks to see whether such pending context exists at upper level protocol.
17224 * If so, it shall clean up the pending context.
17225 *
17226 * Return
17227 * true -- if there is matching pending context of the sequence cleaned
17228 * at ulp;
17229 * false -- if there is no matching pending context of the sequence present
17230 * at ulp.
17231 **/
17232static bool
17233lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17234{
17235 struct lpfc_hba *phba = vport->phba;
17236 int handled;
17237
17238 /* Accepting abort at ulp with SLI4 only */
17239 if (phba->sli_rev < LPFC_SLI_REV4)
17240 return false;
17241
17242 /* Register all caring upper level protocols to attend abort */
17243 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
17244 if (handled)
17245 return true;
17246
17247 return false;
17248}
17249
17250/**
James Smart546fc852011-03-11 16:06:29 -050017251 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040017252 * @phba: Pointer to HBA context object.
17253 * @cmd_iocbq: pointer to the command iocbq structure.
17254 * @rsp_iocbq: pointer to the response iocbq structure.
17255 *
James Smart546fc852011-03-11 16:06:29 -050017256 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040017257 * event. It properly releases the memory allocated to the sequence abort
17258 * accept iocb.
17259 **/
17260static void
James Smart546fc852011-03-11 16:06:29 -050017261lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040017262 struct lpfc_iocbq *cmd_iocbq,
17263 struct lpfc_iocbq *rsp_iocbq)
17264{
James Smart6dd9e312013-01-03 15:43:37 -050017265 struct lpfc_nodelist *ndlp;
17266
17267 if (cmd_iocbq) {
17268 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17269 lpfc_nlp_put(ndlp);
17270 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017271 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050017272 }
James Smart6b5151f2012-01-18 16:24:06 -050017273
17274 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17275 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17277 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
17278 rsp_iocbq->iocb.ulpStatus,
17279 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040017280}
17281
17282/**
James Smart6d368e52011-05-24 11:44:12 -040017283 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17284 * @phba: Pointer to HBA context object.
17285 * @xri: xri id in transaction.
17286 *
17287 * This function validates the xri maps to the known range of XRIs allocated an
17288 * used by the driver.
17289 **/
James Smart7851fe22011-07-22 18:36:52 -040017290uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017291lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17292 uint16_t xri)
17293{
James Smarta2fc4aef2014-09-03 12:57:55 -040017294 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040017295
17296 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17297 if (xri == phba->sli4_hba.xri_ids[i])
17298 return i;
17299 }
17300 return NO_XRI;
17301}
17302
James Smart6d368e52011-05-24 11:44:12 -040017303/**
James Smart546fc852011-03-11 16:06:29 -050017304 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017305 * @phba: Pointer to HBA context object.
17306 * @fc_hdr: pointer to a FC frame header.
17307 *
James Smart546fc852011-03-11 16:06:29 -050017308 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017309 * event after aborting the sequence handling.
17310 **/
James Smart86c67372017-04-21 16:05:04 -070017311void
James Smart6dd9e312013-01-03 15:43:37 -050017312lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17313 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040017314{
James Smart6dd9e312013-01-03 15:43:37 -050017315 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017316 struct lpfc_iocbq *ctiocb = NULL;
17317 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017318 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017319 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017320 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017321 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017322
17323 if (!lpfc_is_link_up(phba))
17324 return;
17325
17326 sid = sli4_sid_from_fc_hdr(fc_hdr);
17327 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017328 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017329
James Smart6dd9e312013-01-03 15:43:37 -050017330 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017331 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017332 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017333 if (!ndlp) {
17334 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17335 "1268 Failed to allocate ndlp for "
17336 "oxid:x%x SID:x%x\n", oxid, sid);
17337 return;
17338 }
James Smart6dd9e312013-01-03 15:43:37 -050017339 /* Put ndlp onto pport node list */
17340 lpfc_enqueue_node(vport, ndlp);
17341 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17342 /* re-setup ndlp without removing from node list */
17343 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17344 if (!ndlp) {
17345 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17346 "3275 Failed to active ndlp found "
17347 "for oxid:x%x SID:x%x\n", oxid, sid);
17348 return;
17349 }
James Smart6669f9b2009-10-02 15:16:45 -040017350 }
17351
James Smart546fc852011-03-11 16:06:29 -050017352 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017353 ctiocb = lpfc_sli_get_iocbq(phba);
17354 if (!ctiocb)
17355 return;
17356
James Smart5ffc2662009-11-18 15:39:44 -050017357 /* Extract the F_CTL field from FC_HDR */
17358 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17359
James Smart6669f9b2009-10-02 15:16:45 -040017360 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017361 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017362 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017363 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17364 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17365 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17366
17367 /* Fill in the rest of iocb fields */
17368 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17369 icmd->ulpBdeCount = 0;
17370 icmd->ulpLe = 1;
17371 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017372 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017373 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017374
James Smart6669f9b2009-10-02 15:16:45 -040017375 ctiocb->iocb_cmpl = NULL;
17376 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017377 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017378 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017379 ctiocb->sli4_xritag = NO_XRI;
17380
James Smartee0f4fe2012-05-09 21:19:14 -040017381 if (fctl & FC_FC_EX_CTX)
17382 /* Exchange responder sent the abort so we
17383 * own the oxid.
17384 */
17385 xri = oxid;
17386 else
17387 xri = rxid;
17388 lxri = lpfc_sli4_xri_inrange(phba, xri);
17389 if (lxri != NO_XRI)
17390 lpfc_set_rrq_active(phba, ndlp, lxri,
17391 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017392 /* For BA_ABTS from exchange responder, if the logical xri with
17393 * the oxid maps to the FCP XRI range, the port no longer has
17394 * that exchange context, send a BLS_RJT. Override the IOCB for
17395 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017396 */
James Smart6dd9e312013-01-03 15:43:37 -050017397 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017398 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050017399 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17400 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17401 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17402 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17403 }
17404
17405 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17406 * the driver no longer has that exchange, send a BLS_RJT. Override
17407 * the IOCB for a BA_RJT.
17408 */
17409 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017410 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17411 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17412 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17413 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17414 }
James Smart6669f9b2009-10-02 15:16:45 -040017415
James Smart5ffc2662009-11-18 15:39:44 -050017416 if (fctl & FC_FC_EX_CTX) {
17417 /* ABTS sent by responder to CT exchange, construction
17418 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17419 * field and RX_ID from ABTS for RX_ID field.
17420 */
James Smart546fc852011-03-11 16:06:29 -050017421 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017422 } else {
17423 /* ABTS sent by initiator to CT exchange, construction
17424 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017425 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017426 */
James Smart546fc852011-03-11 16:06:29 -050017427 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017428 }
James Smartf09c3ac2012-03-01 22:33:29 -050017429 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017430 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017431
James Smart546fc852011-03-11 16:06:29 -050017432 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017433 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17434 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17435 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017436
17437 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17438 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017439 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17440 "2925 Failed to issue CT ABTS RSP x%x on "
17441 "xri x%x, Data x%x\n",
17442 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17443 phba->link_state);
17444 lpfc_nlp_put(ndlp);
17445 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017446 lpfc_sli_release_iocbq(phba, ctiocb);
17447 }
James Smart6669f9b2009-10-02 15:16:45 -040017448}
17449
17450/**
17451 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17452 * @vport: Pointer to the vport on which this sequence was received
17453 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17454 *
17455 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17456 * receive sequence is only partially assembed by the driver, it shall abort
17457 * the partially assembled frames for the sequence. Otherwise, if the
17458 * unsolicited receive sequence has been completely assembled and passed to
17459 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17460 * unsolicited sequence has been aborted. After that, it will issue a basic
17461 * accept to accept the abort.
17462 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017463static void
James Smart6669f9b2009-10-02 15:16:45 -040017464lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17465 struct hbq_dmabuf *dmabuf)
17466{
17467 struct lpfc_hba *phba = vport->phba;
17468 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017469 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017470 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017471
James Smart6669f9b2009-10-02 15:16:45 -040017472 /* Make a copy of fc_hdr before the dmabuf being released */
17473 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017474 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017475
James Smart5ffc2662009-11-18 15:39:44 -050017476 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017477 /* ABTS by responder to exchange, no cleanup needed */
17478 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017479 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017480 /* ABTS by initiator to exchange, need to do cleanup */
17481 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17482 if (aborted == false)
17483 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017484 }
James Smart6dd9e312013-01-03 15:43:37 -050017485 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17486
James Smart86c67372017-04-21 16:05:04 -070017487 if (phba->nvmet_support) {
17488 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17489 return;
17490 }
17491
James Smart6dd9e312013-01-03 15:43:37 -050017492 /* Respond with BA_ACC or BA_RJT accordingly */
17493 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017494}
17495
17496/**
James Smart4f774512009-05-22 14:52:35 -040017497 * lpfc_seq_complete - Indicates if a sequence is complete
17498 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17499 *
17500 * This function checks the sequence, starting with the frame described by
17501 * @dmabuf, to see if all the frames associated with this sequence are present.
17502 * the frames associated with this sequence are linked to the @dmabuf using the
17503 * dbuf list. This function looks for two major things. 1) That the first frame
17504 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17505 * set. 3) That there are no holes in the sequence count. The function will
17506 * return 1 when the sequence is complete, otherwise it will return 0.
17507 **/
17508static int
17509lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17510{
17511 struct fc_frame_header *hdr;
17512 struct lpfc_dmabuf *d_buf;
17513 struct hbq_dmabuf *seq_dmabuf;
17514 uint32_t fctl;
17515 int seq_count = 0;
17516
17517 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17518 /* make sure first fame of sequence has a sequence count of zero */
17519 if (hdr->fh_seq_cnt != seq_count)
17520 return 0;
17521 fctl = (hdr->fh_f_ctl[0] << 16 |
17522 hdr->fh_f_ctl[1] << 8 |
17523 hdr->fh_f_ctl[2]);
17524 /* If last frame of sequence we can return success. */
17525 if (fctl & FC_FC_END_SEQ)
17526 return 1;
17527 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17528 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17529 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17530 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017531 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017532 return 0;
17533 fctl = (hdr->fh_f_ctl[0] << 16 |
17534 hdr->fh_f_ctl[1] << 8 |
17535 hdr->fh_f_ctl[2]);
17536 /* If last frame of sequence we can return success. */
17537 if (fctl & FC_FC_END_SEQ)
17538 return 1;
17539 }
17540 return 0;
17541}
17542
17543/**
17544 * lpfc_prep_seq - Prep sequence for ULP processing
17545 * @vport: Pointer to the vport on which this sequence was received
17546 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17547 *
17548 * This function takes a sequence, described by a list of frames, and creates
17549 * a list of iocbq structures to describe the sequence. This iocbq list will be
17550 * used to issue to the generic unsolicited sequence handler. This routine
17551 * returns a pointer to the first iocbq in the list. If the function is unable
17552 * to allocate an iocbq then it throw out the received frames that were not
17553 * able to be described and return a pointer to the first iocbq. If unable to
17554 * allocate any iocbqs (including the first) this function will return NULL.
17555 **/
17556static struct lpfc_iocbq *
17557lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17558{
James Smart7851fe22011-07-22 18:36:52 -040017559 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017560 struct lpfc_dmabuf *d_buf, *n_buf;
17561 struct lpfc_iocbq *first_iocbq, *iocbq;
17562 struct fc_frame_header *fc_hdr;
17563 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017564 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017565 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017566
17567 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17568 /* remove from receive buffer list */
17569 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017570 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017571 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017572 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017573 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017574 /* Get an iocbq struct to fill in. */
17575 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17576 if (first_iocbq) {
17577 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017578 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017579 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017580 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017581
17582 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17583 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17584 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17585 first_iocbq->iocb.un.rcvels.parmRo =
17586 sli4_did_from_fc_hdr(fc_hdr);
17587 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17588 } else
17589 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017590 first_iocbq->iocb.ulpContext = NO_XRI;
17591 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17592 be16_to_cpu(fc_hdr->fh_ox_id);
17593 /* iocbq is prepped for internal consumption. Physical vpi. */
17594 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17595 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017596 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017597 tot_len = bf_get(lpfc_rcqe_length,
17598 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17599
James Smart4f774512009-05-22 14:52:35 -040017600 first_iocbq->context2 = &seq_dmabuf->dbuf;
17601 first_iocbq->context3 = NULL;
17602 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017603 if (tot_len > LPFC_DATA_BUF_SIZE)
17604 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017605 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017606 else
17607 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17608
James Smart4f774512009-05-22 14:52:35 -040017609 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017610
James Smart7851fe22011-07-22 18:36:52 -040017611 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017612 }
17613 iocbq = first_iocbq;
17614 /*
17615 * Each IOCBq can have two Buffers assigned, so go through the list
17616 * of buffers for this sequence and save two buffers in each IOCBq
17617 */
17618 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17619 if (!iocbq) {
17620 lpfc_in_buf_free(vport->phba, d_buf);
17621 continue;
17622 }
17623 if (!iocbq->context3) {
17624 iocbq->context3 = d_buf;
17625 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017626 /* We need to get the size out of the right CQE */
17627 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17628 len = bf_get(lpfc_rcqe_length,
17629 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017630 pbde = (struct ulp_bde64 *)
17631 &iocbq->iocb.unsli3.sli3Words[4];
17632 if (len > LPFC_DATA_BUF_SIZE)
17633 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17634 else
17635 pbde->tus.f.bdeSize = len;
17636
James Smart7851fe22011-07-22 18:36:52 -040017637 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17638 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017639 } else {
17640 iocbq = lpfc_sli_get_iocbq(vport->phba);
17641 if (!iocbq) {
17642 if (first_iocbq) {
17643 first_iocbq->iocb.ulpStatus =
17644 IOSTAT_FCP_RSP_ERROR;
17645 first_iocbq->iocb.un.ulpWord[4] =
17646 IOERR_NO_RESOURCES;
17647 }
17648 lpfc_in_buf_free(vport->phba, d_buf);
17649 continue;
17650 }
James Smart7851fe22011-07-22 18:36:52 -040017651 /* We need to get the size out of the right CQE */
17652 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17653 len = bf_get(lpfc_rcqe_length,
17654 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017655 iocbq->context2 = d_buf;
17656 iocbq->context3 = NULL;
17657 iocbq->iocb.ulpBdeCount = 1;
17658 if (len > LPFC_DATA_BUF_SIZE)
17659 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17660 LPFC_DATA_BUF_SIZE;
17661 else
17662 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17663
James Smart7851fe22011-07-22 18:36:52 -040017664 tot_len += len;
17665 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17666
James Smart4f774512009-05-22 14:52:35 -040017667 iocbq->iocb.un.rcvels.remoteID = sid;
17668 list_add_tail(&iocbq->list, &first_iocbq->list);
17669 }
17670 }
17671 return first_iocbq;
17672}
17673
James Smart6669f9b2009-10-02 15:16:45 -040017674static void
17675lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17676 struct hbq_dmabuf *seq_dmabuf)
17677{
17678 struct fc_frame_header *fc_hdr;
17679 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17680 struct lpfc_hba *phba = vport->phba;
17681
17682 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17683 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17684 if (!iocbq) {
17685 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17686 "2707 Ring %d handler: Failed to allocate "
17687 "iocb Rctl x%x Type x%x received\n",
17688 LPFC_ELS_RING,
17689 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17690 return;
17691 }
17692 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017693 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017694 iocbq, fc_hdr->fh_r_ctl,
17695 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017696 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017697 "2540 Ring %d handler: unexpected Rctl "
17698 "x%x Type x%x received\n",
17699 LPFC_ELS_RING,
17700 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17701
17702 /* Free iocb created in lpfc_prep_seq */
17703 list_for_each_entry_safe(curr_iocb, next_iocb,
17704 &iocbq->list, list) {
17705 list_del_init(&curr_iocb->list);
17706 lpfc_sli_release_iocbq(phba, curr_iocb);
17707 }
17708 lpfc_sli_release_iocbq(phba, iocbq);
17709}
17710
James Smartae9e28f2017-05-15 15:20:51 -070017711static void
17712lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17713 struct lpfc_iocbq *rspiocb)
17714{
17715 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17716
17717 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017718 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017719 kfree(pcmd);
17720 lpfc_sli_release_iocbq(phba, cmdiocb);
James Smarte817e5d2018-12-13 15:17:53 -080017721 lpfc_drain_txq(phba);
James Smartae9e28f2017-05-15 15:20:51 -070017722}
17723
17724static void
17725lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17726 struct hbq_dmabuf *dmabuf)
17727{
17728 struct fc_frame_header *fc_hdr;
17729 struct lpfc_hba *phba = vport->phba;
17730 struct lpfc_iocbq *iocbq = NULL;
17731 union lpfc_wqe *wqe;
17732 struct lpfc_dmabuf *pcmd = NULL;
17733 uint32_t frame_len;
17734 int rc;
James Smarte817e5d2018-12-13 15:17:53 -080017735 unsigned long iflags;
James Smartae9e28f2017-05-15 15:20:51 -070017736
17737 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17738 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17739
17740 /* Send the received frame back */
17741 iocbq = lpfc_sli_get_iocbq(phba);
James Smarte817e5d2018-12-13 15:17:53 -080017742 if (!iocbq) {
17743 /* Queue cq event and wakeup worker thread to process it */
17744 spin_lock_irqsave(&phba->hbalock, iflags);
17745 list_add_tail(&dmabuf->cq_event.list,
17746 &phba->sli4_hba.sp_queue_event);
17747 phba->hba_flag |= HBA_SP_QUEUE_EVT;
17748 spin_unlock_irqrestore(&phba->hbalock, iflags);
17749 lpfc_worker_wake_up(phba);
17750 return;
17751 }
James Smartae9e28f2017-05-15 15:20:51 -070017752
17753 /* Allocate buffer for command payload */
17754 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17755 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017756 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017757 &pcmd->phys);
17758 if (!pcmd || !pcmd->virt)
17759 goto exit;
17760
17761 INIT_LIST_HEAD(&pcmd->list);
17762
17763 /* copyin the payload */
17764 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17765
17766 /* fill in BDE's for command */
17767 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17768 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17769 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17770 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17771
17772 iocbq->context2 = pcmd;
17773 iocbq->vport = vport;
17774 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17775 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17776
17777 /*
17778 * Setup rest of the iocb as though it were a WQE
17779 * Build the SEND_FRAME WQE
17780 */
17781 wqe = (union lpfc_wqe *)&iocbq->iocb;
17782
17783 wqe->send_frame.frame_len = frame_len;
17784 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17785 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17786 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17787 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17788 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17789 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17790
17791 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17792 iocbq->iocb.ulpLe = 1;
17793 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17794 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17795 if (rc == IOCB_ERROR)
17796 goto exit;
17797
17798 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17799 return;
17800
17801exit:
17802 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17803 "2023 Unable to process MDS loopback frame\n");
17804 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017805 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017806 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017807 if (iocbq)
17808 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017809 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17810}
17811
James Smart4f774512009-05-22 14:52:35 -040017812/**
17813 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17814 * @phba: Pointer to HBA context object.
17815 *
17816 * This function is called with no lock held. This function processes all
17817 * the received buffers and gives it to upper layers when a received buffer
17818 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017819 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017820 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17821 * appropriate receive function when the final frame in a sequence is received.
17822 **/
James Smart4d9ab992009-10-02 15:16:39 -040017823void
17824lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17825 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017826{
James Smart4d9ab992009-10-02 15:16:39 -040017827 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017828 struct fc_frame_header *fc_hdr;
17829 struct lpfc_vport *vport;
17830 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017831 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017832
James Smart4f774512009-05-22 14:52:35 -040017833 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017834 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017835
James Smarte817e5d2018-12-13 15:17:53 -080017836 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
17837 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
17838 vport = phba->pport;
17839 /* Handle MDS Loopback frames */
17840 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17841 return;
17842 }
17843
James Smart4d9ab992009-10-02 15:16:39 -040017844 /* check to see if this a valid type of frame */
17845 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17846 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17847 return;
17848 }
James Smart2ea259e2017-02-12 13:52:27 -080017849
James Smart7851fe22011-07-22 18:36:52 -040017850 if ((bf_get(lpfc_cqe_code,
17851 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17852 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17853 &dmabuf->cq_event.cqe.rcqe_cmpl);
17854 else
17855 fcfi = bf_get(lpfc_rcqe_fcf_id,
17856 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017857
James Smart895427b2017-02-12 13:52:30 -080017858 /* d_id this frame is directed to */
17859 did = sli4_did_from_fc_hdr(fc_hdr);
17860
17861 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017862 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017863 /* throw out the frame */
17864 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17865 return;
17866 }
James Smart939723a2012-05-09 21:19:03 -040017867
James Smart939723a2012-05-09 21:19:03 -040017868 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17869 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17870 (did != Fabric_DID)) {
17871 /*
17872 * Throw out the frame if we are not pt2pt.
17873 * The pt2pt protocol allows for discovery frames
17874 * to be received without a registered VPI.
17875 */
17876 if (!(vport->fc_flag & FC_PT2PT) ||
17877 (phba->link_state == LPFC_HBA_READY)) {
17878 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17879 return;
17880 }
17881 }
17882
James Smart6669f9b2009-10-02 15:16:45 -040017883 /* Handle the basic abort sequence (BA_ABTS) event */
17884 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17885 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17886 return;
17887 }
17888
James Smart4d9ab992009-10-02 15:16:39 -040017889 /* Link this frame */
17890 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17891 if (!seq_dmabuf) {
17892 /* unable to add frame to vport - throw it out */
17893 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17894 return;
17895 }
17896 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017897 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017898 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017899
James Smart6669f9b2009-10-02 15:16:45 -040017900 /* Send the complete sequence to the upper layer protocol */
17901 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017902}
James Smart6fb120a2009-05-22 14:52:59 -040017903
17904/**
17905 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17906 * @phba: pointer to lpfc hba data structure.
17907 *
17908 * This routine is invoked to post rpi header templates to the
17909 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017910 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17911 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017912 *
17913 * This routine does not require any locks. It's usage is expected
17914 * to be driver load or reset recovery when the driver is
17915 * sequential.
17916 *
17917 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017918 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017919 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017920 * When this error occurs, the driver is not guaranteed
17921 * to have any rpi regions posted to the device and
17922 * must either attempt to repost the regions or take a
17923 * fatal error.
17924 **/
17925int
17926lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17927{
17928 struct lpfc_rpi_hdr *rpi_page;
17929 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017930 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017931
James Smart6d368e52011-05-24 11:44:12 -040017932 /* SLI4 ports that support extents do not require RPI headers. */
17933 if (!phba->sli4_hba.rpi_hdrs_in_use)
17934 goto exit;
17935 if (phba->sli4_hba.extents_in_use)
17936 return -EIO;
17937
James Smart6fb120a2009-05-22 14:52:59 -040017938 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017939 /*
17940 * Assign the rpi headers a physical rpi only if the driver
17941 * has not initialized those resources. A port reset only
17942 * needs the headers posted.
17943 */
17944 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17945 LPFC_RPI_RSRC_RDY)
17946 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17947
James Smart6fb120a2009-05-22 14:52:59 -040017948 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17949 if (rc != MBX_SUCCESS) {
17950 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17951 "2008 Error %d posting all rpi "
17952 "headers\n", rc);
17953 rc = -EIO;
17954 break;
17955 }
17956 }
17957
James Smart6d368e52011-05-24 11:44:12 -040017958 exit:
17959 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17960 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017961 return rc;
17962}
17963
17964/**
17965 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17966 * @phba: pointer to lpfc hba data structure.
17967 * @rpi_page: pointer to the rpi memory region.
17968 *
17969 * This routine is invoked to post a single rpi header to the
17970 * HBA consistent with the SLI-4 interface spec. This memory region
17971 * maps up to 64 rpi context regions.
17972 *
17973 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017974 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017975 * -ENOMEM - No available memory
17976 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017977 **/
17978int
17979lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17980{
17981 LPFC_MBOXQ_t *mboxq;
17982 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17983 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017984 uint32_t shdr_status, shdr_add_status;
17985 union lpfc_sli4_cfg_shdr *shdr;
17986
James Smart6d368e52011-05-24 11:44:12 -040017987 /* SLI4 ports that support extents do not require RPI headers. */
17988 if (!phba->sli4_hba.rpi_hdrs_in_use)
17989 return rc;
17990 if (phba->sli4_hba.extents_in_use)
17991 return -EIO;
17992
James Smart6fb120a2009-05-22 14:52:59 -040017993 /* The port is notified of the header region via a mailbox command. */
17994 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17995 if (!mboxq) {
17996 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17997 "2001 Unable to allocate memory for issuing "
17998 "SLI_CONFIG_SPECIAL mailbox command\n");
17999 return -ENOMEM;
18000 }
18001
18002 /* Post all rpi memory regions to the port. */
18003 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040018004 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18005 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18006 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050018007 sizeof(struct lpfc_sli4_cfg_mhdr),
18008 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040018009
18010
18011 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040018012 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18013 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040018014 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18015 hdr_tmpl, rpi_page->page_count);
18016
James Smart6fb120a2009-05-22 14:52:59 -040018017 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18018 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040018019 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040018020 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18021 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18022 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18023 if (rc != MBX_TIMEOUT)
18024 mempool_free(mboxq, phba->mbox_mem_pool);
18025 if (shdr_status || shdr_add_status || rc) {
18026 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18027 "2514 POST_RPI_HDR mailbox failed with "
18028 "status x%x add_status x%x, mbx status x%x\n",
18029 shdr_status, shdr_add_status, rc);
18030 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070018031 } else {
18032 /*
18033 * The next_rpi stores the next logical module-64 rpi value used
18034 * to post physical rpis in subsequent rpi postings.
18035 */
18036 spin_lock_irq(&phba->hbalock);
18037 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18038 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040018039 }
18040 return rc;
18041}
18042
18043/**
18044 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18045 * @phba: pointer to lpfc hba data structure.
18046 *
18047 * This routine is invoked to post rpi header templates to the
18048 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018049 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18050 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018051 *
18052 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018053 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040018054 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
18055 **/
18056int
18057lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18058{
James Smart6d368e52011-05-24 11:44:12 -040018059 unsigned long rpi;
18060 uint16_t max_rpi, rpi_limit;
18061 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018062 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040018063 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040018064
James Smart6fb120a2009-05-22 14:52:59 -040018065 /*
James Smart6d368e52011-05-24 11:44:12 -040018066 * Fetch the next logical rpi. Because this index is logical,
18067 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040018068 */
James Smart4902b382013-10-10 12:20:35 -040018069 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040018070 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18071 rpi_limit = phba->sli4_hba.next_rpi;
18072
James Smart6d368e52011-05-24 11:44:12 -040018073 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18074 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040018075 rpi = LPFC_RPI_ALLOC_ERROR;
18076 else {
18077 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18078 phba->sli4_hba.max_cfg_param.rpi_used++;
18079 phba->sli4_hba.rpi_count++;
18080 }
James Smartbe6bb942015-04-07 15:07:22 -040018081 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18082 "0001 rpi:%x max:%x lim:%x\n",
18083 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040018084
18085 /*
18086 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040018087 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040018088 */
18089 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18090 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040018091 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018092 return rpi;
18093 }
18094
18095 /*
James Smart6d368e52011-05-24 11:44:12 -040018096 * RPI header postings are not required for SLI4 ports capable of
18097 * extents.
18098 */
18099 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040018100 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040018101 return rpi;
18102 }
18103
18104 /*
James Smart6fb120a2009-05-22 14:52:59 -040018105 * If the driver is running low on rpi resources, allocate another
18106 * page now. Note that the next_rpi value is used because
18107 * it represents how many are actually in use whereas max_rpi notes
18108 * how many are supported max by the device.
18109 */
James Smart6d368e52011-05-24 11:44:12 -040018110 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040018111 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018112 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18113 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18114 if (!rpi_hdr) {
18115 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18116 "2002 Error Could not grow rpi "
18117 "count\n");
18118 } else {
James Smart6d368e52011-05-24 11:44:12 -040018119 lrpi = rpi_hdr->start_rpi;
18120 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040018121 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18122 }
18123 }
18124
18125 return rpi;
18126}
18127
18128/**
18129 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18130 * @phba: pointer to lpfc hba data structure.
18131 *
18132 * This routine is invoked to release an rpi to the pool of
18133 * available rpis maintained by the driver.
18134 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018135static void
James Smartd7c47992010-06-08 18:31:54 -040018136__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18137{
18138 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18139 phba->sli4_hba.rpi_count--;
18140 phba->sli4_hba.max_cfg_param.rpi_used--;
18141 }
18142}
18143
18144/**
18145 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18146 * @phba: pointer to lpfc hba data structure.
18147 *
18148 * This routine is invoked to release an rpi to the pool of
18149 * available rpis maintained by the driver.
18150 **/
18151void
James Smart6fb120a2009-05-22 14:52:59 -040018152lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18153{
18154 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040018155 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040018156 spin_unlock_irq(&phba->hbalock);
18157}
18158
18159/**
18160 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18161 * @phba: pointer to lpfc hba data structure.
18162 *
18163 * This routine is invoked to remove the memory region that
18164 * provided rpi via a bitmask.
18165 **/
18166void
18167lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18168{
18169 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040018170 kfree(phba->sli4_hba.rpi_ids);
18171 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040018172}
18173
18174/**
18175 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18176 * @phba: pointer to lpfc hba data structure.
18177 *
18178 * This routine is invoked to remove the memory region that
18179 * provided rpi via a bitmask.
18180 **/
18181int
James Smart6b5151f2012-01-18 16:24:06 -050018182lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18183 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040018184{
18185 LPFC_MBOXQ_t *mboxq;
18186 struct lpfc_hba *phba = ndlp->phba;
18187 int rc;
18188
18189 /* The port is notified of the header region via a mailbox command. */
18190 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18191 if (!mboxq)
18192 return -ENOMEM;
18193
18194 /* Post all rpi memory regions to the port. */
18195 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050018196 if (cmpl) {
18197 mboxq->mbox_cmpl = cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080018198 mboxq->ctx_buf = arg;
18199 mboxq->ctx_ndlp = ndlp;
James Smart72859902012-01-18 16:25:38 -050018200 } else
18201 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050018202 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040018203 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18204 if (rc == MBX_NOT_FINISHED) {
18205 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18206 "2010 Resume RPI Mailbox failed "
18207 "status %d, mbxStatus x%x\n", rc,
18208 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18209 mempool_free(mboxq, phba->mbox_mem_pool);
18210 return -EIO;
18211 }
18212 return 0;
18213}
18214
18215/**
18216 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050018217 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040018218 *
James Smart76a95d72010-11-20 23:11:48 -050018219 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040018220 *
18221 * Returns:
18222 * 0 success
18223 * -Evalue otherwise
18224 **/
18225int
James Smart76a95d72010-11-20 23:11:48 -050018226lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040018227{
18228 LPFC_MBOXQ_t *mboxq;
18229 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040018230 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040018231 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050018232 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040018233 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18234 if (!mboxq)
18235 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050018236 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040018237 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040018238 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040018239 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050018240 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040018241 "2022 INIT VPI Mailbox failed "
18242 "status %d, mbxStatus x%x\n", rc,
18243 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040018244 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040018245 }
James Smart6a9c52c2009-10-02 15:16:51 -040018246 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050018247 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040018248
18249 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040018250}
18251
18252/**
18253 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
18254 * @phba: pointer to lpfc hba data structure.
18255 * @mboxq: Pointer to mailbox object.
18256 *
18257 * This routine is invoked to manually add a single FCF record. The caller
18258 * must pass a completely initialized FCF_Record. This routine takes
18259 * care of the nonembedded mailbox operations.
18260 **/
18261static void
18262lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
18263{
18264 void *virt_addr;
18265 union lpfc_sli4_cfg_shdr *shdr;
18266 uint32_t shdr_status, shdr_add_status;
18267
18268 virt_addr = mboxq->sge_array->addr[0];
18269 /* The IOCTL status is embedded in the mailbox subheader. */
18270 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18271 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18272 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18273
18274 if ((shdr_status || shdr_add_status) &&
18275 (shdr_status != STATUS_FCF_IN_USE))
18276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18277 "2558 ADD_FCF_RECORD mailbox failed with "
18278 "status x%x add_status x%x\n",
18279 shdr_status, shdr_add_status);
18280
18281 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18282}
18283
18284/**
18285 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18286 * @phba: pointer to lpfc hba data structure.
18287 * @fcf_record: pointer to the initialized fcf record to add.
18288 *
18289 * This routine is invoked to manually add a single FCF record. The caller
18290 * must pass a completely initialized FCF_Record. This routine takes
18291 * care of the nonembedded mailbox operations.
18292 **/
18293int
18294lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18295{
18296 int rc = 0;
18297 LPFC_MBOXQ_t *mboxq;
18298 uint8_t *bytep;
18299 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040018300 struct lpfc_mbx_sge sge;
18301 uint32_t alloc_len, req_len;
18302 uint32_t fcfindex;
18303
18304 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18305 if (!mboxq) {
18306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18307 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18308 return -ENOMEM;
18309 }
18310
18311 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18312 sizeof(uint32_t);
18313
18314 /* Allocate DMA memory and set up the non-embedded mailbox command */
18315 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18316 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18317 req_len, LPFC_SLI4_MBX_NEMBED);
18318 if (alloc_len < req_len) {
18319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18320 "2523 Allocated DMA memory size (x%x) is "
18321 "less than the requested DMA memory "
18322 "size (x%x)\n", alloc_len, req_len);
18323 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18324 return -ENOMEM;
18325 }
18326
18327 /*
18328 * Get the first SGE entry from the non-embedded DMA memory. This
18329 * routine only uses a single SGE.
18330 */
18331 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018332 virt_addr = mboxq->sge_array->addr[0];
18333 /*
18334 * Configure the FCF record for FCFI 0. This is the driver's
18335 * hardcoded default and gets used in nonFIP mode.
18336 */
18337 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18338 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18339 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18340
18341 /*
18342 * Copy the fcf_index and the FCF Record Data. The data starts after
18343 * the FCoE header plus word10. The data copy needs to be endian
18344 * correct.
18345 */
18346 bytep += sizeof(uint32_t);
18347 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18348 mboxq->vport = phba->pport;
18349 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18350 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18351 if (rc == MBX_NOT_FINISHED) {
18352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18353 "2515 ADD_FCF_RECORD mailbox failed with "
18354 "status 0x%x\n", rc);
18355 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18356 rc = -EIO;
18357 } else
18358 rc = 0;
18359
18360 return rc;
18361}
18362
18363/**
18364 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18365 * @phba: pointer to lpfc hba data structure.
18366 * @fcf_record: pointer to the fcf record to write the default data.
18367 * @fcf_index: FCF table entry index.
18368 *
18369 * This routine is invoked to build the driver's default FCF record. The
18370 * values used are hardcoded. This routine handles memory initialization.
18371 *
18372 **/
18373void
18374lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18375 struct fcf_record *fcf_record,
18376 uint16_t fcf_index)
18377{
18378 memset(fcf_record, 0, sizeof(struct fcf_record));
18379 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18380 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18381 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18382 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18383 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18384 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18385 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18386 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18387 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18388 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18389 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18390 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18391 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018392 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018393 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18394 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18395 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18396 /* Set the VLAN bit map */
18397 if (phba->valid_vlan) {
18398 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18399 = 1 << (phba->vlan_id % 8);
18400 }
18401}
18402
18403/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018404 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018405 * @phba: pointer to lpfc hba data structure.
18406 * @fcf_index: FCF table entry offset.
18407 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018408 * This routine is invoked to scan the entire FCF table by reading FCF
18409 * record and processing it one at a time starting from the @fcf_index
18410 * for initial FCF discovery or fast FCF failover rediscovery.
18411 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018412 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018413 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018414 **/
18415int
James Smart0c9ab6f2010-02-26 14:15:57 -050018416lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018417{
18418 int rc = 0, error;
18419 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018420
James Smart32b97932009-07-19 10:01:21 -040018421 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018422 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018423 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18424 if (!mboxq) {
18425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18426 "2000 Failed to allocate mbox for "
18427 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018428 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018429 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018430 }
James Smartecfd03c2010-02-12 14:41:27 -050018431 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018432 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018433 if (rc) {
18434 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018435 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018436 }
James Smartecfd03c2010-02-12 14:41:27 -050018437 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018438 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018439 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018440
18441 spin_lock_irq(&phba->hbalock);
18442 phba->hba_flag |= FCF_TS_INPROG;
18443 spin_unlock_irq(&phba->hbalock);
18444
James Smart6fb120a2009-05-22 14:52:59 -040018445 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018446 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018447 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018448 else {
James Smart38b92ef2010-08-04 16:11:39 -040018449 /* Reset eligible FCF count for new scan */
18450 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018451 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018452 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018453 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018454fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018455 if (error) {
18456 if (mboxq)
18457 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018458 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018459 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018460 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018461 spin_unlock_irq(&phba->hbalock);
18462 }
James Smart6fb120a2009-05-22 14:52:59 -040018463 return error;
18464}
James Smarta0c87cb2009-07-19 10:01:10 -040018465
18466/**
James Smarta93ff372010-10-22 11:06:08 -040018467 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018468 * @phba: pointer to lpfc hba data structure.
18469 * @fcf_index: FCF table entry offset.
18470 *
18471 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018472 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018473 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018474 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018475 * otherwise.
18476 **/
18477int
18478lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18479{
18480 int rc = 0, error;
18481 LPFC_MBOXQ_t *mboxq;
18482
18483 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18484 if (!mboxq) {
18485 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18486 "2763 Failed to allocate mbox for "
18487 "READ_FCF cmd\n");
18488 error = -ENOMEM;
18489 goto fail_fcf_read;
18490 }
18491 /* Construct the read FCF record mailbox command */
18492 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18493 if (rc) {
18494 error = -EINVAL;
18495 goto fail_fcf_read;
18496 }
18497 /* Issue the mailbox command asynchronously */
18498 mboxq->vport = phba->pport;
18499 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18500 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18501 if (rc == MBX_NOT_FINISHED)
18502 error = -EIO;
18503 else
18504 error = 0;
18505
18506fail_fcf_read:
18507 if (error && mboxq)
18508 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18509 return error;
18510}
18511
18512/**
18513 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18514 * @phba: pointer to lpfc hba data structure.
18515 * @fcf_index: FCF table entry offset.
18516 *
18517 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018518 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018519 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018520 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018521 * otherwise.
18522 **/
18523int
18524lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18525{
18526 int rc = 0, error;
18527 LPFC_MBOXQ_t *mboxq;
18528
18529 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18530 if (!mboxq) {
18531 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18532 "2758 Failed to allocate mbox for "
18533 "READ_FCF cmd\n");
18534 error = -ENOMEM;
18535 goto fail_fcf_read;
18536 }
18537 /* Construct the read FCF record mailbox command */
18538 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18539 if (rc) {
18540 error = -EINVAL;
18541 goto fail_fcf_read;
18542 }
18543 /* Issue the mailbox command asynchronously */
18544 mboxq->vport = phba->pport;
18545 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18546 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18547 if (rc == MBX_NOT_FINISHED)
18548 error = -EIO;
18549 else
18550 error = 0;
18551
18552fail_fcf_read:
18553 if (error && mboxq)
18554 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18555 return error;
18556}
18557
18558/**
James Smartf5cb5302015-12-16 18:11:52 -050018559 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018560 * phba pointer to the lpfc_hba struct for this port.
18561 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18562 * routine when the rr_bmask is empty. The FCF indecies are put into the
18563 * rr_bmask based on their priority level. Starting from the highest priority
18564 * to the lowest. The most likely FCF candidate will be in the highest
18565 * priority group. When this routine is called it searches the fcf_pri list for
18566 * next lowest priority group and repopulates the rr_bmask with only those
18567 * fcf_indexes.
18568 * returns:
18569 * 1=success 0=failure
18570 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018571static int
James Smart7d791df2011-07-22 18:37:52 -040018572lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18573{
18574 uint16_t next_fcf_pri;
18575 uint16_t last_index;
18576 struct lpfc_fcf_pri *fcf_pri;
18577 int rc;
18578 int ret = 0;
18579
18580 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18581 LPFC_SLI4_FCF_TBL_INDX_MAX);
18582 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18583 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018584
18585 /* Verify the priority list has 2 or more entries */
18586 spin_lock_irq(&phba->hbalock);
18587 if (list_empty(&phba->fcf.fcf_pri_list) ||
18588 list_is_singular(&phba->fcf.fcf_pri_list)) {
18589 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018590 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18591 "3061 Last IDX %d\n", last_index);
18592 return 0; /* Empty rr list */
18593 }
James Smart25626692013-03-01 16:36:54 -050018594 spin_unlock_irq(&phba->hbalock);
18595
James Smart7d791df2011-07-22 18:37:52 -040018596 next_fcf_pri = 0;
18597 /*
18598 * Clear the rr_bmask and set all of the bits that are at this
18599 * priority.
18600 */
18601 memset(phba->fcf.fcf_rr_bmask, 0,
18602 sizeof(*phba->fcf.fcf_rr_bmask));
18603 spin_lock_irq(&phba->hbalock);
18604 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18605 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18606 continue;
18607 /*
18608 * the 1st priority that has not FLOGI failed
18609 * will be the highest.
18610 */
18611 if (!next_fcf_pri)
18612 next_fcf_pri = fcf_pri->fcf_rec.priority;
18613 spin_unlock_irq(&phba->hbalock);
18614 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18615 rc = lpfc_sli4_fcf_rr_index_set(phba,
18616 fcf_pri->fcf_rec.fcf_index);
18617 if (rc)
18618 return 0;
18619 }
18620 spin_lock_irq(&phba->hbalock);
18621 }
18622 /*
18623 * if next_fcf_pri was not set above and the list is not empty then
18624 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018625 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018626 */
18627 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18628 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18629 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18630 /*
18631 * the 1st priority that has not FLOGI failed
18632 * will be the highest.
18633 */
18634 if (!next_fcf_pri)
18635 next_fcf_pri = fcf_pri->fcf_rec.priority;
18636 spin_unlock_irq(&phba->hbalock);
18637 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18638 rc = lpfc_sli4_fcf_rr_index_set(phba,
18639 fcf_pri->fcf_rec.fcf_index);
18640 if (rc)
18641 return 0;
18642 }
18643 spin_lock_irq(&phba->hbalock);
18644 }
18645 } else
18646 ret = 1;
18647 spin_unlock_irq(&phba->hbalock);
18648
18649 return ret;
18650}
18651/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018652 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18653 * @phba: pointer to lpfc hba data structure.
18654 *
18655 * This routine is to get the next eligible FCF record index in a round
18656 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018657 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018658 * shall be returned, otherwise, the next eligible FCF record's index
18659 * shall be returned.
18660 **/
18661uint16_t
18662lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18663{
18664 uint16_t next_fcf_index;
18665
James Smart421c6622013-01-03 15:44:16 -050018666initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018667 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018668 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18669
James Smart7d791df2011-07-22 18:37:52 -040018670next_priority:
James Smart421c6622013-01-03 15:44:16 -050018671 /* Determine the next fcf index to check */
18672 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018673 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18674 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018675 next_fcf_index);
18676
James Smart0c9ab6f2010-02-26 14:15:57 -050018677 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018678 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18679 /*
18680 * If we have wrapped then we need to clear the bits that
18681 * have been tested so that we can detect when we should
18682 * change the priority level.
18683 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018684 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18685 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018686 }
18687
James Smart0c9ab6f2010-02-26 14:15:57 -050018688
James Smart3804dc82010-07-14 15:31:37 -040018689 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018690 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18691 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18692 /*
18693 * If next fcf index is not found check if there are lower
18694 * Priority level fcf's in the fcf_priority list.
18695 * Set up the rr_bmask with all of the avaiable fcf bits
18696 * at that level and continue the selection process.
18697 */
18698 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018699 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018700 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18701 "2844 No roundrobin failover FCF available\n");
James Smart036cad12018-10-23 13:41:06 -070018702
18703 return LPFC_FCOE_FCF_NEXT_NONE;
James Smart3804dc82010-07-14 15:31:37 -040018704 }
18705
James Smart7d791df2011-07-22 18:37:52 -040018706 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18707 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018708 LPFC_FCF_FLOGI_FAILED) {
18709 if (list_is_singular(&phba->fcf.fcf_pri_list))
18710 return LPFC_FCOE_FCF_NEXT_NONE;
18711
James Smart7d791df2011-07-22 18:37:52 -040018712 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018713 }
James Smart7d791df2011-07-22 18:37:52 -040018714
James Smart3804dc82010-07-14 15:31:37 -040018715 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018716 "2845 Get next roundrobin failover FCF (x%x)\n",
18717 next_fcf_index);
18718
James Smart0c9ab6f2010-02-26 14:15:57 -050018719 return next_fcf_index;
18720}
18721
18722/**
18723 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18724 * @phba: pointer to lpfc hba data structure.
18725 *
18726 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018727 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018728 * does not go beyond the range of the driver allocated bmask dimension
18729 * before setting the bit.
18730 *
18731 * Returns 0 if the index bit successfully set, otherwise, it returns
18732 * -EINVAL.
18733 **/
18734int
18735lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18736{
18737 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18738 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018739 "2610 FCF (x%x) reached driver's book "
18740 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018741 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18742 return -EINVAL;
18743 }
18744 /* Set the eligible FCF record index bmask */
18745 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18746
James Smart3804dc82010-07-14 15:31:37 -040018747 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018748 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018749 "bmask\n", fcf_index);
18750
James Smart0c9ab6f2010-02-26 14:15:57 -050018751 return 0;
18752}
18753
18754/**
James Smart3804dc82010-07-14 15:31:37 -040018755 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018756 * @phba: pointer to lpfc hba data structure.
18757 *
18758 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018759 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018760 * does not go beyond the range of the driver allocated bmask dimension
18761 * before clearing the bit.
18762 **/
18763void
18764lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18765{
James Smart9a803a72013-09-06 12:17:56 -040018766 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018767 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18768 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018769 "2762 FCF (x%x) reached driver's book "
18770 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018771 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18772 return;
18773 }
18774 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018775 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018776 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18777 list) {
James Smart7d791df2011-07-22 18:37:52 -040018778 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18779 list_del_init(&fcf_pri->list);
18780 break;
18781 }
18782 }
18783 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018784 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018785
18786 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018787 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018788 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018789}
18790
18791/**
James Smartecfd03c2010-02-12 14:41:27 -050018792 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18793 * @phba: pointer to lpfc hba data structure.
18794 *
18795 * This routine is the completion routine for the rediscover FCF table mailbox
18796 * command. If the mailbox command returned failure, it will try to stop the
18797 * FCF rediscover wait timer.
18798 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018799static void
James Smartecfd03c2010-02-12 14:41:27 -050018800lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18801{
18802 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18803 uint32_t shdr_status, shdr_add_status;
18804
18805 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18806
18807 shdr_status = bf_get(lpfc_mbox_hdr_status,
18808 &redisc_fcf->header.cfg_shdr.response);
18809 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18810 &redisc_fcf->header.cfg_shdr.response);
18811 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018812 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018813 "2746 Requesting for FCF rediscovery failed "
18814 "status x%x add_status x%x\n",
18815 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018816 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018817 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018818 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018819 spin_unlock_irq(&phba->hbalock);
18820 /*
18821 * CVL event triggered FCF rediscover request failed,
18822 * last resort to re-try current registered FCF entry.
18823 */
18824 lpfc_retry_pport_discovery(phba);
18825 } else {
18826 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018827 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018828 spin_unlock_irq(&phba->hbalock);
18829 /*
18830 * DEAD FCF event triggered FCF rediscover request
18831 * failed, last resort to fail over as a link down
18832 * to FCF registration.
18833 */
18834 lpfc_sli4_fcf_dead_failthrough(phba);
18835 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018836 } else {
18837 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018838 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018839 /*
18840 * Start FCF rediscovery wait timer for pending FCF
18841 * before rescan FCF record table.
18842 */
18843 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018844 }
James Smartecfd03c2010-02-12 14:41:27 -050018845
18846 mempool_free(mbox, phba->mbox_mem_pool);
18847}
18848
18849/**
James Smart3804dc82010-07-14 15:31:37 -040018850 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018851 * @phba: pointer to lpfc hba data structure.
18852 *
18853 * This routine is invoked to request for rediscovery of the entire FCF table
18854 * by the port.
18855 **/
18856int
18857lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18858{
18859 LPFC_MBOXQ_t *mbox;
18860 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18861 int rc, length;
18862
James Smart0c9ab6f2010-02-26 14:15:57 -050018863 /* Cancel retry delay timers to all vports before FCF rediscover */
18864 lpfc_cancel_all_vport_retry_delay_timer(phba);
18865
James Smartecfd03c2010-02-12 14:41:27 -050018866 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18867 if (!mbox) {
18868 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18869 "2745 Failed to allocate mbox for "
18870 "requesting FCF rediscover.\n");
18871 return -ENOMEM;
18872 }
18873
18874 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18875 sizeof(struct lpfc_sli4_cfg_mhdr));
18876 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18877 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18878 length, LPFC_SLI4_MBX_EMBED);
18879
18880 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18881 /* Set count to 0 for invalidating the entire FCF database */
18882 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18883
18884 /* Issue the mailbox command asynchronously */
18885 mbox->vport = phba->pport;
18886 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18887 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18888
18889 if (rc == MBX_NOT_FINISHED) {
18890 mempool_free(mbox, phba->mbox_mem_pool);
18891 return -EIO;
18892 }
18893 return 0;
18894}
18895
18896/**
James Smartfc2b9892010-02-26 14:15:29 -050018897 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18898 * @phba: pointer to lpfc hba data structure.
18899 *
18900 * This function is the failover routine as a last resort to the FCF DEAD
18901 * event when driver failed to perform fast FCF failover.
18902 **/
18903void
18904lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18905{
18906 uint32_t link_state;
18907
18908 /*
18909 * Last resort as FCF DEAD event failover will treat this as
18910 * a link down, but save the link state because we don't want
18911 * it to be changed to Link Down unless it is already down.
18912 */
18913 link_state = phba->link_state;
18914 lpfc_linkdown(phba);
18915 phba->link_state = link_state;
18916
18917 /* Unregister FCF if no devices connected to it */
18918 lpfc_unregister_unused_fcf(phba);
18919}
18920
18921/**
James Smart026abb82011-12-13 13:20:45 -050018922 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018923 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018924 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018925 *
James Smart026abb82011-12-13 13:20:45 -050018926 * This function gets SLI3 port configure region 23 data through memory dump
18927 * mailbox command. When it successfully retrieves data, the size of the data
18928 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018929 **/
James Smart026abb82011-12-13 13:20:45 -050018930static uint32_t
18931lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018932{
18933 LPFC_MBOXQ_t *pmb = NULL;
18934 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018935 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018936 int rc;
18937
James Smart026abb82011-12-13 13:20:45 -050018938 if (!rgn23_data)
18939 return 0;
18940
James Smarta0c87cb2009-07-19 10:01:10 -040018941 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18942 if (!pmb) {
18943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018944 "2600 failed to allocate mailbox memory\n");
18945 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018946 }
18947 mb = &pmb->u.mb;
18948
James Smarta0c87cb2009-07-19 10:01:10 -040018949 do {
18950 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18951 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18952
18953 if (rc != MBX_SUCCESS) {
18954 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018955 "2601 failed to read config "
18956 "region 23, rc 0x%x Status 0x%x\n",
18957 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018958 mb->un.varDmp.word_cnt = 0;
18959 }
18960 /*
18961 * dump mem may return a zero when finished or we got a
18962 * mailbox error, either way we are done.
18963 */
18964 if (mb->un.varDmp.word_cnt == 0)
18965 break;
18966 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18967 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18968
18969 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018970 rgn23_data + offset,
18971 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018972 offset += mb->un.varDmp.word_cnt;
18973 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18974
James Smart026abb82011-12-13 13:20:45 -050018975 mempool_free(pmb, phba->mbox_mem_pool);
18976 return offset;
18977}
18978
18979/**
18980 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18981 * @phba: pointer to lpfc hba data structure.
18982 * @rgn23_data: pointer to configure region 23 data.
18983 *
18984 * This function gets SLI4 port configure region 23 data through memory dump
18985 * mailbox command. When it successfully retrieves data, the size of the data
18986 * will be returned, otherwise, 0 will be returned.
18987 **/
18988static uint32_t
18989lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18990{
18991 LPFC_MBOXQ_t *mboxq = NULL;
18992 struct lpfc_dmabuf *mp = NULL;
18993 struct lpfc_mqe *mqe;
18994 uint32_t data_length = 0;
18995 int rc;
18996
18997 if (!rgn23_data)
18998 return 0;
18999
19000 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19001 if (!mboxq) {
19002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19003 "3105 failed to allocate mailbox memory\n");
19004 return 0;
19005 }
19006
19007 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19008 goto out;
19009 mqe = &mboxq->u.mqe;
James Smart3e1f0712018-11-29 16:09:29 -080019010 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smart026abb82011-12-13 13:20:45 -050019011 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19012 if (rc)
19013 goto out;
19014 data_length = mqe->un.mb_words[5];
19015 if (data_length == 0)
19016 goto out;
19017 if (data_length > DMP_RGN23_SIZE) {
19018 data_length = 0;
19019 goto out;
19020 }
19021 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19022out:
19023 mempool_free(mboxq, phba->mbox_mem_pool);
19024 if (mp) {
19025 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19026 kfree(mp);
19027 }
19028 return data_length;
19029}
19030
19031/**
19032 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19033 * @phba: pointer to lpfc hba data structure.
19034 *
19035 * This function read region 23 and parse TLV for port status to
19036 * decide if the user disaled the port. If the TLV indicates the
19037 * port is disabled, the hba_flag is set accordingly.
19038 **/
19039void
19040lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19041{
19042 uint8_t *rgn23_data = NULL;
19043 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19044 uint32_t offset = 0;
19045
19046 /* Get adapter Region 23 data */
19047 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19048 if (!rgn23_data)
19049 goto out;
19050
19051 if (phba->sli_rev < LPFC_SLI_REV4)
19052 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19053 else {
19054 if_type = bf_get(lpfc_sli_intf_if_type,
19055 &phba->sli4_hba.sli_intf);
19056 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19057 goto out;
19058 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19059 }
James Smarta0c87cb2009-07-19 10:01:10 -040019060
19061 if (!data_size)
19062 goto out;
19063
19064 /* Check the region signature first */
19065 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19066 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19067 "2619 Config region 23 has bad signature\n");
19068 goto out;
19069 }
19070 offset += 4;
19071
19072 /* Check the data structure version */
19073 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19075 "2620 Config region 23 has bad version\n");
19076 goto out;
19077 }
19078 offset += 4;
19079
19080 /* Parse TLV entries in the region */
19081 while (offset < data_size) {
19082 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19083 break;
19084 /*
19085 * If the TLV is not driver specific TLV or driver id is
19086 * not linux driver id, skip the record.
19087 */
19088 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19089 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19090 (rgn23_data[offset + 3] != 0)) {
19091 offset += rgn23_data[offset + 1] * 4 + 4;
19092 continue;
19093 }
19094
19095 /* Driver found a driver specific TLV in the config region */
19096 sub_tlv_len = rgn23_data[offset + 1] * 4;
19097 offset += 4;
19098 tlv_offset = 0;
19099
19100 /*
19101 * Search for configured port state sub-TLV.
19102 */
19103 while ((offset < data_size) &&
19104 (tlv_offset < sub_tlv_len)) {
19105 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19106 offset += 4;
19107 tlv_offset += 4;
19108 break;
19109 }
19110 if (rgn23_data[offset] != PORT_STE_TYPE) {
19111 offset += rgn23_data[offset + 1] * 4 + 4;
19112 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19113 continue;
19114 }
19115
19116 /* This HBA contains PORT_STE configured */
19117 if (!rgn23_data[offset + 2])
19118 phba->hba_flag |= LINK_DISABLED;
19119
19120 goto out;
19121 }
19122 }
James Smart026abb82011-12-13 13:20:45 -050019123
James Smarta0c87cb2009-07-19 10:01:10 -040019124out:
James Smarta0c87cb2009-07-19 10:01:10 -040019125 kfree(rgn23_data);
19126 return;
19127}
James Smart695a8142010-01-26 23:08:03 -050019128
19129/**
James Smart52d52442011-05-24 11:42:45 -040019130 * lpfc_wr_object - write an object to the firmware
19131 * @phba: HBA structure that indicates port to create a queue on.
19132 * @dmabuf_list: list of dmabufs to write to the port.
19133 * @size: the total byte value of the objects to write to the port.
19134 * @offset: the current offset to be used to start the transfer.
19135 *
19136 * This routine will create a wr_object mailbox command to send to the port.
19137 * the mailbox command will be constructed using the dma buffers described in
19138 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19139 * BDEs that the imbedded mailbox can support. The @offset variable will be
19140 * used to indicate the starting offset of the transfer and will also return
19141 * the offset after the write object mailbox has completed. @size is used to
19142 * determine the end of the object and whether the eof bit should be set.
19143 *
19144 * Return 0 is successful and offset will contain the the new offset to use
19145 * for the next write.
19146 * Return negative value for error cases.
19147 **/
19148int
19149lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19150 uint32_t size, uint32_t *offset)
19151{
19152 struct lpfc_mbx_wr_object *wr_object;
19153 LPFC_MBOXQ_t *mbox;
19154 int rc = 0, i = 0;
James Smart50212672018-12-13 15:17:57 -080019155 uint32_t shdr_status, shdr_add_status, shdr_change_status;
James Smart52d52442011-05-24 11:42:45 -040019156 uint32_t mbox_tmo;
James Smart52d52442011-05-24 11:42:45 -040019157 struct lpfc_dmabuf *dmabuf;
19158 uint32_t written = 0;
James Smart50212672018-12-13 15:17:57 -080019159 bool check_change_status = false;
James Smart52d52442011-05-24 11:42:45 -040019160
19161 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19162 if (!mbox)
19163 return -ENOMEM;
19164
19165 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19166 LPFC_MBOX_OPCODE_WRITE_OBJECT,
19167 sizeof(struct lpfc_mbx_wr_object) -
19168 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19169
19170 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19171 wr_object->u.request.write_offset = *offset;
19172 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19173 wr_object->u.request.object_name[0] =
19174 cpu_to_le32(wr_object->u.request.object_name[0]);
19175 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19176 list_for_each_entry(dmabuf, dmabuf_list, list) {
19177 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19178 break;
19179 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19180 wr_object->u.request.bde[i].addrHigh =
19181 putPaddrHigh(dmabuf->phys);
19182 if (written + SLI4_PAGE_SIZE >= size) {
19183 wr_object->u.request.bde[i].tus.f.bdeSize =
19184 (size - written);
19185 written += (size - written);
19186 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
James Smart50212672018-12-13 15:17:57 -080019187 bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
19188 check_change_status = true;
James Smart52d52442011-05-24 11:42:45 -040019189 } else {
19190 wr_object->u.request.bde[i].tus.f.bdeSize =
19191 SLI4_PAGE_SIZE;
19192 written += SLI4_PAGE_SIZE;
19193 }
19194 i++;
19195 }
19196 wr_object->u.request.bde_count = i;
19197 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19198 if (!phba->sli4_hba.intr_enable)
19199 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19200 else {
James Smarta183a152011-10-10 21:32:43 -040019201 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040019202 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19203 }
19204 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart50212672018-12-13 15:17:57 -080019205 shdr_status = bf_get(lpfc_mbox_hdr_status,
19206 &wr_object->header.cfg_shdr.response);
19207 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19208 &wr_object->header.cfg_shdr.response);
19209 if (check_change_status) {
19210 shdr_change_status = bf_get(lpfc_wr_object_change_status,
19211 &wr_object->u.response);
19212 switch (shdr_change_status) {
19213 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
19214 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19215 "3198 Firmware write complete: System "
19216 "reboot required to instantiate\n");
19217 break;
19218 case (LPFC_CHANGE_STATUS_FW_RESET):
19219 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19220 "3199 Firmware write complete: Firmware"
19221 " reset required to instantiate\n");
19222 break;
19223 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
19224 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19225 "3200 Firmware write complete: Port "
19226 "Migration or PCI Reset required to "
19227 "instantiate\n");
19228 break;
19229 case (LPFC_CHANGE_STATUS_PCI_RESET):
19230 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19231 "3201 Firmware write complete: PCI "
19232 "Reset required to instantiate\n");
19233 break;
19234 default:
19235 break;
19236 }
19237 }
James Smart52d52442011-05-24 11:42:45 -040019238 if (rc != MBX_TIMEOUT)
19239 mempool_free(mbox, phba->mbox_mem_pool);
19240 if (shdr_status || shdr_add_status || rc) {
19241 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19242 "3025 Write Object mailbox failed with "
19243 "status x%x add_status x%x, mbx status x%x\n",
19244 shdr_status, shdr_add_status, rc);
19245 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080019246 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040019247 } else
19248 *offset += wr_object->u.response.actual_write_length;
19249 return rc;
19250}
19251
19252/**
James Smart695a8142010-01-26 23:08:03 -050019253 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
19254 * @vport: pointer to vport data structure.
19255 *
19256 * This function iterate through the mailboxq and clean up all REG_LOGIN
19257 * and REG_VPI mailbox commands associated with the vport. This function
19258 * is called when driver want to restart discovery of the vport due to
19259 * a Clear Virtual Link event.
19260 **/
19261void
19262lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
19263{
19264 struct lpfc_hba *phba = vport->phba;
19265 LPFC_MBOXQ_t *mb, *nextmb;
19266 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040019267 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040019268 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040019269 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040019270 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050019271 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050019272
James Smartd439d282010-09-29 11:18:45 -040019273 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050019274 spin_lock_irq(&phba->hbalock);
19275 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
19276 if (mb->vport != vport)
19277 continue;
19278
19279 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19280 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19281 continue;
19282
James Smartd439d282010-09-29 11:18:45 -040019283 list_del(&mb->list);
19284 list_add_tail(&mb->list, &mbox_cmd_list);
19285 }
19286 /* Clean up active mailbox command with the vport */
19287 mb = phba->sli.mbox_active;
19288 if (mb && (mb->vport == vport)) {
19289 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
19290 (mb->u.mb.mbxCommand == MBX_REG_VPI))
19291 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19292 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019293 act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -040019294 /* Put reference count for delayed processing */
19295 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19296 /* Unregister the RPI when mailbox complete */
19297 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19298 }
19299 }
James Smart63e801c2010-11-20 23:14:19 -050019300 /* Cleanup any mailbox completions which are not yet processed */
19301 do {
19302 restart_loop = 0;
19303 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19304 /*
19305 * If this mailox is already processed or it is
19306 * for another vport ignore it.
19307 */
19308 if ((mb->vport != vport) ||
19309 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19310 continue;
19311
19312 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19313 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19314 continue;
19315
19316 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19317 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019318 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smart63e801c2010-11-20 23:14:19 -050019319 /* Unregister the RPI when mailbox complete */
19320 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19321 restart_loop = 1;
19322 spin_unlock_irq(&phba->hbalock);
19323 spin_lock(shost->host_lock);
19324 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19325 spin_unlock(shost->host_lock);
19326 spin_lock_irq(&phba->hbalock);
19327 break;
19328 }
19329 }
19330 } while (restart_loop);
19331
James Smartd439d282010-09-29 11:18:45 -040019332 spin_unlock_irq(&phba->hbalock);
19333
19334 /* Release the cleaned-up mailbox commands */
19335 while (!list_empty(&mbox_cmd_list)) {
19336 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050019337 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019338 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
James Smart695a8142010-01-26 23:08:03 -050019339 if (mp) {
19340 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19341 kfree(mp);
19342 }
James Smart3e1f0712018-11-29 16:09:29 -080019343 mb->ctx_buf = NULL;
19344 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
19345 mb->ctx_ndlp = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019346 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019347 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019348 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019349 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019350 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019351 }
James Smart695a8142010-01-26 23:08:03 -050019352 }
James Smart695a8142010-01-26 23:08:03 -050019353 mempool_free(mb, phba->mbox_mem_pool);
19354 }
James Smartd439d282010-09-29 11:18:45 -040019355
19356 /* Release the ndlp with the cleaned-up active mailbox command */
19357 if (act_mbx_ndlp) {
19358 spin_lock(shost->host_lock);
19359 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19360 spin_unlock(shost->host_lock);
19361 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019362 }
James Smart695a8142010-01-26 23:08:03 -050019363}
19364
James Smart2a9bf3d2010-06-07 15:24:45 -040019365/**
19366 * lpfc_drain_txq - Drain the txq
19367 * @phba: Pointer to HBA context object.
19368 *
19369 * This function attempt to submit IOCBs on the txq
19370 * to the adapter. For SLI4 adapters, the txq contains
19371 * ELS IOCBs that have been deferred because the there
19372 * are no SGLs. This congestion can occur with large
19373 * vport counts during node discovery.
19374 **/
19375
19376uint32_t
19377lpfc_drain_txq(struct lpfc_hba *phba)
19378{
19379 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019380 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019381 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019382 unsigned long iflags = 0;
19383 char *fail_msg = NULL;
19384 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019385 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019386 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070019387 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040019388
James Smartdc19e3b2018-05-24 21:08:57 -070019389 if (phba->link_flag & LS_MDS_LOOPBACK) {
19390 /* MDS WQE are posted only to first WQ*/
James Smartcdb42be2019-01-28 11:14:21 -080019391 wq = phba->sli4_hba.hdwq[0].fcp_wq;
James Smartdc19e3b2018-05-24 21:08:57 -070019392 if (unlikely(!wq))
19393 return 0;
19394 pring = wq->pring;
19395 } else {
19396 wq = phba->sli4_hba.els_wq;
19397 if (unlikely(!wq))
19398 return 0;
19399 pring = lpfc_phba_elsring(phba);
19400 }
19401
19402 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019403 return 0;
James Smart895427b2017-02-12 13:52:30 -080019404
James Smart398d81c2013-05-31 17:04:19 -040019405 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019406 list_for_each_entry(piocbq, &pring->txq, list) {
19407 txq_cnt++;
19408 }
19409
19410 if (txq_cnt > pring->txq_max)
19411 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019412
James Smart398d81c2013-05-31 17:04:19 -040019413 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019414
James Smart0e9bb8d2013-03-01 16:35:12 -050019415 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019416 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019417
James Smart19ca7602010-11-20 23:11:55 -050019418 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019419 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019420 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019421 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19422 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019423 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019424 break;
19425 }
James Smart895427b2017-02-12 13:52:30 -080019426 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019427 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019428 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019429 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019430 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019431 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019432 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019433
19434 /* The xri and iocb resources secured,
19435 * attempt to issue request
19436 */
James Smart6d368e52011-05-24 11:44:12 -040019437 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019438 piocbq->sli4_xritag = sglq->sli4_xritag;
19439 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19440 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019441 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019442 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070019443 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019444 fail_msg = " - Wq is full";
19445 else
19446 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19447
19448 if (fail_msg) {
19449 /* Failed means we can't issue and need to cancel */
19450 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19451 "2822 IOCB failed %s iotag 0x%x "
19452 "xri 0x%x\n",
19453 fail_msg,
19454 piocbq->iotag, piocbq->sli4_xritag);
19455 list_add_tail(&piocbq->list, &completions);
19456 }
James Smart398d81c2013-05-31 17:04:19 -040019457 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019458 }
19459
James Smart2a9bf3d2010-06-07 15:24:45 -040019460 /* Cancel all the IOCBs that cannot be issued */
19461 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19462 IOERR_SLI_ABORTED);
19463
James Smart0e9bb8d2013-03-01 16:35:12 -050019464 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019465}
James Smart895427b2017-02-12 13:52:30 -080019466
19467/**
19468 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19469 * @phba: Pointer to HBA context object.
19470 * @pwqe: Pointer to command WQE.
19471 * @sglq: Pointer to the scatter gather queue object.
19472 *
19473 * This routine converts the bpl or bde that is in the WQE
19474 * to a sgl list for the sli4 hardware. The physical address
19475 * of the bpl/bde is converted back to a virtual address.
19476 * If the WQE contains a BPL then the list of BDE's is
19477 * converted to sli4_sge's. If the WQE contains a single
19478 * BDE then it is converted to a single sli_sge.
19479 * The WQE is still in cpu endianness so the contents of
19480 * the bpl can be used without byte swapping.
19481 *
19482 * Returns valid XRI = Success, NO_XRI = Failure.
19483 */
19484static uint16_t
19485lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19486 struct lpfc_sglq *sglq)
19487{
19488 uint16_t xritag = NO_XRI;
19489 struct ulp_bde64 *bpl = NULL;
19490 struct ulp_bde64 bde;
19491 struct sli4_sge *sgl = NULL;
19492 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080019493 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080019494 int numBdes = 0;
19495 int i = 0;
19496 uint32_t offset = 0; /* accumulated offset in the sg request list */
19497 int inbound = 0; /* number of sg reply entries inbound from firmware */
19498 uint32_t cmd;
19499
19500 if (!pwqeq || !sglq)
19501 return xritag;
19502
19503 sgl = (struct sli4_sge *)sglq->sgl;
19504 wqe = &pwqeq->wqe;
19505 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19506
19507 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19508 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19509 return sglq->sli4_xritag;
19510 numBdes = pwqeq->rsvd2;
19511 if (numBdes) {
19512 /* The addrHigh and addrLow fields within the WQE
19513 * have not been byteswapped yet so there is no
19514 * need to swap them back.
19515 */
19516 if (pwqeq->context3)
19517 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19518 else
19519 return xritag;
19520
19521 bpl = (struct ulp_bde64 *)dmabuf->virt;
19522 if (!bpl)
19523 return xritag;
19524
19525 for (i = 0; i < numBdes; i++) {
19526 /* Should already be byte swapped. */
19527 sgl->addr_hi = bpl->addrHigh;
19528 sgl->addr_lo = bpl->addrLow;
19529
19530 sgl->word2 = le32_to_cpu(sgl->word2);
19531 if ((i+1) == numBdes)
19532 bf_set(lpfc_sli4_sge_last, sgl, 1);
19533 else
19534 bf_set(lpfc_sli4_sge_last, sgl, 0);
19535 /* swap the size field back to the cpu so we
19536 * can assign it to the sgl.
19537 */
19538 bde.tus.w = le32_to_cpu(bpl->tus.w);
19539 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19540 /* The offsets in the sgl need to be accumulated
19541 * separately for the request and reply lists.
19542 * The request is always first, the reply follows.
19543 */
19544 switch (cmd) {
19545 case CMD_GEN_REQUEST64_WQE:
19546 /* add up the reply sg entries */
19547 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19548 inbound++;
19549 /* first inbound? reset the offset */
19550 if (inbound == 1)
19551 offset = 0;
19552 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19553 bf_set(lpfc_sli4_sge_type, sgl,
19554 LPFC_SGE_TYPE_DATA);
19555 offset += bde.tus.f.bdeSize;
19556 break;
19557 case CMD_FCP_TRSP64_WQE:
19558 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19559 bf_set(lpfc_sli4_sge_type, sgl,
19560 LPFC_SGE_TYPE_DATA);
19561 break;
19562 case CMD_FCP_TSEND64_WQE:
19563 case CMD_FCP_TRECEIVE64_WQE:
19564 bf_set(lpfc_sli4_sge_type, sgl,
19565 bpl->tus.f.bdeFlags);
19566 if (i < 3)
19567 offset = 0;
19568 else
19569 offset += bde.tus.f.bdeSize;
19570 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19571 break;
19572 }
19573 sgl->word2 = cpu_to_le32(sgl->word2);
19574 bpl++;
19575 sgl++;
19576 }
19577 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19578 /* The addrHigh and addrLow fields of the BDE have not
19579 * been byteswapped yet so they need to be swapped
19580 * before putting them in the sgl.
19581 */
19582 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19583 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19584 sgl->word2 = le32_to_cpu(sgl->word2);
19585 bf_set(lpfc_sli4_sge_last, sgl, 1);
19586 sgl->word2 = cpu_to_le32(sgl->word2);
19587 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19588 }
19589 return sglq->sli4_xritag;
19590}
19591
19592/**
19593 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19594 * @phba: Pointer to HBA context object.
19595 * @ring_number: Base sli ring number
19596 * @pwqe: Pointer to command WQE.
19597 **/
19598int
James Smart1fbf9742019-01-28 11:14:26 -080019599lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
James Smart895427b2017-02-12 13:52:30 -080019600 struct lpfc_iocbq *pwqe)
19601{
James Smart205e8242018-03-05 12:04:03 -080019602 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019603 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019604 struct lpfc_queue *wq;
19605 struct lpfc_sglq *sglq;
19606 struct lpfc_sli_ring *pring;
19607 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019608 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019609
19610 /* NVME_LS and NVME_LS ABTS requests. */
19611 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19612 pring = phba->sli4_hba.nvmels_wq->pring;
James Smart6a828b02019-01-28 11:14:31 -080019613 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19614 qp, wq_access);
James Smart895427b2017-02-12 13:52:30 -080019615 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19616 if (!sglq) {
19617 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19618 return WQE_BUSY;
19619 }
19620 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19621 pwqe->sli4_xritag = sglq->sli4_xritag;
19622 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19623 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19624 return WQE_ERROR;
19625 }
19626 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19627 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019628 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19629 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019630 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019631 return ret;
James Smart895427b2017-02-12 13:52:30 -080019632 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019633
James Smart895427b2017-02-12 13:52:30 -080019634 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19635 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19636 return 0;
19637 }
19638
19639 /* NVME_FCREQ and NVME_ABTS requests */
19640 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19641 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smart1fbf9742019-01-28 11:14:26 -080019642 wq = qp->nvme_wq;
19643 pring = wq->pring;
19644
19645 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->nvme_cq_map);
James Smart895427b2017-02-12 13:52:30 -080019646
James Smart6a828b02019-01-28 11:14:31 -080019647 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19648 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070019649 ret = lpfc_sli4_wq_put(wq, wqe);
19650 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019651 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019652 return ret;
James Smart895427b2017-02-12 13:52:30 -080019653 }
19654 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19655 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19656 return 0;
19657 }
19658
James Smartf358dd02017-02-12 13:52:34 -080019659 /* NVMET requests */
19660 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19661 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smart1fbf9742019-01-28 11:14:26 -080019662 wq = qp->nvme_wq;
19663 pring = wq->pring;
James Smartf358dd02017-02-12 13:52:34 -080019664
James Smartf358dd02017-02-12 13:52:34 -080019665 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019666 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019667 if (pwqe->sli4_xritag == NO_XRI) {
19668 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19669 pwqe->sli4_xritag = sglq->sli4_xritag;
19670 }
19671 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19672 pwqe->sli4_xritag);
James Smart1fbf9742019-01-28 11:14:26 -080019673 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->nvme_cq_map);
19674
James Smart6a828b02019-01-28 11:14:31 -080019675 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
19676 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070019677 ret = lpfc_sli4_wq_put(wq, wqe);
19678 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019679 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019680 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019681 }
19682 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19683 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19684 return 0;
19685 }
James Smart895427b2017-02-12 13:52:30 -080019686 return WQE_ERROR;
19687}
James Smartc4908502019-01-28 11:14:28 -080019688
19689#ifdef LPFC_MXP_STAT
19690/**
19691 * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
19692 * @phba: pointer to lpfc hba data structure.
19693 * @hwqid: belong to which HWQ.
19694 *
19695 * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
19696 * 15 seconds after a test case is running.
19697 *
19698 * The user should call lpfc_debugfs_multixripools_write before running a test
19699 * case to clear stat_snapshot_taken. Then the user starts a test case. During
19700 * test case is running, stat_snapshot_taken is incremented by 1 every time when
19701 * this routine is called from heartbeat timer. When stat_snapshot_taken is
19702 * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
19703 **/
19704void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
19705{
19706 struct lpfc_sli4_hdw_queue *qp;
19707 struct lpfc_multixri_pool *multixri_pool;
19708 struct lpfc_pvt_pool *pvt_pool;
19709 struct lpfc_pbl_pool *pbl_pool;
19710 u32 txcmplq_cnt;
19711
19712 qp = &phba->sli4_hba.hdwq[hwqid];
19713 multixri_pool = qp->p_multixri_pool;
19714 if (!multixri_pool)
19715 return;
19716
19717 if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
19718 pvt_pool = &qp->p_multixri_pool->pvt_pool;
19719 pbl_pool = &qp->p_multixri_pool->pbl_pool;
19720 txcmplq_cnt = qp->fcp_wq->pring->txcmplq_cnt;
19721 if (qp->nvme_wq)
19722 txcmplq_cnt += qp->nvme_wq->pring->txcmplq_cnt;
19723
19724 multixri_pool->stat_pbl_count = pbl_pool->count;
19725 multixri_pool->stat_pvt_count = pvt_pool->count;
19726 multixri_pool->stat_busy_count = txcmplq_cnt;
19727 }
19728
19729 multixri_pool->stat_snapshot_taken++;
19730}
19731#endif
19732
19733/**
19734 * lpfc_adjust_pvt_pool_count - Adjust private pool count
19735 * @phba: pointer to lpfc hba data structure.
19736 * @hwqid: belong to which HWQ.
19737 *
19738 * This routine moves some XRIs from private to public pool when private pool
19739 * is not busy.
19740 **/
19741void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
19742{
19743 struct lpfc_multixri_pool *multixri_pool;
19744 u32 io_req_count;
19745 u32 prev_io_req_count;
19746
19747 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
19748 if (!multixri_pool)
19749 return;
19750 io_req_count = multixri_pool->io_req_count;
19751 prev_io_req_count = multixri_pool->prev_io_req_count;
19752
19753 if (prev_io_req_count != io_req_count) {
19754 /* Private pool is busy */
19755 multixri_pool->prev_io_req_count = io_req_count;
19756 } else {
19757 /* Private pool is not busy.
19758 * Move XRIs from private to public pool.
19759 */
19760 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
19761 }
19762}
19763
19764/**
19765 * lpfc_adjust_high_watermark - Adjust high watermark
19766 * @phba: pointer to lpfc hba data structure.
19767 * @hwqid: belong to which HWQ.
19768 *
19769 * This routine sets high watermark as number of outstanding XRIs,
19770 * but make sure the new value is between xri_limit/2 and xri_limit.
19771 **/
19772void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
19773{
19774 u32 new_watermark;
19775 u32 watermark_max;
19776 u32 watermark_min;
19777 u32 xri_limit;
19778 u32 txcmplq_cnt;
19779 u32 abts_io_bufs;
19780 struct lpfc_multixri_pool *multixri_pool;
19781 struct lpfc_sli4_hdw_queue *qp;
19782
19783 qp = &phba->sli4_hba.hdwq[hwqid];
19784 multixri_pool = qp->p_multixri_pool;
19785 if (!multixri_pool)
19786 return;
19787 xri_limit = multixri_pool->xri_limit;
19788
19789 watermark_max = xri_limit;
19790 watermark_min = xri_limit / 2;
19791
19792 txcmplq_cnt = qp->fcp_wq->pring->txcmplq_cnt;
19793 abts_io_bufs = qp->abts_scsi_io_bufs;
19794 if (qp->nvme_wq) {
19795 txcmplq_cnt += qp->nvme_wq->pring->txcmplq_cnt;
19796 abts_io_bufs += qp->abts_nvme_io_bufs;
19797 }
19798
19799 new_watermark = txcmplq_cnt + abts_io_bufs;
19800 new_watermark = min(watermark_max, new_watermark);
19801 new_watermark = max(watermark_min, new_watermark);
19802 multixri_pool->pvt_pool.high_watermark = new_watermark;
19803
19804#ifdef LPFC_MXP_STAT
19805 multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
19806 new_watermark);
19807#endif
19808}
19809
19810/**
19811 * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
19812 * @phba: pointer to lpfc hba data structure.
19813 * @hwqid: belong to which HWQ.
19814 *
19815 * This routine is called from hearbeat timer when pvt_pool is idle.
19816 * All free XRIs are moved from private to public pool on hwqid with 2 steps.
19817 * The first step moves (all - low_watermark) amount of XRIs.
19818 * The second step moves the rest of XRIs.
19819 **/
19820void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
19821{
19822 struct lpfc_pbl_pool *pbl_pool;
19823 struct lpfc_pvt_pool *pvt_pool;
James Smart6a828b02019-01-28 11:14:31 -080019824 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080019825 struct lpfc_io_buf *lpfc_ncmd;
19826 struct lpfc_io_buf *lpfc_ncmd_next;
19827 unsigned long iflag;
19828 struct list_head tmp_list;
19829 u32 tmp_count;
19830
James Smart6a828b02019-01-28 11:14:31 -080019831 qp = &phba->sli4_hba.hdwq[hwqid];
19832 pbl_pool = &qp->p_multixri_pool->pbl_pool;
19833 pvt_pool = &qp->p_multixri_pool->pvt_pool;
James Smartc4908502019-01-28 11:14:28 -080019834 tmp_count = 0;
19835
James Smart6a828b02019-01-28 11:14:31 -080019836 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
19837 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080019838
19839 if (pvt_pool->count > pvt_pool->low_watermark) {
19840 /* Step 1: move (all - low_watermark) from pvt_pool
19841 * to pbl_pool
19842 */
19843
19844 /* Move low watermark of bufs from pvt_pool to tmp_list */
19845 INIT_LIST_HEAD(&tmp_list);
19846 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
19847 &pvt_pool->list, list) {
19848 list_move_tail(&lpfc_ncmd->list, &tmp_list);
19849 tmp_count++;
19850 if (tmp_count >= pvt_pool->low_watermark)
19851 break;
19852 }
19853
19854 /* Move all bufs from pvt_pool to pbl_pool */
19855 list_splice_init(&pvt_pool->list, &pbl_pool->list);
19856
19857 /* Move all bufs from tmp_list to pvt_pool */
19858 list_splice(&tmp_list, &pvt_pool->list);
19859
19860 pbl_pool->count += (pvt_pool->count - tmp_count);
19861 pvt_pool->count = tmp_count;
19862 } else {
19863 /* Step 2: move the rest from pvt_pool to pbl_pool */
19864 list_splice_init(&pvt_pool->list, &pbl_pool->list);
19865 pbl_pool->count += pvt_pool->count;
19866 pvt_pool->count = 0;
19867 }
19868
19869 spin_unlock(&pvt_pool->lock);
19870 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
19871}
19872
19873/**
19874 * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
19875 * @phba: pointer to lpfc hba data structure
19876 * @pbl_pool: specified public free XRI pool
19877 * @pvt_pool: specified private free XRI pool
19878 * @count: number of XRIs to move
19879 *
19880 * This routine tries to move some free common bufs from the specified pbl_pool
19881 * to the specified pvt_pool. It might move less than count XRIs if there's not
19882 * enough in public pool.
19883 *
19884 * Return:
19885 * true - if XRIs are successfully moved from the specified pbl_pool to the
19886 * specified pvt_pool
19887 * false - if the specified pbl_pool is empty or locked by someone else
19888 **/
19889static bool
James Smart6a828b02019-01-28 11:14:31 -080019890_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
19891 struct lpfc_pbl_pool *pbl_pool,
James Smartc4908502019-01-28 11:14:28 -080019892 struct lpfc_pvt_pool *pvt_pool, u32 count)
19893{
19894 struct lpfc_io_buf *lpfc_ncmd;
19895 struct lpfc_io_buf *lpfc_ncmd_next;
19896 unsigned long iflag;
19897 int ret;
19898
19899 ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
19900 if (ret) {
19901 if (pbl_pool->count) {
19902 /* Move a batch of XRIs from public to private pool */
James Smart6a828b02019-01-28 11:14:31 -080019903 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080019904 list_for_each_entry_safe(lpfc_ncmd,
19905 lpfc_ncmd_next,
19906 &pbl_pool->list,
19907 list) {
19908 list_move_tail(&lpfc_ncmd->list,
19909 &pvt_pool->list);
19910 pvt_pool->count++;
19911 pbl_pool->count--;
19912 count--;
19913 if (count == 0)
19914 break;
19915 }
19916
19917 spin_unlock(&pvt_pool->lock);
19918 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
19919 return true;
19920 }
19921 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
19922 }
19923
19924 return false;
19925}
19926
19927/**
19928 * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
19929 * @phba: pointer to lpfc hba data structure.
19930 * @hwqid: belong to which HWQ.
19931 * @count: number of XRIs to move
19932 *
19933 * This routine tries to find some free common bufs in one of public pools with
19934 * Round Robin method. The search always starts from local hwqid, then the next
19935 * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
19936 * a batch of free common bufs are moved to private pool on hwqid.
19937 * It might move less than count XRIs if there's not enough in public pool.
19938 **/
19939void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
19940{
19941 struct lpfc_multixri_pool *multixri_pool;
19942 struct lpfc_multixri_pool *next_multixri_pool;
19943 struct lpfc_pvt_pool *pvt_pool;
19944 struct lpfc_pbl_pool *pbl_pool;
James Smart6a828b02019-01-28 11:14:31 -080019945 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080019946 u32 next_hwqid;
19947 u32 hwq_count;
19948 int ret;
19949
James Smart6a828b02019-01-28 11:14:31 -080019950 qp = &phba->sli4_hba.hdwq[hwqid];
19951 multixri_pool = qp->p_multixri_pool;
James Smartc4908502019-01-28 11:14:28 -080019952 pvt_pool = &multixri_pool->pvt_pool;
19953 pbl_pool = &multixri_pool->pbl_pool;
19954
19955 /* Check if local pbl_pool is available */
James Smart6a828b02019-01-28 11:14:31 -080019956 ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080019957 if (ret) {
19958#ifdef LPFC_MXP_STAT
19959 multixri_pool->local_pbl_hit_count++;
19960#endif
19961 return;
19962 }
19963
19964 hwq_count = phba->cfg_hdw_queue;
19965
19966 /* Get the next hwqid which was found last time */
19967 next_hwqid = multixri_pool->rrb_next_hwqid;
19968
19969 do {
19970 /* Go to next hwq */
19971 next_hwqid = (next_hwqid + 1) % hwq_count;
19972
19973 next_multixri_pool =
19974 phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
19975 pbl_pool = &next_multixri_pool->pbl_pool;
19976
19977 /* Check if the public free xri pool is available */
19978 ret = _lpfc_move_xri_pbl_to_pvt(
James Smart6a828b02019-01-28 11:14:31 -080019979 phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080019980
19981 /* Exit while-loop if success or all hwqid are checked */
19982 } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
19983
19984 /* Starting point for the next time */
19985 multixri_pool->rrb_next_hwqid = next_hwqid;
19986
19987 if (!ret) {
19988 /* stats: all public pools are empty*/
19989 multixri_pool->pbl_empty_count++;
19990 }
19991
19992#ifdef LPFC_MXP_STAT
19993 if (ret) {
19994 if (next_hwqid == hwqid)
19995 multixri_pool->local_pbl_hit_count++;
19996 else
19997 multixri_pool->other_pbl_hit_count++;
19998 }
19999#endif
20000}
20001
20002/**
20003 * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
20004 * @phba: pointer to lpfc hba data structure.
20005 * @qp: belong to which HWQ.
20006 *
20007 * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
20008 * low watermark.
20009 **/
20010void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
20011{
20012 struct lpfc_multixri_pool *multixri_pool;
20013 struct lpfc_pvt_pool *pvt_pool;
20014
20015 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20016 pvt_pool = &multixri_pool->pvt_pool;
20017
20018 if (pvt_pool->count < pvt_pool->low_watermark)
20019 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20020}
20021
20022/**
20023 * lpfc_release_io_buf - Return one IO buf back to free pool
20024 * @phba: pointer to lpfc hba data structure.
20025 * @lpfc_ncmd: IO buf to be returned.
20026 * @qp: belong to which HWQ.
20027 *
20028 * This routine returns one IO buf back to free pool. If this is an urgent IO,
20029 * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
20030 * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
20031 * xri_limit. If cfg_xri_rebalancing==0, the IO buf is returned to
20032 * lpfc_io_buf_list_put.
20033 **/
20034void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
20035 struct lpfc_sli4_hdw_queue *qp)
20036{
20037 unsigned long iflag;
20038 struct lpfc_pbl_pool *pbl_pool;
20039 struct lpfc_pvt_pool *pvt_pool;
20040 struct lpfc_epd_pool *epd_pool;
20041 u32 txcmplq_cnt;
20042 u32 xri_owned;
20043 u32 xri_limit;
20044 u32 abts_io_bufs;
20045
20046 /* MUST zero fields if buffer is reused by another protocol */
20047 lpfc_ncmd->nvmeCmd = NULL;
20048 lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
20049 lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
20050
20051 if (phba->cfg_xri_rebalancing) {
20052 if (lpfc_ncmd->expedite) {
20053 /* Return to expedite pool */
20054 epd_pool = &phba->epd_pool;
20055 spin_lock_irqsave(&epd_pool->lock, iflag);
20056 list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
20057 epd_pool->count++;
20058 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20059 return;
20060 }
20061
20062 /* Avoid invalid access if an IO sneaks in and is being rejected
20063 * just _after_ xri pools are destroyed in lpfc_offline.
20064 * Nothing much can be done at this point.
20065 */
20066 if (!qp->p_multixri_pool)
20067 return;
20068
20069 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20070 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20071
20072 txcmplq_cnt = qp->fcp_wq->pring->txcmplq_cnt;
20073 abts_io_bufs = qp->abts_scsi_io_bufs;
20074 if (qp->nvme_wq) {
20075 txcmplq_cnt += qp->nvme_wq->pring->txcmplq_cnt;
20076 abts_io_bufs += qp->abts_nvme_io_bufs;
20077 }
20078
20079 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
20080 xri_limit = qp->p_multixri_pool->xri_limit;
20081
20082#ifdef LPFC_MXP_STAT
20083 if (xri_owned <= xri_limit)
20084 qp->p_multixri_pool->below_limit_count++;
20085 else
20086 qp->p_multixri_pool->above_limit_count++;
20087#endif
20088
20089 /* XRI goes to either public or private free xri pool
20090 * based on watermark and xri_limit
20091 */
20092 if ((pvt_pool->count < pvt_pool->low_watermark) ||
20093 (xri_owned < xri_limit &&
20094 pvt_pool->count < pvt_pool->high_watermark)) {
James Smart6a828b02019-01-28 11:14:31 -080020095 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
20096 qp, free_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020097 list_add_tail(&lpfc_ncmd->list,
20098 &pvt_pool->list);
20099 pvt_pool->count++;
20100 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20101 } else {
James Smart6a828b02019-01-28 11:14:31 -080020102 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
20103 qp, free_pub_pool);
James Smartc4908502019-01-28 11:14:28 -080020104 list_add_tail(&lpfc_ncmd->list,
20105 &pbl_pool->list);
20106 pbl_pool->count++;
20107 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20108 }
20109 } else {
James Smart6a828b02019-01-28 11:14:31 -080020110 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
20111 qp, free_xri);
James Smartc4908502019-01-28 11:14:28 -080020112 list_add_tail(&lpfc_ncmd->list,
20113 &qp->lpfc_io_buf_list_put);
20114 qp->put_io_bufs++;
20115 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
20116 iflag);
20117 }
20118}
20119
20120/**
20121 * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
20122 * @phba: pointer to lpfc hba data structure.
20123 * @pvt_pool: pointer to private pool data structure.
20124 * @ndlp: pointer to lpfc nodelist data structure.
20125 *
20126 * This routine tries to get one free IO buf from private pool.
20127 *
20128 * Return:
20129 * pointer to one free IO buf - if private pool is not empty
20130 * NULL - if private pool is empty
20131 **/
20132static struct lpfc_io_buf *
20133lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
James Smart6a828b02019-01-28 11:14:31 -080020134 struct lpfc_sli4_hdw_queue *qp,
James Smartc4908502019-01-28 11:14:28 -080020135 struct lpfc_pvt_pool *pvt_pool,
20136 struct lpfc_nodelist *ndlp)
20137{
20138 struct lpfc_io_buf *lpfc_ncmd;
20139 struct lpfc_io_buf *lpfc_ncmd_next;
20140 unsigned long iflag;
20141
James Smart6a828b02019-01-28 11:14:31 -080020142 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020143 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20144 &pvt_pool->list, list) {
20145 if (lpfc_test_rrq_active(
20146 phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
20147 continue;
20148 list_del(&lpfc_ncmd->list);
20149 pvt_pool->count--;
20150 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20151 return lpfc_ncmd;
20152 }
20153 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20154
20155 return NULL;
20156}
20157
20158/**
20159 * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
20160 * @phba: pointer to lpfc hba data structure.
20161 *
20162 * This routine tries to get one free IO buf from expedite pool.
20163 *
20164 * Return:
20165 * pointer to one free IO buf - if expedite pool is not empty
20166 * NULL - if expedite pool is empty
20167 **/
20168static struct lpfc_io_buf *
20169lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
20170{
20171 struct lpfc_io_buf *lpfc_ncmd;
20172 struct lpfc_io_buf *lpfc_ncmd_next;
20173 unsigned long iflag;
20174 struct lpfc_epd_pool *epd_pool;
20175
20176 epd_pool = &phba->epd_pool;
20177 lpfc_ncmd = NULL;
20178
20179 spin_lock_irqsave(&epd_pool->lock, iflag);
20180 if (epd_pool->count > 0) {
20181 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20182 &epd_pool->list, list) {
20183 list_del(&lpfc_ncmd->list);
20184 epd_pool->count--;
20185 break;
20186 }
20187 }
20188 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20189
20190 return lpfc_ncmd;
20191}
20192
20193/**
20194 * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
20195 * @phba: pointer to lpfc hba data structure.
20196 * @ndlp: pointer to lpfc nodelist data structure.
20197 * @hwqid: belong to which HWQ
20198 * @expedite: 1 means this request is urgent.
20199 *
20200 * This routine will do the following actions and then return a pointer to
20201 * one free IO buf.
20202 *
20203 * 1. If private free xri count is empty, move some XRIs from public to
20204 * private pool.
20205 * 2. Get one XRI from private free xri pool.
20206 * 3. If we fail to get one from pvt_pool and this is an expedite request,
20207 * get one free xri from expedite pool.
20208 *
20209 * Note: ndlp is only used on SCSI side for RRQ testing.
20210 * The caller should pass NULL for ndlp on NVME side.
20211 *
20212 * Return:
20213 * pointer to one free IO buf - if private pool is not empty
20214 * NULL - if private pool is empty
20215 **/
20216static struct lpfc_io_buf *
20217lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
20218 struct lpfc_nodelist *ndlp,
20219 int hwqid, int expedite)
20220{
20221 struct lpfc_sli4_hdw_queue *qp;
20222 struct lpfc_multixri_pool *multixri_pool;
20223 struct lpfc_pvt_pool *pvt_pool;
20224 struct lpfc_io_buf *lpfc_ncmd;
20225
20226 qp = &phba->sli4_hba.hdwq[hwqid];
20227 lpfc_ncmd = NULL;
20228 multixri_pool = qp->p_multixri_pool;
20229 pvt_pool = &multixri_pool->pvt_pool;
20230 multixri_pool->io_req_count++;
20231
20232 /* If pvt_pool is empty, move some XRIs from public to private pool */
20233 if (pvt_pool->count == 0)
20234 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20235
20236 /* Get one XRI from private free xri pool */
James Smart6a828b02019-01-28 11:14:31 -080020237 lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
James Smartc4908502019-01-28 11:14:28 -080020238
20239 if (lpfc_ncmd) {
20240 lpfc_ncmd->hdwq = qp;
20241 lpfc_ncmd->hdwq_no = hwqid;
20242 } else if (expedite) {
20243 /* If we fail to get one from pvt_pool and this is an expedite
20244 * request, get one free xri from expedite pool.
20245 */
20246 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
20247 }
20248
20249 return lpfc_ncmd;
20250}
20251
20252static inline struct lpfc_io_buf *
20253lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
20254{
20255 struct lpfc_sli4_hdw_queue *qp;
20256 struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
20257
20258 qp = &phba->sli4_hba.hdwq[idx];
20259 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
20260 &qp->lpfc_io_buf_list_get, list) {
20261 if (lpfc_test_rrq_active(phba, ndlp,
20262 lpfc_cmd->cur_iocbq.sli4_lxritag))
20263 continue;
20264
20265 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
20266 continue;
20267
20268 list_del_init(&lpfc_cmd->list);
20269 qp->get_io_bufs--;
20270 lpfc_cmd->hdwq = qp;
20271 lpfc_cmd->hdwq_no = idx;
20272 return lpfc_cmd;
20273 }
20274 return NULL;
20275}
20276
20277/**
20278 * lpfc_get_io_buf - Get one IO buffer from free pool
20279 * @phba: The HBA for which this call is being executed.
20280 * @ndlp: pointer to lpfc nodelist data structure.
20281 * @hwqid: belong to which HWQ
20282 * @expedite: 1 means this request is urgent.
20283 *
20284 * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
20285 * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
20286 * a IO buffer from head of @hdwq io_buf_list and returns to caller.
20287 *
20288 * Note: ndlp is only used on SCSI side for RRQ testing.
20289 * The caller should pass NULL for ndlp on NVME side.
20290 *
20291 * Return codes:
20292 * NULL - Error
20293 * Pointer to lpfc_io_buf - Success
20294 **/
20295struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
20296 struct lpfc_nodelist *ndlp,
20297 u32 hwqid, int expedite)
20298{
20299 struct lpfc_sli4_hdw_queue *qp;
20300 unsigned long iflag;
20301 struct lpfc_io_buf *lpfc_cmd;
20302
20303 qp = &phba->sli4_hba.hdwq[hwqid];
20304 lpfc_cmd = NULL;
20305
20306 if (phba->cfg_xri_rebalancing)
20307 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
20308 phba, ndlp, hwqid, expedite);
20309 else {
James Smart6a828b02019-01-28 11:14:31 -080020310 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
20311 qp, alloc_xri_get);
James Smartc4908502019-01-28 11:14:28 -080020312 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
20313 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20314 if (!lpfc_cmd) {
James Smart6a828b02019-01-28 11:14:31 -080020315 lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
20316 qp, alloc_xri_put);
James Smartc4908502019-01-28 11:14:28 -080020317 list_splice(&qp->lpfc_io_buf_list_put,
20318 &qp->lpfc_io_buf_list_get);
20319 qp->get_io_bufs += qp->put_io_bufs;
20320 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
20321 qp->put_io_bufs = 0;
20322 spin_unlock(&qp->io_buf_list_put_lock);
20323 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
20324 expedite)
20325 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20326 }
20327 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
20328 }
20329
20330 return lpfc_cmd;
20331}