blob: 90f039a41480ffae0c273ef7d9784a849c8724d4 [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 Smart19ca7602010-11-20 23:11:55 -05001136 struct lpfc_scsi_buf *lpfc_cmd;
1137 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) {
1143 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1144 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 Smart2a9bf3d2010-06-07 15:24:45 -04001599
James Smart92d7f7b2007-06-17 19:56:38 -05001600 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1601 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001602 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1603 BUG_ON(!piocb->vport);
1604 if (!(piocb->vport->load_flag & FC_UNLOADING))
1605 mod_timer(&piocb->vport->els_tmofunc,
1606 jiffies +
1607 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1608 }
dea31012005-04-17 16:05:31 -05001609
James Smart2e0fef82007-06-17 19:56:36 -05001610 return 0;
dea31012005-04-17 16:05:31 -05001611}
1612
James Smarte59058c2008-08-24 21:49:00 -04001613/**
James Smart3621a712009-04-06 18:47:14 -04001614 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001615 * @phba: Pointer to HBA context object.
1616 * @pring: Pointer to driver SLI ring object.
1617 *
1618 * This function is called with hbalock held to get next
1619 * iocb in txq of the given ring. If there is any iocb in
1620 * the txq, the function returns first iocb in the list after
1621 * removing the iocb from the list, else it returns NULL.
1622 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001623struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001624lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001625{
dea31012005-04-17 16:05:31 -05001626 struct lpfc_iocbq *cmd_iocb;
1627
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001628 lockdep_assert_held(&phba->hbalock);
1629
James Smart858c9f62007-06-17 19:56:39 -05001630 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001631 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001632}
1633
James Smarte59058c2008-08-24 21:49:00 -04001634/**
James Smart3621a712009-04-06 18:47:14 -04001635 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001636 * @phba: Pointer to HBA context object.
1637 * @pring: Pointer to driver SLI ring object.
1638 *
1639 * This function is called with hbalock held and the caller must post the
1640 * iocb without releasing the lock. If the caller releases the lock,
1641 * iocb slot returned by the function is not guaranteed to be available.
1642 * The function returns pointer to the next available iocb slot if there
1643 * is available slot in the ring, else it returns NULL.
1644 * If the get index of the ring is ahead of the put index, the function
1645 * will post an error attention event to the worker thread to take the
1646 * HBA to offline state.
1647 **/
dea31012005-04-17 16:05:31 -05001648static IOCB_t *
1649lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1650{
James Smart34b02dc2008-08-24 21:49:55 -04001651 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001652 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001653
1654 lockdep_assert_held(&phba->hbalock);
1655
James Smart7e56aa22012-08-03 12:35:34 -04001656 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1657 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1658 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001659
James Smart7e56aa22012-08-03 12:35:34 -04001660 if (unlikely(pring->sli.sli3.local_getidx ==
1661 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001662
James Smart7e56aa22012-08-03 12:35:34 -04001663 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001664
James Smart7e56aa22012-08-03 12:35:34 -04001665 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001666 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001667 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001668 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001669 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001670 pring->sli.sli3.local_getidx,
1671 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001672
James Smart2e0fef82007-06-17 19:56:36 -05001673 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001674 /*
1675 * All error attention handlers are posted to
1676 * worker thread
1677 */
1678 phba->work_ha |= HA_ERATT;
1679 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001680
James Smart5e9d9b82008-06-14 22:52:53 -04001681 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001682
1683 return NULL;
1684 }
1685
James Smart7e56aa22012-08-03 12:35:34 -04001686 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001687 return NULL;
1688 }
1689
James Smarted957682007-06-17 19:56:37 -05001690 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001691}
1692
James Smarte59058c2008-08-24 21:49:00 -04001693/**
James Smart3621a712009-04-06 18:47:14 -04001694 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001695 * @phba: Pointer to HBA context object.
1696 * @iocbq: Pointer to driver iocb object.
1697 *
1698 * This function gets an iotag for the iocb. If there is no unused iotag and
1699 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1700 * array and assigns a new iotag.
1701 * The function returns the allocated iotag if successful, else returns zero.
1702 * Zero is not a valid iotag.
1703 * The caller is not required to hold any lock.
1704 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001705uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001706lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001707{
James Smart2e0fef82007-06-17 19:56:36 -05001708 struct lpfc_iocbq **new_arr;
1709 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001710 size_t new_len;
1711 struct lpfc_sli *psli = &phba->sli;
1712 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001713
James Smart2e0fef82007-06-17 19:56:36 -05001714 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001715 iotag = psli->last_iotag;
1716 if(++iotag < psli->iocbq_lookup_len) {
1717 psli->last_iotag = iotag;
1718 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001719 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001720 iocbq->iotag = iotag;
1721 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001722 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001723 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1724 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001725 spin_unlock_irq(&phba->hbalock);
Kees Cook6396bb22018-06-12 14:03:40 -07001726 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001727 GFP_KERNEL);
1728 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001729 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001730 old_arr = psli->iocbq_lookup;
1731 if (new_len <= psli->iocbq_lookup_len) {
1732 /* highly unprobable case */
1733 kfree(new_arr);
1734 iotag = psli->last_iotag;
1735 if(++iotag < psli->iocbq_lookup_len) {
1736 psli->last_iotag = iotag;
1737 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001738 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001739 iocbq->iotag = iotag;
1740 return iotag;
1741 }
James Smart2e0fef82007-06-17 19:56:36 -05001742 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001743 return 0;
1744 }
1745 if (psli->iocbq_lookup)
1746 memcpy(new_arr, old_arr,
1747 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001748 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001749 psli->iocbq_lookup = new_arr;
1750 psli->iocbq_lookup_len = new_len;
1751 psli->last_iotag = iotag;
1752 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001753 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001754 iocbq->iotag = iotag;
1755 kfree(old_arr);
1756 return iotag;
1757 }
James Smart8f6d98d2006-08-01 07:34:00 -04001758 } else
James Smart2e0fef82007-06-17 19:56:36 -05001759 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001760
James Smartbc739052010-08-04 16:11:18 -04001761 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001762 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1763 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001764
James Bottomley604a3e32005-10-29 10:28:33 -05001765 return 0;
dea31012005-04-17 16:05:31 -05001766}
1767
James Smarte59058c2008-08-24 21:49:00 -04001768/**
James Smart3621a712009-04-06 18:47:14 -04001769 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001770 * @phba: Pointer to HBA context object.
1771 * @pring: Pointer to driver SLI ring object.
1772 * @iocb: Pointer to iocb slot in the ring.
1773 * @nextiocb: Pointer to driver iocb object which need to be
1774 * posted to firmware.
1775 *
1776 * This function is called with hbalock held to post a new iocb to
1777 * the firmware. This function copies the new iocb to ring iocb slot and
1778 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1779 * a completion call back for this iocb else the function will free the
1780 * iocb object.
1781 **/
dea31012005-04-17 16:05:31 -05001782static void
1783lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1784 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1785{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001786 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001787 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001788 * Set up an iotag
dea31012005-04-17 16:05:31 -05001789 */
James Bottomley604a3e32005-10-29 10:28:33 -05001790 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001791
James Smarte2a0a9d2008-12-04 22:40:02 -05001792
James Smarta58cbd52007-08-02 11:09:43 -04001793 if (pring->ringno == LPFC_ELS_RING) {
1794 lpfc_debugfs_slow_ring_trc(phba,
1795 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1796 *(((uint32_t *) &nextiocb->iocb) + 4),
1797 *(((uint32_t *) &nextiocb->iocb) + 6),
1798 *(((uint32_t *) &nextiocb->iocb) + 7));
1799 }
1800
dea31012005-04-17 16:05:31 -05001801 /*
1802 * Issue iocb command to adapter
1803 */
James Smart92d7f7b2007-06-17 19:56:38 -05001804 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001805 wmb();
1806 pring->stats.iocb_cmd++;
1807
1808 /*
1809 * If there is no completion routine to call, we can release the
1810 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1811 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1812 */
1813 if (nextiocb->iocb_cmpl)
1814 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001815 else
James Smart2e0fef82007-06-17 19:56:36 -05001816 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001817
1818 /*
1819 * Let the HBA know what IOCB slot will be the next one the
1820 * driver will put a command into.
1821 */
James Smart7e56aa22012-08-03 12:35:34 -04001822 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1823 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001824}
1825
James Smarte59058c2008-08-24 21:49:00 -04001826/**
James Smart3621a712009-04-06 18:47:14 -04001827 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001828 * @phba: Pointer to HBA context object.
1829 * @pring: Pointer to driver SLI ring object.
1830 *
1831 * The caller is not required to hold any lock for calling this function.
1832 * This function updates the chip attention bits for the ring to inform firmware
1833 * that there are pending work to be done for this ring and requests an
1834 * interrupt when there is space available in the ring. This function is
1835 * called when the driver is unable to post more iocbs to the ring due
1836 * to unavailability of space in the ring.
1837 **/
dea31012005-04-17 16:05:31 -05001838static void
James Smart2e0fef82007-06-17 19:56:36 -05001839lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001840{
1841 int ringno = pring->ringno;
1842
1843 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1844
1845 wmb();
1846
1847 /*
1848 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1849 * The HBA will tell us when an IOCB entry is available.
1850 */
1851 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1852 readl(phba->CAregaddr); /* flush */
1853
1854 pring->stats.iocb_cmd_full++;
1855}
1856
James Smarte59058c2008-08-24 21:49:00 -04001857/**
James Smart3621a712009-04-06 18:47:14 -04001858 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001859 * @phba: Pointer to HBA context object.
1860 * @pring: Pointer to driver SLI ring object.
1861 *
1862 * This function updates the chip attention register bit for the
1863 * given ring to inform HBA that there is more work to be done
1864 * in this ring. The caller is not required to hold any lock.
1865 **/
dea31012005-04-17 16:05:31 -05001866static void
James Smart2e0fef82007-06-17 19:56:36 -05001867lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001868{
1869 int ringno = pring->ringno;
1870
1871 /*
1872 * Tell the HBA that there is work to do in this ring.
1873 */
James Smart34b02dc2008-08-24 21:49:55 -04001874 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1875 wmb();
1876 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1877 readl(phba->CAregaddr); /* flush */
1878 }
dea31012005-04-17 16:05:31 -05001879}
1880
James Smarte59058c2008-08-24 21:49:00 -04001881/**
James Smart3621a712009-04-06 18:47:14 -04001882 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001883 * @phba: Pointer to HBA context object.
1884 * @pring: Pointer to driver SLI ring object.
1885 *
1886 * This function is called with hbalock held to post pending iocbs
1887 * in the txq to the firmware. This function is called when driver
1888 * detects space available in the ring.
1889 **/
dea31012005-04-17 16:05:31 -05001890static void
James Smart2e0fef82007-06-17 19:56:36 -05001891lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001892{
1893 IOCB_t *iocb;
1894 struct lpfc_iocbq *nextiocb;
1895
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001896 lockdep_assert_held(&phba->hbalock);
1897
dea31012005-04-17 16:05:31 -05001898 /*
1899 * Check to see if:
1900 * (a) there is anything on the txq to send
1901 * (b) link is up
1902 * (c) link attention events can be processed (fcp ring only)
1903 * (d) IOCB processing is not blocked by the outstanding mbox command.
1904 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001905
1906 if (lpfc_is_link_up(phba) &&
1907 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001908 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001909 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001910
1911 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1912 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1913 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1914
1915 if (iocb)
1916 lpfc_sli_update_ring(phba, pring);
1917 else
1918 lpfc_sli_update_full_ring(phba, pring);
1919 }
1920
1921 return;
1922}
1923
James Smarte59058c2008-08-24 21:49:00 -04001924/**
James Smart3621a712009-04-06 18:47:14 -04001925 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001926 * @phba: Pointer to HBA context object.
1927 * @hbqno: HBQ number.
1928 *
1929 * This function is called with hbalock held to get the next
1930 * available slot for the given HBQ. If there is free slot
1931 * available for the HBQ it will return pointer to the next available
1932 * HBQ entry else it will return NULL.
1933 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001934static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001935lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1936{
1937 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1938
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001939 lockdep_assert_held(&phba->hbalock);
1940
James Smarted957682007-06-17 19:56:37 -05001941 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1942 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1943 hbqp->next_hbqPutIdx = 0;
1944
1945 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001946 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001947 uint32_t getidx = le32_to_cpu(raw_index);
1948
1949 hbqp->local_hbqGetIdx = getidx;
1950
1951 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1952 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001953 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001954 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001955 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001956 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001957 hbqp->entry_count);
1958
1959 phba->link_state = LPFC_HBA_ERROR;
1960 return NULL;
1961 }
1962
1963 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1964 return NULL;
1965 }
1966
James Smart51ef4c22007-08-02 11:10:31 -04001967 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1968 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001969}
1970
James Smarte59058c2008-08-24 21:49:00 -04001971/**
James Smart3621a712009-04-06 18:47:14 -04001972 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001973 * @phba: Pointer to HBA context object.
1974 *
1975 * This function is called with no lock held to free all the
1976 * hbq buffers while uninitializing the SLI interface. It also
1977 * frees the HBQ buffers returned by the firmware but not yet
1978 * processed by the upper layers.
1979 **/
James Smarted957682007-06-17 19:56:37 -05001980void
1981lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1982{
James Smart92d7f7b2007-06-17 19:56:38 -05001983 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1984 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001985 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001986 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001987
James Smart51ef4c22007-08-02 11:10:31 -04001988 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001989 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001990 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001991 for (i = 0; i < hbq_count; ++i) {
1992 list_for_each_entry_safe(dmabuf, next_dmabuf,
1993 &phba->hbqs[i].hbq_buffer_list, list) {
1994 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1995 list_del(&hbq_buf->dbuf.list);
1996 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1997 }
James Smarta8adb832007-10-27 13:37:53 -04001998 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001999 }
James Smart3163f722008-02-08 18:50:25 -05002000
2001 /* Mark the HBQs not in use */
2002 phba->hbq_in_use = 0;
2003 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05002004}
2005
James Smarte59058c2008-08-24 21:49:00 -04002006/**
James Smart3621a712009-04-06 18:47:14 -04002007 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002008 * @phba: Pointer to HBA context object.
2009 * @hbqno: HBQ number.
2010 * @hbq_buf: Pointer to HBQ buffer.
2011 *
2012 * This function is called with the hbalock held to post a
2013 * hbq buffer to the firmware. If the function finds an empty
2014 * slot in the HBQ, it will post the buffer. The function will return
2015 * pointer to the hbq entry if it successfully post the buffer
2016 * else it will return NULL.
2017 **/
James Smart3772a992009-05-22 14:50:54 -04002018static int
James Smarted957682007-06-17 19:56:37 -05002019lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05002020 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05002021{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002022 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002023 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2024}
2025
2026/**
2027 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2028 * @phba: Pointer to HBA context object.
2029 * @hbqno: HBQ number.
2030 * @hbq_buf: Pointer to HBQ buffer.
2031 *
2032 * This function is called with the hbalock held to post a hbq buffer to the
2033 * firmware. If the function finds an empty slot in the HBQ, it will post the
2034 * buffer and place it on the hbq_buffer_list. The function will return zero if
2035 * it successfully post the buffer else it will return an error.
2036 **/
2037static int
2038lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2039 struct hbq_dmabuf *hbq_buf)
2040{
James Smarted957682007-06-17 19:56:37 -05002041 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002042 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002043
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002044 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002045 /* Get next HBQ entry slot to use */
2046 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2047 if (hbqe) {
2048 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2049
James Smart92d7f7b2007-06-17 19:56:38 -05002050 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2051 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002052 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002053 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002054 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2055 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2056 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002057 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2058 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002059 /* flush */
James Smarted957682007-06-17 19:56:37 -05002060 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002061 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002062 return 0;
2063 } else
2064 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002065}
2066
James Smart4f774512009-05-22 14:52:35 -04002067/**
2068 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2069 * @phba: Pointer to HBA context object.
2070 * @hbqno: HBQ number.
2071 * @hbq_buf: Pointer to HBQ buffer.
2072 *
2073 * This function is called with the hbalock held to post an RQE to the SLI4
2074 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2075 * the hbq_buffer_list and return zero, otherwise it will return an error.
2076 **/
2077static int
2078lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2079 struct hbq_dmabuf *hbq_buf)
2080{
2081 int rc;
2082 struct lpfc_rqe hrqe;
2083 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002084 struct lpfc_queue *hrq;
2085 struct lpfc_queue *drq;
2086
2087 if (hbqno != LPFC_ELS_HBQ)
2088 return 1;
2089 hrq = phba->sli4_hba.hdr_rq;
2090 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002091
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002092 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002093 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2094 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2095 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2096 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002097 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002098 if (rc < 0)
2099 return rc;
James Smart895427b2017-02-12 13:52:30 -08002100 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002101 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2102 return 0;
2103}
2104
James Smarte59058c2008-08-24 21:49:00 -04002105/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002106static struct lpfc_hbq_init lpfc_els_hbq = {
2107 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002108 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002109 .mask_count = 0,
2110 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002111 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002112 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002113 .init_count = 40,
2114 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002115};
James Smarted957682007-06-17 19:56:37 -05002116
James Smarte59058c2008-08-24 21:49:00 -04002117/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002118struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002119 &lpfc_els_hbq,
2120};
2121
James Smarte59058c2008-08-24 21:49:00 -04002122/**
James Smart3621a712009-04-06 18:47:14 -04002123 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002124 * @phba: Pointer to HBA context object.
2125 * @hbqno: HBQ number.
2126 * @count: Number of HBQ buffers to be posted.
2127 *
James Smartd7c255b2008-08-24 21:50:00 -04002128 * This function is called with no lock held to post more hbq buffers to the
2129 * given HBQ. The function returns the number of HBQ buffers successfully
2130 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002131 **/
James Smart311464e2007-08-02 11:10:37 -04002132static int
James Smart92d7f7b2007-06-17 19:56:38 -05002133lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2134{
James Smartd7c255b2008-08-24 21:50:00 -04002135 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002136 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002137 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002138 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002139 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002140 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002141
James Smartd7c255b2008-08-24 21:50:00 -04002142 if ((phba->hbqs[hbqno].buffer_count + count) >
2143 lpfc_hbq_defs[hbqno]->entry_count)
2144 count = lpfc_hbq_defs[hbqno]->entry_count -
2145 phba->hbqs[hbqno].buffer_count;
2146 if (!count)
2147 return 0;
2148 /* Allocate HBQ entries */
2149 for (i = 0; i < count; i++) {
2150 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2151 if (!hbq_buffer)
2152 break;
2153 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2154 }
James Smart3163f722008-02-08 18:50:25 -05002155 /* Check whether HBQ is still in use */
2156 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002157 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002158 goto err;
2159 while (!list_empty(&hbq_buf_list)) {
2160 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2161 dbuf.list);
2162 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2163 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002164 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002165 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002166 posted++;
2167 } else
James Smart51ef4c22007-08-02 11:10:31 -04002168 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002169 }
James Smart3163f722008-02-08 18:50:25 -05002170 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002171 return posted;
2172err:
2173 spin_unlock_irqrestore(&phba->hbalock, flags);
2174 while (!list_empty(&hbq_buf_list)) {
2175 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2176 dbuf.list);
2177 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2178 }
James Smart92d7f7b2007-06-17 19:56:38 -05002179 return 0;
James Smarted957682007-06-17 19:56:37 -05002180}
2181
James Smarte59058c2008-08-24 21:49:00 -04002182/**
James Smart3621a712009-04-06 18:47:14 -04002183 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002184 * @phba: Pointer to HBA context object.
2185 * @qno: HBQ number.
2186 *
2187 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002188 * is called with no lock held. The function returns the number of HBQ entries
2189 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002190 **/
James Smarted957682007-06-17 19:56:37 -05002191int
James Smart92d7f7b2007-06-17 19:56:38 -05002192lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002193{
James Smartdef9c7a2009-12-21 17:02:28 -05002194 if (phba->sli_rev == LPFC_SLI_REV4)
2195 return 0;
2196 else
2197 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2198 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002199}
2200
James Smarte59058c2008-08-24 21:49:00 -04002201/**
James Smart3621a712009-04-06 18:47:14 -04002202 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002203 * @phba: Pointer to HBA context object.
2204 * @qno: HBQ queue number.
2205 *
2206 * This function is called from SLI initialization code path with
2207 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002208 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002209 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002210static int
James Smart92d7f7b2007-06-17 19:56:38 -05002211lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002212{
James Smartdef9c7a2009-12-21 17:02:28 -05002213 if (phba->sli_rev == LPFC_SLI_REV4)
2214 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002215 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002216 else
2217 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2218 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002219}
2220
James Smarte59058c2008-08-24 21:49:00 -04002221/**
James Smart3772a992009-05-22 14:50:54 -04002222 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2223 * @phba: Pointer to HBA context object.
2224 * @hbqno: HBQ number.
2225 *
2226 * This function removes the first hbq buffer on an hbq list and returns a
2227 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2228 **/
2229static struct hbq_dmabuf *
2230lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2231{
2232 struct lpfc_dmabuf *d_buf;
2233
2234 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2235 if (!d_buf)
2236 return NULL;
2237 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2238}
2239
2240/**
James Smart2d7dbc42017-02-12 13:52:35 -08002241 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2242 * @phba: Pointer to HBA context object.
2243 * @hbqno: HBQ number.
2244 *
2245 * This function removes the first RQ buffer on an RQ buffer list and returns a
2246 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2247 **/
2248static struct rqb_dmabuf *
2249lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2250{
2251 struct lpfc_dmabuf *h_buf;
2252 struct lpfc_rqb *rqbp;
2253
2254 rqbp = hrq->rqbp;
2255 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2256 struct lpfc_dmabuf, list);
2257 if (!h_buf)
2258 return NULL;
2259 rqbp->buffer_count--;
2260 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2261}
2262
2263/**
James Smart3621a712009-04-06 18:47:14 -04002264 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002265 * @phba: Pointer to HBA context object.
2266 * @tag: Tag of the hbq buffer.
2267 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002268 * This function searches for the hbq buffer associated with the given tag in
2269 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2270 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002271 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002272static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002273lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2274{
James Smart92d7f7b2007-06-17 19:56:38 -05002275 struct lpfc_dmabuf *d_buf;
2276 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002277 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002278
James Smart51ef4c22007-08-02 11:10:31 -04002279 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002280 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002281 return NULL;
2282
James Smart3772a992009-05-22 14:50:54 -04002283 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002284 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002285 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002286 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002287 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002288 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002289 }
2290 }
James Smart3772a992009-05-22 14:50:54 -04002291 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002292 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002293 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002294 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002295 return NULL;
James Smarted957682007-06-17 19:56:37 -05002296}
2297
James Smarte59058c2008-08-24 21:49:00 -04002298/**
James Smart3621a712009-04-06 18:47:14 -04002299 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002300 * @phba: Pointer to HBA context object.
2301 * @hbq_buffer: Pointer to HBQ buffer.
2302 *
2303 * This function is called with hbalock. This function gives back
2304 * the hbq buffer to firmware. If the HBQ does not have space to
2305 * post the buffer, it will free the buffer.
2306 **/
James Smarted957682007-06-17 19:56:37 -05002307void
James Smart51ef4c22007-08-02 11:10:31 -04002308lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002309{
2310 uint32_t hbqno;
2311
James Smart51ef4c22007-08-02 11:10:31 -04002312 if (hbq_buffer) {
2313 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002314 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002315 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002316 }
2317}
2318
James Smarte59058c2008-08-24 21:49:00 -04002319/**
James Smart3621a712009-04-06 18:47:14 -04002320 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002321 * @mbxCommand: mailbox command code.
2322 *
2323 * This function is called by the mailbox event handler function to verify
2324 * that the completed mailbox command is a legitimate mailbox command. If the
2325 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2326 * and the mailbox event handler will take the HBA offline.
2327 **/
dea31012005-04-17 16:05:31 -05002328static int
2329lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2330{
2331 uint8_t ret;
2332
2333 switch (mbxCommand) {
2334 case MBX_LOAD_SM:
2335 case MBX_READ_NV:
2336 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002337 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002338 case MBX_RUN_BIU_DIAG:
2339 case MBX_INIT_LINK:
2340 case MBX_DOWN_LINK:
2341 case MBX_CONFIG_LINK:
2342 case MBX_CONFIG_RING:
2343 case MBX_RESET_RING:
2344 case MBX_READ_CONFIG:
2345 case MBX_READ_RCONFIG:
2346 case MBX_READ_SPARM:
2347 case MBX_READ_STATUS:
2348 case MBX_READ_RPI:
2349 case MBX_READ_XRI:
2350 case MBX_READ_REV:
2351 case MBX_READ_LNK_STAT:
2352 case MBX_REG_LOGIN:
2353 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002354 case MBX_CLEAR_LA:
2355 case MBX_DUMP_MEMORY:
2356 case MBX_DUMP_CONTEXT:
2357 case MBX_RUN_DIAGS:
2358 case MBX_RESTART:
2359 case MBX_UPDATE_CFG:
2360 case MBX_DOWN_LOAD:
2361 case MBX_DEL_LD_ENTRY:
2362 case MBX_RUN_PROGRAM:
2363 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002364 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002365 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002366 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002367 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002368 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002369 case MBX_LOAD_AREA:
2370 case MBX_RUN_BIU_DIAG64:
2371 case MBX_CONFIG_PORT:
2372 case MBX_READ_SPARM64:
2373 case MBX_READ_RPI64:
2374 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002375 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002376 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002377 case MBX_SET_DEBUG:
2378 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002379 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002380 case MBX_REG_VPI:
2381 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002382 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002383 case MBX_PORT_CAPABILITIES:
2384 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002385 case MBX_SLI4_CONFIG:
2386 case MBX_SLI4_REQ_FTRS:
2387 case MBX_REG_FCFI:
2388 case MBX_UNREG_FCFI:
2389 case MBX_REG_VFI:
2390 case MBX_UNREG_VFI:
2391 case MBX_INIT_VPI:
2392 case MBX_INIT_VFI:
2393 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002394 case MBX_READ_EVENT_LOG_STATUS:
2395 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002396 case MBX_SECURITY_MGMT:
2397 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002398 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002399 ret = mbxCommand;
2400 break;
2401 default:
2402 ret = MBX_SHUTDOWN;
2403 break;
2404 }
James Smart2e0fef82007-06-17 19:56:36 -05002405 return ret;
dea31012005-04-17 16:05:31 -05002406}
James Smarte59058c2008-08-24 21:49:00 -04002407
2408/**
James Smart3621a712009-04-06 18:47:14 -04002409 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002410 * @phba: Pointer to HBA context object.
2411 * @pmboxq: Pointer to mailbox command.
2412 *
2413 * This is completion handler function for mailbox commands issued from
2414 * lpfc_sli_issue_mbox_wait function. This function is called by the
2415 * mailbox event handler function with no lock held. This function
2416 * will wake up thread waiting on the wait queue pointed by context1
2417 * of the mailbox.
2418 **/
James Smart04c68492009-05-22 14:52:52 -04002419void
James Smart2e0fef82007-06-17 19:56:36 -05002420lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002421{
James Smart858c9f62007-06-17 19:56:39 -05002422 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002423 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002424
2425 /*
James Smarte29d74f2018-03-05 12:04:07 -08002426 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002427 * continued running.
2428 */
James Smart7054a602007-04-25 09:52:34 -04002429 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002430 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002431 pmbox_done = (struct completion *)pmboxq->context3;
2432 if (pmbox_done)
2433 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002434 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002435 return;
2436}
2437
James Smarte59058c2008-08-24 21:49:00 -04002438
2439/**
James Smart3621a712009-04-06 18:47:14 -04002440 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002441 * @phba: Pointer to HBA context object.
2442 * @pmb: Pointer to mailbox object.
2443 *
2444 * This function is the default mailbox completion handler. It
2445 * frees the memory resources associated with the completed mailbox
2446 * command. If the completed command is a REG_LOGIN mailbox command,
2447 * this function will issue a UREG_LOGIN to re-claim the RPI.
2448 **/
dea31012005-04-17 16:05:31 -05002449void
James Smart2e0fef82007-06-17 19:56:36 -05002450lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002451{
James Smartd439d282010-09-29 11:18:45 -04002452 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002453 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002454 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002455 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002456 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002457 int rc;
2458
James Smart3e1f0712018-11-29 16:09:29 -08002459 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smart7054a602007-04-25 09:52:34 -04002460
dea31012005-04-17 16:05:31 -05002461 if (mp) {
2462 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2463 kfree(mp);
2464 }
James Smart7054a602007-04-25 09:52:34 -04002465
2466 /*
2467 * If a REG_LOGIN succeeded after node is destroyed or node
2468 * is in re-discovery driver need to cleanup the RPI.
2469 */
James Smart2e0fef82007-06-17 19:56:36 -05002470 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002471 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2472 !pmb->u.mb.mbxStatus) {
2473 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002474 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002475 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002476 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002477 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002478 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2479 if (rc != MBX_NOT_FINISHED)
2480 return;
2481 }
2482
James Smart695a8142010-01-26 23:08:03 -05002483 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2484 !(phba->pport->load_flag & FC_UNLOADING) &&
2485 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002486 shost = lpfc_shost_from_vport(vport);
2487 spin_lock_irq(shost->host_lock);
2488 vport->vpi_state |= LPFC_VPI_REGISTERED;
2489 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2490 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002491 }
2492
James Smartd439d282010-09-29 11:18:45 -04002493 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -08002494 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -04002495 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002496 pmb->ctx_buf = NULL;
2497 pmb->ctx_ndlp = NULL;
2498 }
2499
2500 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2501 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2502
2503 /* Check to see if there are any deferred events to process */
2504 if (ndlp) {
2505 lpfc_printf_vlog(
2506 vport,
2507 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2508 "1438 UNREG cmpl deferred mbox x%x "
2509 "on NPort x%x Data: x%x x%x %p\n",
2510 ndlp->nlp_rpi, ndlp->nlp_DID,
2511 ndlp->nlp_flag, ndlp->nlp_defer_did, ndlp);
2512
2513 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2514 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
James Smart00292e02018-12-13 15:17:55 -08002515 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002516 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2517 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002518 } else {
2519 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002520 }
James Smartdea16bd2018-11-29 16:09:30 -08002521 }
James Smart3e1f0712018-11-29 16:09:29 -08002522 pmb->ctx_ndlp = NULL;
James Smartd439d282010-09-29 11:18:45 -04002523 }
2524
James Smartdcf2a4e2010-09-29 11:18:53 -04002525 /* Check security permission status on INIT_LINK mailbox command */
2526 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2527 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2528 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2529 "2860 SLI authentication is required "
2530 "for INIT_LINK but has not done yet\n");
2531
James Smart04c68492009-05-22 14:52:52 -04002532 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2533 lpfc_sli4_mbox_cmd_free(phba, pmb);
2534 else
2535 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002536}
James Smartbe6bb942015-04-07 15:07:22 -04002537 /**
2538 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2539 * @phba: Pointer to HBA context object.
2540 * @pmb: Pointer to mailbox object.
2541 *
2542 * This function is the unreg rpi mailbox completion handler. It
2543 * frees the memory resources associated with the completed mailbox
2544 * command. An additional refrenece is put on the ndlp to prevent
2545 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2546 * the unreg mailbox command completes, this routine puts the
2547 * reference back.
2548 *
2549 **/
2550void
2551lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2552{
2553 struct lpfc_vport *vport = pmb->vport;
2554 struct lpfc_nodelist *ndlp;
2555
James Smart3e1f0712018-11-29 16:09:29 -08002556 ndlp = pmb->ctx_ndlp;
James Smartbe6bb942015-04-07 15:07:22 -04002557 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2558 if (phba->sli_rev == LPFC_SLI_REV4 &&
2559 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002560 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002561 LPFC_SLI_INTF_IF_TYPE_2)) {
2562 if (ndlp) {
James Smartdea16bd2018-11-29 16:09:30 -08002563 lpfc_printf_vlog(
2564 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2565 "0010 UNREG_LOGIN vpi:%x "
2566 "rpi:%x DID:%x defer x%x flg x%x "
2567 "map:%x %p\n",
2568 vport->vpi, ndlp->nlp_rpi,
2569 ndlp->nlp_DID, ndlp->nlp_defer_did,
2570 ndlp->nlp_flag,
2571 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002572 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002573 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002574
2575 /* Check to see if there are any deferred
2576 * events to process
2577 */
2578 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2579 (ndlp->nlp_defer_did !=
2580 NLP_EVT_NOTHING_PENDING)) {
2581 lpfc_printf_vlog(
2582 vport, KERN_INFO, LOG_DISCOVERY,
2583 "4111 UNREG cmpl deferred "
2584 "clr x%x on "
2585 "NPort x%x Data: x%x %p\n",
2586 ndlp->nlp_rpi, ndlp->nlp_DID,
2587 ndlp->nlp_defer_did, ndlp);
James Smart00292e02018-12-13 15:17:55 -08002588 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002589 ndlp->nlp_defer_did =
2590 NLP_EVT_NOTHING_PENDING;
2591 lpfc_issue_els_plogi(
2592 vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002593 } else {
2594 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002595 }
James Smartbe6bb942015-04-07 15:07:22 -04002596 }
2597 }
2598 }
2599
2600 mempool_free(pmb, phba->mbox_mem_pool);
2601}
dea31012005-04-17 16:05:31 -05002602
James Smarte59058c2008-08-24 21:49:00 -04002603/**
James Smart3621a712009-04-06 18:47:14 -04002604 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002605 * @phba: Pointer to HBA context object.
2606 *
2607 * This function is called with no lock held. This function processes all
2608 * the completed mailbox commands and gives it to upper layers. The interrupt
2609 * service routine processes mailbox completion interrupt and adds completed
2610 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2611 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2612 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2613 * function returns the mailbox commands to the upper layer by calling the
2614 * completion handler function of each mailbox.
2615 **/
dea31012005-04-17 16:05:31 -05002616int
James Smart2e0fef82007-06-17 19:56:36 -05002617lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002618{
James Smart92d7f7b2007-06-17 19:56:38 -05002619 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002620 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002621 int rc;
2622 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002623
2624 phba->sli.slistat.mbox_event++;
2625
James Smart92d7f7b2007-06-17 19:56:38 -05002626 /* Get all completed mailboxe buffers into the cmplq */
2627 spin_lock_irq(&phba->hbalock);
2628 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2629 spin_unlock_irq(&phba->hbalock);
2630
dea31012005-04-17 16:05:31 -05002631 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002632 do {
2633 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2634 if (pmb == NULL)
2635 break;
2636
James Smart04c68492009-05-22 14:52:52 -04002637 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002638
James Smart858c9f62007-06-17 19:56:39 -05002639 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2640 if (pmb->vport) {
2641 lpfc_debugfs_disc_trc(pmb->vport,
2642 LPFC_DISC_TRC_MBOX_VPORT,
2643 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2644 (uint32_t)pmbox->mbxCommand,
2645 pmbox->un.varWords[0],
2646 pmbox->un.varWords[1]);
2647 }
2648 else {
2649 lpfc_debugfs_disc_trc(phba->pport,
2650 LPFC_DISC_TRC_MBOX,
2651 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2652 (uint32_t)pmbox->mbxCommand,
2653 pmbox->un.varWords[0],
2654 pmbox->un.varWords[1]);
2655 }
2656 }
2657
dea31012005-04-17 16:05:31 -05002658 /*
2659 * It is a fatal error if unknown mbox command completion.
2660 */
2661 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2662 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002663 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002664 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002665 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002666 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002667 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002668 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002669 lpfc_sli_config_mbox_subsys_get(phba,
2670 pmb),
2671 lpfc_sli_config_mbox_opcode_get(phba,
2672 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002673 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002674 phba->work_hs = HS_FFER3;
2675 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002676 continue;
dea31012005-04-17 16:05:31 -05002677 }
2678
dea31012005-04-17 16:05:31 -05002679 if (pmbox->mbxStatus) {
2680 phba->sli.slistat.mbox_stat_err++;
2681 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2682 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002683 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002684 LOG_MBOX | LOG_SLI,
2685 "(%d):0305 Mbox cmd cmpl "
2686 "error - RETRYing Data: x%x "
2687 "(x%x/x%x) x%x x%x x%x\n",
2688 pmb->vport ? pmb->vport->vpi : 0,
2689 pmbox->mbxCommand,
2690 lpfc_sli_config_mbox_subsys_get(phba,
2691 pmb),
2692 lpfc_sli_config_mbox_opcode_get(phba,
2693 pmb),
2694 pmbox->mbxStatus,
2695 pmbox->un.varWords[0],
2696 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002697 pmbox->mbxStatus = 0;
2698 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002699 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002700 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002701 continue;
dea31012005-04-17 16:05:31 -05002702 }
2703 }
2704
2705 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002706 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002707 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002708 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2709 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002710 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002711 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002712 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2713 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002714 pmb->mbox_cmpl,
2715 *((uint32_t *) pmbox),
2716 pmbox->un.varWords[0],
2717 pmbox->un.varWords[1],
2718 pmbox->un.varWords[2],
2719 pmbox->un.varWords[3],
2720 pmbox->un.varWords[4],
2721 pmbox->un.varWords[5],
2722 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002723 pmbox->un.varWords[7],
2724 pmbox->un.varWords[8],
2725 pmbox->un.varWords[9],
2726 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002727
James Smart92d7f7b2007-06-17 19:56:38 -05002728 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002729 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002730 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002731 return 0;
dea31012005-04-17 16:05:31 -05002732}
James Smart92d7f7b2007-06-17 19:56:38 -05002733
James Smarte59058c2008-08-24 21:49:00 -04002734/**
James Smart3621a712009-04-06 18:47:14 -04002735 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002736 * @phba: Pointer to HBA context object.
2737 * @pring: Pointer to driver SLI ring object.
2738 * @tag: buffer tag.
2739 *
2740 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2741 * is set in the tag the buffer is posted for a particular exchange,
2742 * the function will return the buffer without replacing the buffer.
2743 * If the buffer is for unsolicited ELS or CT traffic, this function
2744 * returns the buffer and also posts another buffer to the firmware.
2745 **/
James Smart76bb24e2007-10-27 13:38:00 -04002746static struct lpfc_dmabuf *
2747lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002748 struct lpfc_sli_ring *pring,
2749 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002750{
James Smart9f1e1b52008-12-04 22:39:40 -05002751 struct hbq_dmabuf *hbq_entry;
2752
James Smart76bb24e2007-10-27 13:38:00 -04002753 if (tag & QUE_BUFTAG_BIT)
2754 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002755 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2756 if (!hbq_entry)
2757 return NULL;
2758 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002759}
James Smart57127f12007-10-27 13:37:05 -04002760
James Smart3772a992009-05-22 14:50:54 -04002761/**
2762 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2763 * @phba: Pointer to HBA context object.
2764 * @pring: Pointer to driver SLI ring object.
2765 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2766 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2767 * @fch_type: the type for the first frame of the sequence.
2768 *
2769 * This function is called with no lock held. This function uses the r_ctl and
2770 * type of the received sequence to find the correct callback function to call
2771 * to process the sequence.
2772 **/
2773static int
2774lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2775 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2776 uint32_t fch_type)
2777{
2778 int i;
2779
James Smartf358dd02017-02-12 13:52:34 -08002780 switch (fch_type) {
2781 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002782 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002783 return 1;
2784 default:
2785 break;
2786 }
2787
James Smart3772a992009-05-22 14:50:54 -04002788 /* unSolicited Responses */
2789 if (pring->prt[0].profile) {
2790 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2791 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2792 saveq);
2793 return 1;
2794 }
2795 /* We must search, based on rctl / type
2796 for the right routine */
2797 for (i = 0; i < pring->num_mask; i++) {
2798 if ((pring->prt[i].rctl == fch_r_ctl) &&
2799 (pring->prt[i].type == fch_type)) {
2800 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2801 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2802 (phba, pring, saveq);
2803 return 1;
2804 }
2805 }
2806 return 0;
2807}
James Smarte59058c2008-08-24 21:49:00 -04002808
2809/**
James Smart3621a712009-04-06 18:47:14 -04002810 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002811 * @phba: Pointer to HBA context object.
2812 * @pring: Pointer to driver SLI ring object.
2813 * @saveq: Pointer to the unsolicited iocb.
2814 *
2815 * This function is called with no lock held by the ring event handler
2816 * when there is an unsolicited iocb posted to the response ring by the
2817 * firmware. This function gets the buffer associated with the iocbs
2818 * and calls the event handler for the ring. This function handles both
2819 * qring buffers and hbq buffers.
2820 * When the function returns 1 the caller can free the iocb object otherwise
2821 * upper layer functions will free the iocb objects.
2822 **/
dea31012005-04-17 16:05:31 -05002823static int
2824lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2825 struct lpfc_iocbq *saveq)
2826{
2827 IOCB_t * irsp;
2828 WORD5 * w5p;
2829 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002830 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002831 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002832
dea31012005-04-17 16:05:31 -05002833 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002834
2835 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2836 if (pring->lpfc_sli_rcv_async_status)
2837 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2838 else
2839 lpfc_printf_log(phba,
2840 KERN_WARNING,
2841 LOG_SLI,
2842 "0316 Ring %d handler: unexpected "
2843 "ASYNC_STATUS iocb received evt_code "
2844 "0x%x\n",
2845 pring->ringno,
2846 irsp->un.asyncstat.evt_code);
2847 return 1;
2848 }
2849
James Smart3163f722008-02-08 18:50:25 -05002850 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2851 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2852 if (irsp->ulpBdeCount > 0) {
2853 dmzbuf = lpfc_sli_get_buff(phba, pring,
2854 irsp->un.ulpWord[3]);
2855 lpfc_in_buf_free(phba, dmzbuf);
2856 }
2857
2858 if (irsp->ulpBdeCount > 1) {
2859 dmzbuf = lpfc_sli_get_buff(phba, pring,
2860 irsp->unsli3.sli3Words[3]);
2861 lpfc_in_buf_free(phba, dmzbuf);
2862 }
2863
2864 if (irsp->ulpBdeCount > 2) {
2865 dmzbuf = lpfc_sli_get_buff(phba, pring,
2866 irsp->unsli3.sli3Words[7]);
2867 lpfc_in_buf_free(phba, dmzbuf);
2868 }
2869
2870 return 1;
2871 }
2872
James Smart92d7f7b2007-06-17 19:56:38 -05002873 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002874 if (irsp->ulpBdeCount != 0) {
2875 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002876 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002877 if (!saveq->context2)
2878 lpfc_printf_log(phba,
2879 KERN_ERR,
2880 LOG_SLI,
2881 "0341 Ring %d Cannot find buffer for "
2882 "an unsolicited iocb. tag 0x%x\n",
2883 pring->ringno,
2884 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002885 }
2886 if (irsp->ulpBdeCount == 2) {
2887 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002888 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002889 if (!saveq->context3)
2890 lpfc_printf_log(phba,
2891 KERN_ERR,
2892 LOG_SLI,
2893 "0342 Ring %d Cannot find buffer for an"
2894 " unsolicited iocb. tag 0x%x\n",
2895 pring->ringno,
2896 irsp->unsli3.sli3Words[7]);
2897 }
2898 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002899 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002900 if (irsp->ulpBdeCount != 0) {
2901 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2902 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002903 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002904 lpfc_printf_log(phba,
2905 KERN_ERR,
2906 LOG_SLI,
2907 "0343 Ring %d Cannot find "
2908 "buffer for an unsolicited iocb"
2909 ". tag 0x%x\n", pring->ringno,
2910 irsp->un.ulpWord[3]);
2911 }
2912 if (irsp->ulpBdeCount == 2) {
2913 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2914 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002915 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002916 lpfc_printf_log(phba,
2917 KERN_ERR,
2918 LOG_SLI,
2919 "0344 Ring %d Cannot find "
2920 "buffer for an unsolicited "
2921 "iocb. tag 0x%x\n",
2922 pring->ringno,
2923 irsp->unsli3.sli3Words[7]);
2924 }
2925 }
James Smart92d7f7b2007-06-17 19:56:38 -05002926 }
James Smart9c2face2008-01-11 01:53:18 -05002927 if (irsp->ulpBdeCount != 0 &&
2928 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2929 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2930 int found = 0;
2931
2932 /* search continue save q for same XRI */
2933 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002934 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2935 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002936 list_add_tail(&saveq->list, &iocbq->list);
2937 found = 1;
2938 break;
2939 }
2940 }
2941 if (!found)
2942 list_add_tail(&saveq->clist,
2943 &pring->iocb_continue_saveq);
2944 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2945 list_del_init(&iocbq->clist);
2946 saveq = iocbq;
2947 irsp = &(saveq->iocb);
2948 } else
2949 return 0;
2950 }
2951 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2952 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2953 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002954 Rctl = FC_RCTL_ELS_REQ;
2955 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002956 } else {
2957 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2958 Rctl = w5p->hcsw.Rctl;
2959 Type = w5p->hcsw.Type;
2960
2961 /* Firmware Workaround */
2962 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2963 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2964 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002965 Rctl = FC_RCTL_ELS_REQ;
2966 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002967 w5p->hcsw.Rctl = Rctl;
2968 w5p->hcsw.Type = Type;
2969 }
2970 }
James Smart92d7f7b2007-06-17 19:56:38 -05002971
James Smart3772a992009-05-22 14:50:54 -04002972 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002973 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002974 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002975 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002976 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002977
James Smart92d7f7b2007-06-17 19:56:38 -05002978 return 1;
dea31012005-04-17 16:05:31 -05002979}
2980
James Smarte59058c2008-08-24 21:49:00 -04002981/**
James Smart3621a712009-04-06 18:47:14 -04002982 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002983 * @phba: Pointer to HBA context object.
2984 * @pring: Pointer to driver SLI ring object.
2985 * @prspiocb: Pointer to response iocb object.
2986 *
2987 * This function looks up the iocb_lookup table to get the command iocb
2988 * corresponding to the given response iocb using the iotag of the
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002989 * response iocb. This function is called with the hbalock held
2990 * for sli3 devices or the ring_lock for sli4 devices.
James Smarte59058c2008-08-24 21:49:00 -04002991 * This function returns the command iocb object if it finds the command
2992 * iocb else returns NULL.
2993 **/
dea31012005-04-17 16:05:31 -05002994static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002995lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2996 struct lpfc_sli_ring *pring,
2997 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002998{
dea31012005-04-17 16:05:31 -05002999 struct lpfc_iocbq *cmd_iocb = NULL;
3000 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003001 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003002
James Bottomley604a3e32005-10-29 10:28:33 -05003003 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05003004
James Bottomley604a3e32005-10-29 10:28:33 -05003005 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3006 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003007 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07003008 /* remove from txcmpl queue list */
3009 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04003010 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07003011 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003012 }
dea31012005-04-17 16:05:31 -05003013 }
3014
dea31012005-04-17 16:05:31 -05003015 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07003016 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05003017 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003018 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05003019 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05003020 return NULL;
3021}
3022
James Smarte59058c2008-08-24 21:49:00 -04003023/**
James Smart3772a992009-05-22 14:50:54 -04003024 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3025 * @phba: Pointer to HBA context object.
3026 * @pring: Pointer to driver SLI ring object.
3027 * @iotag: IOCB tag.
3028 *
3029 * This function looks up the iocb_lookup table to get the command iocb
3030 * corresponding to the given iotag. This function is called with the
3031 * hbalock held.
3032 * This function returns the command iocb object if it finds the command
3033 * iocb else returns NULL.
3034 **/
3035static struct lpfc_iocbq *
3036lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3037 struct lpfc_sli_ring *pring, uint16_t iotag)
3038{
James Smart895427b2017-02-12 13:52:30 -08003039 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04003040
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003041 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04003042 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3043 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003044 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3045 /* remove from txcmpl queue list */
3046 list_del_init(&cmd_iocb->list);
3047 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04003048 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003049 }
James Smart3772a992009-05-22 14:50:54 -04003050 }
James Smart89533e92016-10-13 15:06:15 -07003051
James Smart3772a992009-05-22 14:50:54 -04003052 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08003053 "0372 iotag x%x lookup error: max iotag (x%x) "
3054 "iocb_flag x%x\n",
3055 iotag, phba->sli.last_iotag,
3056 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003057 return NULL;
3058}
3059
3060/**
James Smart3621a712009-04-06 18:47:14 -04003061 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003062 * @phba: Pointer to HBA context object.
3063 * @pring: Pointer to driver SLI ring object.
3064 * @saveq: Pointer to the response iocb to be processed.
3065 *
3066 * This function is called by the ring event handler for non-fcp
3067 * rings when there is a new response iocb in the response ring.
3068 * The caller is not required to hold any locks. This function
3069 * gets the command iocb associated with the response iocb and
3070 * calls the completion handler for the command iocb. If there
3071 * is no completion handler, the function will free the resources
3072 * associated with command iocb. If the response iocb is for
3073 * an already aborted command iocb, the status of the completion
3074 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3075 * This function always returns 1.
3076 **/
dea31012005-04-17 16:05:31 -05003077static int
James Smart2e0fef82007-06-17 19:56:36 -05003078lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003079 struct lpfc_iocbq *saveq)
3080{
James Smart2e0fef82007-06-17 19:56:36 -05003081 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003082 int rc = 1;
3083 unsigned long iflag;
3084
3085 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003086 if (phba->sli_rev == LPFC_SLI_REV4)
3087 spin_lock_irqsave(&pring->ring_lock, iflag);
3088 else
3089 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003090 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003091 if (phba->sli_rev == LPFC_SLI_REV4)
3092 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3093 else
3094 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart2e0fef82007-06-17 19:56:36 -05003095
dea31012005-04-17 16:05:31 -05003096 if (cmdiocbp) {
3097 if (cmdiocbp->iocb_cmpl) {
3098 /*
James Smartea2151b2008-09-07 11:52:10 -04003099 * If an ELS command failed send an event to mgmt
3100 * application.
3101 */
3102 if (saveq->iocb.ulpStatus &&
3103 (pring->ringno == LPFC_ELS_RING) &&
3104 (cmdiocbp->iocb.ulpCommand ==
3105 CMD_ELS_REQUEST64_CR))
3106 lpfc_send_els_failure_event(phba,
3107 cmdiocbp, saveq);
3108
3109 /*
dea31012005-04-17 16:05:31 -05003110 * Post all ELS completions to the worker thread.
3111 * All other are passed to the completion callback.
3112 */
3113 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003114 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3115 (cmdiocbp->iocb_flag &
3116 LPFC_DRIVER_ABORTED)) {
3117 spin_lock_irqsave(&phba->hbalock,
3118 iflag);
James Smart07951072007-04-25 09:51:38 -04003119 cmdiocbp->iocb_flag &=
3120 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003121 spin_unlock_irqrestore(&phba->hbalock,
3122 iflag);
James Smart07951072007-04-25 09:51:38 -04003123 saveq->iocb.ulpStatus =
3124 IOSTAT_LOCAL_REJECT;
3125 saveq->iocb.un.ulpWord[4] =
3126 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003127
3128 /* Firmware could still be in progress
3129 * of DMAing payload, so don't free data
3130 * buffer till after a hbeat.
3131 */
James Smart341af102010-01-26 23:07:37 -05003132 spin_lock_irqsave(&phba->hbalock,
3133 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003134 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003135 spin_unlock_irqrestore(&phba->hbalock,
3136 iflag);
3137 }
James Smart0f65ff62010-02-26 14:14:23 -05003138 if (phba->sli_rev == LPFC_SLI_REV4) {
3139 if (saveq->iocb_flag &
3140 LPFC_EXCHANGE_BUSY) {
3141 /* Set cmdiocb flag for the
3142 * exchange busy so sgl (xri)
3143 * will not be released until
3144 * the abort xri is received
3145 * from hba.
3146 */
3147 spin_lock_irqsave(
3148 &phba->hbalock, iflag);
3149 cmdiocbp->iocb_flag |=
3150 LPFC_EXCHANGE_BUSY;
3151 spin_unlock_irqrestore(
3152 &phba->hbalock, iflag);
3153 }
3154 if (cmdiocbp->iocb_flag &
3155 LPFC_DRIVER_ABORTED) {
3156 /*
3157 * Clear LPFC_DRIVER_ABORTED
3158 * bit in case it was driver
3159 * initiated abort.
3160 */
3161 spin_lock_irqsave(
3162 &phba->hbalock, iflag);
3163 cmdiocbp->iocb_flag &=
3164 ~LPFC_DRIVER_ABORTED;
3165 spin_unlock_irqrestore(
3166 &phba->hbalock, iflag);
3167 cmdiocbp->iocb.ulpStatus =
3168 IOSTAT_LOCAL_REJECT;
3169 cmdiocbp->iocb.un.ulpWord[4] =
3170 IOERR_ABORT_REQUESTED;
3171 /*
3172 * For SLI4, irsiocb contains
3173 * NO_XRI in sli_xritag, it
3174 * shall not affect releasing
3175 * sgl (xri) process.
3176 */
3177 saveq->iocb.ulpStatus =
3178 IOSTAT_LOCAL_REJECT;
3179 saveq->iocb.un.ulpWord[4] =
3180 IOERR_SLI_ABORTED;
3181 spin_lock_irqsave(
3182 &phba->hbalock, iflag);
3183 saveq->iocb_flag |=
3184 LPFC_DELAY_MEM_FREE;
3185 spin_unlock_irqrestore(
3186 &phba->hbalock, iflag);
3187 }
James Smart07951072007-04-25 09:51:38 -04003188 }
dea31012005-04-17 16:05:31 -05003189 }
James Smart2e0fef82007-06-17 19:56:36 -05003190 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003191 } else
3192 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003193 } else {
3194 /*
3195 * Unknown initiating command based on the response iotag.
3196 * This could be the case on the ELS ring because of
3197 * lpfc_els_abort().
3198 */
3199 if (pring->ringno != LPFC_ELS_RING) {
3200 /*
3201 * Ring <ringno> handler: unexpected completion IoTag
3202 * <IoTag>
3203 */
James Smarta257bf92009-04-06 18:48:10 -04003204 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003205 "0322 Ring %d handler: "
3206 "unexpected completion IoTag x%x "
3207 "Data: x%x x%x x%x x%x\n",
3208 pring->ringno,
3209 saveq->iocb.ulpIoTag,
3210 saveq->iocb.ulpStatus,
3211 saveq->iocb.un.ulpWord[4],
3212 saveq->iocb.ulpCommand,
3213 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003214 }
3215 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003216
dea31012005-04-17 16:05:31 -05003217 return rc;
3218}
3219
James Smarte59058c2008-08-24 21:49:00 -04003220/**
James Smart3621a712009-04-06 18:47:14 -04003221 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003222 * @phba: Pointer to HBA context object.
3223 * @pring: Pointer to driver SLI ring object.
3224 *
3225 * This function is called from the iocb ring event handlers when
3226 * put pointer is ahead of the get pointer for a ring. This function signal
3227 * an error attention condition to the worker thread and the worker
3228 * thread will transition the HBA to offline state.
3229 **/
James Smart2e0fef82007-06-17 19:56:36 -05003230static void
3231lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003232{
James Smart34b02dc2008-08-24 21:49:55 -04003233 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003234 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003235 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003236 * rsp ring <portRspMax>
3237 */
3238 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003239 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003240 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003241 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003242 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003243
James Smart2e0fef82007-06-17 19:56:36 -05003244 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003245
3246 /*
3247 * All error attention handlers are posted to
3248 * worker thread
3249 */
3250 phba->work_ha |= HA_ERATT;
3251 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003252
James Smart5e9d9b82008-06-14 22:52:53 -04003253 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003254
3255 return;
3256}
3257
James Smarte59058c2008-08-24 21:49:00 -04003258/**
James Smart3621a712009-04-06 18:47:14 -04003259 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003260 * @ptr: Pointer to address of HBA context object.
3261 *
3262 * This function is invoked by the Error Attention polling timer when the
3263 * timer times out. It will check the SLI Error Attention register for
3264 * possible attention events. If so, it will post an Error Attention event
3265 * and wake up worker thread to process it. Otherwise, it will set up the
3266 * Error Attention polling timer for the next poll.
3267 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003268void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003269{
3270 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003271 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003272 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003273
Kees Cookf22eb4d2017-09-06 20:24:26 -07003274 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003275
James Smartaa6fbb72012-08-03 12:36:03 -04003276 /* Here we will also keep track of interrupts per sec of the hba */
3277 sli_intr = phba->sli.slistat.sli_intr;
3278
3279 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3280 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3281 sli_intr);
3282 else
3283 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3284
James Smart65791f12016-07-06 12:35:56 -07003285 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3286 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003287 phba->sli.slistat.sli_ips = cnt;
3288
3289 phba->sli.slistat.sli_prev_intr = sli_intr;
3290
James Smart93996272008-08-24 21:50:30 -04003291 /* Check chip HA register for error event */
3292 eratt = lpfc_sli_check_eratt(phba);
3293
3294 if (eratt)
3295 /* Tell the worker thread there is work to do */
3296 lpfc_worker_wake_up(phba);
3297 else
3298 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003299 mod_timer(&phba->eratt_poll,
3300 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003301 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003302 return;
3303}
3304
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003305
James Smarte59058c2008-08-24 21:49:00 -04003306/**
James Smart3621a712009-04-06 18:47:14 -04003307 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003308 * @phba: Pointer to HBA context object.
3309 * @pring: Pointer to driver SLI ring object.
3310 * @mask: Host attention register mask for this ring.
3311 *
3312 * This function is called from the interrupt context when there is a ring
3313 * event for the fcp ring. The caller does not hold any lock.
3314 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003315 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003316 * LE bit set. The function will call the completion handler of the command iocb
3317 * if the response iocb indicates a completion for a command iocb or it is
3318 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3319 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003320 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003321 * to check it explicitly.
3322 */
3323int
James Smart2e0fef82007-06-17 19:56:36 -05003324lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3325 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003326{
James Smart34b02dc2008-08-24 21:49:55 -04003327 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003328 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003329 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003330 struct lpfc_iocbq *cmdiocbq = NULL;
3331 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003332 uint32_t status;
3333 uint32_t portRspPut, portRspMax;
3334 int rc = 1;
3335 lpfc_iocb_type type;
3336 unsigned long iflag;
3337 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003338
James Smart2e0fef82007-06-17 19:56:36 -05003339 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003340 pring->stats.iocb_event++;
3341
dea31012005-04-17 16:05:31 -05003342 /*
3343 * The next available response entry should never exceed the maximum
3344 * entries. If it does, treat it as an adapter hardware error.
3345 */
James Smart7e56aa22012-08-03 12:35:34 -04003346 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003347 portRspPut = le32_to_cpu(pgp->rspPutInx);
3348 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003349 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003350 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003351 return 1;
3352 }
James Smart45ed1192009-10-02 15:17:02 -04003353 if (phba->fcp_ring_in_use) {
3354 spin_unlock_irqrestore(&phba->hbalock, iflag);
3355 return 1;
3356 } else
3357 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003358
3359 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003360 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003361 /*
3362 * Fetch an entry off the ring and copy it into a local data
3363 * structure. The copy involves a byte-swap since the
3364 * network byte order and pci byte orders are different.
3365 */
James Smarted957682007-06-17 19:56:37 -05003366 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003367 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003368
James Smart7e56aa22012-08-03 12:35:34 -04003369 if (++pring->sli.sli3.rspidx >= portRspMax)
3370 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003371
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003372 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3373 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003374 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003375 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003376 irsp = &rspiocbq.iocb;
3377
dea31012005-04-17 16:05:31 -05003378 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3379 pring->stats.iocb_rsp++;
3380 rsp_cmpl++;
3381
3382 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003383 /*
3384 * If resource errors reported from HBA, reduce
3385 * queuedepths of the SCSI device.
3386 */
3387 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003388 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3389 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003390 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003391 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003392 spin_lock_irqsave(&phba->hbalock, iflag);
3393 }
3394
dea31012005-04-17 16:05:31 -05003395 /* Rsp ring <ringno> error: IOCB */
3396 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003397 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003398 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003399 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003400 irsp->un.ulpWord[0],
3401 irsp->un.ulpWord[1],
3402 irsp->un.ulpWord[2],
3403 irsp->un.ulpWord[3],
3404 irsp->un.ulpWord[4],
3405 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003406 *(uint32_t *)&irsp->un1,
3407 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003408 }
3409
3410 switch (type) {
3411 case LPFC_ABORT_IOCB:
3412 case LPFC_SOL_IOCB:
3413 /*
3414 * Idle exchange closed via ABTS from port. No iocb
3415 * resources need to be recovered.
3416 */
3417 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003418 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003419 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003420 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003421 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003422 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003423 break;
3424 }
3425
James Bottomley604a3e32005-10-29 10:28:33 -05003426 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3427 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003428 if (unlikely(!cmdiocbq))
3429 break;
3430 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3431 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3432 if (cmdiocbq->iocb_cmpl) {
3433 spin_unlock_irqrestore(&phba->hbalock, iflag);
3434 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3435 &rspiocbq);
3436 spin_lock_irqsave(&phba->hbalock, iflag);
3437 }
dea31012005-04-17 16:05:31 -05003438 break;
James Smarta4bc3372006-12-02 13:34:16 -05003439 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003440 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003441 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003442 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003443 break;
dea31012005-04-17 16:05:31 -05003444 default:
3445 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3446 char adaptermsg[LPFC_MAX_ADPTMSG];
3447 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3448 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3449 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003450 dev_warn(&((phba->pcidev)->dev),
3451 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003452 phba->brd_no, adaptermsg);
3453 } else {
3454 /* Unknown IOCB command */
3455 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003456 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003457 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003458 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003459 irsp->ulpStatus,
3460 irsp->ulpIoTag,
3461 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003462 }
3463 break;
3464 }
3465
3466 /*
3467 * The response IOCB has been processed. Update the ring
3468 * pointer in SLIM. If the port response put pointer has not
3469 * been updated, sync the pgp->rspPutInx and fetch the new port
3470 * response put pointer.
3471 */
James Smart7e56aa22012-08-03 12:35:34 -04003472 writel(pring->sli.sli3.rspidx,
3473 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003474
James Smart7e56aa22012-08-03 12:35:34 -04003475 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003476 portRspPut = le32_to_cpu(pgp->rspPutInx);
3477 }
3478
3479 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3480 pring->stats.iocb_rsp_full++;
3481 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3482 writel(status, phba->CAregaddr);
3483 readl(phba->CAregaddr);
3484 }
3485 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3486 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3487 pring->stats.iocb_cmd_empty++;
3488
3489 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003490 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003491 lpfc_sli_resume_iocb(phba, pring);
3492
3493 if ((pring->lpfc_sli_cmd_available))
3494 (pring->lpfc_sli_cmd_available) (phba, pring);
3495
3496 }
3497
James Smart45ed1192009-10-02 15:17:02 -04003498 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003499 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003500 return rc;
3501}
3502
James Smarte59058c2008-08-24 21:49:00 -04003503/**
James Smart3772a992009-05-22 14:50:54 -04003504 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3505 * @phba: Pointer to HBA context object.
3506 * @pring: Pointer to driver SLI ring object.
3507 * @rspiocbp: Pointer to driver response IOCB object.
3508 *
3509 * This function is called from the worker thread when there is a slow-path
3510 * response IOCB to process. This function chains all the response iocbs until
3511 * seeing the iocb with the LE bit set. The function will call
3512 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3513 * completion of a command iocb. The function will call the
3514 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3515 * The function frees the resources or calls the completion handler if this
3516 * iocb is an abort completion. The function returns NULL when the response
3517 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3518 * this function shall chain the iocb on to the iocb_continueq and return the
3519 * response iocb passed in.
3520 **/
3521static struct lpfc_iocbq *
3522lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3523 struct lpfc_iocbq *rspiocbp)
3524{
3525 struct lpfc_iocbq *saveq;
3526 struct lpfc_iocbq *cmdiocbp;
3527 struct lpfc_iocbq *next_iocb;
3528 IOCB_t *irsp = NULL;
3529 uint32_t free_saveq;
3530 uint8_t iocb_cmd_type;
3531 lpfc_iocb_type type;
3532 unsigned long iflag;
3533 int rc;
3534
3535 spin_lock_irqsave(&phba->hbalock, iflag);
3536 /* First add the response iocb to the countinueq list */
3537 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3538 pring->iocb_continueq_cnt++;
3539
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003540 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003541 irsp = &rspiocbp->iocb;
3542 if (irsp->ulpLe) {
3543 /*
3544 * By default, the driver expects to free all resources
3545 * associated with this iocb completion.
3546 */
3547 free_saveq = 1;
3548 saveq = list_get_first(&pring->iocb_continueq,
3549 struct lpfc_iocbq, list);
3550 irsp = &(saveq->iocb);
3551 list_del_init(&pring->iocb_continueq);
3552 pring->iocb_continueq_cnt = 0;
3553
3554 pring->stats.iocb_rsp++;
3555
3556 /*
3557 * If resource errors reported from HBA, reduce
3558 * queuedepths of the SCSI device.
3559 */
3560 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003561 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3562 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003563 spin_unlock_irqrestore(&phba->hbalock, iflag);
3564 phba->lpfc_rampdown_queue_depth(phba);
3565 spin_lock_irqsave(&phba->hbalock, iflag);
3566 }
3567
3568 if (irsp->ulpStatus) {
3569 /* Rsp ring <ringno> error: IOCB */
3570 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3571 "0328 Rsp Ring %d error: "
3572 "IOCB Data: "
3573 "x%x x%x x%x x%x "
3574 "x%x x%x x%x x%x "
3575 "x%x x%x x%x x%x "
3576 "x%x x%x x%x x%x\n",
3577 pring->ringno,
3578 irsp->un.ulpWord[0],
3579 irsp->un.ulpWord[1],
3580 irsp->un.ulpWord[2],
3581 irsp->un.ulpWord[3],
3582 irsp->un.ulpWord[4],
3583 irsp->un.ulpWord[5],
3584 *(((uint32_t *) irsp) + 6),
3585 *(((uint32_t *) irsp) + 7),
3586 *(((uint32_t *) irsp) + 8),
3587 *(((uint32_t *) irsp) + 9),
3588 *(((uint32_t *) irsp) + 10),
3589 *(((uint32_t *) irsp) + 11),
3590 *(((uint32_t *) irsp) + 12),
3591 *(((uint32_t *) irsp) + 13),
3592 *(((uint32_t *) irsp) + 14),
3593 *(((uint32_t *) irsp) + 15));
3594 }
3595
3596 /*
3597 * Fetch the IOCB command type and call the correct completion
3598 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3599 * get freed back to the lpfc_iocb_list by the discovery
3600 * kernel thread.
3601 */
3602 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3603 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3604 switch (type) {
3605 case LPFC_SOL_IOCB:
3606 spin_unlock_irqrestore(&phba->hbalock, iflag);
3607 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3608 spin_lock_irqsave(&phba->hbalock, iflag);
3609 break;
3610
3611 case LPFC_UNSOL_IOCB:
3612 spin_unlock_irqrestore(&phba->hbalock, iflag);
3613 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3614 spin_lock_irqsave(&phba->hbalock, iflag);
3615 if (!rc)
3616 free_saveq = 0;
3617 break;
3618
3619 case LPFC_ABORT_IOCB:
3620 cmdiocbp = NULL;
3621 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3622 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3623 saveq);
3624 if (cmdiocbp) {
3625 /* Call the specified completion routine */
3626 if (cmdiocbp->iocb_cmpl) {
3627 spin_unlock_irqrestore(&phba->hbalock,
3628 iflag);
3629 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3630 saveq);
3631 spin_lock_irqsave(&phba->hbalock,
3632 iflag);
3633 } else
3634 __lpfc_sli_release_iocbq(phba,
3635 cmdiocbp);
3636 }
3637 break;
3638
3639 case LPFC_UNKNOWN_IOCB:
3640 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3641 char adaptermsg[LPFC_MAX_ADPTMSG];
3642 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3643 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3644 MAX_MSG_DATA);
3645 dev_warn(&((phba->pcidev)->dev),
3646 "lpfc%d: %s\n",
3647 phba->brd_no, adaptermsg);
3648 } else {
3649 /* Unknown IOCB command */
3650 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3651 "0335 Unknown IOCB "
3652 "command Data: x%x "
3653 "x%x x%x x%x\n",
3654 irsp->ulpCommand,
3655 irsp->ulpStatus,
3656 irsp->ulpIoTag,
3657 irsp->ulpContext);
3658 }
3659 break;
3660 }
3661
3662 if (free_saveq) {
3663 list_for_each_entry_safe(rspiocbp, next_iocb,
3664 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003665 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003666 __lpfc_sli_release_iocbq(phba, rspiocbp);
3667 }
3668 __lpfc_sli_release_iocbq(phba, saveq);
3669 }
3670 rspiocbp = NULL;
3671 }
3672 spin_unlock_irqrestore(&phba->hbalock, iflag);
3673 return rspiocbp;
3674}
3675
3676/**
3677 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003678 * @phba: Pointer to HBA context object.
3679 * @pring: Pointer to driver SLI ring object.
3680 * @mask: Host attention register mask for this ring.
3681 *
James Smart3772a992009-05-22 14:50:54 -04003682 * This routine wraps the actual slow_ring event process routine from the
3683 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003684 **/
James Smart3772a992009-05-22 14:50:54 -04003685void
James Smart2e0fef82007-06-17 19:56:36 -05003686lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3687 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003688{
James Smart3772a992009-05-22 14:50:54 -04003689 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3690}
3691
3692/**
3693 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3694 * @phba: Pointer to HBA context object.
3695 * @pring: Pointer to driver SLI ring object.
3696 * @mask: Host attention register mask for this ring.
3697 *
3698 * This function is called from the worker thread when there is a ring event
3699 * for non-fcp rings. The caller does not hold any lock. The function will
3700 * remove each response iocb in the response ring and calls the handle
3701 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3702 **/
3703static void
3704lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3705 struct lpfc_sli_ring *pring, uint32_t mask)
3706{
James Smart34b02dc2008-08-24 21:49:55 -04003707 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003708 IOCB_t *entry;
3709 IOCB_t *irsp = NULL;
3710 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003711 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003712 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003713 uint32_t status;
dea31012005-04-17 16:05:31 -05003714
James Smart34b02dc2008-08-24 21:49:55 -04003715 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003716 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003717 pring->stats.iocb_event++;
3718
dea31012005-04-17 16:05:31 -05003719 /*
3720 * The next available response entry should never exceed the maximum
3721 * entries. If it does, treat it as an adapter hardware error.
3722 */
James Smart7e56aa22012-08-03 12:35:34 -04003723 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003724 portRspPut = le32_to_cpu(pgp->rspPutInx);
3725 if (portRspPut >= portRspMax) {
3726 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003727 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003728 * rsp ring <portRspMax>
3729 */
James Smarted957682007-06-17 19:56:37 -05003730 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003731 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003732 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003733 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003734
James Smart2e0fef82007-06-17 19:56:36 -05003735 phba->link_state = LPFC_HBA_ERROR;
3736 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003737
3738 phba->work_hs = HS_FFER3;
3739 lpfc_handle_eratt(phba);
3740
James Smart3772a992009-05-22 14:50:54 -04003741 return;
dea31012005-04-17 16:05:31 -05003742 }
3743
3744 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003745 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003746 /*
3747 * Build a completion list and call the appropriate handler.
3748 * The process is to get the next available response iocb, get
3749 * a free iocb from the list, copy the response data into the
3750 * free iocb, insert to the continuation list, and update the
3751 * next response index to slim. This process makes response
3752 * iocb's in the ring available to DMA as fast as possible but
3753 * pays a penalty for a copy operation. Since the iocb is
3754 * only 32 bytes, this penalty is considered small relative to
3755 * the PCI reads for register values and a slim write. When
3756 * the ulpLe field is set, the entire Command has been
3757 * received.
3758 */
James Smarted957682007-06-17 19:56:37 -05003759 entry = lpfc_resp_iocb(phba, pring);
3760
James Smart858c9f62007-06-17 19:56:39 -05003761 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003762 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003763 if (rspiocbp == NULL) {
3764 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003765 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003766 break;
3767 }
3768
James Smarted957682007-06-17 19:56:37 -05003769 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3770 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003771 irsp = &rspiocbp->iocb;
3772
James Smart7e56aa22012-08-03 12:35:34 -04003773 if (++pring->sli.sli3.rspidx >= portRspMax)
3774 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003775
James Smarta58cbd52007-08-02 11:09:43 -04003776 if (pring->ringno == LPFC_ELS_RING) {
3777 lpfc_debugfs_slow_ring_trc(phba,
3778 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3779 *(((uint32_t *) irsp) + 4),
3780 *(((uint32_t *) irsp) + 6),
3781 *(((uint32_t *) irsp) + 7));
3782 }
3783
James Smart7e56aa22012-08-03 12:35:34 -04003784 writel(pring->sli.sli3.rspidx,
3785 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003786
James Smart3772a992009-05-22 14:50:54 -04003787 spin_unlock_irqrestore(&phba->hbalock, iflag);
3788 /* Handle the response IOCB */
3789 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3790 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003791
3792 /*
3793 * If the port response put pointer has not been updated, sync
3794 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3795 * response put pointer.
3796 */
James Smart7e56aa22012-08-03 12:35:34 -04003797 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003798 portRspPut = le32_to_cpu(pgp->rspPutInx);
3799 }
James Smart7e56aa22012-08-03 12:35:34 -04003800 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003801
James Smart92d7f7b2007-06-17 19:56:38 -05003802 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003803 /* At least one response entry has been freed */
3804 pring->stats.iocb_rsp_full++;
3805 /* SET RxRE_RSP in Chip Att register */
3806 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3807 writel(status, phba->CAregaddr);
3808 readl(phba->CAregaddr); /* flush */
3809 }
3810 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3811 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3812 pring->stats.iocb_cmd_empty++;
3813
3814 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003815 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003816 lpfc_sli_resume_iocb(phba, pring);
3817
3818 if ((pring->lpfc_sli_cmd_available))
3819 (pring->lpfc_sli_cmd_available) (phba, pring);
3820
3821 }
3822
James Smart2e0fef82007-06-17 19:56:36 -05003823 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003824 return;
dea31012005-04-17 16:05:31 -05003825}
3826
James Smarte59058c2008-08-24 21:49:00 -04003827/**
James Smart4f774512009-05-22 14:52:35 -04003828 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3829 * @phba: Pointer to HBA context object.
3830 * @pring: Pointer to driver SLI ring object.
3831 * @mask: Host attention register mask for this ring.
3832 *
3833 * This function is called from the worker thread when there is a pending
3834 * ELS response iocb on the driver internal slow-path response iocb worker
3835 * queue. The caller does not hold any lock. The function will remove each
3836 * response iocb from the response worker queue and calls the handle
3837 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3838 **/
3839static void
3840lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3841 struct lpfc_sli_ring *pring, uint32_t mask)
3842{
3843 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003844 struct hbq_dmabuf *dmabuf;
3845 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003846 unsigned long iflag;
James Smart0ef01a22018-09-10 10:30:45 -07003847 int count = 0;
James Smart4f774512009-05-22 14:52:35 -04003848
James Smart45ed1192009-10-02 15:17:02 -04003849 spin_lock_irqsave(&phba->hbalock, iflag);
3850 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3851 spin_unlock_irqrestore(&phba->hbalock, iflag);
3852 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003853 /* Get the response iocb from the head of work queue */
3854 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003855 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003856 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003857 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003858
3859 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3860 case CQE_CODE_COMPL_WQE:
3861 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3862 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003863 /* Translate ELS WCQE to response IOCBQ */
3864 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3865 irspiocbq);
3866 if (irspiocbq)
3867 lpfc_sli_sp_handle_rspiocb(phba, pring,
3868 irspiocbq);
James Smart0ef01a22018-09-10 10:30:45 -07003869 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003870 break;
3871 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003872 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003873 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3874 cq_event);
3875 lpfc_sli4_handle_received_buffer(phba, dmabuf);
James Smart0ef01a22018-09-10 10:30:45 -07003876 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003877 break;
3878 default:
3879 break;
3880 }
James Smart0ef01a22018-09-10 10:30:45 -07003881
3882 /* Limit the number of events to 64 to avoid soft lockups */
3883 if (count == 64)
3884 break;
James Smart4f774512009-05-22 14:52:35 -04003885 }
3886}
3887
3888/**
James Smart3621a712009-04-06 18:47:14 -04003889 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003890 * @phba: Pointer to HBA context object.
3891 * @pring: Pointer to driver SLI ring object.
3892 *
3893 * This function aborts all iocbs in the given ring and frees all the iocb
3894 * objects in txq. This function issues an abort iocb for all the iocb commands
3895 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3896 * the return of this function. The caller is not required to hold any locks.
3897 **/
James Smart2e0fef82007-06-17 19:56:36 -05003898void
dea31012005-04-17 16:05:31 -05003899lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3900{
James Smart2534ba72007-04-25 09:52:20 -04003901 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003902 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003903
James Smart92d7f7b2007-06-17 19:56:38 -05003904 if (pring->ringno == LPFC_ELS_RING) {
3905 lpfc_fabric_abort_hba(phba);
3906 }
3907
dea31012005-04-17 16:05:31 -05003908 /* Error everything on txq and txcmplq
3909 * First do the txq.
3910 */
James Smartdb55fba2014-04-04 13:52:02 -04003911 if (phba->sli_rev >= LPFC_SLI_REV4) {
3912 spin_lock_irq(&pring->ring_lock);
3913 list_splice_init(&pring->txq, &completions);
3914 pring->txq_cnt = 0;
3915 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003916
James Smartdb55fba2014-04-04 13:52:02 -04003917 spin_lock_irq(&phba->hbalock);
3918 /* Next issue ABTS for everything on the txcmplq */
3919 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3920 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3921 spin_unlock_irq(&phba->hbalock);
3922 } else {
3923 spin_lock_irq(&phba->hbalock);
3924 list_splice_init(&pring->txq, &completions);
3925 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003926
James Smartdb55fba2014-04-04 13:52:02 -04003927 /* Next issue ABTS for everything on the txcmplq */
3928 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3929 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3930 spin_unlock_irq(&phba->hbalock);
3931 }
James Smart2534ba72007-04-25 09:52:20 -04003932
James Smarta257bf92009-04-06 18:48:10 -04003933 /* Cancel all the IOCBs from the completions list */
3934 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3935 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003936}
3937
James Smarte59058c2008-08-24 21:49:00 -04003938/**
James Smart895427b2017-02-12 13:52:30 -08003939 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3940 * @phba: Pointer to HBA context object.
3941 * @pring: Pointer to driver SLI ring object.
3942 *
3943 * This function aborts all iocbs in the given ring and frees all the iocb
3944 * objects in txq. This function issues an abort iocb for all the iocb commands
3945 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3946 * the return of this function. The caller is not required to hold any locks.
3947 **/
3948void
3949lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3950{
3951 LIST_HEAD(completions);
3952 struct lpfc_iocbq *iocb, *next_iocb;
3953
3954 if (pring->ringno == LPFC_ELS_RING)
3955 lpfc_fabric_abort_hba(phba);
3956
3957 spin_lock_irq(&phba->hbalock);
3958 /* Next issue ABTS for everything on the txcmplq */
3959 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3960 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3961 spin_unlock_irq(&phba->hbalock);
3962}
3963
3964
3965/**
James Smartdb55fba2014-04-04 13:52:02 -04003966 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3967 * @phba: Pointer to HBA context object.
3968 * @pring: Pointer to driver SLI ring object.
3969 *
3970 * This function aborts all iocbs in FCP rings and frees all the iocb
3971 * objects in txq. This function issues an abort iocb for all the iocb commands
3972 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3973 * the return of this function. The caller is not required to hold any locks.
3974 **/
3975void
3976lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3977{
3978 struct lpfc_sli *psli = &phba->sli;
3979 struct lpfc_sli_ring *pring;
3980 uint32_t i;
3981
3982 /* Look on all the FCP Rings for the iotag */
3983 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08003984 for (i = 0; i < phba->cfg_hdw_queue; i++) {
3985 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003986 lpfc_sli_abort_iocb_ring(phba, pring);
3987 }
3988 } else {
James Smart895427b2017-02-12 13:52:30 -08003989 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003990 lpfc_sli_abort_iocb_ring(phba, pring);
3991 }
3992}
3993
James Smart895427b2017-02-12 13:52:30 -08003994/**
3995 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3996 * @phba: Pointer to HBA context object.
3997 *
3998 * This function aborts all wqes in NVME rings. This function issues an
3999 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
4000 * the txcmplq is not guaranteed to complete before the return of this
4001 * function. The caller is not required to hold any locks.
4002 **/
4003void
4004lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
4005{
4006 struct lpfc_sli_ring *pring;
4007 uint32_t i;
4008
James Smartcdb42be2019-01-28 11:14:21 -08004009 if ((phba->sli_rev < LPFC_SLI_REV4) ||
4010 !(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
James Smart895427b2017-02-12 13:52:30 -08004011 return;
4012
4013 /* Abort all IO on each NVME ring. */
James Smartcdb42be2019-01-28 11:14:21 -08004014 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4015 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
James Smart895427b2017-02-12 13:52:30 -08004016 lpfc_sli_abort_wqe_ring(phba, pring);
4017 }
4018}
4019
James Smartdb55fba2014-04-04 13:52:02 -04004020
4021/**
James Smart3621a712009-04-06 18:47:14 -04004022 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04004023 * @phba: Pointer to HBA context object.
4024 *
4025 * This function flushes all iocbs in the fcp ring and frees all the iocb
4026 * objects in txq and txcmplq. This function will not issue abort iocbs
4027 * for all the iocb commands in txcmplq, they will just be returned with
4028 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4029 * slot has been permanently disabled.
4030 **/
4031void
4032lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
4033{
4034 LIST_HEAD(txq);
4035 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04004036 struct lpfc_sli *psli = &phba->sli;
4037 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04004038 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004039 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04004040
4041 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04004042 /* Indicate the I/O queues are flushed */
4043 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04004044 spin_unlock_irq(&phba->hbalock);
4045
James Smartdb55fba2014-04-04 13:52:02 -04004046 /* Look on all the FCP Rings for the iotag */
4047 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004048 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4049 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
James Smarta8e497d2008-08-24 21:50:11 -04004050
James Smartdb55fba2014-04-04 13:52:02 -04004051 spin_lock_irq(&pring->ring_lock);
4052 /* Retrieve everything on txq */
4053 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004054 list_for_each_entry_safe(piocb, next_iocb,
4055 &pring->txcmplq, list)
4056 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004057 /* Retrieve everything on the txcmplq */
4058 list_splice_init(&pring->txcmplq, &txcmplq);
4059 pring->txq_cnt = 0;
4060 pring->txcmplq_cnt = 0;
4061 spin_unlock_irq(&pring->ring_lock);
4062
4063 /* Flush the txq */
4064 lpfc_sli_cancel_iocbs(phba, &txq,
4065 IOSTAT_LOCAL_REJECT,
4066 IOERR_SLI_DOWN);
4067 /* Flush the txcmpq */
4068 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4069 IOSTAT_LOCAL_REJECT,
4070 IOERR_SLI_DOWN);
4071 }
4072 } else {
James Smart895427b2017-02-12 13:52:30 -08004073 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004074
4075 spin_lock_irq(&phba->hbalock);
4076 /* Retrieve everything on txq */
4077 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004078 list_for_each_entry_safe(piocb, next_iocb,
4079 &pring->txcmplq, list)
4080 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004081 /* Retrieve everything on the txcmplq */
4082 list_splice_init(&pring->txcmplq, &txcmplq);
4083 pring->txq_cnt = 0;
4084 pring->txcmplq_cnt = 0;
4085 spin_unlock_irq(&phba->hbalock);
4086
4087 /* Flush the txq */
4088 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4089 IOERR_SLI_DOWN);
4090 /* Flush the txcmpq */
4091 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4092 IOERR_SLI_DOWN);
4093 }
James Smarta8e497d2008-08-24 21:50:11 -04004094}
4095
4096/**
James Smart895427b2017-02-12 13:52:30 -08004097 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4098 * @phba: Pointer to HBA context object.
4099 *
4100 * This function flushes all wqes in the nvme rings and frees all resources
4101 * in the txcmplq. This function does not issue abort wqes for the IO
4102 * commands in txcmplq, they will just be returned with
4103 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4104 * slot has been permanently disabled.
4105 **/
4106void
4107lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4108{
4109 LIST_HEAD(txcmplq);
4110 struct lpfc_sli_ring *pring;
4111 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004112 struct lpfc_iocbq *piocb, *next_iocb;
James Smart895427b2017-02-12 13:52:30 -08004113
James Smartcdb42be2019-01-28 11:14:21 -08004114 if ((phba->sli_rev < LPFC_SLI_REV4) ||
4115 !(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
James Smart895427b2017-02-12 13:52:30 -08004116 return;
4117
4118 /* Hint to other driver operations that a flush is in progress. */
4119 spin_lock_irq(&phba->hbalock);
4120 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4121 spin_unlock_irq(&phba->hbalock);
4122
4123 /* Cycle through all NVME rings and complete each IO with
4124 * a local driver reason code. This is a flush so no
4125 * abort exchange to FW.
4126 */
James Smartcdb42be2019-01-28 11:14:21 -08004127 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4128 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
James Smart895427b2017-02-12 13:52:30 -08004129
James Smart895427b2017-02-12 13:52:30 -08004130 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -08004131 list_for_each_entry_safe(piocb, next_iocb,
4132 &pring->txcmplq, list)
4133 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4134 /* Retrieve everything on the txcmplq */
James Smart895427b2017-02-12 13:52:30 -08004135 list_splice_init(&pring->txcmplq, &txcmplq);
4136 pring->txcmplq_cnt = 0;
4137 spin_unlock_irq(&pring->ring_lock);
4138
4139 /* Flush the txcmpq &&&PAE */
4140 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4141 IOSTAT_LOCAL_REJECT,
4142 IOERR_SLI_DOWN);
4143 }
4144}
4145
4146/**
James Smart3772a992009-05-22 14:50:54 -04004147 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004148 * @phba: Pointer to HBA context object.
4149 * @mask: Bit mask to be checked.
4150 *
4151 * This function reads the host status register and compares
4152 * with the provided bit mask to check if HBA completed
4153 * the restart. This function will wait in a loop for the
4154 * HBA to complete restart. If the HBA does not restart within
4155 * 15 iterations, the function will reset the HBA again. The
4156 * function returns 1 when HBA fail to restart otherwise returns
4157 * zero.
4158 **/
James Smart3772a992009-05-22 14:50:54 -04004159static int
4160lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004161{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004162 uint32_t status;
4163 int i = 0;
4164 int retval = 0;
dea31012005-04-17 16:05:31 -05004165
Jamie Wellnitz41415862006-02-28 19:25:27 -05004166 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004167 if (lpfc_readl(phba->HSregaddr, &status))
4168 return 1;
dea31012005-04-17 16:05:31 -05004169
Jamie Wellnitz41415862006-02-28 19:25:27 -05004170 /*
4171 * Check status register every 100ms for 5 retries, then every
4172 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4173 * every 2.5 sec for 4.
4174 * Break our of the loop if errors occurred during init.
4175 */
4176 while (((status & mask) != mask) &&
4177 !(status & HS_FFERM) &&
4178 i++ < 20) {
dea31012005-04-17 16:05:31 -05004179
Jamie Wellnitz41415862006-02-28 19:25:27 -05004180 if (i <= 5)
4181 msleep(10);
4182 else if (i <= 10)
4183 msleep(500);
4184 else
4185 msleep(2500);
dea31012005-04-17 16:05:31 -05004186
Jamie Wellnitz41415862006-02-28 19:25:27 -05004187 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004188 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004189 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004190 lpfc_sli_brdrestart(phba);
4191 }
4192 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004193 if (lpfc_readl(phba->HSregaddr, &status)) {
4194 retval = 1;
4195 break;
4196 }
dea31012005-04-17 16:05:31 -05004197 }
dea31012005-04-17 16:05:31 -05004198
Jamie Wellnitz41415862006-02-28 19:25:27 -05004199 /* Check to see if any errors occurred during init */
4200 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004201 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4202 "2751 Adapter failed to restart, "
4203 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4204 status,
4205 readl(phba->MBslimaddr + 0xa8),
4206 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004207 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004208 retval = 1;
4209 }
dea31012005-04-17 16:05:31 -05004210
Jamie Wellnitz41415862006-02-28 19:25:27 -05004211 return retval;
dea31012005-04-17 16:05:31 -05004212}
4213
James Smartda0436e2009-05-22 14:51:39 -04004214/**
4215 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4216 * @phba: Pointer to HBA context object.
4217 * @mask: Bit mask to be checked.
4218 *
4219 * This function checks the host status register to check if HBA is
4220 * ready. This function will wait in a loop for the HBA to be ready
4221 * If the HBA is not ready , the function will will reset the HBA PCI
4222 * function again. The function returns 1 when HBA fail to be ready
4223 * otherwise returns zero.
4224 **/
4225static int
4226lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4227{
4228 uint32_t status;
4229 int retval = 0;
4230
4231 /* Read the HBA Host Status Register */
4232 status = lpfc_sli4_post_status_check(phba);
4233
4234 if (status) {
4235 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4236 lpfc_sli_brdrestart(phba);
4237 status = lpfc_sli4_post_status_check(phba);
4238 }
4239
4240 /* Check to see if any errors occurred during init */
4241 if (status) {
4242 phba->link_state = LPFC_HBA_ERROR;
4243 retval = 1;
4244 } else
4245 phba->sli4_hba.intr_enable = 0;
4246
4247 return retval;
4248}
4249
4250/**
4251 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4252 * @phba: Pointer to HBA context object.
4253 * @mask: Bit mask to be checked.
4254 *
4255 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4256 * from the API jump table function pointer from the lpfc_hba struct.
4257 **/
4258int
4259lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4260{
4261 return phba->lpfc_sli_brdready(phba, mask);
4262}
4263
James Smart92908312006-03-07 15:04:13 -05004264#define BARRIER_TEST_PATTERN (0xdeadbeef)
4265
James Smarte59058c2008-08-24 21:49:00 -04004266/**
James Smart3621a712009-04-06 18:47:14 -04004267 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004268 * @phba: Pointer to HBA context object.
4269 *
James Smart1b511972011-12-13 13:23:09 -05004270 * This function is called before resetting an HBA. This function is called
4271 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004272 **/
James Smart2e0fef82007-06-17 19:56:36 -05004273void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004274{
James Smart65a29c12006-07-06 15:50:50 -04004275 uint32_t __iomem *resp_buf;
4276 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004277 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004278 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004279 int i;
4280 uint8_t hdrtype;
4281
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004282 lockdep_assert_held(&phba->hbalock);
4283
James Smart92908312006-03-07 15:04:13 -05004284 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4285 if (hdrtype != 0x80 ||
4286 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4287 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4288 return;
4289
4290 /*
4291 * Tell the other part of the chip to suspend temporarily all
4292 * its DMA activity.
4293 */
James Smart65a29c12006-07-06 15:50:50 -04004294 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004295
4296 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004297 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4298 return;
James Smart92908312006-03-07 15:04:13 -05004299 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4300 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004301 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004302
James Smart9940b972011-03-11 16:06:12 -05004303 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4304 return;
4305 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004306 /* Clear Chip error bit */
4307 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004308 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004309 }
4310
4311 mbox = 0;
4312 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4313 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4314
4315 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004316 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004317 writel(mbox, mbox_buf);
4318
James Smart9940b972011-03-11 16:06:12 -05004319 for (i = 0; i < 50; i++) {
4320 if (lpfc_readl((resp_buf + 1), &resp_data))
4321 return;
4322 if (resp_data != ~(BARRIER_TEST_PATTERN))
4323 mdelay(1);
4324 else
4325 break;
4326 }
4327 resp_data = 0;
4328 if (lpfc_readl((resp_buf + 1), &resp_data))
4329 return;
4330 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004331 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004332 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004333 goto restore_hc;
4334 else
4335 goto clear_errat;
4336 }
4337
4338 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004339 resp_data = 0;
4340 for (i = 0; i < 500; i++) {
4341 if (lpfc_readl(resp_buf, &resp_data))
4342 return;
4343 if (resp_data != mbox)
4344 mdelay(1);
4345 else
4346 break;
4347 }
James Smart92908312006-03-07 15:04:13 -05004348
4349clear_errat:
4350
James Smart9940b972011-03-11 16:06:12 -05004351 while (++i < 500) {
4352 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4353 return;
4354 if (!(ha_copy & HA_ERATT))
4355 mdelay(1);
4356 else
4357 break;
4358 }
James Smart92908312006-03-07 15:04:13 -05004359
4360 if (readl(phba->HAregaddr) & HA_ERATT) {
4361 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004362 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004363 }
4364
4365restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004366 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004367 writel(hc_copy, phba->HCregaddr);
4368 readl(phba->HCregaddr); /* flush */
4369}
4370
James Smarte59058c2008-08-24 21:49:00 -04004371/**
James Smart3621a712009-04-06 18:47:14 -04004372 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004373 * @phba: Pointer to HBA context object.
4374 *
4375 * This function issues a kill_board mailbox command and waits for
4376 * the error attention interrupt. This function is called for stopping
4377 * the firmware processing. The caller is not required to hold any
4378 * locks. This function calls lpfc_hba_down_post function to free
4379 * any pending commands after the kill. The function will return 1 when it
4380 * fails to kill the board else will return 0.
4381 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004382int
James Smart2e0fef82007-06-17 19:56:36 -05004383lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004384{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004385 struct lpfc_sli *psli;
4386 LPFC_MBOXQ_t *pmb;
4387 uint32_t status;
4388 uint32_t ha_copy;
4389 int retval;
4390 int i = 0;
4391
4392 psli = &phba->sli;
4393
4394 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004395 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004396 "0329 Kill HBA Data: x%x x%x\n",
4397 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004398
James Smart98c9ea52007-10-27 13:37:33 -04004399 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4400 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004401 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004402
4403 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004404 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004405 if (lpfc_readl(phba->HCregaddr, &status)) {
4406 spin_unlock_irq(&phba->hbalock);
4407 mempool_free(pmb, phba->mbox_mem_pool);
4408 return 1;
4409 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004410 status &= ~HC_ERINT_ENA;
4411 writel(status, phba->HCregaddr);
4412 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004413 phba->link_flag |= LS_IGNORE_ERATT;
4414 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004415
4416 lpfc_kill_board(phba, pmb);
4417 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4418 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4419
4420 if (retval != MBX_SUCCESS) {
4421 if (retval != MBX_BUSY)
4422 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004423 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4424 "2752 KILL_BOARD command failed retval %d\n",
4425 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004426 spin_lock_irq(&phba->hbalock);
4427 phba->link_flag &= ~LS_IGNORE_ERATT;
4428 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004429 return 1;
4430 }
4431
James Smartf4b4c682009-05-22 14:53:12 -04004432 spin_lock_irq(&phba->hbalock);
4433 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4434 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004435
Jamie Wellnitz41415862006-02-28 19:25:27 -05004436 mempool_free(pmb, phba->mbox_mem_pool);
4437
4438 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4439 * attention every 100ms for 3 seconds. If we don't get ERATT after
4440 * 3 seconds we still set HBA_ERROR state because the status of the
4441 * board is now undefined.
4442 */
James Smart9940b972011-03-11 16:06:12 -05004443 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4444 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004445 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4446 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004447 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4448 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004449 }
4450
4451 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004452 if (ha_copy & HA_ERATT) {
4453 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004454 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004455 }
James Smart2e0fef82007-06-17 19:56:36 -05004456 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004457 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004458 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004459 phba->link_flag &= ~LS_IGNORE_ERATT;
4460 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004461
Jamie Wellnitz41415862006-02-28 19:25:27 -05004462 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004463 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004464
James Smart2e0fef82007-06-17 19:56:36 -05004465 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004466}
4467
James Smarte59058c2008-08-24 21:49:00 -04004468/**
James Smart3772a992009-05-22 14:50:54 -04004469 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004470 * @phba: Pointer to HBA context object.
4471 *
4472 * This function resets the HBA by writing HC_INITFF to the control
4473 * register. After the HBA resets, this function resets all the iocb ring
4474 * indices. This function disables PCI layer parity checking during
4475 * the reset.
4476 * This function returns 0 always.
4477 * The caller is not required to hold any locks.
4478 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004479int
James Smart2e0fef82007-06-17 19:56:36 -05004480lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004481{
4482 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004483 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004484 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004485 int i;
dea31012005-04-17 16:05:31 -05004486
Jamie Wellnitz41415862006-02-28 19:25:27 -05004487 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004488
Jamie Wellnitz41415862006-02-28 19:25:27 -05004489 /* Reset HBA */
4490 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004491 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004492 (phba->pport) ? phba->pport->port_state : 0,
4493 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004494
4495 /* perform board reset */
4496 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004497 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004498 if (phba->pport) {
4499 phba->pport->fc_myDID = 0;
4500 phba->pport->fc_prevDID = 0;
4501 }
dea31012005-04-17 16:05:31 -05004502
Jamie Wellnitz41415862006-02-28 19:25:27 -05004503 /* Turn off parity checking and serr during the physical reset */
4504 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4505 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4506 (cfg_value &
4507 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4508
James Smart3772a992009-05-22 14:50:54 -04004509 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4510
Jamie Wellnitz41415862006-02-28 19:25:27 -05004511 /* Now toggle INITFF bit in the Host Control Register */
4512 writel(HC_INITFF, phba->HCregaddr);
4513 mdelay(1);
4514 readl(phba->HCregaddr); /* flush */
4515 writel(0, phba->HCregaddr);
4516 readl(phba->HCregaddr); /* flush */
4517
4518 /* Restore PCI cmd register */
4519 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004520
4521 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004522 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004523 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004524 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004525 pring->sli.sli3.rspidx = 0;
4526 pring->sli.sli3.next_cmdidx = 0;
4527 pring->sli.sli3.local_getidx = 0;
4528 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004529 pring->missbufcnt = 0;
4530 }
dea31012005-04-17 16:05:31 -05004531
James Smart2e0fef82007-06-17 19:56:36 -05004532 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004533 return 0;
4534}
4535
James Smarte59058c2008-08-24 21:49:00 -04004536/**
James Smartda0436e2009-05-22 14:51:39 -04004537 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4538 * @phba: Pointer to HBA context object.
4539 *
4540 * This function resets a SLI4 HBA. This function disables PCI layer parity
4541 * checking during resets the device. The caller is not required to hold
4542 * any locks.
4543 *
4544 * This function returns 0 always.
4545 **/
4546int
4547lpfc_sli4_brdreset(struct lpfc_hba *phba)
4548{
4549 struct lpfc_sli *psli = &phba->sli;
4550 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004551 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004552
4553 /* Reset HBA */
4554 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004555 "0295 Reset HBA Data: x%x x%x x%x\n",
4556 phba->pport->port_state, psli->sli_flag,
4557 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004558
4559 /* perform board reset */
4560 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004561 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004562 phba->pport->fc_myDID = 0;
4563 phba->pport->fc_prevDID = 0;
4564
James Smartda0436e2009-05-22 14:51:39 -04004565 spin_lock_irq(&phba->hbalock);
4566 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4567 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004568 spin_unlock_irq(&phba->hbalock);
4569
James Smart02936352014-04-04 13:52:12 -04004570 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4571 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4572 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4573 return rc;
4574 }
4575
James Smartda0436e2009-05-22 14:51:39 -04004576 /* Now physically reset the device */
4577 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4578 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004579
4580 /* Turn off parity checking and serr during the physical reset */
4581 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4582 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4583 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4584
James Smart88318812012-09-29 11:29:29 -04004585 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004586 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004587
James Smartbe858b62010-12-15 17:57:20 -05004588 /* Restore PCI cmd register */
4589 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4590
James Smart27b01b82012-05-09 21:19:44 -04004591 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004592}
4593
4594/**
4595 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004596 * @phba: Pointer to HBA context object.
4597 *
4598 * This function is called in the SLI initialization code path to
4599 * restart the HBA. The caller is not required to hold any lock.
4600 * This function writes MBX_RESTART mailbox command to the SLIM and
4601 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4602 * function to free any pending commands. The function enables
4603 * POST only during the first initialization. The function returns zero.
4604 * The function does not guarantee completion of MBX_RESTART mailbox
4605 * command before the return of this function.
4606 **/
James Smartda0436e2009-05-22 14:51:39 -04004607static int
4608lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004609{
4610 MAILBOX_t *mb;
4611 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004612 volatile uint32_t word0;
4613 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004614 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004615
James Smart2e0fef82007-06-17 19:56:36 -05004616 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004617
James Smart0d878412009-10-02 15:16:56 -04004618 /* Take PCIe device Advanced Error Reporting (AER) state */
4619 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4620
Jamie Wellnitz41415862006-02-28 19:25:27 -05004621 psli = &phba->sli;
4622
4623 /* Restart HBA */
4624 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004625 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004626 (phba->pport) ? phba->pport->port_state : 0,
4627 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004628
4629 word0 = 0;
4630 mb = (MAILBOX_t *) &word0;
4631 mb->mbxCommand = MBX_RESTART;
4632 mb->mbxHc = 1;
4633
James Smart92908312006-03-07 15:04:13 -05004634 lpfc_reset_barrier(phba);
4635
Jamie Wellnitz41415862006-02-28 19:25:27 -05004636 to_slim = phba->MBslimaddr;
4637 writel(*(uint32_t *) mb, to_slim);
4638 readl(to_slim); /* flush */
4639
4640 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004641 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004642 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004643 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004644 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004645 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004646 writel(*(uint32_t *) mb, to_slim);
4647 readl(to_slim); /* flush */
4648
4649 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004650 if (phba->pport)
4651 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004652 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004653 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004654 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004655
James Smart64ba8812006-08-02 15:24:34 -04004656 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004657 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04004658
James Smarteaf15d52008-12-04 22:39:29 -05004659 /* Give the INITFF and Post time to settle. */
4660 mdelay(100);
dea31012005-04-17 16:05:31 -05004661
James Smart0d878412009-10-02 15:16:56 -04004662 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4663 if (hba_aer_enabled)
4664 pci_disable_pcie_error_reporting(phba->pcidev);
4665
Jamie Wellnitz41415862006-02-28 19:25:27 -05004666 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004667
4668 return 0;
4669}
4670
James Smarte59058c2008-08-24 21:49:00 -04004671/**
James Smartda0436e2009-05-22 14:51:39 -04004672 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4673 * @phba: Pointer to HBA context object.
4674 *
4675 * This function is called in the SLI initialization code path to restart
4676 * a SLI4 HBA. The caller is not required to hold any lock.
4677 * At the end of the function, it calls lpfc_hba_down_post function to
4678 * free any pending commands.
4679 **/
4680static int
4681lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4682{
4683 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004684 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004685 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004686
4687 /* Restart HBA */
4688 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4689 "0296 Restart HBA Data: x%x x%x\n",
4690 phba->pport->port_state, psli->sli_flag);
4691
James Smart75baf692010-06-08 18:31:21 -04004692 /* Take PCIe device Advanced Error Reporting (AER) state */
4693 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4694
James Smart27b01b82012-05-09 21:19:44 -04004695 rc = lpfc_sli4_brdreset(phba);
James Smart5a9eeff2018-11-29 16:09:32 -08004696 if (rc)
4697 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004698
4699 spin_lock_irq(&phba->hbalock);
4700 phba->pport->stopped = 0;
4701 phba->link_state = LPFC_INIT_START;
4702 phba->hba_flag = 0;
4703 spin_unlock_irq(&phba->hbalock);
4704
4705 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004706 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04004707
James Smart75baf692010-06-08 18:31:21 -04004708 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4709 if (hba_aer_enabled)
4710 pci_disable_pcie_error_reporting(phba->pcidev);
4711
James Smartda0436e2009-05-22 14:51:39 -04004712 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004713 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004714
James Smart27b01b82012-05-09 21:19:44 -04004715 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004716}
4717
4718/**
4719 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4720 * @phba: Pointer to HBA context object.
4721 *
4722 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4723 * API jump table function pointer from the lpfc_hba struct.
4724**/
4725int
4726lpfc_sli_brdrestart(struct lpfc_hba *phba)
4727{
4728 return phba->lpfc_sli_brdrestart(phba);
4729}
4730
4731/**
James Smart3621a712009-04-06 18:47:14 -04004732 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004733 * @phba: Pointer to HBA context object.
4734 *
4735 * This function is called after a HBA restart to wait for successful
4736 * restart of the HBA. Successful restart of the HBA is indicated by
4737 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4738 * iteration, the function will restart the HBA again. The function returns
4739 * zero if HBA successfully restarted else returns negative error code.
4740 **/
James Smart4492b732017-04-27 15:08:26 -07004741int
dea31012005-04-17 16:05:31 -05004742lpfc_sli_chipset_init(struct lpfc_hba *phba)
4743{
4744 uint32_t status, i = 0;
4745
4746 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004747 if (lpfc_readl(phba->HSregaddr, &status))
4748 return -EIO;
dea31012005-04-17 16:05:31 -05004749
4750 /* Check status register to see what current state is */
4751 i = 0;
4752 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4753
James Smartdcf2a4e2010-09-29 11:18:53 -04004754 /* Check every 10ms for 10 retries, then every 100ms for 90
4755 * retries, then every 1 sec for 50 retires for a total of
4756 * ~60 seconds before reset the board again and check every
4757 * 1 sec for 50 retries. The up to 60 seconds before the
4758 * board ready is required by the Falcon FIPS zeroization
4759 * complete, and any reset the board in between shall cause
4760 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004761 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004762 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004763 /* Adapter failed to init, timeout, status reg
4764 <status> */
James Smarted957682007-06-17 19:56:37 -05004765 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004766 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004767 "timeout, status reg x%x, "
4768 "FW Data: A8 x%x AC x%x\n", status,
4769 readl(phba->MBslimaddr + 0xa8),
4770 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004771 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004772 return -ETIMEDOUT;
4773 }
4774
4775 /* Check to see if any errors occurred during init */
4776 if (status & HS_FFERM) {
4777 /* ERROR: During chipset initialization */
4778 /* Adapter failed to init, chipset, status reg
4779 <status> */
James Smarted957682007-06-17 19:56:37 -05004780 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004781 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004782 "chipset, status reg x%x, "
4783 "FW Data: A8 x%x AC x%x\n", status,
4784 readl(phba->MBslimaddr + 0xa8),
4785 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004786 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004787 return -EIO;
4788 }
4789
James Smartdcf2a4e2010-09-29 11:18:53 -04004790 if (i <= 10)
dea31012005-04-17 16:05:31 -05004791 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004792 else if (i <= 100)
4793 msleep(100);
4794 else
4795 msleep(1000);
dea31012005-04-17 16:05:31 -05004796
James Smartdcf2a4e2010-09-29 11:18:53 -04004797 if (i == 150) {
4798 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004799 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004800 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004801 }
4802 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004803 if (lpfc_readl(phba->HSregaddr, &status))
4804 return -EIO;
dea31012005-04-17 16:05:31 -05004805 }
4806
4807 /* Check to see if any errors occurred during init */
4808 if (status & HS_FFERM) {
4809 /* ERROR: During chipset initialization */
4810 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004811 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004812 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004813 "status reg x%x, "
4814 "FW Data: A8 x%x AC x%x\n", status,
4815 readl(phba->MBslimaddr + 0xa8),
4816 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004817 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004818 return -EIO;
4819 }
4820
4821 /* Clear all interrupt enable conditions */
4822 writel(0, phba->HCregaddr);
4823 readl(phba->HCregaddr); /* flush */
4824
4825 /* setup host attn register */
4826 writel(0xffffffff, phba->HAregaddr);
4827 readl(phba->HAregaddr); /* flush */
4828 return 0;
4829}
4830
James Smarte59058c2008-08-24 21:49:00 -04004831/**
James Smart3621a712009-04-06 18:47:14 -04004832 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004833 *
4834 * This function calculates and returns the number of HBQs required to be
4835 * configured.
4836 **/
James Smart78b2d852007-08-02 11:10:21 -04004837int
James Smarted957682007-06-17 19:56:37 -05004838lpfc_sli_hbq_count(void)
4839{
James Smart92d7f7b2007-06-17 19:56:38 -05004840 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004841}
4842
James Smarte59058c2008-08-24 21:49:00 -04004843/**
James Smart3621a712009-04-06 18:47:14 -04004844 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004845 *
4846 * This function adds the number of hbq entries in every HBQ to get
4847 * the total number of hbq entries required for the HBA and returns
4848 * the total count.
4849 **/
James Smarted957682007-06-17 19:56:37 -05004850static int
4851lpfc_sli_hbq_entry_count(void)
4852{
4853 int hbq_count = lpfc_sli_hbq_count();
4854 int count = 0;
4855 int i;
4856
4857 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004858 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004859 return count;
4860}
4861
James Smarte59058c2008-08-24 21:49:00 -04004862/**
James Smart3621a712009-04-06 18:47:14 -04004863 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004864 *
4865 * This function calculates amount of memory required for all hbq entries
4866 * to be configured and returns the total memory required.
4867 **/
dea31012005-04-17 16:05:31 -05004868int
James Smarted957682007-06-17 19:56:37 -05004869lpfc_sli_hbq_size(void)
4870{
4871 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4872}
4873
James Smarte59058c2008-08-24 21:49:00 -04004874/**
James Smart3621a712009-04-06 18:47:14 -04004875 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004876 * @phba: Pointer to HBA context object.
4877 *
4878 * This function is called during the SLI initialization to configure
4879 * all the HBQs and post buffers to the HBQ. The caller is not
4880 * required to hold any locks. This function will return zero if successful
4881 * else it will return negative error code.
4882 **/
James Smarted957682007-06-17 19:56:37 -05004883static int
4884lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4885{
4886 int hbq_count = lpfc_sli_hbq_count();
4887 LPFC_MBOXQ_t *pmb;
4888 MAILBOX_t *pmbox;
4889 uint32_t hbqno;
4890 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004891
James Smart92d7f7b2007-06-17 19:56:38 -05004892 /* Get a Mailbox buffer to setup mailbox
4893 * commands for HBA initialization
4894 */
James Smarted957682007-06-17 19:56:37 -05004895 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4896
4897 if (!pmb)
4898 return -ENOMEM;
4899
James Smart04c68492009-05-22 14:52:52 -04004900 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004901
4902 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4903 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004904 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004905
4906 hbq_entry_index = 0;
4907 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4908 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4909 phba->hbqs[hbqno].hbqPutIdx = 0;
4910 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4911 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004912 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004913 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4914 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004915 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4916
4917 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4918 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4919 mbxStatus <status>, ring <num> */
4920
4921 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004922 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004923 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004924 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004925 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004926 pmbox->mbxStatus, hbqno);
4927
4928 phba->link_state = LPFC_HBA_ERROR;
4929 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004930 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004931 }
4932 }
4933 phba->hbq_count = hbq_count;
4934
James Smarted957682007-06-17 19:56:37 -05004935 mempool_free(pmb, phba->mbox_mem_pool);
4936
James Smart92d7f7b2007-06-17 19:56:38 -05004937 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004938 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4939 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004940 return 0;
4941}
4942
James Smarte59058c2008-08-24 21:49:00 -04004943/**
James Smart4f774512009-05-22 14:52:35 -04004944 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4945 * @phba: Pointer to HBA context object.
4946 *
4947 * This function is called during the SLI initialization to configure
4948 * all the HBQs and post buffers to the HBQ. The caller is not
4949 * required to hold any locks. This function will return zero if successful
4950 * else it will return negative error code.
4951 **/
4952static int
4953lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4954{
4955 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004956 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4957 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004958 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004959 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004960 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004961 return 0;
4962}
4963
4964/**
James Smart3621a712009-04-06 18:47:14 -04004965 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004966 * @phba: Pointer to HBA context object.
4967 * @sli_mode: sli mode - 2/3
4968 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004969 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004970 * to issue config_port mailbox command. This function restarts the
4971 * HBA firmware and issues a config_port mailbox command to configure
4972 * the SLI interface in the sli mode specified by sli_mode
4973 * variable. The caller is not required to hold any locks.
4974 * The function returns 0 if successful, else returns negative error
4975 * code.
4976 **/
James Smart93996272008-08-24 21:50:30 -04004977int
4978lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004979{
4980 LPFC_MBOXQ_t *pmb;
4981 uint32_t resetcount = 0, rc = 0, done = 0;
4982
4983 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4984 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004985 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004986 return -ENOMEM;
4987 }
4988
James Smarted957682007-06-17 19:56:37 -05004989 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004990 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004991 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004992 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004993 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004994 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004995 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004996 rc = lpfc_sli_chipset_init(phba);
4997 if (rc)
4998 break;
4999
James Smart2e0fef82007-06-17 19:56:36 -05005000 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04005001 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005002 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005003 resetcount++;
5004
James Smarted957682007-06-17 19:56:37 -05005005 /* Call pre CONFIG_PORT mailbox command initialization. A
5006 * value of 0 means the call was successful. Any other
5007 * nonzero value is a failure, but if ERESTART is returned,
5008 * the driver may reset the HBA and try again.
5009 */
dea31012005-04-17 16:05:31 -05005010 rc = lpfc_config_port_prep(phba);
5011 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05005012 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05005013 continue;
James Smart34b02dc2008-08-24 21:49:55 -04005014 } else if (rc)
dea31012005-04-17 16:05:31 -05005015 break;
James Smart6d368e52011-05-24 11:44:12 -04005016
James Smart2e0fef82007-06-17 19:56:36 -05005017 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05005018 lpfc_config_port(phba, pmb);
5019 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04005020 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5021 LPFC_SLI3_HBQ_ENABLED |
5022 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04005023 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05005024 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05005025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005026 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05005027 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04005028 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005029 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04005030 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005031 spin_unlock_irq(&phba->hbalock);
5032 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04005033 } else {
5034 /* Allow asynchronous mailbox command to go through */
5035 spin_lock_irq(&phba->hbalock);
5036 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5037 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05005038 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05005039
5040 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5041 (pmb->u.mb.un.varCfgPort.gasabt == 0))
5042 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5043 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04005044 }
dea31012005-04-17 16:05:31 -05005045 }
James Smarted957682007-06-17 19:56:37 -05005046 if (!done) {
5047 rc = -EINVAL;
5048 goto do_prep_failed;
5049 }
James Smart04c68492009-05-22 14:52:52 -04005050 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5051 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04005052 rc = -ENXIO;
5053 goto do_prep_failed;
5054 }
James Smart04c68492009-05-22 14:52:52 -04005055 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04005056 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005057 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5058 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5059 phba->max_vpi : phba->max_vports;
5060
James Smart34b02dc2008-08-24 21:49:55 -04005061 } else
5062 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04005063 phba->fips_level = 0;
5064 phba->fips_spec_rev = 0;
5065 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04005066 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04005067 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
5068 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5069 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5070 "2850 Security Crypto Active. FIPS x%d "
5071 "(Spec Rev: x%d)",
5072 phba->fips_level, phba->fips_spec_rev);
5073 }
5074 if (pmb->u.mb.un.varCfgPort.sec_err) {
5075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5076 "2856 Config Port Security Crypto "
5077 "Error: x%x ",
5078 pmb->u.mb.un.varCfgPort.sec_err);
5079 }
James Smart04c68492009-05-22 14:52:52 -04005080 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005081 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005082 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005083 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005084
5085 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5086 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005087
James Smartf44ac122018-03-05 12:04:08 -08005088 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5089 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5090 phba->cfg_enable_bg = 0;
5091 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smarte2a0a9d2008-12-04 22:40:02 -05005092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5093 "0443 Adapter did not grant "
5094 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005095 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005096 }
James Smart34b02dc2008-08-24 21:49:55 -04005097 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005098 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005099 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005100 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005101 }
James Smart92d7f7b2007-06-17 19:56:38 -05005102do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005103 mempool_free(pmb, phba->mbox_mem_pool);
5104 return rc;
5105}
5106
James Smarte59058c2008-08-24 21:49:00 -04005107
5108/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005109 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005110 * @phba: Pointer to HBA context object.
5111 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005112 * This function is the main SLI initialization function. This function
5113 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005114 * error attention handler code. Caller is not required to hold any
5115 * locks. This function issues config_port mailbox command to configure
5116 * the SLI, setup iocb rings and HBQ rings. In the end the function
5117 * calls the config_port_post function to issue init_link mailbox
5118 * command and to start the discovery. The function will return zero
5119 * if successful, else it will return negative error code.
5120 **/
James Smarted957682007-06-17 19:56:37 -05005121int
5122lpfc_sli_hba_setup(struct lpfc_hba *phba)
5123{
5124 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005125 int mode = 3, i;
5126 int longs;
James Smarted957682007-06-17 19:56:37 -05005127
James Smart12247e82016-07-06 12:36:09 -07005128 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005129 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005130 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005131 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005132 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005133 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005134 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005135 break;
5136 }
James Smarted957682007-06-17 19:56:37 -05005137 mode = 2;
5138 break;
5139 case 0:
5140 case 3:
5141 break;
5142 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005144 "1819 Unrecognized sli_mode parameter: %d.\n",
5145 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005146
5147 break;
5148 }
James Smartb5c53952016-03-31 14:12:30 -07005149 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005150
James Smart93996272008-08-24 21:50:30 -04005151 rc = lpfc_sli_config_port(phba, mode);
5152
James Smart12247e82016-07-06 12:36:09 -07005153 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005155 "1820 Unable to select SLI-3. "
5156 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005157 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005158 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005159 else if (rc && mode == 2)
5160 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005161 if (rc)
dea31012005-04-17 16:05:31 -05005162 goto lpfc_sli_hba_setup_error;
5163
James Smart0d878412009-10-02 15:16:56 -04005164 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5165 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5166 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5167 if (!rc) {
5168 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5169 "2709 This device supports "
5170 "Advanced Error Reporting (AER)\n");
5171 spin_lock_irq(&phba->hbalock);
5172 phba->hba_flag |= HBA_AER_ENABLED;
5173 spin_unlock_irq(&phba->hbalock);
5174 } else {
5175 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5176 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005177 "Advanced Error Reporting (AER): %d\n",
5178 rc);
James Smart0d878412009-10-02 15:16:56 -04005179 phba->cfg_aer_support = 0;
5180 }
5181 }
5182
James Smarted957682007-06-17 19:56:37 -05005183 if (phba->sli_rev == 3) {
5184 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5185 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005186 } else {
5187 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5188 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005189 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005190 }
5191
5192 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005193 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5194 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005195 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005196
5197 if (rc)
5198 goto lpfc_sli_hba_setup_error;
5199
James Smart6d368e52011-05-24 11:44:12 -04005200 /* Initialize VPIs. */
5201 if (phba->sli_rev == LPFC_SLI_REV3) {
5202 /*
5203 * The VPI bitmask and physical ID array are allocated
5204 * and initialized once only - at driver load. A port
5205 * reset doesn't need to reinitialize this memory.
5206 */
5207 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5208 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005209 phba->vpi_bmask = kcalloc(longs,
5210 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005211 GFP_KERNEL);
5212 if (!phba->vpi_bmask) {
5213 rc = -ENOMEM;
5214 goto lpfc_sli_hba_setup_error;
5215 }
5216
Kees Cook6396bb22018-06-12 14:03:40 -07005217 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5218 sizeof(uint16_t),
5219 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005220 if (!phba->vpi_ids) {
5221 kfree(phba->vpi_bmask);
5222 rc = -ENOMEM;
5223 goto lpfc_sli_hba_setup_error;
5224 }
5225 for (i = 0; i < phba->max_vpi; i++)
5226 phba->vpi_ids[i] = i;
5227 }
5228 }
5229
James Smart93996272008-08-24 21:50:30 -04005230 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005231 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5232 rc = lpfc_sli_hbq_setup(phba);
5233 if (rc)
5234 goto lpfc_sli_hba_setup_error;
5235 }
James Smart04c68492009-05-22 14:52:52 -04005236 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005237 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005238 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005239
5240 rc = lpfc_config_port_post(phba);
5241 if (rc)
5242 goto lpfc_sli_hba_setup_error;
5243
James Smarted957682007-06-17 19:56:37 -05005244 return rc;
5245
James Smart92d7f7b2007-06-17 19:56:38 -05005246lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005247 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005249 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005250 return rc;
5251}
5252
James Smartda0436e2009-05-22 14:51:39 -04005253/**
5254 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5255 * @phba: Pointer to HBA context object.
5256 * @mboxq: mailbox pointer.
5257 * This function issue a dump mailbox command to read config region
5258 * 23 and parse the records in the region and populate driver
5259 * data structure.
5260 **/
5261static int
James Smartff78d8f2011-12-13 13:21:35 -05005262lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005263{
James Smartff78d8f2011-12-13 13:21:35 -05005264 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005265 struct lpfc_dmabuf *mp;
5266 struct lpfc_mqe *mqe;
5267 uint32_t data_length;
5268 int rc;
5269
5270 /* Program the default value of vlan_id and fc_map */
5271 phba->valid_vlan = 0;
5272 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5273 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5274 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5275
James Smartff78d8f2011-12-13 13:21:35 -05005276 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5277 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005278 return -ENOMEM;
5279
James Smartff78d8f2011-12-13 13:21:35 -05005280 mqe = &mboxq->u.mqe;
5281 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5282 rc = -ENOMEM;
5283 goto out_free_mboxq;
5284 }
5285
James Smart3e1f0712018-11-29 16:09:29 -08005286 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04005287 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5288
5289 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5290 "(%d):2571 Mailbox cmd x%x Status x%x "
5291 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5292 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5293 "CQ: x%x x%x x%x x%x\n",
5294 mboxq->vport ? mboxq->vport->vpi : 0,
5295 bf_get(lpfc_mqe_command, mqe),
5296 bf_get(lpfc_mqe_status, mqe),
5297 mqe->un.mb_words[0], mqe->un.mb_words[1],
5298 mqe->un.mb_words[2], mqe->un.mb_words[3],
5299 mqe->un.mb_words[4], mqe->un.mb_words[5],
5300 mqe->un.mb_words[6], mqe->un.mb_words[7],
5301 mqe->un.mb_words[8], mqe->un.mb_words[9],
5302 mqe->un.mb_words[10], mqe->un.mb_words[11],
5303 mqe->un.mb_words[12], mqe->un.mb_words[13],
5304 mqe->un.mb_words[14], mqe->un.mb_words[15],
5305 mqe->un.mb_words[16], mqe->un.mb_words[50],
5306 mboxq->mcqe.word0,
5307 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5308 mboxq->mcqe.trailer);
5309
5310 if (rc) {
5311 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5312 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005313 rc = -EIO;
5314 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005315 }
5316 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005317 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005318 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5319 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005320 rc = -EIO;
5321 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005322 }
James Smartda0436e2009-05-22 14:51:39 -04005323
5324 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5325 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5326 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005327 rc = 0;
5328
5329out_free_mboxq:
5330 mempool_free(mboxq, phba->mbox_mem_pool);
5331 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005332}
5333
5334/**
5335 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5336 * @phba: pointer to lpfc hba data structure.
5337 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5338 * @vpd: pointer to the memory to hold resulting port vpd data.
5339 * @vpd_size: On input, the number of bytes allocated to @vpd.
5340 * On output, the number of data bytes in @vpd.
5341 *
5342 * This routine executes a READ_REV SLI4 mailbox command. In
5343 * addition, this routine gets the port vpd data.
5344 *
5345 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005346 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005347 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005348 **/
5349static int
5350lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5351 uint8_t *vpd, uint32_t *vpd_size)
5352{
5353 int rc = 0;
5354 uint32_t dma_size;
5355 struct lpfc_dmabuf *dmabuf;
5356 struct lpfc_mqe *mqe;
5357
5358 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5359 if (!dmabuf)
5360 return -ENOMEM;
5361
5362 /*
5363 * Get a DMA buffer for the vpd data resulting from the READ_REV
5364 * mailbox command.
5365 */
5366 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005367 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5368 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005369 if (!dmabuf->virt) {
5370 kfree(dmabuf);
5371 return -ENOMEM;
5372 }
James Smartda0436e2009-05-22 14:51:39 -04005373
5374 /*
5375 * The SLI4 implementation of READ_REV conflicts at word1,
5376 * bits 31:16 and SLI4 adds vpd functionality not present
5377 * in SLI3. This code corrects the conflicts.
5378 */
5379 lpfc_read_rev(phba, mboxq);
5380 mqe = &mboxq->u.mqe;
5381 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5382 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5383 mqe->un.read_rev.word1 &= 0x0000FFFF;
5384 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5385 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5386
5387 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5388 if (rc) {
5389 dma_free_coherent(&phba->pcidev->dev, dma_size,
5390 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005391 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005392 return -EIO;
5393 }
5394
James Smartda0436e2009-05-22 14:51:39 -04005395 /*
5396 * The available vpd length cannot be bigger than the
5397 * DMA buffer passed to the port. Catch the less than
5398 * case and update the caller's size.
5399 */
5400 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5401 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5402
James Smartd7c47992010-06-08 18:31:54 -04005403 memcpy(vpd, dmabuf->virt, *vpd_size);
5404
James Smartda0436e2009-05-22 14:51:39 -04005405 dma_free_coherent(&phba->pcidev->dev, dma_size,
5406 dmabuf->virt, dmabuf->phys);
5407 kfree(dmabuf);
5408 return 0;
5409}
5410
5411/**
James Smartcd1c8302011-10-10 21:33:25 -04005412 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5413 * @phba: pointer to lpfc hba data structure.
5414 *
5415 * This routine retrieves SLI4 device physical port name this PCI function
5416 * is attached to.
5417 *
5418 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005419 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005420 * otherwise - failed to retrieve physical port name
5421 **/
5422static int
5423lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5424{
5425 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005426 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5427 struct lpfc_controller_attribute *cntl_attr;
5428 struct lpfc_mbx_get_port_name *get_port_name;
5429 void *virtaddr = NULL;
5430 uint32_t alloclen, reqlen;
5431 uint32_t shdr_status, shdr_add_status;
5432 union lpfc_sli4_cfg_shdr *shdr;
5433 char cport_name = 0;
5434 int rc;
5435
5436 /* We assume nothing at this point */
5437 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5438 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5439
5440 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5441 if (!mboxq)
5442 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005443 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005444 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5445 lpfc_sli4_read_config(phba);
5446 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5447 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005448
5449 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5450 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5451 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5452 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5453 LPFC_SLI4_MBX_NEMBED);
5454 if (alloclen < reqlen) {
5455 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5456 "3084 Allocated DMA memory size (%d) is "
5457 "less than the requested DMA memory size "
5458 "(%d)\n", alloclen, reqlen);
5459 rc = -ENOMEM;
5460 goto out_free_mboxq;
5461 }
5462 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5463 virtaddr = mboxq->sge_array->addr[0];
5464 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5465 shdr = &mbx_cntl_attr->cfg_shdr;
5466 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5467 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5468 if (shdr_status || shdr_add_status || rc) {
5469 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5470 "3085 Mailbox x%x (x%x/x%x) failed, "
5471 "rc:x%x, status:x%x, add_status:x%x\n",
5472 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5473 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5474 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5475 rc, shdr_status, shdr_add_status);
5476 rc = -ENXIO;
5477 goto out_free_mboxq;
5478 }
5479 cntl_attr = &mbx_cntl_attr->cntl_attr;
5480 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5481 phba->sli4_hba.lnk_info.lnk_tp =
5482 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5483 phba->sli4_hba.lnk_info.lnk_no =
5484 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5485 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5486 "3086 lnk_type:%d, lnk_numb:%d\n",
5487 phba->sli4_hba.lnk_info.lnk_tp,
5488 phba->sli4_hba.lnk_info.lnk_no);
5489
5490retrieve_ppname:
5491 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5492 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5493 sizeof(struct lpfc_mbx_get_port_name) -
5494 sizeof(struct lpfc_sli4_cfg_mhdr),
5495 LPFC_SLI4_MBX_EMBED);
5496 get_port_name = &mboxq->u.mqe.un.get_port_name;
5497 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5498 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5499 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5500 phba->sli4_hba.lnk_info.lnk_tp);
5501 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5502 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5503 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5504 if (shdr_status || shdr_add_status || rc) {
5505 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5506 "3087 Mailbox x%x (x%x/x%x) failed: "
5507 "rc:x%x, status:x%x, add_status:x%x\n",
5508 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5509 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5510 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5511 rc, shdr_status, shdr_add_status);
5512 rc = -ENXIO;
5513 goto out_free_mboxq;
5514 }
5515 switch (phba->sli4_hba.lnk_info.lnk_no) {
5516 case LPFC_LINK_NUMBER_0:
5517 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5518 &get_port_name->u.response);
5519 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5520 break;
5521 case LPFC_LINK_NUMBER_1:
5522 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5523 &get_port_name->u.response);
5524 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5525 break;
5526 case LPFC_LINK_NUMBER_2:
5527 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5528 &get_port_name->u.response);
5529 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5530 break;
5531 case LPFC_LINK_NUMBER_3:
5532 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5533 &get_port_name->u.response);
5534 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5535 break;
5536 default:
5537 break;
5538 }
5539
5540 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5541 phba->Port[0] = cport_name;
5542 phba->Port[1] = '\0';
5543 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5544 "3091 SLI get port name: %s\n", phba->Port);
5545 }
5546
5547out_free_mboxq:
5548 if (rc != MBX_TIMEOUT) {
5549 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5550 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5551 else
5552 mempool_free(mboxq, phba->mbox_mem_pool);
5553 }
5554 return rc;
5555}
5556
5557/**
James Smartda0436e2009-05-22 14:51:39 -04005558 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5559 * @phba: pointer to lpfc hba data structure.
5560 *
5561 * This routine is called to explicitly arm the SLI4 device's completion and
5562 * event queues
5563 **/
5564static void
5565lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5566{
James Smart895427b2017-02-12 13:52:30 -08005567 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005568 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartcdb42be2019-01-28 11:14:21 -08005569 struct lpfc_sli4_hdw_queue *qp;
James Smartda0436e2009-05-22 14:51:39 -04005570
James Smartb71413d2018-02-22 08:18:40 -08005571 sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5572 sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5573 if (sli4_hba->nvmels_cq)
5574 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
James Smart895427b2017-02-12 13:52:30 -08005575 LPFC_QUEUE_REARM);
5576
James Smartcdb42be2019-01-28 11:14:21 -08005577 qp = sli4_hba->hdwq;
5578 if (sli4_hba->hdwq) {
5579 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
5580 sli4_hba->sli4_cq_release(qp[qidx].fcp_cq,
James Smart895427b2017-02-12 13:52:30 -08005581 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08005582 sli4_hba->sli4_cq_release(qp[qidx].nvme_cq,
James Smart895427b2017-02-12 13:52:30 -08005583 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08005584 }
James Smart1ba981f2014-02-20 09:56:45 -05005585
James Smartcdb42be2019-01-28 11:14:21 -08005586 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++)
5587 sli4_hba->sli4_eq_release(qp[qidx].hba_eq,
5588 LPFC_QUEUE_REARM);
5589 }
James Smart1ba981f2014-02-20 09:56:45 -05005590
James Smart2d7dbc42017-02-12 13:52:35 -08005591 if (phba->nvmet_support) {
5592 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smartb71413d2018-02-22 08:18:40 -08005593 sli4_hba->sli4_cq_release(
5594 sli4_hba->nvmet_cqset[qidx],
James Smart2d7dbc42017-02-12 13:52:35 -08005595 LPFC_QUEUE_REARM);
5596 }
James Smart2e90f4b2011-12-13 13:22:37 -05005597 }
James Smartda0436e2009-05-22 14:51:39 -04005598}
5599
5600/**
James Smart6d368e52011-05-24 11:44:12 -04005601 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5602 * @phba: Pointer to HBA context object.
5603 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005604 * @extnt_count: buffer to hold port available extent count.
5605 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005606 *
James Smartb76f2dc2011-07-22 18:37:42 -04005607 * This function calls the port and retrievs the number of available
5608 * extents and their size for a particular extent type.
5609 *
5610 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005611 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005612int
James Smart6d368e52011-05-24 11:44:12 -04005613lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5614 uint16_t *extnt_count, uint16_t *extnt_size)
5615{
5616 int rc = 0;
5617 uint32_t length;
5618 uint32_t mbox_tmo;
5619 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5620 LPFC_MBOXQ_t *mbox;
5621
5622 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5623 if (!mbox)
5624 return -ENOMEM;
5625
5626 /* Find out how many extents are available for this resource type */
5627 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5628 sizeof(struct lpfc_sli4_cfg_mhdr));
5629 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5630 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5631 length, LPFC_SLI4_MBX_EMBED);
5632
5633 /* Send an extents count of 0 - the GET doesn't use it. */
5634 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5635 LPFC_SLI4_MBX_EMBED);
5636 if (unlikely(rc)) {
5637 rc = -EIO;
5638 goto err_exit;
5639 }
5640
5641 if (!phba->sli4_hba.intr_enable)
5642 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5643 else {
James Smarta183a152011-10-10 21:32:43 -04005644 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005645 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5646 }
5647 if (unlikely(rc)) {
5648 rc = -EIO;
5649 goto err_exit;
5650 }
5651
5652 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5653 if (bf_get(lpfc_mbox_hdr_status,
5654 &rsrc_info->header.cfg_shdr.response)) {
5655 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5656 "2930 Failed to get resource extents "
5657 "Status 0x%x Add'l Status 0x%x\n",
5658 bf_get(lpfc_mbox_hdr_status,
5659 &rsrc_info->header.cfg_shdr.response),
5660 bf_get(lpfc_mbox_hdr_add_status,
5661 &rsrc_info->header.cfg_shdr.response));
5662 rc = -EIO;
5663 goto err_exit;
5664 }
5665
5666 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5667 &rsrc_info->u.rsp);
5668 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5669 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005670
5671 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5672 "3162 Retrieved extents type-%d from port: count:%d, "
5673 "size:%d\n", type, *extnt_count, *extnt_size);
5674
5675err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005676 mempool_free(mbox, phba->mbox_mem_pool);
5677 return rc;
5678}
5679
5680/**
5681 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5682 * @phba: Pointer to HBA context object.
5683 * @type: The extent type to check.
5684 *
5685 * This function reads the current available extents from the port and checks
5686 * if the extent count or extent size has changed since the last access.
5687 * Callers use this routine post port reset to understand if there is a
5688 * extent reprovisioning requirement.
5689 *
5690 * Returns:
5691 * -Error: error indicates problem.
5692 * 1: Extent count or size has changed.
5693 * 0: No changes.
5694 **/
5695static int
5696lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5697{
5698 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5699 uint16_t size_diff, rsrc_ext_size;
5700 int rc = 0;
5701 struct lpfc_rsrc_blks *rsrc_entry;
5702 struct list_head *rsrc_blk_list = NULL;
5703
5704 size_diff = 0;
5705 curr_ext_cnt = 0;
5706 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5707 &rsrc_ext_cnt,
5708 &rsrc_ext_size);
5709 if (unlikely(rc))
5710 return -EIO;
5711
5712 switch (type) {
5713 case LPFC_RSC_TYPE_FCOE_RPI:
5714 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5715 break;
5716 case LPFC_RSC_TYPE_FCOE_VPI:
5717 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5718 break;
5719 case LPFC_RSC_TYPE_FCOE_XRI:
5720 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5721 break;
5722 case LPFC_RSC_TYPE_FCOE_VFI:
5723 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5724 break;
5725 default:
5726 break;
5727 }
5728
5729 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5730 curr_ext_cnt++;
5731 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5732 size_diff++;
5733 }
5734
5735 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5736 rc = 1;
5737
5738 return rc;
5739}
5740
5741/**
5742 * lpfc_sli4_cfg_post_extnts -
5743 * @phba: Pointer to HBA context object.
5744 * @extnt_cnt - number of available extents.
5745 * @type - the extent type (rpi, xri, vfi, vpi).
5746 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5747 * @mbox - pointer to the caller's allocated mailbox structure.
5748 *
5749 * This function executes the extents allocation request. It also
5750 * takes care of the amount of memory needed to allocate or get the
5751 * allocated extents. It is the caller's responsibility to evaluate
5752 * the response.
5753 *
5754 * Returns:
5755 * -Error: Error value describes the condition found.
5756 * 0: if successful
5757 **/
5758static int
James Smart8a9d2e82012-05-09 21:16:12 -04005759lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005760 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5761{
5762 int rc = 0;
5763 uint32_t req_len;
5764 uint32_t emb_len;
5765 uint32_t alloc_len, mbox_tmo;
5766
5767 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005768 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005769
5770 /*
5771 * Calculate the size of an embedded mailbox. The uint32_t
5772 * accounts for extents-specific word.
5773 */
5774 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5775 sizeof(uint32_t);
5776
5777 /*
5778 * Presume the allocation and response will fit into an embedded
5779 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5780 */
5781 *emb = LPFC_SLI4_MBX_EMBED;
5782 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005783 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005784 sizeof(union lpfc_sli4_cfg_shdr) +
5785 sizeof(uint32_t);
5786 *emb = LPFC_SLI4_MBX_NEMBED;
5787 }
5788
5789 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5790 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5791 req_len, *emb);
5792 if (alloc_len < req_len) {
5793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005794 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005795 "less than the requested DMA memory "
5796 "size (x%x)\n", alloc_len, req_len);
5797 return -ENOMEM;
5798 }
James Smart8a9d2e82012-05-09 21:16:12 -04005799 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005800 if (unlikely(rc))
5801 return -EIO;
5802
5803 if (!phba->sli4_hba.intr_enable)
5804 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5805 else {
James Smarta183a152011-10-10 21:32:43 -04005806 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005807 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5808 }
5809
5810 if (unlikely(rc))
5811 rc = -EIO;
5812 return rc;
5813}
5814
5815/**
5816 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5817 * @phba: Pointer to HBA context object.
5818 * @type: The resource extent type to allocate.
5819 *
5820 * This function allocates the number of elements for the specified
5821 * resource type.
5822 **/
5823static int
5824lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5825{
5826 bool emb = false;
5827 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5828 uint16_t rsrc_id, rsrc_start, j, k;
5829 uint16_t *ids;
5830 int i, rc;
5831 unsigned long longs;
5832 unsigned long *bmask;
5833 struct lpfc_rsrc_blks *rsrc_blks;
5834 LPFC_MBOXQ_t *mbox;
5835 uint32_t length;
5836 struct lpfc_id_range *id_array = NULL;
5837 void *virtaddr = NULL;
5838 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5839 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5840 struct list_head *ext_blk_list;
5841
5842 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5843 &rsrc_cnt,
5844 &rsrc_size);
5845 if (unlikely(rc))
5846 return -EIO;
5847
5848 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5849 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5850 "3009 No available Resource Extents "
5851 "for resource type 0x%x: Count: 0x%x, "
5852 "Size 0x%x\n", type, rsrc_cnt,
5853 rsrc_size);
5854 return -ENOMEM;
5855 }
5856
James Smart8a9d2e82012-05-09 21:16:12 -04005857 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5858 "2903 Post resource extents type-0x%x: "
5859 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005860
5861 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5862 if (!mbox)
5863 return -ENOMEM;
5864
James Smart8a9d2e82012-05-09 21:16:12 -04005865 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005866 if (unlikely(rc)) {
5867 rc = -EIO;
5868 goto err_exit;
5869 }
5870
5871 /*
5872 * Figure out where the response is located. Then get local pointers
5873 * to the response data. The port does not guarantee to respond to
5874 * all extents counts request so update the local variable with the
5875 * allocated count from the port.
5876 */
5877 if (emb == LPFC_SLI4_MBX_EMBED) {
5878 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5879 id_array = &rsrc_ext->u.rsp.id[0];
5880 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5881 } else {
5882 virtaddr = mbox->sge_array->addr[0];
5883 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5884 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5885 id_array = &n_rsrc->id;
5886 }
5887
5888 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5889 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5890
5891 /*
5892 * Based on the resource size and count, correct the base and max
5893 * resource values.
5894 */
5895 length = sizeof(struct lpfc_rsrc_blks);
5896 switch (type) {
5897 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005898 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005899 sizeof(unsigned long),
5900 GFP_KERNEL);
5901 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5902 rc = -ENOMEM;
5903 goto err_exit;
5904 }
Kees Cook6396bb22018-06-12 14:03:40 -07005905 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005906 sizeof(uint16_t),
5907 GFP_KERNEL);
5908 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5909 kfree(phba->sli4_hba.rpi_bmask);
5910 rc = -ENOMEM;
5911 goto err_exit;
5912 }
5913
5914 /*
5915 * The next_rpi was initialized with the maximum available
5916 * count but the port may allocate a smaller number. Catch
5917 * that case and update the next_rpi.
5918 */
5919 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5920
5921 /* Initialize local ptrs for common extent processing later. */
5922 bmask = phba->sli4_hba.rpi_bmask;
5923 ids = phba->sli4_hba.rpi_ids;
5924 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5925 break;
5926 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005927 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005928 GFP_KERNEL);
5929 if (unlikely(!phba->vpi_bmask)) {
5930 rc = -ENOMEM;
5931 goto err_exit;
5932 }
Kees Cook6396bb22018-06-12 14:03:40 -07005933 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04005934 GFP_KERNEL);
5935 if (unlikely(!phba->vpi_ids)) {
5936 kfree(phba->vpi_bmask);
5937 rc = -ENOMEM;
5938 goto err_exit;
5939 }
5940
5941 /* Initialize local ptrs for common extent processing later. */
5942 bmask = phba->vpi_bmask;
5943 ids = phba->vpi_ids;
5944 ext_blk_list = &phba->lpfc_vpi_blk_list;
5945 break;
5946 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07005947 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005948 sizeof(unsigned long),
5949 GFP_KERNEL);
5950 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5951 rc = -ENOMEM;
5952 goto err_exit;
5953 }
James Smart8a9d2e82012-05-09 21:16:12 -04005954 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07005955 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005956 sizeof(uint16_t),
5957 GFP_KERNEL);
5958 if (unlikely(!phba->sli4_hba.xri_ids)) {
5959 kfree(phba->sli4_hba.xri_bmask);
5960 rc = -ENOMEM;
5961 goto err_exit;
5962 }
5963
5964 /* Initialize local ptrs for common extent processing later. */
5965 bmask = phba->sli4_hba.xri_bmask;
5966 ids = phba->sli4_hba.xri_ids;
5967 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5968 break;
5969 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07005970 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005971 sizeof(unsigned long),
5972 GFP_KERNEL);
5973 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5974 rc = -ENOMEM;
5975 goto err_exit;
5976 }
Kees Cook6396bb22018-06-12 14:03:40 -07005977 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005978 sizeof(uint16_t),
5979 GFP_KERNEL);
5980 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5981 kfree(phba->sli4_hba.vfi_bmask);
5982 rc = -ENOMEM;
5983 goto err_exit;
5984 }
5985
5986 /* Initialize local ptrs for common extent processing later. */
5987 bmask = phba->sli4_hba.vfi_bmask;
5988 ids = phba->sli4_hba.vfi_ids;
5989 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5990 break;
5991 default:
5992 /* Unsupported Opcode. Fail call. */
5993 id_array = NULL;
5994 bmask = NULL;
5995 ids = NULL;
5996 ext_blk_list = NULL;
5997 goto err_exit;
5998 }
5999
6000 /*
6001 * Complete initializing the extent configuration with the
6002 * allocated ids assigned to this function. The bitmask serves
6003 * as an index into the array and manages the available ids. The
6004 * array just stores the ids communicated to the port via the wqes.
6005 */
6006 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6007 if ((i % 2) == 0)
6008 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6009 &id_array[k]);
6010 else
6011 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6012 &id_array[k]);
6013
6014 rsrc_blks = kzalloc(length, GFP_KERNEL);
6015 if (unlikely(!rsrc_blks)) {
6016 rc = -ENOMEM;
6017 kfree(bmask);
6018 kfree(ids);
6019 goto err_exit;
6020 }
6021 rsrc_blks->rsrc_start = rsrc_id;
6022 rsrc_blks->rsrc_size = rsrc_size;
6023 list_add_tail(&rsrc_blks->list, ext_blk_list);
6024 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08006025 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart5e5b5112019-01-28 11:14:22 -08006026 phba->sli4_hba.io_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08006027 lpfc_sli4_get_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08006028 }
James Smart6d368e52011-05-24 11:44:12 -04006029
6030 while (rsrc_id < (rsrc_start + rsrc_size)) {
6031 ids[j] = rsrc_id;
6032 rsrc_id++;
6033 j++;
6034 }
6035 /* Entire word processed. Get next word.*/
6036 if ((i % 2) == 1)
6037 k++;
6038 }
6039 err_exit:
6040 lpfc_sli4_mbox_cmd_free(phba, mbox);
6041 return rc;
6042}
6043
James Smart895427b2017-02-12 13:52:30 -08006044
6045
James Smart6d368e52011-05-24 11:44:12 -04006046/**
6047 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6048 * @phba: Pointer to HBA context object.
6049 * @type: the extent's type.
6050 *
6051 * This function deallocates all extents of a particular resource type.
6052 * SLI4 does not allow for deallocating a particular extent range. It
6053 * is the caller's responsibility to release all kernel memory resources.
6054 **/
6055static int
6056lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6057{
6058 int rc;
6059 uint32_t length, mbox_tmo = 0;
6060 LPFC_MBOXQ_t *mbox;
6061 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6062 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6063
6064 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6065 if (!mbox)
6066 return -ENOMEM;
6067
6068 /*
6069 * This function sends an embedded mailbox because it only sends the
6070 * the resource type. All extents of this type are released by the
6071 * port.
6072 */
6073 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6074 sizeof(struct lpfc_sli4_cfg_mhdr));
6075 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6076 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6077 length, LPFC_SLI4_MBX_EMBED);
6078
6079 /* Send an extents count of 0 - the dealloc doesn't use it. */
6080 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6081 LPFC_SLI4_MBX_EMBED);
6082 if (unlikely(rc)) {
6083 rc = -EIO;
6084 goto out_free_mbox;
6085 }
6086 if (!phba->sli4_hba.intr_enable)
6087 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6088 else {
James Smarta183a152011-10-10 21:32:43 -04006089 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006090 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6091 }
6092 if (unlikely(rc)) {
6093 rc = -EIO;
6094 goto out_free_mbox;
6095 }
6096
6097 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6098 if (bf_get(lpfc_mbox_hdr_status,
6099 &dealloc_rsrc->header.cfg_shdr.response)) {
6100 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6101 "2919 Failed to release resource extents "
6102 "for type %d - Status 0x%x Add'l Status 0x%x. "
6103 "Resource memory not released.\n",
6104 type,
6105 bf_get(lpfc_mbox_hdr_status,
6106 &dealloc_rsrc->header.cfg_shdr.response),
6107 bf_get(lpfc_mbox_hdr_add_status,
6108 &dealloc_rsrc->header.cfg_shdr.response));
6109 rc = -EIO;
6110 goto out_free_mbox;
6111 }
6112
6113 /* Release kernel memory resources for the specific type. */
6114 switch (type) {
6115 case LPFC_RSC_TYPE_FCOE_VPI:
6116 kfree(phba->vpi_bmask);
6117 kfree(phba->vpi_ids);
6118 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6119 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6120 &phba->lpfc_vpi_blk_list, list) {
6121 list_del_init(&rsrc_blk->list);
6122 kfree(rsrc_blk);
6123 }
James Smart16a3a202013-04-17 20:14:38 -04006124 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006125 break;
6126 case LPFC_RSC_TYPE_FCOE_XRI:
6127 kfree(phba->sli4_hba.xri_bmask);
6128 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006129 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6130 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6131 list_del_init(&rsrc_blk->list);
6132 kfree(rsrc_blk);
6133 }
6134 break;
6135 case LPFC_RSC_TYPE_FCOE_VFI:
6136 kfree(phba->sli4_hba.vfi_bmask);
6137 kfree(phba->sli4_hba.vfi_ids);
6138 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6139 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6140 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6141 list_del_init(&rsrc_blk->list);
6142 kfree(rsrc_blk);
6143 }
6144 break;
6145 case LPFC_RSC_TYPE_FCOE_RPI:
6146 /* RPI bitmask and physical id array are cleaned up earlier. */
6147 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6148 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6149 list_del_init(&rsrc_blk->list);
6150 kfree(rsrc_blk);
6151 }
6152 break;
6153 default:
6154 break;
6155 }
6156
6157 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6158
6159 out_free_mbox:
6160 mempool_free(mbox, phba->mbox_mem_pool);
6161 return rc;
6162}
6163
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006164static void
James Smart7bdedb32016-07-06 12:36:00 -07006165lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6166 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006167{
James Smart65791f12016-07-06 12:35:56 -07006168 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006169
James Smart65791f12016-07-06 12:35:56 -07006170 len = sizeof(struct lpfc_mbx_set_feature) -
6171 sizeof(struct lpfc_sli4_cfg_mhdr);
6172 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6173 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6174 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006175
James Smart7bdedb32016-07-06 12:36:00 -07006176 switch (feature) {
6177 case LPFC_SET_UE_RECOVERY:
6178 bf_set(lpfc_mbx_set_feature_UER,
6179 &mbox->u.mqe.un.set_feature, 1);
6180 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6181 mbox->u.mqe.un.set_feature.param_len = 8;
6182 break;
6183 case LPFC_SET_MDS_DIAGS:
6184 bf_set(lpfc_mbx_set_feature_mds,
6185 &mbox->u.mqe.un.set_feature, 1);
6186 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006187 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006188 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6189 mbox->u.mqe.un.set_feature.param_len = 8;
6190 break;
James Smart65791f12016-07-06 12:35:56 -07006191 }
James Smart7bdedb32016-07-06 12:36:00 -07006192
6193 return;
James Smart65791f12016-07-06 12:35:56 -07006194}
6195
James Smart6d368e52011-05-24 11:44:12 -04006196/**
James Smart1165a5c2018-11-29 16:09:39 -08006197 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6198 * @phba: Pointer to HBA context object.
6199 *
6200 * Disable FW logging into host memory on the adapter. To
6201 * be done before reading logs from the host memory.
6202 **/
6203void
6204lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6205{
6206 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6207
6208 ras_fwlog->ras_active = false;
6209
6210 /* Disable FW logging to host memory */
6211 writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6212 phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6213}
6214
6215/**
James Smartd2cc9bc2018-09-10 10:30:50 -07006216 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6217 * @phba: Pointer to HBA context object.
6218 *
6219 * This function is called to free memory allocated for RAS FW logging
6220 * support in the driver.
6221 **/
6222void
6223lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6224{
6225 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6226 struct lpfc_dmabuf *dmabuf, *next;
6227
6228 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6229 list_for_each_entry_safe(dmabuf, next,
6230 &ras_fwlog->fwlog_buff_list,
6231 list) {
6232 list_del(&dmabuf->list);
6233 dma_free_coherent(&phba->pcidev->dev,
6234 LPFC_RAS_MAX_ENTRY_SIZE,
6235 dmabuf->virt, dmabuf->phys);
6236 kfree(dmabuf);
6237 }
6238 }
6239
6240 if (ras_fwlog->lwpd.virt) {
6241 dma_free_coherent(&phba->pcidev->dev,
6242 sizeof(uint32_t) * 2,
6243 ras_fwlog->lwpd.virt,
6244 ras_fwlog->lwpd.phys);
6245 ras_fwlog->lwpd.virt = NULL;
6246 }
6247
6248 ras_fwlog->ras_active = false;
6249}
6250
6251/**
6252 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6253 * @phba: Pointer to HBA context object.
6254 * @fwlog_buff_count: Count of buffers to be created.
6255 *
6256 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6257 * to update FW log is posted to the adapter.
6258 * Buffer count is calculated based on module param ras_fwlog_buffsize
6259 * Size of each buffer posted to FW is 64K.
6260 **/
6261
6262static int
6263lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6264 uint32_t fwlog_buff_count)
6265{
6266 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6267 struct lpfc_dmabuf *dmabuf;
6268 int rc = 0, i = 0;
6269
6270 /* Initialize List */
6271 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6272
6273 /* Allocate memory for the LWPD */
6274 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6275 sizeof(uint32_t) * 2,
6276 &ras_fwlog->lwpd.phys,
6277 GFP_KERNEL);
6278 if (!ras_fwlog->lwpd.virt) {
James Smartcb349902018-11-29 16:09:27 -08006279 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006280 "6185 LWPD Memory Alloc Failed\n");
6281
6282 return -ENOMEM;
6283 }
6284
6285 ras_fwlog->fw_buffcount = fwlog_buff_count;
6286 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6287 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6288 GFP_KERNEL);
6289 if (!dmabuf) {
6290 rc = -ENOMEM;
6291 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6292 "6186 Memory Alloc failed FW logging");
6293 goto free_mem;
6294 }
6295
Sabyasachi Gupta359d0ac2018-11-18 20:08:48 +05306296 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
James Smartd2cc9bc2018-09-10 10:30:50 -07006297 LPFC_RAS_MAX_ENTRY_SIZE,
6298 &dmabuf->phys,
6299 GFP_KERNEL);
6300 if (!dmabuf->virt) {
6301 kfree(dmabuf);
6302 rc = -ENOMEM;
6303 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6304 "6187 DMA Alloc Failed FW logging");
6305 goto free_mem;
6306 }
James Smartd2cc9bc2018-09-10 10:30:50 -07006307 dmabuf->buffer_tag = i;
6308 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6309 }
6310
6311free_mem:
6312 if (rc)
6313 lpfc_sli4_ras_dma_free(phba);
6314
6315 return rc;
6316}
6317
6318/**
6319 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6320 * @phba: pointer to lpfc hba data structure.
6321 * @pmboxq: pointer to the driver internal queue element for mailbox command.
6322 *
6323 * Completion handler for driver's RAS MBX command to the device.
6324 **/
6325static void
6326lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6327{
6328 MAILBOX_t *mb;
6329 union lpfc_sli4_cfg_shdr *shdr;
6330 uint32_t shdr_status, shdr_add_status;
6331 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6332
6333 mb = &pmb->u.mb;
6334
6335 shdr = (union lpfc_sli4_cfg_shdr *)
6336 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6337 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6338 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6339
6340 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
James Smartcb349902018-11-29 16:09:27 -08006341 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smartd2cc9bc2018-09-10 10:30:50 -07006342 "6188 FW LOG mailbox "
6343 "completed with status x%x add_status x%x,"
6344 " mbx status x%x\n",
6345 shdr_status, shdr_add_status, mb->mbxStatus);
James Smartcb349902018-11-29 16:09:27 -08006346
6347 ras_fwlog->ras_hwsupport = false;
James Smartd2cc9bc2018-09-10 10:30:50 -07006348 goto disable_ras;
6349 }
6350
6351 ras_fwlog->ras_active = true;
6352 mempool_free(pmb, phba->mbox_mem_pool);
6353
6354 return;
6355
6356disable_ras:
6357 /* Free RAS DMA memory */
6358 lpfc_sli4_ras_dma_free(phba);
6359 mempool_free(pmb, phba->mbox_mem_pool);
6360}
6361
6362/**
6363 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6364 * @phba: pointer to lpfc hba data structure.
6365 * @fwlog_level: Logging verbosity level.
6366 * @fwlog_enable: Enable/Disable logging.
6367 *
6368 * Initialize memory and post mailbox command to enable FW logging in host
6369 * memory.
6370 **/
6371int
6372lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6373 uint32_t fwlog_level,
6374 uint32_t fwlog_enable)
6375{
6376 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6377 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6378 struct lpfc_dmabuf *dmabuf;
6379 LPFC_MBOXQ_t *mbox;
6380 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6381 int rc = 0;
6382
6383 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6384 phba->cfg_ras_fwlog_buffsize);
6385 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6386
6387 /*
6388 * If re-enabling FW logging support use earlier allocated
6389 * DMA buffers while posting MBX command.
6390 **/
6391 if (!ras_fwlog->lwpd.virt) {
6392 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6393 if (rc) {
6394 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smartcb349902018-11-29 16:09:27 -08006395 "6189 FW Log Memory Allocation Failed");
James Smartd2cc9bc2018-09-10 10:30:50 -07006396 return rc;
6397 }
6398 }
6399
6400 /* Setup Mailbox command */
6401 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6402 if (!mbox) {
James Smartcb349902018-11-29 16:09:27 -08006403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006404 "6190 RAS MBX Alloc Failed");
6405 rc = -ENOMEM;
6406 goto mem_free;
6407 }
6408
6409 ras_fwlog->fw_loglevel = fwlog_level;
6410 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6411 sizeof(struct lpfc_sli4_cfg_mhdr));
6412
6413 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6414 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6415 len, LPFC_SLI4_MBX_EMBED);
6416
6417 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6418 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6419 fwlog_enable);
6420 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6421 ras_fwlog->fw_loglevel);
6422 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6423 ras_fwlog->fw_buffcount);
6424 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6425 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6426
6427 /* Update DMA buffer address */
6428 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6429 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6430
6431 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6432 putPaddrLow(dmabuf->phys);
6433
6434 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6435 putPaddrHigh(dmabuf->phys);
6436 }
6437
6438 /* Update LPWD address */
6439 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6440 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6441
6442 mbox->vport = phba->pport;
6443 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6444
6445 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6446
6447 if (rc == MBX_NOT_FINISHED) {
James Smartcb349902018-11-29 16:09:27 -08006448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6449 "6191 FW-Log Mailbox failed. "
James Smartd2cc9bc2018-09-10 10:30:50 -07006450 "status %d mbxStatus : x%x", rc,
6451 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6452 mempool_free(mbox, phba->mbox_mem_pool);
6453 rc = -EIO;
6454 goto mem_free;
6455 } else
6456 rc = 0;
6457mem_free:
6458 if (rc)
6459 lpfc_sli4_ras_dma_free(phba);
6460
6461 return rc;
6462}
6463
6464/**
6465 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6466 * @phba: Pointer to HBA context object.
6467 *
6468 * Check if RAS is supported on the adapter and initialize it.
6469 **/
6470void
6471lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6472{
6473 /* Check RAS FW Log needs to be enabled or not */
6474 if (lpfc_check_fwlog_support(phba))
6475 return;
6476
6477 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6478 LPFC_RAS_ENABLE_LOGGING);
6479}
6480
6481/**
James Smart6d368e52011-05-24 11:44:12 -04006482 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6483 * @phba: Pointer to HBA context object.
6484 *
6485 * This function allocates all SLI4 resource identifiers.
6486 **/
6487int
6488lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6489{
6490 int i, rc, error = 0;
6491 uint16_t count, base;
6492 unsigned long longs;
6493
James Smartff78d8f2011-12-13 13:21:35 -05006494 if (!phba->sli4_hba.rpi_hdrs_in_use)
6495 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006496 if (phba->sli4_hba.extents_in_use) {
6497 /*
6498 * The port supports resource extents. The XRI, VPI, VFI, RPI
6499 * resource extent count must be read and allocated before
6500 * provisioning the resource id arrays.
6501 */
6502 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6503 LPFC_IDX_RSRC_RDY) {
6504 /*
6505 * Extent-based resources are set - the driver could
6506 * be in a port reset. Figure out if any corrective
6507 * actions need to be taken.
6508 */
6509 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6510 LPFC_RSC_TYPE_FCOE_VFI);
6511 if (rc != 0)
6512 error++;
6513 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6514 LPFC_RSC_TYPE_FCOE_VPI);
6515 if (rc != 0)
6516 error++;
6517 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6518 LPFC_RSC_TYPE_FCOE_XRI);
6519 if (rc != 0)
6520 error++;
6521 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6522 LPFC_RSC_TYPE_FCOE_RPI);
6523 if (rc != 0)
6524 error++;
6525
6526 /*
6527 * It's possible that the number of resources
6528 * provided to this port instance changed between
6529 * resets. Detect this condition and reallocate
6530 * resources. Otherwise, there is no action.
6531 */
6532 if (error) {
6533 lpfc_printf_log(phba, KERN_INFO,
6534 LOG_MBOX | LOG_INIT,
6535 "2931 Detected extent resource "
6536 "change. Reallocating all "
6537 "extents.\n");
6538 rc = lpfc_sli4_dealloc_extent(phba,
6539 LPFC_RSC_TYPE_FCOE_VFI);
6540 rc = lpfc_sli4_dealloc_extent(phba,
6541 LPFC_RSC_TYPE_FCOE_VPI);
6542 rc = lpfc_sli4_dealloc_extent(phba,
6543 LPFC_RSC_TYPE_FCOE_XRI);
6544 rc = lpfc_sli4_dealloc_extent(phba,
6545 LPFC_RSC_TYPE_FCOE_RPI);
6546 } else
6547 return 0;
6548 }
6549
6550 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6551 if (unlikely(rc))
6552 goto err_exit;
6553
6554 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6555 if (unlikely(rc))
6556 goto err_exit;
6557
6558 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6559 if (unlikely(rc))
6560 goto err_exit;
6561
6562 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6563 if (unlikely(rc))
6564 goto err_exit;
6565 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6566 LPFC_IDX_RSRC_RDY);
6567 return rc;
6568 } else {
6569 /*
6570 * The port does not support resource extents. The XRI, VPI,
6571 * VFI, RPI resource ids were determined from READ_CONFIG.
6572 * Just allocate the bitmasks and provision the resource id
6573 * arrays. If a port reset is active, the resources don't
6574 * need any action - just exit.
6575 */
6576 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006577 LPFC_IDX_RSRC_RDY) {
6578 lpfc_sli4_dealloc_resource_identifiers(phba);
6579 lpfc_sli4_remove_rpis(phba);
6580 }
James Smart6d368e52011-05-24 11:44:12 -04006581 /* RPIs. */
6582 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006583 if (count <= 0) {
6584 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6585 "3279 Invalid provisioning of "
6586 "rpi:%d\n", count);
6587 rc = -EINVAL;
6588 goto err_exit;
6589 }
James Smart6d368e52011-05-24 11:44:12 -04006590 base = phba->sli4_hba.max_cfg_param.rpi_base;
6591 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006592 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006593 sizeof(unsigned long),
6594 GFP_KERNEL);
6595 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6596 rc = -ENOMEM;
6597 goto err_exit;
6598 }
Kees Cook6396bb22018-06-12 14:03:40 -07006599 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006600 GFP_KERNEL);
6601 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6602 rc = -ENOMEM;
6603 goto free_rpi_bmask;
6604 }
6605
6606 for (i = 0; i < count; i++)
6607 phba->sli4_hba.rpi_ids[i] = base + i;
6608
6609 /* VPIs. */
6610 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006611 if (count <= 0) {
6612 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6613 "3280 Invalid provisioning of "
6614 "vpi:%d\n", count);
6615 rc = -EINVAL;
6616 goto free_rpi_ids;
6617 }
James Smart6d368e52011-05-24 11:44:12 -04006618 base = phba->sli4_hba.max_cfg_param.vpi_base;
6619 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006620 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006621 GFP_KERNEL);
6622 if (unlikely(!phba->vpi_bmask)) {
6623 rc = -ENOMEM;
6624 goto free_rpi_ids;
6625 }
Kees Cook6396bb22018-06-12 14:03:40 -07006626 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006627 GFP_KERNEL);
6628 if (unlikely(!phba->vpi_ids)) {
6629 rc = -ENOMEM;
6630 goto free_vpi_bmask;
6631 }
6632
6633 for (i = 0; i < count; i++)
6634 phba->vpi_ids[i] = base + i;
6635
6636 /* XRIs. */
6637 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006638 if (count <= 0) {
6639 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6640 "3281 Invalid provisioning of "
6641 "xri:%d\n", count);
6642 rc = -EINVAL;
6643 goto free_vpi_ids;
6644 }
James Smart6d368e52011-05-24 11:44:12 -04006645 base = phba->sli4_hba.max_cfg_param.xri_base;
6646 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006647 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006648 sizeof(unsigned long),
6649 GFP_KERNEL);
6650 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6651 rc = -ENOMEM;
6652 goto free_vpi_ids;
6653 }
James Smart41899be2012-03-01 22:34:19 -05006654 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006655 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006656 GFP_KERNEL);
6657 if (unlikely(!phba->sli4_hba.xri_ids)) {
6658 rc = -ENOMEM;
6659 goto free_xri_bmask;
6660 }
6661
6662 for (i = 0; i < count; i++)
6663 phba->sli4_hba.xri_ids[i] = base + i;
6664
6665 /* VFIs. */
6666 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006667 if (count <= 0) {
6668 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669 "3282 Invalid provisioning of "
6670 "vfi:%d\n", count);
6671 rc = -EINVAL;
6672 goto free_xri_ids;
6673 }
James Smart6d368e52011-05-24 11:44:12 -04006674 base = phba->sli4_hba.max_cfg_param.vfi_base;
6675 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006676 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006677 sizeof(unsigned long),
6678 GFP_KERNEL);
6679 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6680 rc = -ENOMEM;
6681 goto free_xri_ids;
6682 }
Kees Cook6396bb22018-06-12 14:03:40 -07006683 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006684 GFP_KERNEL);
6685 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6686 rc = -ENOMEM;
6687 goto free_vfi_bmask;
6688 }
6689
6690 for (i = 0; i < count; i++)
6691 phba->sli4_hba.vfi_ids[i] = base + i;
6692
6693 /*
6694 * Mark all resources ready. An HBA reset doesn't need
6695 * to reset the initialization.
6696 */
6697 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6698 LPFC_IDX_RSRC_RDY);
6699 return 0;
6700 }
6701
6702 free_vfi_bmask:
6703 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006704 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006705 free_xri_ids:
6706 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006707 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006708 free_xri_bmask:
6709 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006710 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006711 free_vpi_ids:
6712 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006713 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006714 free_vpi_bmask:
6715 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006716 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006717 free_rpi_ids:
6718 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006719 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006720 free_rpi_bmask:
6721 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006722 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006723 err_exit:
6724 return rc;
6725}
6726
6727/**
6728 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6729 * @phba: Pointer to HBA context object.
6730 *
6731 * This function allocates the number of elements for the specified
6732 * resource type.
6733 **/
6734int
6735lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6736{
6737 if (phba->sli4_hba.extents_in_use) {
6738 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6739 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6740 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6741 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6742 } else {
6743 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006744 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006745 kfree(phba->vpi_ids);
6746 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6747 kfree(phba->sli4_hba.xri_bmask);
6748 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006749 kfree(phba->sli4_hba.vfi_bmask);
6750 kfree(phba->sli4_hba.vfi_ids);
6751 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6752 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6753 }
6754
6755 return 0;
6756}
6757
6758/**
James Smartb76f2dc2011-07-22 18:37:42 -04006759 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6760 * @phba: Pointer to HBA context object.
6761 * @type: The resource extent type.
6762 * @extnt_count: buffer to hold port extent count response
6763 * @extnt_size: buffer to hold port extent size response.
6764 *
6765 * This function calls the port to read the host allocated extents
6766 * for a particular type.
6767 **/
6768int
6769lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6770 uint16_t *extnt_cnt, uint16_t *extnt_size)
6771{
6772 bool emb;
6773 int rc = 0;
6774 uint16_t curr_blks = 0;
6775 uint32_t req_len, emb_len;
6776 uint32_t alloc_len, mbox_tmo;
6777 struct list_head *blk_list_head;
6778 struct lpfc_rsrc_blks *rsrc_blk;
6779 LPFC_MBOXQ_t *mbox;
6780 void *virtaddr = NULL;
6781 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6782 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6783 union lpfc_sli4_cfg_shdr *shdr;
6784
6785 switch (type) {
6786 case LPFC_RSC_TYPE_FCOE_VPI:
6787 blk_list_head = &phba->lpfc_vpi_blk_list;
6788 break;
6789 case LPFC_RSC_TYPE_FCOE_XRI:
6790 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6791 break;
6792 case LPFC_RSC_TYPE_FCOE_VFI:
6793 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6794 break;
6795 case LPFC_RSC_TYPE_FCOE_RPI:
6796 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6797 break;
6798 default:
6799 return -EIO;
6800 }
6801
6802 /* Count the number of extents currently allocatd for this type. */
6803 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6804 if (curr_blks == 0) {
6805 /*
6806 * The GET_ALLOCATED mailbox does not return the size,
6807 * just the count. The size should be just the size
6808 * stored in the current allocated block and all sizes
6809 * for an extent type are the same so set the return
6810 * value now.
6811 */
6812 *extnt_size = rsrc_blk->rsrc_size;
6813 }
6814 curr_blks++;
6815 }
6816
James Smartb76f2dc2011-07-22 18:37:42 -04006817 /*
6818 * Calculate the size of an embedded mailbox. The uint32_t
6819 * accounts for extents-specific word.
6820 */
6821 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6822 sizeof(uint32_t);
6823
6824 /*
6825 * Presume the allocation and response will fit into an embedded
6826 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6827 */
6828 emb = LPFC_SLI4_MBX_EMBED;
6829 req_len = emb_len;
6830 if (req_len > emb_len) {
6831 req_len = curr_blks * sizeof(uint16_t) +
6832 sizeof(union lpfc_sli4_cfg_shdr) +
6833 sizeof(uint32_t);
6834 emb = LPFC_SLI4_MBX_NEMBED;
6835 }
6836
6837 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6838 if (!mbox)
6839 return -ENOMEM;
6840 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6841
6842 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6843 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6844 req_len, emb);
6845 if (alloc_len < req_len) {
6846 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6847 "2983 Allocated DMA memory size (x%x) is "
6848 "less than the requested DMA memory "
6849 "size (x%x)\n", alloc_len, req_len);
6850 rc = -ENOMEM;
6851 goto err_exit;
6852 }
6853 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6854 if (unlikely(rc)) {
6855 rc = -EIO;
6856 goto err_exit;
6857 }
6858
6859 if (!phba->sli4_hba.intr_enable)
6860 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6861 else {
James Smarta183a152011-10-10 21:32:43 -04006862 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006863 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6864 }
6865
6866 if (unlikely(rc)) {
6867 rc = -EIO;
6868 goto err_exit;
6869 }
6870
6871 /*
6872 * Figure out where the response is located. Then get local pointers
6873 * to the response data. The port does not guarantee to respond to
6874 * all extents counts request so update the local variable with the
6875 * allocated count from the port.
6876 */
6877 if (emb == LPFC_SLI4_MBX_EMBED) {
6878 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6879 shdr = &rsrc_ext->header.cfg_shdr;
6880 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6881 } else {
6882 virtaddr = mbox->sge_array->addr[0];
6883 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6884 shdr = &n_rsrc->cfg_shdr;
6885 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6886 }
6887
6888 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6889 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6890 "2984 Failed to read allocated resources "
6891 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6892 type,
6893 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6894 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6895 rc = -EIO;
6896 goto err_exit;
6897 }
6898 err_exit:
6899 lpfc_sli4_mbox_cmd_free(phba, mbox);
6900 return rc;
6901}
6902
6903/**
James Smart0ef69962017-04-21 16:04:50 -07006904 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006905 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006906 * @pring: Pointer to driver SLI ring object.
6907 * @sgl_list: linked link of sgl buffers to post
6908 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006909 *
James Smart895427b2017-02-12 13:52:30 -08006910 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006911 * repost them to the port by using SGL block post. This is needed after a
6912 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006913 * of buffer sgls which contains contiguous xris and uses the non-embedded
6914 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006915 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6916 * mailbox command for posting.
6917 *
6918 * Returns: 0 = success, non-zero failure.
6919 **/
6920static int
James Smart895427b2017-02-12 13:52:30 -08006921lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6922 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006923{
6924 struct lpfc_sglq *sglq_entry = NULL;
6925 struct lpfc_sglq *sglq_entry_next = NULL;
6926 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006927 int status, total_cnt;
6928 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006929 int last_xritag = NO_XRI;
6930 LIST_HEAD(prep_sgl_list);
6931 LIST_HEAD(blck_sgl_list);
6932 LIST_HEAD(allc_sgl_list);
6933 LIST_HEAD(post_sgl_list);
6934 LIST_HEAD(free_sgl_list);
6935
James Smart38c20672013-03-01 16:37:44 -05006936 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006937 spin_lock(&phba->sli4_hba.sgl_list_lock);
6938 list_splice_init(sgl_list, &allc_sgl_list);
6939 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006940 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006941
James Smart895427b2017-02-12 13:52:30 -08006942 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006943 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6944 &allc_sgl_list, list) {
6945 list_del_init(&sglq_entry->list);
6946 block_cnt++;
6947 if ((last_xritag != NO_XRI) &&
6948 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6949 /* a hole in xri block, form a sgl posting block */
6950 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6951 post_cnt = block_cnt - 1;
6952 /* prepare list for next posting block */
6953 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6954 block_cnt = 1;
6955 } else {
6956 /* prepare list for next posting block */
6957 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6958 /* enough sgls for non-embed sgl mbox command */
6959 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6960 list_splice_init(&prep_sgl_list,
6961 &blck_sgl_list);
6962 post_cnt = block_cnt;
6963 block_cnt = 0;
6964 }
6965 }
6966 num_posted++;
6967
6968 /* keep track of last sgl's xritag */
6969 last_xritag = sglq_entry->sli4_xritag;
6970
James Smart895427b2017-02-12 13:52:30 -08006971 /* end of repost sgl list condition for buffers */
6972 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006973 if (post_cnt == 0) {
6974 list_splice_init(&prep_sgl_list,
6975 &blck_sgl_list);
6976 post_cnt = block_cnt;
6977 } else if (block_cnt == 1) {
6978 status = lpfc_sli4_post_sgl(phba,
6979 sglq_entry->phys, 0,
6980 sglq_entry->sli4_xritag);
6981 if (!status) {
6982 /* successful, put sgl to posted list */
6983 list_add_tail(&sglq_entry->list,
6984 &post_sgl_list);
6985 } else {
6986 /* Failure, put sgl to free list */
6987 lpfc_printf_log(phba, KERN_WARNING,
6988 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006989 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006990 "sgl, xritag:x%x\n",
6991 sglq_entry->sli4_xritag);
6992 list_add_tail(&sglq_entry->list,
6993 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006994 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006995 }
6996 }
6997 }
6998
6999 /* continue until a nembed page worth of sgls */
7000 if (post_cnt == 0)
7001 continue;
7002
James Smart895427b2017-02-12 13:52:30 -08007003 /* post the buffer list sgls as a block */
7004 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7005 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04007006
7007 if (!status) {
7008 /* success, put sgl list to posted sgl list */
7009 list_splice_init(&blck_sgl_list, &post_sgl_list);
7010 } else {
7011 /* Failure, put sgl list to free sgl list */
7012 sglq_entry_first = list_first_entry(&blck_sgl_list,
7013 struct lpfc_sglq,
7014 list);
7015 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007016 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04007017 "xritag:x%x-x%x\n",
7018 sglq_entry_first->sli4_xritag,
7019 (sglq_entry_first->sli4_xritag +
7020 post_cnt - 1));
7021 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007022 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007023 }
7024
7025 /* don't reset xirtag due to hole in xri block */
7026 if (block_cnt == 0)
7027 last_xritag = NO_XRI;
7028
James Smart895427b2017-02-12 13:52:30 -08007029 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04007030 post_cnt = 0;
7031 }
7032
James Smart895427b2017-02-12 13:52:30 -08007033 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04007034 lpfc_free_sgl_list(phba, &free_sgl_list);
7035
James Smart895427b2017-02-12 13:52:30 -08007036 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04007037 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05007038 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007039 spin_lock(&phba->sli4_hba.sgl_list_lock);
7040 list_splice_init(&post_sgl_list, sgl_list);
7041 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007042 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007043 } else {
7044 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007045 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04007046 return -EIO;
7047 }
James Smart895427b2017-02-12 13:52:30 -08007048
7049 /* return the number of XRIs actually posted */
7050 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007051}
7052
James Smart0794d602019-01-28 11:14:19 -08007053/**
James Smart5e5b5112019-01-28 11:14:22 -08007054 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
James Smart0794d602019-01-28 11:14:19 -08007055 * @phba: pointer to lpfc hba data structure.
7056 *
7057 * This routine walks the list of nvme buffers that have been allocated and
7058 * repost them to the port by using SGL block post. This is needed after a
7059 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7060 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
James Smart5e5b5112019-01-28 11:14:22 -08007061 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
James Smart0794d602019-01-28 11:14:19 -08007062 *
7063 * Returns: 0 = success, non-zero failure.
7064 **/
7065int
James Smart5e5b5112019-01-28 11:14:22 -08007066lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
James Smart0794d602019-01-28 11:14:19 -08007067{
7068 LIST_HEAD(post_nblist);
7069 int num_posted, rc = 0;
7070
7071 /* get all NVME buffers need to repost to a local list */
James Smart5e5b5112019-01-28 11:14:22 -08007072 lpfc_io_buf_flush(phba, &post_nblist);
James Smart0794d602019-01-28 11:14:19 -08007073
7074 /* post the list of nvme buffer sgls to port if available */
7075 if (!list_empty(&post_nblist)) {
James Smart5e5b5112019-01-28 11:14:22 -08007076 num_posted = lpfc_sli4_post_io_sgl_list(
7077 phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
James Smart0794d602019-01-28 11:14:19 -08007078 /* failed to post any nvme buffer, return error */
7079 if (num_posted == 0)
7080 rc = -EIO;
7081 }
7082 return rc;
7083}
7084
James Smart61bda8f2016-10-13 15:06:05 -07007085void
7086lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7087{
7088 uint32_t len;
7089
7090 len = sizeof(struct lpfc_mbx_set_host_data) -
7091 sizeof(struct lpfc_sli4_cfg_mhdr);
7092 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7093 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7094 LPFC_SLI4_MBX_EMBED);
7095
7096 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08007097 mbox->u.mqe.un.set_host_data.param_len =
7098 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07007099 snprintf(mbox->u.mqe.un.set_host_data.data,
7100 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7101 "Linux %s v"LPFC_DRIVER_VERSION,
7102 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7103}
7104
James Smarta8cf5df2017-05-15 15:20:46 -07007105int
James Smart6c621a22017-05-15 15:20:45 -07007106lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07007107 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07007108{
7109 int rc, i;
7110 struct lpfc_rqe hrqe;
7111 struct lpfc_rqe drqe;
7112 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08007113 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07007114 struct rqb_dmabuf *rqb_buffer;
7115 LIST_HEAD(rqb_buf_list);
7116
James Smart411de512018-01-30 15:58:52 -08007117 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007118 rqbp = hrq->rqbp;
7119 for (i = 0; i < count; i++) {
7120 /* IF RQ is already full, don't bother */
7121 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
7122 break;
7123 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7124 if (!rqb_buffer)
7125 break;
7126 rqb_buffer->hrq = hrq;
7127 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07007128 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07007129 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7130 }
7131 while (!list_empty(&rqb_buf_list)) {
7132 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7133 hbuf.list);
7134
7135 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7136 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7137 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7138 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7139 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7140 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08007141 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7142 "6421 Cannot post to HRQ %d: %x %x %x "
7143 "DRQ %x %x\n",
7144 hrq->queue_id,
7145 hrq->host_index,
7146 hrq->hba_index,
7147 hrq->entry_count,
7148 drq->host_index,
7149 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07007150 rqbp->rqb_free_buffer(phba, rqb_buffer);
7151 } else {
7152 list_add_tail(&rqb_buffer->hbuf.list,
7153 &rqbp->rqb_buffer_list);
7154 rqbp->buffer_count++;
7155 }
7156 }
James Smart411de512018-01-30 15:58:52 -08007157 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007158 return 1;
7159}
7160
James Smart8a9d2e82012-05-09 21:16:12 -04007161/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08007162 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04007163 * @phba: Pointer to HBA context object.
7164 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08007165 * This function is the main SLI4 device initialization PCI function. This
7166 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04007167 * HBA error attention handler code. Caller is not required to hold any
7168 * locks.
7169 **/
7170int
7171lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7172{
James Smart6c621a22017-05-15 15:20:45 -07007173 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04007174 LPFC_MBOXQ_t *mboxq;
7175 struct lpfc_mqe *mqe;
7176 uint8_t *vpd;
7177 uint32_t vpd_size;
7178 uint32_t ftr_rsp = 0;
7179 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7180 struct lpfc_vport *vport = phba->pport;
7181 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08007182 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04007183
7184 /* Perform a PCI function reset to start from clean */
7185 rc = lpfc_pci_function_reset(phba);
7186 if (unlikely(rc))
7187 return -ENODEV;
7188
7189 /* Check the HBA Host Status Register for readyness */
7190 rc = lpfc_sli4_post_status_check(phba);
7191 if (unlikely(rc))
7192 return -ENODEV;
7193 else {
7194 spin_lock_irq(&phba->hbalock);
7195 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7196 spin_unlock_irq(&phba->hbalock);
7197 }
7198
7199 /*
7200 * Allocate a single mailbox container for initializing the
7201 * port.
7202 */
7203 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7204 if (!mboxq)
7205 return -ENOMEM;
7206
James Smartda0436e2009-05-22 14:51:39 -04007207 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04007208 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04007209 vpd = kzalloc(vpd_size, GFP_KERNEL);
7210 if (!vpd) {
7211 rc = -ENOMEM;
7212 goto out_free_mbox;
7213 }
7214
7215 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05007216 if (unlikely(rc)) {
7217 kfree(vpd);
7218 goto out_free_mbox;
7219 }
James Smart572709e2013-07-15 18:32:43 -04007220
James Smartda0436e2009-05-22 14:51:39 -04007221 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04007222 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07007223 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05007224 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007225 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7226 } else {
James Smart76a95d72010-11-20 23:11:48 -05007227 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007228 }
James Smart45ed1192009-10-02 15:17:02 -04007229
7230 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7231 LPFC_DCBX_CEE_MODE)
7232 phba->hba_flag |= HBA_FIP_SUPPORT;
7233 else
7234 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7235
James Smart4f2e66c2012-05-09 21:17:07 -04007236 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
7237
James Smartc31098c2011-04-16 11:03:33 -04007238 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007239 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7240 "0376 READ_REV Error. SLI Level %d "
7241 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05007242 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04007243 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007244 kfree(vpd);
7245 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007246 }
James Smartcd1c8302011-10-10 21:33:25 -04007247
7248 /*
James Smartff78d8f2011-12-13 13:21:35 -05007249 * Continue initialization with default values even if driver failed
7250 * to read FCoE param config regions, only read parameters if the
7251 * board is FCoE
7252 */
7253 if (phba->hba_flag & HBA_FCOE_MODE &&
7254 lpfc_sli4_read_fcoe_params(phba))
7255 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7256 "2570 Failed to read FCoE parameters\n");
7257
7258 /*
James Smartcd1c8302011-10-10 21:33:25 -04007259 * Retrieve sli4 device physical port name, failure of doing it
7260 * is considered as non-fatal.
7261 */
7262 rc = lpfc_sli4_retrieve_pport_name(phba);
7263 if (!rc)
7264 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7265 "3080 Successful retrieving SLI4 device "
7266 "physical port name: %s.\n", phba->Port);
7267
James Smartda0436e2009-05-22 14:51:39 -04007268 /*
7269 * Evaluate the read rev and vpd data. Populate the driver
7270 * state with the results. If this routine fails, the failure
7271 * is not fatal as the driver will use generic values.
7272 */
7273 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7274 if (unlikely(!rc)) {
7275 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7276 "0377 Error %d parsing vpd. "
7277 "Using defaults.\n", rc);
7278 rc = 0;
7279 }
James Smart76a95d72010-11-20 23:11:48 -05007280 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04007281
James Smartf1126682009-06-10 17:22:44 -04007282 /* Save information as VPD data */
7283 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7284 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08007285
7286 /*
7287 * This is because first G7 ASIC doesn't support the standard
7288 * 0x5a NVME cmd descriptor type/subtype
7289 */
7290 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7291 LPFC_SLI_INTF_IF_TYPE_6) &&
7292 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7293 (phba->vpd.rev.smRev == 0) &&
7294 (phba->cfg_nvme_embed_cmd == 1))
7295 phba->cfg_nvme_embed_cmd = 0;
7296
James Smartf1126682009-06-10 17:22:44 -04007297 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7298 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7299 &mqe->un.read_rev);
7300 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7301 &mqe->un.read_rev);
7302 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7303 &mqe->un.read_rev);
7304 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7305 &mqe->un.read_rev);
7306 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7307 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7308 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7309 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7310 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7311 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7312 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7313 "(%d):0380 READ_REV Status x%x "
7314 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7315 mboxq->vport ? mboxq->vport->vpi : 0,
7316 bf_get(lpfc_mqe_status, mqe),
7317 phba->vpd.rev.opFwName,
7318 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7319 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04007320
James Smart572709e2013-07-15 18:32:43 -04007321 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
7322 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
7323 if (phba->pport->cfg_lun_queue_depth > rc) {
7324 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7325 "3362 LUN queue depth changed from %d to %d\n",
7326 phba->pport->cfg_lun_queue_depth, rc);
7327 phba->pport->cfg_lun_queue_depth = rc;
7328 }
7329
James Smart65791f12016-07-06 12:35:56 -07007330 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07007331 LPFC_SLI_INTF_IF_TYPE_0) {
7332 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7333 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7334 if (rc == MBX_SUCCESS) {
7335 phba->hba_flag |= HBA_RECOVERABLE_UE;
7336 /* Set 1Sec interval to detect UE */
7337 phba->eratt_poll_interval = 1;
7338 phba->sli4_hba.ue_to_sr = bf_get(
7339 lpfc_mbx_set_feature_UESR,
7340 &mboxq->u.mqe.un.set_feature);
7341 phba->sli4_hba.ue_to_rp = bf_get(
7342 lpfc_mbx_set_feature_UERP,
7343 &mboxq->u.mqe.un.set_feature);
7344 }
7345 }
7346
7347 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7348 /* Enable MDS Diagnostics only if the SLI Port supports it */
7349 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7350 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7351 if (rc != MBX_SUCCESS)
7352 phba->mds_diags_support = 0;
7353 }
James Smart572709e2013-07-15 18:32:43 -04007354
James Smartda0436e2009-05-22 14:51:39 -04007355 /*
7356 * Discover the port's supported feature set and match it against the
7357 * hosts requests.
7358 */
7359 lpfc_request_features(phba, mboxq);
7360 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7361 if (unlikely(rc)) {
7362 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007363 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007364 }
7365
7366 /*
7367 * The port must support FCP initiator mode as this is the
7368 * only mode running in the host.
7369 */
7370 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7371 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7372 "0378 No support for fcpi mode.\n");
7373 ftr_rsp++;
7374 }
James Smart0bc2b7c2018-02-22 08:18:48 -08007375
7376 /* Performance Hints are ONLY for FCoE */
7377 if (phba->hba_flag & HBA_FCOE_MODE) {
7378 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7379 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7380 else
7381 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7382 }
7383
James Smartda0436e2009-05-22 14:51:39 -04007384 /*
7385 * If the port cannot support the host's requested features
7386 * then turn off the global config parameters to disable the
7387 * feature in the driver. This is not a fatal error.
7388 */
James Smartf44ac122018-03-05 12:04:08 -08007389 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7390 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7391 phba->cfg_enable_bg = 0;
7392 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04007393 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08007394 }
James Smartbf086112011-08-21 21:48:13 -04007395 }
James Smartda0436e2009-05-22 14:51:39 -04007396
7397 if (phba->max_vpi && phba->cfg_enable_npiv &&
7398 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7399 ftr_rsp++;
7400
7401 if (ftr_rsp) {
7402 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7403 "0379 Feature Mismatch Data: x%08x %08x "
7404 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7405 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7406 phba->cfg_enable_npiv, phba->max_vpi);
7407 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7408 phba->cfg_enable_bg = 0;
7409 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7410 phba->cfg_enable_npiv = 0;
7411 }
7412
7413 /* These SLI3 features are assumed in SLI4 */
7414 spin_lock_irq(&phba->hbalock);
7415 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7416 spin_unlock_irq(&phba->hbalock);
7417
James Smart6d368e52011-05-24 11:44:12 -04007418 /*
7419 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7420 * calls depends on these resources to complete port setup.
7421 */
7422 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7423 if (rc) {
7424 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7425 "2920 Failed to alloc Resource IDs "
7426 "rc = x%x\n", rc);
7427 goto out_free_mbox;
7428 }
7429
James Smart61bda8f2016-10-13 15:06:05 -07007430 lpfc_set_host_data(phba, mboxq);
7431
7432 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7433 if (rc) {
7434 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7435 "2134 Failed to set host os driver version %x",
7436 rc);
7437 }
7438
James Smartda0436e2009-05-22 14:51:39 -04007439 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007440 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7441 if (rc) {
7442 phba->link_state = LPFC_HBA_ERROR;
7443 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007444 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007445 }
7446
James Smartda0436e2009-05-22 14:51:39 -04007447 mboxq->vport = vport;
7448 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart3e1f0712018-11-29 16:09:29 -08007449 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04007450 if (rc == MBX_SUCCESS) {
7451 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7452 rc = 0;
7453 }
7454
7455 /*
7456 * This memory was allocated by the lpfc_read_sparam routine. Release
7457 * it to the mbuf pool.
7458 */
7459 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7460 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -08007461 mboxq->ctx_buf = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007462 if (unlikely(rc)) {
7463 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7464 "0382 READ_SPARAM command failed "
7465 "status %d, mbxStatus x%x\n",
7466 rc, bf_get(lpfc_mqe_status, mqe));
7467 phba->link_state = LPFC_HBA_ERROR;
7468 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007469 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007470 }
7471
James Smart05580562011-05-24 11:40:48 -04007472 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007473
7474 /* Update the fc_host data structures with new wwn. */
7475 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7476 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7477
James Smart895427b2017-02-12 13:52:30 -08007478 /* Create all the SLI4 queues */
7479 rc = lpfc_sli4_queue_create(phba);
7480 if (rc) {
7481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7482 "3089 Failed to allocate queues\n");
7483 rc = -ENODEV;
7484 goto out_free_mbox;
7485 }
7486 /* Set up all the queues to the device */
7487 rc = lpfc_sli4_queue_setup(phba);
7488 if (unlikely(rc)) {
7489 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7490 "0381 Error %d during queue setup.\n ", rc);
7491 goto out_stop_timers;
7492 }
7493 /* Initialize the driver internal SLI layer lists. */
7494 lpfc_sli4_setup(phba);
7495 lpfc_sli4_queue_init(phba);
7496
7497 /* update host els xri-sgl sizes and mappings */
7498 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007499 if (unlikely(rc)) {
7500 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7501 "1400 Failed to update xri-sgl size and "
7502 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007503 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007504 }
7505
James Smart8a9d2e82012-05-09 21:16:12 -04007506 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007507 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7508 phba->sli4_hba.els_xri_cnt);
7509 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007510 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7511 "0582 Error %d during els sgl post "
7512 "operation\n", rc);
7513 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007514 goto out_destroy_queue;
7515 }
7516 phba->sli4_hba.els_xri_cnt = rc;
7517
James Smartf358dd02017-02-12 13:52:34 -08007518 if (phba->nvmet_support) {
7519 /* update host nvmet xri-sgl sizes and mappings */
7520 rc = lpfc_sli4_nvmet_sgl_update(phba);
7521 if (unlikely(rc)) {
7522 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7523 "6308 Failed to update nvmet-sgl size "
7524 "and mapping: %d\n", rc);
7525 goto out_destroy_queue;
7526 }
7527
7528 /* register the nvmet sgl pool to the port */
7529 rc = lpfc_sli4_repost_sgl_list(
7530 phba,
7531 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7532 phba->sli4_hba.nvmet_xri_cnt);
7533 if (unlikely(rc < 0)) {
7534 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7535 "3117 Error %d during nvmet "
7536 "sgl post\n", rc);
7537 rc = -ENODEV;
7538 goto out_destroy_queue;
7539 }
7540 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007541
7542 cnt = phba->cfg_iocb_cnt * 1024;
7543 /* We need 1 iocbq for every SGL, for IO processing */
7544 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08007545 } else {
James Smart0794d602019-01-28 11:14:19 -08007546 /* update host common xri-sgl sizes and mappings */
James Smart5e5b5112019-01-28 11:14:22 -08007547 rc = lpfc_sli4_io_sgl_update(phba);
James Smart895427b2017-02-12 13:52:30 -08007548 if (unlikely(rc)) {
7549 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7550 "6082 Failed to update nvme-sgl size "
7551 "and mapping: %d\n", rc);
7552 goto out_destroy_queue;
7553 }
James Smart6c621a22017-05-15 15:20:45 -07007554
James Smart0794d602019-01-28 11:14:19 -08007555 /* register the allocated common sgl pool to the port */
James Smart5e5b5112019-01-28 11:14:22 -08007556 rc = lpfc_sli4_repost_io_sgl_list(phba);
James Smart0794d602019-01-28 11:14:19 -08007557 if (unlikely(rc)) {
7558 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7559 "6116 Error %d during nvme sgl post "
7560 "operation\n", rc);
7561 /* Some NVME buffers were moved to abort nvme list */
7562 /* A pci function reset will repost them */
7563 rc = -ENODEV;
7564 goto out_destroy_queue;
7565 }
James Smart6c621a22017-05-15 15:20:45 -07007566 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07007567 }
7568
7569 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007570 /* Initialize and populate the iocb list per host */
7571 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007572 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07007573 phba->cfg_iocb_cnt, cnt);
7574 rc = lpfc_init_iocb_list(phba, cnt);
7575 if (rc) {
7576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007577 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007578 goto out_destroy_queue;
7579 }
James Smart8a9d2e82012-05-09 21:16:12 -04007580 }
7581
James Smart11e644e2017-06-15 22:56:48 -07007582 if (phba->nvmet_support)
7583 lpfc_nvmet_create_targetport(phba);
7584
James Smart2d7dbc42017-02-12 13:52:35 -08007585 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007586 /* Post initial buffers to all RQs created */
7587 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7588 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7589 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7590 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7591 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007592 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007593 rqbp->buffer_count = 0;
7594
James Smart2d7dbc42017-02-12 13:52:35 -08007595 lpfc_post_rq_buffer(
7596 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7597 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007598 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007599 }
7600 }
7601
James Smartda0436e2009-05-22 14:51:39 -04007602 /* Post the rpi header region to the device. */
7603 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7604 if (unlikely(rc)) {
7605 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7606 "0393 Error %d during rpi post operation\n",
7607 rc);
7608 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007609 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007610 }
James Smart97f2ecf2012-03-01 22:35:23 -05007611 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007612
James Smart895427b2017-02-12 13:52:30 -08007613 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007614 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007615 /*
7616 * The FC Port needs to register FCFI (index 0)
7617 */
7618 lpfc_reg_fcfi(phba, mboxq);
7619 mboxq->vport = phba->pport;
7620 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7621 if (rc != MBX_SUCCESS)
7622 goto out_unset_queue;
7623 rc = 0;
7624 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7625 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007626 } else {
7627 /* We are a NVME Target mode with MRQ > 1 */
7628
7629 /* First register the FCFI */
7630 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7631 mboxq->vport = phba->pport;
7632 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7633 if (rc != MBX_SUCCESS)
7634 goto out_unset_queue;
7635 rc = 0;
7636 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7637 &mboxq->u.mqe.un.reg_fcfi_mrq);
7638
7639 /* Next register the MRQs */
7640 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7641 mboxq->vport = phba->pport;
7642 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7643 if (rc != MBX_SUCCESS)
7644 goto out_unset_queue;
7645 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007646 }
7647 /* Check if the port is configured to be disabled */
7648 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007649 }
7650
7651 /* Arm the CQs and then EQs on device */
7652 lpfc_sli4_arm_cqeq_intr(phba);
7653
7654 /* Indicate device interrupt mode */
7655 phba->sli4_hba.intr_enable = 1;
7656
7657 /* Allow asynchronous mailbox command to go through */
7658 spin_lock_irq(&phba->hbalock);
7659 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7660 spin_unlock_irq(&phba->hbalock);
7661
7662 /* Post receive buffers to the device */
7663 lpfc_sli4_rb_setup(phba);
7664
James Smartfc2b9892010-02-26 14:15:29 -05007665 /* Reset HBA FCF states after HBA reset */
7666 phba->fcf.fcf_flag = 0;
7667 phba->fcf.current_rec.flag = 0;
7668
James Smartda0436e2009-05-22 14:51:39 -04007669 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007670 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007671 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007672
7673 /* Start heart beat timer */
7674 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007675 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007676 phba->hb_outstanding = 0;
7677 phba->last_completion_time = jiffies;
7678
7679 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007680 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007681 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007682
James Smart75baf692010-06-08 18:31:21 -04007683 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7684 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7685 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7686 if (!rc) {
7687 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7688 "2829 This device supports "
7689 "Advanced Error Reporting (AER)\n");
7690 spin_lock_irq(&phba->hbalock);
7691 phba->hba_flag |= HBA_AER_ENABLED;
7692 spin_unlock_irq(&phba->hbalock);
7693 } else {
7694 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7695 "2830 This device does not support "
7696 "Advanced Error Reporting (AER)\n");
7697 phba->cfg_aer_support = 0;
7698 }
James Smart0a96e972011-07-22 18:37:28 -04007699 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007700 }
7701
James Smartda0436e2009-05-22 14:51:39 -04007702 /*
7703 * The port is ready, set the host's link state to LINK_DOWN
7704 * in preparation for link interrupts.
7705 */
James Smartda0436e2009-05-22 14:51:39 -04007706 spin_lock_irq(&phba->hbalock);
7707 phba->link_state = LPFC_LINK_DOWN;
James Smart1dc5ec22018-10-23 13:41:11 -07007708
7709 /* Check if physical ports are trunked */
7710 if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
7711 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
7712 if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
7713 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
7714 if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
7715 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
7716 if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
7717 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04007718 spin_unlock_irq(&phba->hbalock);
James Smart1dc5ec22018-10-23 13:41:11 -07007719
James Smart026abb82011-12-13 13:20:45 -05007720 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7721 (phba->hba_flag & LINK_DISABLED)) {
7722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7723 "3103 Adapter Link is disabled.\n");
7724 lpfc_down_link(phba, mboxq);
7725 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7726 if (rc != MBX_SUCCESS) {
7727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7728 "3104 Adapter failed to issue "
7729 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7730 goto out_unset_queue;
7731 }
7732 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007733 /* don't perform init_link on SLI4 FC port loopback test */
7734 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7735 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7736 if (rc)
7737 goto out_unset_queue;
7738 }
James Smart5350d872011-10-10 21:33:49 -04007739 }
7740 mempool_free(mboxq, phba->mbox_mem_pool);
7741 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007742out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007743 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007744 lpfc_sli4_queue_unset(phba);
7745out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007746 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007747 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007748out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007749 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007750out_free_mbox:
7751 mempool_free(mboxq, phba->mbox_mem_pool);
7752 return rc;
7753}
James Smarte59058c2008-08-24 21:49:00 -04007754
7755/**
James Smart3621a712009-04-06 18:47:14 -04007756 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007757 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007758 *
James Smarte59058c2008-08-24 21:49:00 -04007759 * This is the callback function for mailbox timer. The mailbox
7760 * timer is armed when a new mailbox command is issued and the timer
7761 * is deleted when the mailbox complete. The function is called by
7762 * the kernel timer code when a mailbox does not complete within
7763 * expected time. This function wakes up the worker thread to
7764 * process the mailbox timeout and returns. All the processing is
7765 * done by the worker thread function lpfc_mbox_timeout_handler.
7766 **/
dea31012005-04-17 16:05:31 -05007767void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007768lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007769{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007770 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007771 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007772 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007773
James Smart2e0fef82007-06-17 19:56:36 -05007774 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007775 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007776 if (!tmo_posted)
7777 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7778 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7779
James Smart5e9d9b82008-06-14 22:52:53 -04007780 if (!tmo_posted)
7781 lpfc_worker_wake_up(phba);
7782 return;
dea31012005-04-17 16:05:31 -05007783}
7784
James Smarte8d3c3b2013-10-10 12:21:30 -04007785/**
7786 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7787 * are pending
7788 * @phba: Pointer to HBA context object.
7789 *
7790 * This function checks if any mailbox completions are present on the mailbox
7791 * completion queue.
7792 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007793static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007794lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7795{
7796
7797 uint32_t idx;
7798 struct lpfc_queue *mcq;
7799 struct lpfc_mcqe *mcqe;
7800 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08007801 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04007802
7803 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7804 return false;
7805
7806 /* Check for completions on mailbox completion queue */
7807
7808 mcq = phba->sli4_hba.mbx_cq;
7809 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08007810 qe_valid = mcq->qe_valid;
7811 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
James Smarte8d3c3b2013-10-10 12:21:30 -04007812 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7813 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7814 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7815 pending_completions = true;
7816 break;
7817 }
7818 idx = (idx + 1) % mcq->entry_count;
7819 if (mcq->hba_index == idx)
7820 break;
James Smart7365f6f2018-02-22 08:18:46 -08007821
7822 /* if the index wrapped around, toggle the valid bit */
7823 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7824 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04007825 }
7826 return pending_completions;
7827
7828}
7829
7830/**
7831 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7832 * that were missed.
7833 * @phba: Pointer to HBA context object.
7834 *
7835 * For sli4, it is possible to miss an interrupt. As such mbox completions
7836 * maybe missed causing erroneous mailbox timeouts to occur. This function
7837 * checks to see if mbox completions are on the mailbox completion queue
7838 * and will process all the completions associated with the eq for the
7839 * mailbox completion queue.
7840 **/
7841bool
7842lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7843{
James Smartb71413d2018-02-22 08:18:40 -08007844 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04007845 uint32_t eqidx;
7846 struct lpfc_queue *fpeq = NULL;
7847 struct lpfc_eqe *eqe;
7848 bool mbox_pending;
7849
7850 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7851 return false;
7852
7853 /* Find the eq associated with the mcq */
7854
James Smartcdb42be2019-01-28 11:14:21 -08007855 if (sli4_hba->hdwq)
7856 for (eqidx = 0; eqidx < phba->cfg_hdw_queue; eqidx++)
7857 if (sli4_hba->hdwq[eqidx].hba_eq->queue_id ==
James Smartb71413d2018-02-22 08:18:40 -08007858 sli4_hba->mbx_cq->assoc_qid) {
James Smartcdb42be2019-01-28 11:14:21 -08007859 fpeq = sli4_hba->hdwq[eqidx].hba_eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04007860 break;
7861 }
7862 if (!fpeq)
7863 return false;
7864
7865 /* Turn off interrupts from this EQ */
7866
James Smartb71413d2018-02-22 08:18:40 -08007867 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04007868
7869 /* Check to see if a mbox completion is pending */
7870
7871 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7872
7873 /*
7874 * If a mbox completion is pending, process all the events on EQ
7875 * associated with the mbox completion queue (this could include
7876 * mailbox commands, async events, els commands, receive queue data
7877 * and fcp commands)
7878 */
7879
7880 if (mbox_pending)
7881 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7882 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7883 fpeq->EQ_processed++;
7884 }
7885
7886 /* Always clear and re-arm the EQ */
7887
James Smartb71413d2018-02-22 08:18:40 -08007888 sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04007889
7890 return mbox_pending;
7891
7892}
James Smarte59058c2008-08-24 21:49:00 -04007893
7894/**
James Smart3621a712009-04-06 18:47:14 -04007895 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007896 * @phba: Pointer to HBA context object.
7897 *
7898 * This function is called from worker thread when a mailbox command times out.
7899 * The caller is not required to hold any locks. This function will reset the
7900 * HBA and recover all the pending commands.
7901 **/
dea31012005-04-17 16:05:31 -05007902void
7903lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7904{
James Smart2e0fef82007-06-17 19:56:36 -05007905 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007906 MAILBOX_t *mb = NULL;
7907
James Smart1dcb58e2007-04-25 09:51:30 -04007908 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007909
James Smarte8d3c3b2013-10-10 12:21:30 -04007910 /* If the mailbox completed, process the completion and return */
7911 if (lpfc_sli4_process_missed_mbox_completions(phba))
7912 return;
7913
James Smarteb016562014-09-03 12:58:06 -04007914 if (pmbox != NULL)
7915 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007916 /* Check the pmbox pointer first. There is a race condition
7917 * between the mbox timeout handler getting executed in the
7918 * worklist and the mailbox actually completing. When this
7919 * race condition occurs, the mbox_active will be NULL.
7920 */
7921 spin_lock_irq(&phba->hbalock);
7922 if (pmbox == NULL) {
7923 lpfc_printf_log(phba, KERN_WARNING,
7924 LOG_MBOX | LOG_SLI,
7925 "0353 Active Mailbox cleared - mailbox timeout "
7926 "exiting\n");
7927 spin_unlock_irq(&phba->hbalock);
7928 return;
7929 }
7930
dea31012005-04-17 16:05:31 -05007931 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007932 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007933 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007934 mb->mbxCommand,
7935 phba->pport->port_state,
7936 phba->sli.sli_flag,
7937 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007938 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007939
James Smart1dcb58e2007-04-25 09:51:30 -04007940 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7941 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007942 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007943 */
James Smart2e0fef82007-06-17 19:56:36 -05007944 spin_lock_irq(&phba->pport->work_port_lock);
7945 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7946 spin_unlock_irq(&phba->pport->work_port_lock);
7947 spin_lock_irq(&phba->hbalock);
7948 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007949 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007950 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007951
James Smartdb55fba2014-04-04 13:52:02 -04007952 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007953
7954 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007955 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007956
7957 /* Reset the HBA device */
7958 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007959}
7960
James Smarte59058c2008-08-24 21:49:00 -04007961/**
James Smart3772a992009-05-22 14:50:54 -04007962 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007963 * @phba: Pointer to HBA context object.
7964 * @pmbox: Pointer to mailbox object.
7965 * @flag: Flag indicating how the mailbox need to be processed.
7966 *
7967 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007968 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7969 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007970 * The mailbox command can be submitted in polling mode, in which case
7971 * this function will wait in a polling loop for the completion of the
7972 * mailbox.
7973 * If the mailbox is submitted in no_wait mode (not polling) the
7974 * function will submit the command and returns immediately without waiting
7975 * for the mailbox completion. The no_wait is supported only when HBA
7976 * is in SLI2/SLI3 mode - interrupts are enabled.
7977 * The SLI interface allows only one mailbox pending at a time. If the
7978 * mailbox is issued in polling mode and there is already a mailbox
7979 * pending, then the function will return an error. If the mailbox is issued
7980 * in NO_WAIT mode and there is a mailbox pending already, the function
7981 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7982 * The sli layer owns the mailbox object until the completion of mailbox
7983 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7984 * return codes the caller owns the mailbox command after the return of
7985 * the function.
7986 **/
James Smart3772a992009-05-22 14:50:54 -04007987static int
7988lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7989 uint32_t flag)
dea31012005-04-17 16:05:31 -05007990{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007991 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007992 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007993 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007994 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007995 int i;
James Smart09372822008-01-11 01:52:54 -05007996 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007997 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007998 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007999 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04008000 int processing_queue = 0;
8001
8002 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8003 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04008004 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04008005 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04008006 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8007 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8008 return MBX_SUCCESS;
8009 }
James Smart58da1ff2008-04-07 10:15:56 -04008010 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04008011 pmbox = lpfc_mbox_get(phba);
8012 if (!pmbox) {
8013 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8014 return MBX_SUCCESS;
8015 }
8016 }
dea31012005-04-17 16:05:31 -05008017
James Smarted957682007-06-17 19:56:37 -05008018 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05008019 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05008020 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04008021 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05008022 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05008023 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008024 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04008025 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05008026 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04008027 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05008028 }
8029 }
8030
Linas Vepstas8d63f372007-02-14 14:28:36 -06008031 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04008032 if (unlikely(pci_channel_offline(phba->pcidev))) {
8033 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8034 goto out_not_finished;
8035 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008036
James Smarta257bf92009-04-06 18:48:10 -04008037 /* If HBA has a deferred error attention, fail the iocb. */
8038 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8039 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8040 goto out_not_finished;
8041 }
8042
dea31012005-04-17 16:05:31 -05008043 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05008044
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008045 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05008046 status = MBX_SUCCESS;
8047
James Smart2e0fef82007-06-17 19:56:36 -05008048 if (phba->link_state == LPFC_HBA_ERROR) {
8049 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05008050
8051 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008052 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8053 "(%d):0311 Mailbox command x%x cannot "
8054 "issue Data: x%x x%x\n",
8055 pmbox->vport ? pmbox->vport->vpi : 0,
8056 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008057 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05008058 }
8059
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008060 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05008061 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8062 !(hc_copy & HC_MBINT_ENA)) {
8063 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8064 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04008065 "(%d):2528 Mailbox command x%x cannot "
8066 "issue Data: x%x x%x\n",
8067 pmbox->vport ? pmbox->vport->vpi : 0,
8068 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05008069 goto out_not_finished;
8070 }
James Smart92908312006-03-07 15:04:13 -05008071 }
8072
dea31012005-04-17 16:05:31 -05008073 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8074 /* Polling for a mbox command when another one is already active
8075 * is not allowed in SLI. Also, the driver must have established
8076 * SLI2 mode to queue and process multiple mbox commands.
8077 */
8078
8079 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05008080 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008081
8082 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008083 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8084 "(%d):2529 Mailbox command x%x "
8085 "cannot issue Data: x%x x%x\n",
8086 pmbox->vport ? pmbox->vport->vpi : 0,
8087 pmbox->u.mb.mbxCommand,
8088 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008089 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008090 }
8091
James Smart3772a992009-05-22 14:50:54 -04008092 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05008093 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008094 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008095 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8096 "(%d):2530 Mailbox command x%x "
8097 "cannot issue Data: x%x x%x\n",
8098 pmbox->vport ? pmbox->vport->vpi : 0,
8099 pmbox->u.mb.mbxCommand,
8100 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008101 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008102 }
8103
dea31012005-04-17 16:05:31 -05008104 /* Another mailbox command is still being processed, queue this
8105 * command to be processed later.
8106 */
8107 lpfc_mbox_put(phba, pmbox);
8108
8109 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05008110 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008111 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008112 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008113 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07008114 mbx->mbxCommand,
8115 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008116 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008117
8118 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05008119 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008120
James Smart858c9f62007-06-17 19:56:39 -05008121 if (pmbox->vport) {
8122 lpfc_debugfs_disc_trc(pmbox->vport,
8123 LPFC_DISC_TRC_MBOX_VPORT,
8124 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008125 (uint32_t)mbx->mbxCommand,
8126 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008127 }
8128 else {
8129 lpfc_debugfs_disc_trc(phba->pport,
8130 LPFC_DISC_TRC_MBOX,
8131 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008132 (uint32_t)mbx->mbxCommand,
8133 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008134 }
8135
James Smart2e0fef82007-06-17 19:56:36 -05008136 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05008137 }
8138
dea31012005-04-17 16:05:31 -05008139 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8140
8141 /* If we are not polling, we MUST be in SLI2 mode */
8142 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04008143 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008144 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05008145 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008146 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008147 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008148 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8149 "(%d):2531 Mailbox command x%x "
8150 "cannot issue Data: x%x x%x\n",
8151 pmbox->vport ? pmbox->vport->vpi : 0,
8152 pmbox->u.mb.mbxCommand,
8153 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008154 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008155 }
8156 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04008157 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8158 1000);
8159 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05008160 }
8161
8162 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05008163 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008164 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05008165 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008166 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07008167 mbx->mbxCommand,
8168 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008169 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008170
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008171 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05008172 if (pmbox->vport) {
8173 lpfc_debugfs_disc_trc(pmbox->vport,
8174 LPFC_DISC_TRC_MBOX_VPORT,
8175 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008176 (uint32_t)mbx->mbxCommand,
8177 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008178 }
8179 else {
8180 lpfc_debugfs_disc_trc(phba->pport,
8181 LPFC_DISC_TRC_MBOX,
8182 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008183 (uint32_t)mbx->mbxCommand,
8184 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008185 }
8186 }
8187
dea31012005-04-17 16:05:31 -05008188 psli->slistat.mbox_cmd++;
8189 evtctr = psli->slistat.mbox_event;
8190
8191 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008192 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05008193
James Smart3772a992009-05-22 14:50:54 -04008194 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04008195 /* Populate mbox extension offset word. */
8196 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008197 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008198 = (uint8_t *)phba->mbox_ext
8199 - (uint8_t *)phba->mbox;
8200 }
8201
8202 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008203 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8204 lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8205 (uint8_t *)phba->mbox_ext,
8206 pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008207 }
8208 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008209 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008210 } else {
James Smart7a470272010-03-15 11:25:20 -04008211 /* Populate mbox extension offset word. */
8212 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008213 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008214 = MAILBOX_HBA_EXT_OFFSET;
8215
8216 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008217 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -04008218 lpfc_memcpy_to_slim(phba->MBslimaddr +
8219 MAILBOX_HBA_EXT_OFFSET,
James Smart3e1f0712018-11-29 16:09:29 -08008220 pmbox->ctx_buf, pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008221
James Smart895427b2017-02-12 13:52:30 -08008222 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008223 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08008224 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8225 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008226
8227 /* First copy mbox command data to HBA SLIM, skip past first
8228 word */
8229 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008230 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05008231 MAILBOX_CMD_SIZE - sizeof (uint32_t));
8232
8233 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008234 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05008235 to_slim = phba->MBslimaddr;
8236 writel(ldata, to_slim);
8237 readl(to_slim); /* flush */
8238
James Smart895427b2017-02-12 13:52:30 -08008239 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008240 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04008241 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008242 }
8243
8244 wmb();
dea31012005-04-17 16:05:31 -05008245
8246 switch (flag) {
8247 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05008248 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05008249 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05008250 /* Interrupt board to do it */
8251 writel(CA_MBATT, phba->CAregaddr);
8252 readl(phba->CAregaddr); /* flush */
8253 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05008254 break;
8255
8256 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05008257 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05008258 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05008259 /* Interrupt board to do it */
8260 writel(CA_MBATT, phba->CAregaddr);
8261 readl(phba->CAregaddr); /* flush */
8262
James Smart3772a992009-05-22 14:50:54 -04008263 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008264 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04008265 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008266 word0 = le32_to_cpu(word0);
8267 } else {
8268 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05008269 if (lpfc_readl(phba->MBslimaddr, &word0)) {
8270 spin_unlock_irqrestore(&phba->hbalock,
8271 drvr_flag);
8272 goto out_not_finished;
8273 }
dea31012005-04-17 16:05:31 -05008274 }
8275
8276 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008277 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8278 spin_unlock_irqrestore(&phba->hbalock,
8279 drvr_flag);
8280 goto out_not_finished;
8281 }
James Smarta183a152011-10-10 21:32:43 -04008282 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8283 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05008284 i = 0;
dea31012005-04-17 16:05:31 -05008285 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05008286 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8287 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05008288 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05008289 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05008290 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008291 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05008292 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04008293 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008294 }
8295
8296 /* Check if we took a mbox interrupt while we were
8297 polling */
8298 if (((word0 & OWN_CHIP) != OWN_CHIP)
8299 && (evtctr != psli->slistat.mbox_event))
8300 break;
8301
James Smart09372822008-01-11 01:52:54 -05008302 if (i++ > 10) {
8303 spin_unlock_irqrestore(&phba->hbalock,
8304 drvr_flag);
8305 msleep(1);
8306 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8307 }
dea31012005-04-17 16:05:31 -05008308
James Smart3772a992009-05-22 14:50:54 -04008309 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008310 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04008311 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008312 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008313 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05008314 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04008315 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05008316 /* Check real SLIM for any errors */
8317 slimword0 = readl(phba->MBslimaddr);
8318 slimmb = (MAILBOX_t *) & slimword0;
8319 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8320 && slimmb->mbxStatus) {
8321 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04008322 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008323 word0 = slimword0;
8324 }
8325 }
8326 } else {
8327 /* First copy command data */
8328 word0 = readl(phba->MBslimaddr);
8329 }
8330 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008331 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8332 spin_unlock_irqrestore(&phba->hbalock,
8333 drvr_flag);
8334 goto out_not_finished;
8335 }
dea31012005-04-17 16:05:31 -05008336 }
8337
James Smart3772a992009-05-22 14:50:54 -04008338 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008339 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08008340 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8341 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008342 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008343 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
James Smart7a470272010-03-15 11:25:20 -04008344 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -08008345 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008346 pmbox->out_ext_byte_len);
8347 }
dea31012005-04-17 16:05:31 -05008348 } else {
8349 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008350 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08008351 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008352 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008353 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8354 lpfc_memcpy_from_slim(
8355 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008356 phba->MBslimaddr +
8357 MAILBOX_HBA_EXT_OFFSET,
8358 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05008359 }
8360 }
8361
8362 writel(HA_MBATT, phba->HAregaddr);
8363 readl(phba->HAregaddr); /* flush */
8364
8365 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008366 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05008367 }
8368
James Smart2e0fef82007-06-17 19:56:36 -05008369 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8370 return status;
James Smart58da1ff2008-04-07 10:15:56 -04008371
8372out_not_finished:
8373 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04008374 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04008375 lpfc_mbox_cmpl_put(phba, pmbox);
8376 }
8377 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008378}
8379
James Smarte59058c2008-08-24 21:49:00 -04008380/**
James Smartf1126682009-06-10 17:22:44 -04008381 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8382 * @phba: Pointer to HBA context object.
8383 *
8384 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8385 * the driver internal pending mailbox queue. It will then try to wait out the
8386 * possible outstanding mailbox command before return.
8387 *
8388 * Returns:
8389 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8390 * the outstanding mailbox command timed out.
8391 **/
8392static int
8393lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8394{
8395 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008396 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008397 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008398
8399 /* Mark the asynchronous mailbox command posting as blocked */
8400 spin_lock_irq(&phba->hbalock);
8401 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008402 /* Determine how long we might wait for the active mailbox
8403 * command to be gracefully completed by firmware.
8404 */
James Smarta183a152011-10-10 21:32:43 -04008405 if (phba->sli.mbox_active)
8406 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8407 phba->sli.mbox_active) *
8408 1000) + jiffies;
8409 spin_unlock_irq(&phba->hbalock);
8410
James Smarte8d3c3b2013-10-10 12:21:30 -04008411 /* Make sure the mailbox is really active */
8412 if (timeout)
8413 lpfc_sli4_process_missed_mbox_completions(phba);
8414
James Smartf1126682009-06-10 17:22:44 -04008415 /* Wait for the outstnading mailbox command to complete */
8416 while (phba->sli.mbox_active) {
8417 /* Check active mailbox complete status every 2ms */
8418 msleep(2);
8419 if (time_after(jiffies, timeout)) {
8420 /* Timeout, marked the outstanding cmd not complete */
8421 rc = 1;
8422 break;
8423 }
8424 }
8425
8426 /* Can not cleanly block async mailbox command, fails it */
8427 if (rc) {
8428 spin_lock_irq(&phba->hbalock);
8429 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8430 spin_unlock_irq(&phba->hbalock);
8431 }
8432 return rc;
8433}
8434
8435/**
8436 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8437 * @phba: Pointer to HBA context object.
8438 *
8439 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8440 * commands from the driver internal pending mailbox queue. It makes sure
8441 * that there is no outstanding mailbox command before resuming posting
8442 * asynchronous mailbox commands. If, for any reason, there is outstanding
8443 * mailbox command, it will try to wait it out before resuming asynchronous
8444 * mailbox command posting.
8445 **/
8446static void
8447lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8448{
8449 struct lpfc_sli *psli = &phba->sli;
8450
8451 spin_lock_irq(&phba->hbalock);
8452 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8453 /* Asynchronous mailbox posting is not blocked, do nothing */
8454 spin_unlock_irq(&phba->hbalock);
8455 return;
8456 }
8457
8458 /* Outstanding synchronous mailbox command is guaranteed to be done,
8459 * successful or timeout, after timing-out the outstanding mailbox
8460 * command shall always be removed, so just unblock posting async
8461 * mailbox command and resume
8462 */
8463 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8464 spin_unlock_irq(&phba->hbalock);
8465
8466 /* wake up worker thread to post asynchronlous mailbox command */
8467 lpfc_worker_wake_up(phba);
8468}
8469
8470/**
James Smart2d843ed2012-09-29 11:29:06 -04008471 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8472 * @phba: Pointer to HBA context object.
8473 * @mboxq: Pointer to mailbox object.
8474 *
8475 * The function waits for the bootstrap mailbox register ready bit from
8476 * port for twice the regular mailbox command timeout value.
8477 *
8478 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8479 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8480 **/
8481static int
8482lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8483{
8484 uint32_t db_ready;
8485 unsigned long timeout;
8486 struct lpfc_register bmbx_reg;
8487
8488 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8489 * 1000) + jiffies;
8490
8491 do {
8492 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8493 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8494 if (!db_ready)
8495 msleep(2);
8496
8497 if (time_after(jiffies, timeout))
8498 return MBXERR_ERROR;
8499 } while (!db_ready);
8500
8501 return 0;
8502}
8503
8504/**
James Smartda0436e2009-05-22 14:51:39 -04008505 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8506 * @phba: Pointer to HBA context object.
8507 * @mboxq: Pointer to mailbox object.
8508 *
8509 * The function posts a mailbox to the port. The mailbox is expected
8510 * to be comletely filled in and ready for the port to operate on it.
8511 * This routine executes a synchronous completion operation on the
8512 * mailbox by polling for its completion.
8513 *
8514 * The caller must not be holding any locks when calling this routine.
8515 *
8516 * Returns:
8517 * MBX_SUCCESS - mailbox posted successfully
8518 * Any of the MBX error values.
8519 **/
8520static int
8521lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8522{
8523 int rc = MBX_SUCCESS;
8524 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008525 uint32_t mcqe_status;
8526 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008527 struct lpfc_sli *psli = &phba->sli;
8528 struct lpfc_mqe *mb = &mboxq->u.mqe;
8529 struct lpfc_bmbx_create *mbox_rgn;
8530 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008531
8532 /*
8533 * Only one mailbox can be active to the bootstrap mailbox region
8534 * at a time and there is no queueing provided.
8535 */
8536 spin_lock_irqsave(&phba->hbalock, iflag);
8537 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8538 spin_unlock_irqrestore(&phba->hbalock, iflag);
8539 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008540 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008541 "cannot issue Data: x%x x%x\n",
8542 mboxq->vport ? mboxq->vport->vpi : 0,
8543 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008544 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8545 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008546 psli->sli_flag, MBX_POLL);
8547 return MBXERR_ERROR;
8548 }
8549 /* The server grabs the token and owns it until release */
8550 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8551 phba->sli.mbox_active = mboxq;
8552 spin_unlock_irqrestore(&phba->hbalock, iflag);
8553
James Smart2d843ed2012-09-29 11:29:06 -04008554 /* wait for bootstrap mbox register for readyness */
8555 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8556 if (rc)
8557 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008558 /*
8559 * Initialize the bootstrap memory region to avoid stale data areas
8560 * in the mailbox post. Then copy the caller's mailbox contents to
8561 * the bmbx mailbox region.
8562 */
8563 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8564 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07008565 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8566 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008567
8568 /* Post the high mailbox dma address to the port and wait for ready. */
8569 dma_address = &phba->sli4_hba.bmbx.dma_address;
8570 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8571
James Smart2d843ed2012-09-29 11:29:06 -04008572 /* wait for bootstrap mbox register for hi-address write done */
8573 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8574 if (rc)
8575 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008576
8577 /* Post the low mailbox dma address to the port. */
8578 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008579
James Smart2d843ed2012-09-29 11:29:06 -04008580 /* wait for bootstrap mbox register for low address write done */
8581 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8582 if (rc)
8583 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008584
8585 /*
8586 * Read the CQ to ensure the mailbox has completed.
8587 * If so, update the mailbox status so that the upper layers
8588 * can complete the request normally.
8589 */
James Smart48f8fdb2018-05-04 20:37:51 -07008590 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8591 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008592 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07008593 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8594 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04008595 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008596 /*
8597 * When the CQE status indicates a failure and the mailbox status
8598 * indicates success then copy the CQE status into the mailbox status
8599 * (and prefix it with x4000).
8600 */
James Smartda0436e2009-05-22 14:51:39 -04008601 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008602 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8603 bf_set(lpfc_mqe_status, mb,
8604 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008605 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008606 } else
8607 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008608
8609 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008610 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008611 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8612 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008613 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8614 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8615 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008616 bf_get(lpfc_mqe_status, mb),
8617 mb->un.mb_words[0], mb->un.mb_words[1],
8618 mb->un.mb_words[2], mb->un.mb_words[3],
8619 mb->un.mb_words[4], mb->un.mb_words[5],
8620 mb->un.mb_words[6], mb->un.mb_words[7],
8621 mb->un.mb_words[8], mb->un.mb_words[9],
8622 mb->un.mb_words[10], mb->un.mb_words[11],
8623 mb->un.mb_words[12], mboxq->mcqe.word0,
8624 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8625 mboxq->mcqe.trailer);
8626exit:
8627 /* We are holding the token, no needed for lock when release */
8628 spin_lock_irqsave(&phba->hbalock, iflag);
8629 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8630 phba->sli.mbox_active = NULL;
8631 spin_unlock_irqrestore(&phba->hbalock, iflag);
8632 return rc;
8633}
8634
8635/**
8636 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8637 * @phba: Pointer to HBA context object.
8638 * @pmbox: Pointer to mailbox object.
8639 * @flag: Flag indicating how the mailbox need to be processed.
8640 *
8641 * This function is called by discovery code and HBA management code to submit
8642 * a mailbox command to firmware with SLI-4 interface spec.
8643 *
8644 * Return codes the caller owns the mailbox command after the return of the
8645 * function.
8646 **/
8647static int
8648lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8649 uint32_t flag)
8650{
8651 struct lpfc_sli *psli = &phba->sli;
8652 unsigned long iflags;
8653 int rc;
8654
James Smartb76f2dc2011-07-22 18:37:42 -04008655 /* dump from issue mailbox command if setup */
8656 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8657
James Smart8fa38512009-07-19 10:01:03 -04008658 rc = lpfc_mbox_dev_check(phba);
8659 if (unlikely(rc)) {
8660 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008661 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008662 "cannot issue Data: x%x x%x\n",
8663 mboxq->vport ? mboxq->vport->vpi : 0,
8664 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008665 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8666 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008667 psli->sli_flag, flag);
8668 goto out_not_finished;
8669 }
8670
James Smartda0436e2009-05-22 14:51:39 -04008671 /* Detect polling mode and jump to a handler */
8672 if (!phba->sli4_hba.intr_enable) {
8673 if (flag == MBX_POLL)
8674 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8675 else
8676 rc = -EIO;
8677 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008678 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008679 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008680 "(x%x/x%x) failure: "
8681 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8682 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008683 mboxq->vport ? mboxq->vport->vpi : 0,
8684 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008685 lpfc_sli_config_mbox_subsys_get(phba,
8686 mboxq),
8687 lpfc_sli_config_mbox_opcode_get(phba,
8688 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008689 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8690 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8691 bf_get(lpfc_mcqe_ext_status,
8692 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008693 psli->sli_flag, flag);
8694 return rc;
8695 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008696 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8697 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008698 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008699 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008700 mboxq->vport ? mboxq->vport->vpi : 0,
8701 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008702 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8703 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008704 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008705 /* Try to block the asynchronous mailbox posting */
8706 rc = lpfc_sli4_async_mbox_block(phba);
8707 if (!rc) {
8708 /* Successfully blocked, now issue sync mbox cmd */
8709 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8710 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008711 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008712 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008713 "(%d):2597 Sync Mailbox command "
8714 "x%x (x%x/x%x) failure: "
8715 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8716 "Data: x%x x%x\n,",
8717 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008718 mboxq->u.mb.mbxCommand,
8719 lpfc_sli_config_mbox_subsys_get(phba,
8720 mboxq),
8721 lpfc_sli_config_mbox_opcode_get(phba,
8722 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008723 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8724 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8725 bf_get(lpfc_mcqe_ext_status,
8726 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008727 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008728 /* Unblock the async mailbox posting afterward */
8729 lpfc_sli4_async_mbox_unblock(phba);
8730 }
8731 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008732 }
8733
8734 /* Now, interrupt mode asynchrous mailbox command */
8735 rc = lpfc_mbox_cmd_check(phba, mboxq);
8736 if (rc) {
8737 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008738 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008739 "cannot issue Data: x%x x%x\n",
8740 mboxq->vport ? mboxq->vport->vpi : 0,
8741 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008742 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8743 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008744 psli->sli_flag, flag);
8745 goto out_not_finished;
8746 }
James Smartda0436e2009-05-22 14:51:39 -04008747
8748 /* Put the mailbox command to the driver internal FIFO */
8749 psli->slistat.mbox_busy++;
8750 spin_lock_irqsave(&phba->hbalock, iflags);
8751 lpfc_mbox_put(phba, mboxq);
8752 spin_unlock_irqrestore(&phba->hbalock, iflags);
8753 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8754 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008755 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008756 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8757 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008758 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8759 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008760 phba->pport->port_state,
8761 psli->sli_flag, MBX_NOWAIT);
8762 /* Wake up worker thread to transport mailbox command from head */
8763 lpfc_worker_wake_up(phba);
8764
8765 return MBX_BUSY;
8766
8767out_not_finished:
8768 return MBX_NOT_FINISHED;
8769}
8770
8771/**
8772 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8773 * @phba: Pointer to HBA context object.
8774 *
8775 * This function is called by worker thread to send a mailbox command to
8776 * SLI4 HBA firmware.
8777 *
8778 **/
8779int
8780lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8781{
8782 struct lpfc_sli *psli = &phba->sli;
8783 LPFC_MBOXQ_t *mboxq;
8784 int rc = MBX_SUCCESS;
8785 unsigned long iflags;
8786 struct lpfc_mqe *mqe;
8787 uint32_t mbx_cmnd;
8788
8789 /* Check interrupt mode before post async mailbox command */
8790 if (unlikely(!phba->sli4_hba.intr_enable))
8791 return MBX_NOT_FINISHED;
8792
8793 /* Check for mailbox command service token */
8794 spin_lock_irqsave(&phba->hbalock, iflags);
8795 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8796 spin_unlock_irqrestore(&phba->hbalock, iflags);
8797 return MBX_NOT_FINISHED;
8798 }
8799 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8800 spin_unlock_irqrestore(&phba->hbalock, iflags);
8801 return MBX_NOT_FINISHED;
8802 }
8803 if (unlikely(phba->sli.mbox_active)) {
8804 spin_unlock_irqrestore(&phba->hbalock, iflags);
8805 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8806 "0384 There is pending active mailbox cmd\n");
8807 return MBX_NOT_FINISHED;
8808 }
8809 /* Take the mailbox command service token */
8810 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8811
8812 /* Get the next mailbox command from head of queue */
8813 mboxq = lpfc_mbox_get(phba);
8814
8815 /* If no more mailbox command waiting for post, we're done */
8816 if (!mboxq) {
8817 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8818 spin_unlock_irqrestore(&phba->hbalock, iflags);
8819 return MBX_SUCCESS;
8820 }
8821 phba->sli.mbox_active = mboxq;
8822 spin_unlock_irqrestore(&phba->hbalock, iflags);
8823
8824 /* Check device readiness for posting mailbox command */
8825 rc = lpfc_mbox_dev_check(phba);
8826 if (unlikely(rc))
8827 /* Driver clean routine will clean up pending mailbox */
8828 goto out_not_finished;
8829
8830 /* Prepare the mbox command to be posted */
8831 mqe = &mboxq->u.mqe;
8832 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8833
8834 /* Start timer for the mbox_tmo and log some mailbox post messages */
8835 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008836 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008837
8838 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008839 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008840 "x%x x%x\n",
8841 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008842 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8843 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008844 phba->pport->port_state, psli->sli_flag);
8845
8846 if (mbx_cmnd != MBX_HEARTBEAT) {
8847 if (mboxq->vport) {
8848 lpfc_debugfs_disc_trc(mboxq->vport,
8849 LPFC_DISC_TRC_MBOX_VPORT,
8850 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8851 mbx_cmnd, mqe->un.mb_words[0],
8852 mqe->un.mb_words[1]);
8853 } else {
8854 lpfc_debugfs_disc_trc(phba->pport,
8855 LPFC_DISC_TRC_MBOX,
8856 "MBOX Send: cmd:x%x mb:x%x x%x",
8857 mbx_cmnd, mqe->un.mb_words[0],
8858 mqe->un.mb_words[1]);
8859 }
8860 }
8861 psli->slistat.mbox_cmd++;
8862
8863 /* Post the mailbox command to the port */
8864 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8865 if (rc != MBX_SUCCESS) {
8866 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008867 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008868 "cannot issue Data: x%x x%x\n",
8869 mboxq->vport ? mboxq->vport->vpi : 0,
8870 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008871 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8872 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008873 psli->sli_flag, MBX_NOWAIT);
8874 goto out_not_finished;
8875 }
8876
8877 return rc;
8878
8879out_not_finished:
8880 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008881 if (phba->sli.mbox_active) {
8882 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8883 __lpfc_mbox_cmpl_put(phba, mboxq);
8884 /* Release the token */
8885 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8886 phba->sli.mbox_active = NULL;
8887 }
James Smartda0436e2009-05-22 14:51:39 -04008888 spin_unlock_irqrestore(&phba->hbalock, iflags);
8889
8890 return MBX_NOT_FINISHED;
8891}
8892
8893/**
8894 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8895 * @phba: Pointer to HBA context object.
8896 * @pmbox: Pointer to mailbox object.
8897 * @flag: Flag indicating how the mailbox need to be processed.
8898 *
8899 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8900 * the API jump table function pointer from the lpfc_hba struct.
8901 *
8902 * Return codes the caller owns the mailbox command after the return of the
8903 * function.
8904 **/
8905int
8906lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8907{
8908 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8909}
8910
8911/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008912 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008913 * @phba: The hba struct for which this call is being executed.
8914 * @dev_grp: The HBA PCI-Device group number.
8915 *
8916 * This routine sets up the mbox interface API function jump table in @phba
8917 * struct.
8918 * Returns: 0 - success, -ENODEV - failure.
8919 **/
8920int
8921lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8922{
8923
8924 switch (dev_grp) {
8925 case LPFC_PCI_DEV_LP:
8926 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8927 phba->lpfc_sli_handle_slow_ring_event =
8928 lpfc_sli_handle_slow_ring_event_s3;
8929 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8930 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8931 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8932 break;
8933 case LPFC_PCI_DEV_OC:
8934 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8935 phba->lpfc_sli_handle_slow_ring_event =
8936 lpfc_sli_handle_slow_ring_event_s4;
8937 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8938 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8939 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8940 break;
8941 default:
8942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8943 "1420 Invalid HBA PCI-device group: 0x%x\n",
8944 dev_grp);
8945 return -ENODEV;
8946 break;
8947 }
8948 return 0;
8949}
8950
8951/**
James Smart3621a712009-04-06 18:47:14 -04008952 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008953 * @phba: Pointer to HBA context object.
8954 * @pring: Pointer to driver SLI ring object.
8955 * @piocb: Pointer to address of newly added command iocb.
8956 *
8957 * This function is called with hbalock held to add a command
8958 * iocb to the txq when SLI layer cannot submit the command iocb
8959 * to the ring.
8960 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008961void
James Smart92d7f7b2007-06-17 19:56:38 -05008962__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008963 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008964{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008965 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008966 /* Insert the caller's iocb in the txq tail for later processing. */
8967 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008968}
8969
James Smarte59058c2008-08-24 21:49:00 -04008970/**
James Smart3621a712009-04-06 18:47:14 -04008971 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008972 * @phba: Pointer to HBA context object.
8973 * @pring: Pointer to driver SLI ring object.
8974 * @piocb: Pointer to address of newly added command iocb.
8975 *
8976 * This function is called with hbalock held before a new
8977 * iocb is submitted to the firmware. This function checks
8978 * txq to flush the iocbs in txq to Firmware before
8979 * submitting new iocbs to the Firmware.
8980 * If there are iocbs in the txq which need to be submitted
8981 * to firmware, lpfc_sli_next_iocb returns the first element
8982 * of the txq after dequeuing it from txq.
8983 * If there is no iocb in the txq then the function will return
8984 * *piocb and *piocb is set to NULL. Caller needs to check
8985 * *piocb to find if there are more commands in the txq.
8986 **/
dea31012005-04-17 16:05:31 -05008987static struct lpfc_iocbq *
8988lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008989 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008990{
8991 struct lpfc_iocbq * nextiocb;
8992
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008993 lockdep_assert_held(&phba->hbalock);
8994
dea31012005-04-17 16:05:31 -05008995 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8996 if (!nextiocb) {
8997 nextiocb = *piocb;
8998 *piocb = NULL;
8999 }
9000
9001 return nextiocb;
9002}
9003
James Smarte59058c2008-08-24 21:49:00 -04009004/**
James Smart3772a992009-05-22 14:50:54 -04009005 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009006 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04009007 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04009008 * @piocb: Pointer to command iocb.
9009 * @flag: Flag indicating if this command can be put into txq.
9010 *
James Smart3772a992009-05-22 14:50:54 -04009011 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
9012 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
9013 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
9014 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
9015 * this function allows only iocbs for posting buffers. This function finds
9016 * next available slot in the command ring and posts the command to the
9017 * available slot and writes the port attention register to request HBA start
9018 * processing new iocb. If there is no slot available in the ring and
9019 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9020 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04009021 *
James Smart3772a992009-05-22 14:50:54 -04009022 * This function is called with hbalock held. The function will return success
9023 * after it successfully submit the iocb to firmware or after adding to the
9024 * txq.
James Smarte59058c2008-08-24 21:49:00 -04009025 **/
James Smart98c9ea52007-10-27 13:37:33 -04009026static int
James Smart3772a992009-05-22 14:50:54 -04009027__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05009028 struct lpfc_iocbq *piocb, uint32_t flag)
9029{
9030 struct lpfc_iocbq *nextiocb;
9031 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08009032 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05009033
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009034 lockdep_assert_held(&phba->hbalock);
9035
James Smart92d7f7b2007-06-17 19:56:38 -05009036 if (piocb->iocb_cmpl && (!piocb->vport) &&
9037 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9038 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
9039 lpfc_printf_log(phba, KERN_ERR,
9040 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04009041 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05009042 piocb->iocb.ulpCommand);
9043 dump_stack();
9044 return IOCB_ERROR;
9045 }
9046
9047
Linas Vepstas8d63f372007-02-14 14:28:36 -06009048 /* If the PCI channel is in offline state, do not post iocbs. */
9049 if (unlikely(pci_channel_offline(phba->pcidev)))
9050 return IOCB_ERROR;
9051
James Smarta257bf92009-04-06 18:48:10 -04009052 /* If HBA has a deferred error attention, fail the iocb. */
9053 if (unlikely(phba->hba_flag & DEFER_ERATT))
9054 return IOCB_ERROR;
9055
dea31012005-04-17 16:05:31 -05009056 /*
9057 * We should never get an IOCB if we are in a < LINK_DOWN state
9058 */
James Smart2e0fef82007-06-17 19:56:36 -05009059 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05009060 return IOCB_ERROR;
9061
9062 /*
9063 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04009064 * outstanding event.
dea31012005-04-17 16:05:31 -05009065 */
James Smart0b727fe2007-10-27 13:37:25 -04009066 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05009067 goto iocb_busy;
9068
James Smart2e0fef82007-06-17 19:56:36 -05009069 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05009070 /*
James Smart2680eea2007-04-25 09:52:55 -04009071 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05009072 * can be issued if the link is not up.
9073 */
9074 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04009075 case CMD_GEN_REQUEST64_CR:
9076 case CMD_GEN_REQUEST64_CX:
9077 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9078 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04009079 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04009080 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9081 MENLO_TRANSPORT_TYPE))
9082
9083 goto iocb_busy;
9084 break;
dea31012005-04-17 16:05:31 -05009085 case CMD_QUE_RING_BUF_CN:
9086 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05009087 /*
9088 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9089 * completion, iocb_cmpl MUST be 0.
9090 */
9091 if (piocb->iocb_cmpl)
9092 piocb->iocb_cmpl = NULL;
9093 /*FALLTHROUGH*/
9094 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04009095 case CMD_CLOSE_XRI_CN:
9096 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05009097 break;
9098 default:
9099 goto iocb_busy;
9100 }
9101
9102 /*
9103 * For FCP commands, we must be in a state where we can process link
9104 * attention events.
9105 */
James Smart895427b2017-02-12 13:52:30 -08009106 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05009107 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05009108 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05009109 }
dea31012005-04-17 16:05:31 -05009110
dea31012005-04-17 16:05:31 -05009111 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9112 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9113 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9114
9115 if (iocb)
9116 lpfc_sli_update_ring(phba, pring);
9117 else
9118 lpfc_sli_update_full_ring(phba, pring);
9119
9120 if (!piocb)
9121 return IOCB_SUCCESS;
9122
9123 goto out_busy;
9124
9125 iocb_busy:
9126 pring->stats.iocb_cmd_delay++;
9127
9128 out_busy:
9129
9130 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009131 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05009132 return IOCB_SUCCESS;
9133 }
9134
9135 return IOCB_BUSY;
9136}
9137
James Smart3772a992009-05-22 14:50:54 -04009138/**
James Smart4f774512009-05-22 14:52:35 -04009139 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9140 * @phba: Pointer to HBA context object.
9141 * @piocb: Pointer to command iocb.
9142 * @sglq: Pointer to the scatter gather queue object.
9143 *
9144 * This routine converts the bpl or bde that is in the IOCB
9145 * to a sgl list for the sli4 hardware. The physical address
9146 * of the bpl/bde is converted back to a virtual address.
9147 * If the IOCB contains a BPL then the list of BDE's is
9148 * converted to sli4_sge's. If the IOCB contains a single
9149 * BDE then it is converted to a single sli_sge.
9150 * The IOCB is still in cpu endianess so the contents of
9151 * the bpl can be used without byte swapping.
9152 *
9153 * Returns valid XRI = Success, NO_XRI = Failure.
9154**/
9155static uint16_t
9156lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9157 struct lpfc_sglq *sglq)
9158{
9159 uint16_t xritag = NO_XRI;
9160 struct ulp_bde64 *bpl = NULL;
9161 struct ulp_bde64 bde;
9162 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05009163 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04009164 IOCB_t *icmd;
9165 int numBdes = 0;
9166 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05009167 uint32_t offset = 0; /* accumulated offset in the sg request list */
9168 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04009169
9170 if (!piocbq || !sglq)
9171 return xritag;
9172
9173 sgl = (struct sli4_sge *)sglq->sgl;
9174 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05009175 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9176 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009177 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9178 numBdes = icmd->un.genreq64.bdl.bdeSize /
9179 sizeof(struct ulp_bde64);
9180 /* The addrHigh and addrLow fields within the IOCB
9181 * have not been byteswapped yet so there is no
9182 * need to swap them back.
9183 */
James Smart1b511972011-12-13 13:23:09 -05009184 if (piocbq->context3)
9185 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9186 else
9187 return xritag;
James Smart4f774512009-05-22 14:52:35 -04009188
James Smart1b511972011-12-13 13:23:09 -05009189 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04009190 if (!bpl)
9191 return xritag;
9192
9193 for (i = 0; i < numBdes; i++) {
9194 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05009195 sgl->addr_hi = bpl->addrHigh;
9196 sgl->addr_lo = bpl->addrLow;
9197
James Smart05580562011-05-24 11:40:48 -04009198 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009199 if ((i+1) == numBdes)
9200 bf_set(lpfc_sli4_sge_last, sgl, 1);
9201 else
9202 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05009203 /* swap the size field back to the cpu so we
9204 * can assign it to the sgl.
9205 */
9206 bde.tus.w = le32_to_cpu(bpl->tus.w);
9207 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05009208 /* The offsets in the sgl need to be accumulated
9209 * separately for the request and reply lists.
9210 * The request is always first, the reply follows.
9211 */
9212 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9213 /* add up the reply sg entries */
9214 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9215 inbound++;
9216 /* first inbound? reset the offset */
9217 if (inbound == 1)
9218 offset = 0;
9219 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04009220 bf_set(lpfc_sli4_sge_type, sgl,
9221 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05009222 offset += bde.tus.f.bdeSize;
9223 }
James Smart546fc852011-03-11 16:06:29 -05009224 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009225 bpl++;
9226 sgl++;
9227 }
9228 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9229 /* The addrHigh and addrLow fields of the BDE have not
9230 * been byteswapped yet so they need to be swapped
9231 * before putting them in the sgl.
9232 */
9233 sgl->addr_hi =
9234 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9235 sgl->addr_lo =
9236 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04009237 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009238 bf_set(lpfc_sli4_sge_last, sgl, 1);
9239 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05009240 sgl->sge_len =
9241 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04009242 }
9243 return sglq->sli4_xritag;
9244}
9245
9246/**
James Smart4f774512009-05-22 14:52:35 -04009247 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9248 * @phba: Pointer to HBA context object.
9249 * @piocb: Pointer to command iocb.
9250 * @wqe: Pointer to the work queue entry.
9251 *
9252 * This routine converts the iocb command to its Work Queue Entry
9253 * equivalent. The wqe pointer should not have any fields set when
9254 * this routine is called because it will memcpy over them.
9255 * This routine does not set the CQ_ID or the WQEC bits in the
9256 * wqe.
9257 *
9258 * Returns: 0 = Success, IOCB_ERROR = Failure.
9259 **/
9260static int
9261lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08009262 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04009263{
James Smart5ffc2662009-11-18 15:39:44 -05009264 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04009265 uint8_t ct = 0;
9266 uint32_t fip;
9267 uint32_t abort_tag;
9268 uint8_t command_type = ELS_COMMAND_NON_FIP;
9269 uint8_t cmnd;
9270 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04009271 uint16_t abrt_iotag;
9272 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04009273 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04009274 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05009275 int numBdes, i;
9276 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04009277 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05009278 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05009279 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04009280
James Smart45ed1192009-10-02 15:17:02 -04009281 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04009282 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04009283 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04009284 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05009285 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04009286 command_type = ELS_COMMAND_FIP;
9287 else
9288 command_type = ELS_COMMAND_NON_FIP;
9289
James Smartb5c53952016-03-31 14:12:30 -07009290 if (phba->fcp_embed_io)
9291 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04009292 /* Some of the fields are in the right position already */
9293 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07009294 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
9295 /* The ct field has moved so reset */
9296 wqe->generic.wqe_com.word7 = 0;
9297 wqe->generic.wqe_com.word10 = 0;
9298 }
James Smartb5c53952016-03-31 14:12:30 -07009299
9300 abort_tag = (uint32_t) iocbq->iotag;
9301 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009302 /* words0-2 bpl convert bde */
9303 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05009304 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9305 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04009306 bpl = (struct ulp_bde64 *)
9307 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9308 if (!bpl)
9309 return IOCB_ERROR;
9310
9311 /* Should already be byte swapped. */
9312 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
9313 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
9314 /* swap the size field back to the cpu so we
9315 * can assign it to the sgl.
9316 */
9317 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05009318 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9319 total_len = 0;
9320 for (i = 0; i < numBdes; i++) {
9321 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9322 total_len += bde.tus.f.bdeSize;
9323 }
James Smart4f774512009-05-22 14:52:35 -04009324 } else
James Smart5ffc2662009-11-18 15:39:44 -05009325 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04009326
9327 iocbq->iocb.ulpIoTag = iocbq->iotag;
9328 cmnd = iocbq->iocb.ulpCommand;
9329
9330 switch (iocbq->iocb.ulpCommand) {
9331 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04009332 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9333 ndlp = iocbq->context_un.ndlp;
9334 else
9335 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009336 if (!iocbq->iocb.ulpLe) {
9337 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9338 "2007 Only Limited Edition cmd Format"
9339 " supported 0x%x\n",
9340 iocbq->iocb.ulpCommand);
9341 return IOCB_ERROR;
9342 }
James Smartff78d8f2011-12-13 13:21:35 -05009343
James Smart5ffc2662009-11-18 15:39:44 -05009344 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009345 /* Els_reguest64 has a TMO */
9346 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9347 iocbq->iocb.ulpTimeout);
9348 /* Need a VF for word 4 set the vf bit*/
9349 bf_set(els_req64_vf, &wqe->els_req, 0);
9350 /* And a VFID for word 12 */
9351 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04009352 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04009353 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9354 iocbq->iocb.ulpContext);
9355 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9356 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04009357 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05009358 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05009359 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9360 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05009361 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9362 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05009363 if_type = bf_get(lpfc_sli_intf_if_type,
9364 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009365 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05009366 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05009367 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05009368 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05009369 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05009370 *pcmd == ELS_CMD_PLOGI)) {
9371 bf_set(els_req64_sp, &wqe->els_req, 1);
9372 bf_set(els_req64_sid, &wqe->els_req,
9373 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009374 if ((*pcmd == ELS_CMD_FLOGI) &&
9375 !(phba->fc_topology ==
9376 LPFC_TOPOLOGY_LOOP))
9377 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009378 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9379 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009380 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009381 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009382 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9383 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9384 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9385 }
James Smartc8685952009-11-18 15:39:16 -05009386 }
James Smart6d368e52011-05-24 11:44:12 -04009387 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9388 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009389 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9390 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9391 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9392 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9393 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9394 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009395 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009396 break;
James Smart5ffc2662009-11-18 15:39:44 -05009397 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009398 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9399 iocbq->iocb.un.ulpWord[3]);
9400 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009401 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009402 /* The entire sequence is transmitted for this IOCB */
9403 xmit_len = total_len;
9404 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009405 if (phba->link_flag & LS_LOOPBACK_MODE)
9406 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04009407 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009408 /* word3 iocb=io_tag32 wqe=reserved */
9409 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009410 /* word4 relative_offset memcpy */
9411 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009412 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9413 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9414 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9415 LPFC_WQE_IOD_WRITE);
9416 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9417 LPFC_WQE_LENLOC_WORD12);
9418 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009419 wqe->xmit_sequence.xmit_len = xmit_len;
9420 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009421 break;
James Smart4f774512009-05-22 14:52:35 -04009422 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009423 /* word3 iocb=iotag32 wqe=seq_payload_len */
9424 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009425 /* word4 iocb=rsvd wqe=rsvd */
9426 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9427 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009428 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009429 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009430 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9431 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9432 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9433 LPFC_WQE_LENLOC_WORD3);
9434 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009435 break;
James Smart4f774512009-05-22 14:52:35 -04009436 case CMD_FCP_IWRITE64_CR:
9437 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009438 /* word3 iocb=iotag wqe=payload_offset_len */
9439 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009440 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9441 xmit_len + sizeof(struct fcp_rsp));
9442 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9443 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009444 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9445 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9446 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9447 iocbq->iocb.ulpFCP2Rcvy);
9448 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9449 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009450 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9451 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9452 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009453 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009454 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009455 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9456 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009457 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9458 if (iocbq->priority) {
9459 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9460 (iocbq->priority << 1));
9461 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009462 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9463 (phba->cfg_XLanePriority << 1));
9464 }
9465 }
James Smartb5c53952016-03-31 14:12:30 -07009466 /* Note, word 10 is already initialized to 0 */
9467
James Smart414abe02018-06-26 08:24:26 -07009468 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9469 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009470 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9471 else
9472 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9473
James Smartb5c53952016-03-31 14:12:30 -07009474 if (phba->fcp_embed_io) {
9475 struct lpfc_scsi_buf *lpfc_cmd;
9476 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009477 struct fcp_cmnd *fcp_cmnd;
9478 uint32_t *ptr;
9479
9480 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009481
9482 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009483 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009484 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9485
9486 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009487 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009488 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009489 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9490 wqe->generic.bde.addrHigh = 0;
9491 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009492
James Smart205e8242018-03-05 12:04:03 -08009493 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9494 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009495
9496 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009497 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009498 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9499 }
James Smart7851fe22011-07-22 18:36:52 -04009500 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009501 case CMD_FCP_IREAD64_CR:
9502 /* word3 iocb=iotag wqe=payload_offset_len */
9503 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009504 bf_set(payload_offset_len, &wqe->fcp_iread,
9505 xmit_len + sizeof(struct fcp_rsp));
9506 bf_set(cmd_buff_len, &wqe->fcp_iread,
9507 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009508 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9509 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9510 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9511 iocbq->iocb.ulpFCP2Rcvy);
9512 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009513 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009514 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9515 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9516 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009517 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009518 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009519 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9520 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009521 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9522 if (iocbq->priority) {
9523 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9524 (iocbq->priority << 1));
9525 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009526 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9527 (phba->cfg_XLanePriority << 1));
9528 }
9529 }
James Smartb5c53952016-03-31 14:12:30 -07009530 /* Note, word 10 is already initialized to 0 */
9531
James Smart414abe02018-06-26 08:24:26 -07009532 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9533 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009534 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9535 else
9536 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9537
James Smartb5c53952016-03-31 14:12:30 -07009538 if (phba->fcp_embed_io) {
9539 struct lpfc_scsi_buf *lpfc_cmd;
9540 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009541 struct fcp_cmnd *fcp_cmnd;
9542 uint32_t *ptr;
9543
9544 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009545
9546 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009547 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009548 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9549
9550 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009551 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009552 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009553 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9554 wqe->generic.bde.addrHigh = 0;
9555 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009556
James Smart205e8242018-03-05 12:04:03 -08009557 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9558 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009559
9560 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009561 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009562 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9563 }
James Smart7851fe22011-07-22 18:36:52 -04009564 break;
James Smartf1126682009-06-10 17:22:44 -04009565 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009566 /* word3 iocb=iotag wqe=payload_offset_len */
9567 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9568 bf_set(payload_offset_len, &wqe->fcp_icmd,
9569 xmit_len + sizeof(struct fcp_rsp));
9570 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9571 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009572 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009573 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009574 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009575 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9576 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9577 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9578 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9579 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009580 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9581 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009582 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9583 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009584 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9585 if (iocbq->priority) {
9586 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9587 (iocbq->priority << 1));
9588 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009589 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9590 (phba->cfg_XLanePriority << 1));
9591 }
9592 }
James Smartb5c53952016-03-31 14:12:30 -07009593 /* Note, word 10 is already initialized to 0 */
9594
9595 if (phba->fcp_embed_io) {
9596 struct lpfc_scsi_buf *lpfc_cmd;
9597 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009598 struct fcp_cmnd *fcp_cmnd;
9599 uint32_t *ptr;
9600
9601 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009602
9603 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009604 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009605 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9606
9607 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009608 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009609 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009610 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9611 wqe->generic.bde.addrHigh = 0;
9612 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009613
James Smart205e8242018-03-05 12:04:03 -08009614 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9615 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009616
9617 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009618 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009619 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9620 }
James Smart7851fe22011-07-22 18:36:52 -04009621 break;
James Smart4f774512009-05-22 14:52:35 -04009622 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009623 /* For this command calculate the xmit length of the
9624 * request bde.
9625 */
9626 xmit_len = 0;
9627 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9628 sizeof(struct ulp_bde64);
9629 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009630 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009631 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9632 break;
James Smart63e801c2010-11-20 23:14:19 -05009633 xmit_len += bde.tus.f.bdeSize;
9634 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009635 /* word3 iocb=IO_TAG wqe=request_payload_len */
9636 wqe->gen_req.request_payload_len = xmit_len;
9637 /* word4 iocb=parameter wqe=relative_offset memcpy */
9638 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009639 /* word6 context tag copied in memcpy */
9640 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9641 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9642 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9643 "2015 Invalid CT %x command 0x%x\n",
9644 ct, iocbq->iocb.ulpCommand);
9645 return IOCB_ERROR;
9646 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009647 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9648 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9649 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9650 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9651 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9652 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9653 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9654 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009655 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009656 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009657 break;
James Smart4f774512009-05-22 14:52:35 -04009658 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009659 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009660 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009661 /* word3 iocb=iotag32 wqe=response_payload_len */
9662 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009663 /* word4 */
9664 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009665 /* word5 iocb=rsvd wge=did */
9666 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009667 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9668
9669 if_type = bf_get(lpfc_sli_intf_if_type,
9670 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009671 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009672 if (iocbq->vport->fc_flag & FC_PT2PT) {
9673 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9674 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9675 iocbq->vport->fc_myDID);
9676 if (iocbq->vport->fc_myDID == Fabric_DID) {
9677 bf_set(wqe_els_did,
9678 &wqe->xmit_els_rsp.wqe_dest, 0);
9679 }
9680 }
9681 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009682 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9683 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9684 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9685 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009686 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009687 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009688 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009689 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009690 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9691 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9692 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9693 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9694 LPFC_WQE_LENLOC_WORD3);
9695 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009696 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9697 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009698 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9699 iocbq->context2)->virt);
9700 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009701 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9702 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009703 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009704 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9705 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009706 phba->vpi_ids[phba->pport->vpi]);
9707 }
James Smart4f774512009-05-22 14:52:35 -04009708 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009709 break;
James Smart4f774512009-05-22 14:52:35 -04009710 case CMD_CLOSE_XRI_CN:
9711 case CMD_ABORT_XRI_CN:
9712 case CMD_ABORT_XRI_CX:
9713 /* words 0-2 memcpy should be 0 rserved */
9714 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009715 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9716 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9717 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9718 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9719 } else
9720 fip = 0;
9721
9722 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009723 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009724 * The link is down, or the command was ELS_FIP
9725 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009726 * on the wire.
9727 */
9728 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9729 else
9730 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9731 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009732 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9733 wqe->abort_cmd.rsrvd5 = 0;
9734 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009735 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9736 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009737 /*
9738 * The abort handler will send us CMD_ABORT_XRI_CN or
9739 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9740 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009741 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9742 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9743 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9744 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009745 cmnd = CMD_ABORT_XRI_CX;
9746 command_type = OTHER_COMMAND;
9747 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009748 break;
James Smart6669f9b2009-10-02 15:16:45 -04009749 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009750 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009751 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009752 * we re-construct this WQE here based on information in
9753 * iocbq from scratch.
9754 */
9755 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009756 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009757 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009758 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9759 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009760 LPFC_ABTS_UNSOL_INT) {
9761 /* ABTS sent by initiator to CT exchange, the
9762 * RX_ID field will be filled with the newly
9763 * allocated responder XRI.
9764 */
9765 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9766 iocbq->sli4_xritag);
9767 } else {
9768 /* ABTS sent by responder to CT exchange, the
9769 * RX_ID field will be filled with the responder
9770 * RX_ID from ABTS.
9771 */
9772 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009773 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009774 }
James Smart6669f9b2009-10-02 15:16:45 -04009775 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9776 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009777
9778 /* Use CT=VPI */
9779 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9780 ndlp->nlp_DID);
9781 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9782 iocbq->iocb.ulpContext);
9783 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009784 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009785 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009786 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9787 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9788 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009789 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9790 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009791 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9792 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9793 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9794 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9795 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9796 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9797 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9798 }
9799
James Smart7851fe22011-07-22 18:36:52 -04009800 break;
James Smartae9e28f2017-05-15 15:20:51 -07009801 case CMD_SEND_FRAME:
9802 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9803 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9804 return 0;
James Smart4f774512009-05-22 14:52:35 -04009805 case CMD_XRI_ABORTED_CX:
9806 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009807 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9808 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9809 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9810 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9811 default:
9812 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9813 "2014 Invalid command 0x%x\n",
9814 iocbq->iocb.ulpCommand);
9815 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009816 break;
James Smart4f774512009-05-22 14:52:35 -04009817 }
James Smart6d368e52011-05-24 11:44:12 -04009818
James Smart8012cc32012-10-31 14:44:49 -04009819 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9820 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9821 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9822 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9823 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9824 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9825 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9826 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009827 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9828 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9829 wqe->generic.wqe_com.abort_tag = abort_tag;
9830 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9831 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9832 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9833 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009834 return 0;
9835}
9836
9837/**
9838 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9839 * @phba: Pointer to HBA context object.
9840 * @ring_number: SLI ring number to issue iocb on.
9841 * @piocb: Pointer to command iocb.
9842 * @flag: Flag indicating if this command can be put into txq.
9843 *
9844 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9845 * an iocb command to an HBA with SLI-4 interface spec.
9846 *
9847 * This function is called with hbalock held. The function will return success
9848 * after it successfully submit the iocb to firmware or after adding to the
9849 * txq.
9850 **/
9851static int
9852__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9853 struct lpfc_iocbq *piocb, uint32_t flag)
9854{
9855 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -08009856 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -05009857 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009858 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009859
James Smart895427b2017-02-12 13:52:30 -08009860 /* Get the WQ */
9861 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9862 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -08009863 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].fcp_wq;
James Smart895427b2017-02-12 13:52:30 -08009864 } else {
9865 wq = phba->sli4_hba.els_wq;
9866 }
9867
9868 /* Get corresponding ring */
9869 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009870
James Smartb5c53952016-03-31 14:12:30 -07009871 /*
9872 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -07009873 */
James Smartb5c53952016-03-31 14:12:30 -07009874
James Smart895427b2017-02-12 13:52:30 -08009875 lockdep_assert_held(&phba->hbalock);
9876
James Smart4f774512009-05-22 14:52:35 -04009877 if (piocb->sli4_xritag == NO_XRI) {
9878 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009879 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009880 sglq = NULL;
9881 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009882 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009883 if (!(flag & SLI_IOCB_RET_IOCB)) {
9884 __lpfc_sli_ringtx_put(phba,
9885 pring, piocb);
9886 return IOCB_SUCCESS;
9887 } else {
9888 return IOCB_BUSY;
9889 }
9890 } else {
James Smart895427b2017-02-12 13:52:30 -08009891 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009892 if (!sglq) {
9893 if (!(flag & SLI_IOCB_RET_IOCB)) {
9894 __lpfc_sli_ringtx_put(phba,
9895 pring,
9896 piocb);
9897 return IOCB_SUCCESS;
9898 } else
9899 return IOCB_BUSY;
9900 }
9901 }
James Smart4f774512009-05-22 14:52:35 -04009902 }
James Smart2ea259e2017-02-12 13:52:27 -08009903 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009904 /* These IO's already have an XRI and a mapped sgl. */
9905 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009906 else {
James Smart6d368e52011-05-24 11:44:12 -04009907 /*
9908 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009909 * sglq is on the active list
9910 */
James Smartedccdc12013-01-03 15:43:45 -05009911 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009912 if (!sglq)
9913 return IOCB_ERROR;
9914 }
9915
9916 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009917 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009918 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009919 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009920 return IOCB_ERROR;
9921 }
9922
James Smart205e8242018-03-05 12:04:03 -08009923 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -04009924 return IOCB_ERROR;
9925
James Smart205e8242018-03-05 12:04:03 -08009926 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -08009927 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009928 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9929
9930 return 0;
9931}
9932
9933/**
James Smart3772a992009-05-22 14:50:54 -04009934 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9935 *
9936 * This routine wraps the actual lockless version for issusing IOCB function
9937 * pointer from the lpfc_hba struct.
9938 *
9939 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009940 * IOCB_ERROR - Error
9941 * IOCB_SUCCESS - Success
9942 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009943 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009944int
James Smart3772a992009-05-22 14:50:54 -04009945__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9946 struct lpfc_iocbq *piocb, uint32_t flag)
9947{
9948 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9949}
9950
9951/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009952 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009953 * @phba: The hba struct for which this call is being executed.
9954 * @dev_grp: The HBA PCI-Device group number.
9955 *
9956 * This routine sets up the SLI interface API function jump table in @phba
9957 * struct.
9958 * Returns: 0 - success, -ENODEV - failure.
9959 **/
9960int
9961lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9962{
9963
9964 switch (dev_grp) {
9965 case LPFC_PCI_DEV_LP:
9966 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9967 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9968 break;
James Smart4f774512009-05-22 14:52:35 -04009969 case LPFC_PCI_DEV_OC:
9970 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9971 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9972 break;
James Smart3772a992009-05-22 14:50:54 -04009973 default:
9974 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9975 "1419 Invalid HBA PCI-device group: 0x%x\n",
9976 dev_grp);
9977 return -ENODEV;
9978 break;
9979 }
9980 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9981 return 0;
9982}
James Smart92d7f7b2007-06-17 19:56:38 -05009983
James Smarta1efe162015-05-21 13:55:20 -04009984/**
James Smart895427b2017-02-12 13:52:30 -08009985 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009986 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009987 * @piocb: Pointer to command iocb.
9988 *
James Smart895427b2017-02-12 13:52:30 -08009989 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9990 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009991 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009992 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009993 */
James Smart895427b2017-02-12 13:52:30 -08009994struct lpfc_sli_ring *
9995lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009996{
James Smart5e5b5112019-01-28 11:14:22 -08009997 struct lpfc_scsi_buf *lpfc_cmd;
9998
James Smart895427b2017-02-12 13:52:30 -08009999 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -080010000 if (unlikely(!phba->sli4_hba.hdwq))
James Smart7370d102019-01-28 11:14:20 -080010001 return NULL;
10002 /*
10003 * for abort iocb hba_wqidx should already
10004 * be setup based on what work queue we used.
10005 */
10006 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5e5b5112019-01-28 11:14:22 -080010007 lpfc_cmd = (struct lpfc_scsi_buf *)piocb->context1;
James Smart1fbf9742019-01-28 11:14:26 -080010008 piocb->hba_wqidx = lpfc_cmd->hdwq_no;
James Smart9bd2bff52014-09-03 12:57:30 -040010009 }
James Smartcdb42be2019-01-28 11:14:21 -080010010 return phba->sli4_hba.hdwq[piocb->hba_wqidx].fcp_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010011 } else {
10012 if (unlikely(!phba->sli4_hba.els_wq))
10013 return NULL;
10014 piocb->hba_wqidx = 0;
10015 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -040010016 }
James Smart9bd2bff52014-09-03 12:57:30 -040010017}
10018
James Smarte59058c2008-08-24 21:49:00 -040010019/**
James Smart3621a712009-04-06 18:47:14 -040010020 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040010021 * @phba: Pointer to HBA context object.
10022 * @pring: Pointer to driver SLI ring object.
10023 * @piocb: Pointer to command iocb.
10024 * @flag: Flag indicating if this command can be put into txq.
10025 *
10026 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10027 * function. This function gets the hbalock and calls
10028 * __lpfc_sli_issue_iocb function and will return the error returned
10029 * by __lpfc_sli_issue_iocb function. This wrapper is used by
10030 * functions which do not hold hbalock.
10031 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010032int
James Smart3772a992009-05-22 14:50:54 -040010033lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -050010034 struct lpfc_iocbq *piocb, uint32_t flag)
10035{
James Smart895427b2017-02-12 13:52:30 -080010036 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -040010037 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -040010038 struct lpfc_queue *fpeq;
10039 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -050010040 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040010041 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -050010042
James Smart7e56aa22012-08-03 12:35:34 -040010043 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010044 pring = lpfc_sli4_calc_ring(phba, piocb);
10045 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010046 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -040010047
James Smart9bd2bff52014-09-03 12:57:30 -040010048 spin_lock_irqsave(&pring->ring_lock, iflags);
10049 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10050 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -040010051
James Smart9bd2bff52014-09-03 12:57:30 -040010052 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -080010053 idx = piocb->hba_wqidx;
10054 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -040010055
James Smart895427b2017-02-12 13:52:30 -080010056 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -040010057
James Smart9bd2bff52014-09-03 12:57:30 -040010058 /* Get associated EQ with this index */
James Smartcdb42be2019-01-28 11:14:21 -080010059 fpeq = phba->sli4_hba.hdwq[idx].hba_eq;
James Smartba20c852012-08-03 12:36:52 -040010060
James Smart9bd2bff52014-09-03 12:57:30 -040010061 /* Turn off interrupts from this EQ */
James Smartb71413d2018-02-22 08:18:40 -080010062 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -040010063
James Smart9bd2bff52014-09-03 12:57:30 -040010064 /*
10065 * Process all the events on FCP EQ
10066 */
10067 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
10068 lpfc_sli4_hba_handle_eqe(phba,
10069 eqe, idx);
10070 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -040010071 }
James Smartba20c852012-08-03 12:36:52 -040010072
James Smart9bd2bff52014-09-03 12:57:30 -040010073 /* Always clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -080010074 phba->sli4_hba.sli4_eq_release(fpeq,
James Smart9bd2bff52014-09-03 12:57:30 -040010075 LPFC_QUEUE_REARM);
10076 }
James Smart895427b2017-02-12 13:52:30 -080010077 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -040010078 }
James Smart7e56aa22012-08-03 12:35:34 -040010079 } else {
10080 /* For now, SLI2/3 will still use hbalock */
10081 spin_lock_irqsave(&phba->hbalock, iflags);
10082 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10083 spin_unlock_irqrestore(&phba->hbalock, iflags);
10084 }
James Smart92d7f7b2007-06-17 19:56:38 -050010085 return rc;
10086}
10087
James Smarte59058c2008-08-24 21:49:00 -040010088/**
James Smart3621a712009-04-06 18:47:14 -040010089 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010090 * @phba: Pointer to HBA context object.
10091 *
10092 * This function is called while driver attaches with the
10093 * HBA to setup the extra ring. The extra ring is used
10094 * only when driver needs to support target mode functionality
10095 * or IP over FC functionalities.
10096 *
James Smart895427b2017-02-12 13:52:30 -080010097 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -040010098 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010099static int
10100lpfc_extra_ring_setup( struct lpfc_hba *phba)
10101{
10102 struct lpfc_sli *psli;
10103 struct lpfc_sli_ring *pring;
10104
10105 psli = &phba->sli;
10106
10107 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -050010108
10109 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -080010110 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -040010111 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10112 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10113 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10114 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010115
James Smarta4bc3372006-12-02 13:34:16 -050010116 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -080010117 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -050010118
James Smart7e56aa22012-08-03 12:35:34 -040010119 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10120 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10121 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10122 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010123
10124 /* Setup default profile for this ring */
10125 pring->iotag_max = 4096;
10126 pring->num_mask = 1;
10127 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -050010128 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10129 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010130 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10131 return 0;
10132}
10133
James Smartcb69f7d2011-12-13 13:21:57 -050010134/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10135 * @phba: Pointer to HBA context object.
10136 * @iocbq: Pointer to iocb object.
10137 *
10138 * The async_event handler calls this routine when it receives
10139 * an ASYNC_STATUS_CN event from the port. The port generates
10140 * this event when an Abort Sequence request to an rport fails
10141 * twice in succession. The abort could be originated by the
10142 * driver or by the port. The ABTS could have been for an ELS
10143 * or FCP IO. The port only generates this event when an ABTS
10144 * fails to complete after one retry.
10145 */
10146static void
10147lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10148 struct lpfc_iocbq *iocbq)
10149{
10150 struct lpfc_nodelist *ndlp = NULL;
10151 uint16_t rpi = 0, vpi = 0;
10152 struct lpfc_vport *vport = NULL;
10153
10154 /* The rpi in the ulpContext is vport-sensitive. */
10155 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10156 rpi = iocbq->iocb.ulpContext;
10157
10158 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10159 "3092 Port generated ABTS async event "
10160 "on vpi %d rpi %d status 0x%x\n",
10161 vpi, rpi, iocbq->iocb.ulpStatus);
10162
10163 vport = lpfc_find_vport_by_vpid(phba, vpi);
10164 if (!vport)
10165 goto err_exit;
10166 ndlp = lpfc_findnode_rpi(vport, rpi);
10167 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
10168 goto err_exit;
10169
10170 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10171 lpfc_sli_abts_recover_port(vport, ndlp);
10172 return;
10173
10174 err_exit:
10175 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10176 "3095 Event Context not found, no "
10177 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10178 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10179 vpi, rpi);
10180}
10181
10182/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10183 * @phba: pointer to HBA context object.
10184 * @ndlp: nodelist pointer for the impacted rport.
10185 * @axri: pointer to the wcqe containing the failed exchange.
10186 *
10187 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10188 * port. The port generates this event when an abort exchange request to an
10189 * rport fails twice in succession with no reply. The abort could be originated
10190 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
10191 */
10192void
10193lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10194 struct lpfc_nodelist *ndlp,
10195 struct sli4_wcqe_xri_aborted *axri)
10196{
10197 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -050010198 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -050010199
James Smart6b5151f2012-01-18 16:24:06 -050010200 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -050010201 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10202 "3115 Node Context not found, driver "
10203 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -050010204 return;
10205 }
10206
James Smartcb69f7d2011-12-13 13:21:57 -050010207 vport = ndlp->vport;
10208 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10209 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -050010210 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -040010211 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -050010212 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -050010213 bf_get(lpfc_wcqe_xa_status, axri),
10214 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -050010215
James Smart5c1db2a2012-03-01 22:34:36 -050010216 /*
10217 * Catch the ABTS protocol failure case. Older OCe FW releases returned
10218 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10219 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10220 */
James Smarte3d2b802012-08-14 14:25:43 -040010221 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -050010222 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10223 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -050010224 lpfc_sli_abts_recover_port(vport, ndlp);
10225}
10226
James Smarte59058c2008-08-24 21:49:00 -040010227/**
James Smart3621a712009-04-06 18:47:14 -040010228 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -040010229 * @phba: Pointer to HBA context object.
10230 * @pring: Pointer to driver SLI ring object.
10231 * @iocbq: Pointer to iocb object.
10232 *
10233 * This function is called by the slow ring event handler
10234 * function when there is an ASYNC event iocb in the ring.
10235 * This function is called with no lock held.
10236 * Currently this function handles only temperature related
10237 * ASYNC events. The function decodes the temperature sensor
10238 * event message and posts events for the management applications.
10239 **/
James Smart98c9ea52007-10-27 13:37:33 -040010240static void
James Smart57127f12007-10-27 13:37:05 -040010241lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10242 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10243{
10244 IOCB_t *icmd;
10245 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -040010246 struct temp_event temp_event_data;
10247 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -040010248 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -040010249
10250 icmd = &iocbq->iocb;
10251 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -040010252
James Smartcb69f7d2011-12-13 13:21:57 -050010253 switch (evt_code) {
10254 case ASYNC_TEMP_WARN:
10255 case ASYNC_TEMP_SAFE:
10256 temp_event_data.data = (uint32_t) icmd->ulpContext;
10257 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10258 if (evt_code == ASYNC_TEMP_WARN) {
10259 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10260 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10261 "0347 Adapter is very hot, please take "
10262 "corrective action. temperature : %d Celsius\n",
10263 (uint32_t) icmd->ulpContext);
10264 } else {
10265 temp_event_data.event_code = LPFC_NORMAL_TEMP;
10266 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10267 "0340 Adapter temperature is OK now. "
10268 "temperature : %d Celsius\n",
10269 (uint32_t) icmd->ulpContext);
10270 }
10271
10272 /* Send temperature change event to applications */
10273 shost = lpfc_shost_from_vport(phba->pport);
10274 fc_host_post_vendor_event(shost, fc_get_event_number(),
10275 sizeof(temp_event_data), (char *) &temp_event_data,
10276 LPFC_NL_VENDOR_ID);
10277 break;
10278 case ASYNC_STATUS_CN:
10279 lpfc_sli_abts_err_handler(phba, iocbq);
10280 break;
10281 default:
James Smarta257bf92009-04-06 18:48:10 -040010282 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -050010283 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -040010284 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -040010285 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -040010286 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
10287 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
10288 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
10289 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -050010290 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -040010291 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10292 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10293 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10294 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10295
James Smartcb69f7d2011-12-13 13:21:57 -050010296 break;
James Smart57127f12007-10-27 13:37:05 -040010297 }
James Smart57127f12007-10-27 13:37:05 -040010298}
10299
10300
James Smarte59058c2008-08-24 21:49:00 -040010301/**
James Smart895427b2017-02-12 13:52:30 -080010302 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010303 * @phba: Pointer to HBA context object.
10304 *
10305 * lpfc_sli_setup sets up rings of the SLI interface with
10306 * number of iocbs per ring and iotags. This function is
10307 * called while driver attach to the HBA and before the
10308 * interrupts are enabled. So there is no need for locking.
10309 *
10310 * This function always returns 0.
10311 **/
dea31012005-04-17 16:05:31 -050010312int
James Smart895427b2017-02-12 13:52:30 -080010313lpfc_sli4_setup(struct lpfc_hba *phba)
10314{
10315 struct lpfc_sli_ring *pring;
10316
10317 pring = phba->sli4_hba.els_wq->pring;
10318 pring->num_mask = LPFC_MAX_RING_MASK;
10319 pring->prt[0].profile = 0; /* Mask 0 */
10320 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10321 pring->prt[0].type = FC_TYPE_ELS;
10322 pring->prt[0].lpfc_sli_rcv_unsol_event =
10323 lpfc_els_unsol_event;
10324 pring->prt[1].profile = 0; /* Mask 1 */
10325 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10326 pring->prt[1].type = FC_TYPE_ELS;
10327 pring->prt[1].lpfc_sli_rcv_unsol_event =
10328 lpfc_els_unsol_event;
10329 pring->prt[2].profile = 0; /* Mask 2 */
10330 /* NameServer Inquiry */
10331 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10332 /* NameServer */
10333 pring->prt[2].type = FC_TYPE_CT;
10334 pring->prt[2].lpfc_sli_rcv_unsol_event =
10335 lpfc_ct_unsol_event;
10336 pring->prt[3].profile = 0; /* Mask 3 */
10337 /* NameServer response */
10338 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10339 /* NameServer */
10340 pring->prt[3].type = FC_TYPE_CT;
10341 pring->prt[3].lpfc_sli_rcv_unsol_event =
10342 lpfc_ct_unsol_event;
10343 return 0;
10344}
10345
10346/**
10347 * lpfc_sli_setup - SLI ring setup function
10348 * @phba: Pointer to HBA context object.
10349 *
10350 * lpfc_sli_setup sets up rings of the SLI interface with
10351 * number of iocbs per ring and iotags. This function is
10352 * called while driver attach to the HBA and before the
10353 * interrupts are enabled. So there is no need for locking.
10354 *
10355 * This function always returns 0. SLI3 only.
10356 **/
10357int
dea31012005-04-17 16:05:31 -050010358lpfc_sli_setup(struct lpfc_hba *phba)
10359{
James Smarted957682007-06-17 19:56:37 -050010360 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050010361 struct lpfc_sli *psli = &phba->sli;
10362 struct lpfc_sli_ring *pring;
10363
James Smart2a76a282012-08-03 12:35:54 -040010364 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010365 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010366
James Bottomley604a3e32005-10-29 10:28:33 -050010367 psli->iocbq_lookup = NULL;
10368 psli->iocbq_lookup_len = 0;
10369 psli->last_iotag = 0;
10370
dea31012005-04-17 16:05:31 -050010371 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010372 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010373 switch (i) {
10374 case LPFC_FCP_RING: /* ring 0 - FCP */
10375 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010376 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10377 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10378 pring->sli.sli3.numCiocb +=
10379 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10380 pring->sli.sli3.numRiocb +=
10381 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10382 pring->sli.sli3.numCiocb +=
10383 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10384 pring->sli.sli3.numRiocb +=
10385 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10386 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010387 SLI3_IOCB_CMD_SIZE :
10388 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010389 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010390 SLI3_IOCB_RSP_SIZE :
10391 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010392 pring->iotag_ctr = 0;
10393 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010394 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010395 pring->fast_iotag = pring->iotag_max;
10396 pring->num_mask = 0;
10397 break;
James Smarta4bc3372006-12-02 13:34:16 -050010398 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010399 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010400 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10401 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10402 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010403 SLI3_IOCB_CMD_SIZE :
10404 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010405 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010406 SLI3_IOCB_RSP_SIZE :
10407 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010408 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010409 pring->num_mask = 0;
10410 break;
10411 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10412 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010413 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10414 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10415 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010416 SLI3_IOCB_CMD_SIZE :
10417 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010418 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010419 SLI3_IOCB_RSP_SIZE :
10420 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010421 pring->fast_iotag = 0;
10422 pring->iotag_ctr = 0;
10423 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010424 pring->lpfc_sli_rcv_async_status =
10425 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010426 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010427 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010428 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10429 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010430 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010431 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010432 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010433 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10434 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010435 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010436 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010437 pring->prt[2].profile = 0; /* Mask 2 */
10438 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010439 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010440 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010441 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010442 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010443 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010444 pring->prt[3].profile = 0; /* Mask 3 */
10445 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010446 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010447 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010448 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010449 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010450 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010451 break;
10452 }
James Smart7e56aa22012-08-03 12:35:34 -040010453 totiocbsize += (pring->sli.sli3.numCiocb *
10454 pring->sli.sli3.sizeCiocb) +
10455 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010456 }
James Smarted957682007-06-17 19:56:37 -050010457 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010458 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010459 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10460 "SLI2 SLIM Data: x%x x%lx\n",
10461 phba->brd_no, totiocbsize,
10462 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010463 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010464 if (phba->cfg_multi_ring_support == 2)
10465 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010466
10467 return 0;
10468}
10469
James Smarte59058c2008-08-24 21:49:00 -040010470/**
James Smart895427b2017-02-12 13:52:30 -080010471 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010472 * @phba: Pointer to HBA context object.
10473 *
James Smart895427b2017-02-12 13:52:30 -080010474 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010475 * ring. This function also initializes ring indices of each ring.
10476 * This function is called during the initialization of the SLI
10477 * interface of an HBA.
10478 * This function is called with no lock held and always returns
10479 * 1.
10480 **/
James Smart895427b2017-02-12 13:52:30 -080010481void
10482lpfc_sli4_queue_init(struct lpfc_hba *phba)
10483{
10484 struct lpfc_sli *psli;
10485 struct lpfc_sli_ring *pring;
10486 int i;
10487
10488 psli = &phba->sli;
10489 spin_lock_irq(&phba->hbalock);
10490 INIT_LIST_HEAD(&psli->mboxq);
10491 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10492 /* Initialize list headers for txq and txcmplq as double linked lists */
James Smartcdb42be2019-01-28 11:14:21 -080010493 for (i = 0; i < phba->cfg_hdw_queue; i++) {
10494 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010495 pring->flag = 0;
10496 pring->ringno = LPFC_FCP_RING;
10497 INIT_LIST_HEAD(&pring->txq);
10498 INIT_LIST_HEAD(&pring->txcmplq);
10499 INIT_LIST_HEAD(&pring->iocb_continueq);
10500 spin_lock_init(&pring->ring_lock);
10501 }
10502 pring = phba->sli4_hba.els_wq->pring;
10503 pring->flag = 0;
10504 pring->ringno = LPFC_ELS_RING;
10505 INIT_LIST_HEAD(&pring->txq);
10506 INIT_LIST_HEAD(&pring->txcmplq);
10507 INIT_LIST_HEAD(&pring->iocb_continueq);
10508 spin_lock_init(&pring->ring_lock);
10509
James Smartcdb42be2019-01-28 11:14:21 -080010510 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10511 for (i = 0; i < phba->cfg_hdw_queue; i++) {
10512 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
10513 pring->flag = 0;
10514 pring->ringno = LPFC_FCP_RING;
10515 INIT_LIST_HEAD(&pring->txq);
10516 INIT_LIST_HEAD(&pring->txcmplq);
10517 INIT_LIST_HEAD(&pring->iocb_continueq);
10518 spin_lock_init(&pring->ring_lock);
10519 }
James Smart895427b2017-02-12 13:52:30 -080010520 pring = phba->sli4_hba.nvmels_wq->pring;
10521 pring->flag = 0;
10522 pring->ringno = LPFC_ELS_RING;
10523 INIT_LIST_HEAD(&pring->txq);
10524 INIT_LIST_HEAD(&pring->txcmplq);
10525 INIT_LIST_HEAD(&pring->iocb_continueq);
10526 spin_lock_init(&pring->ring_lock);
10527 }
10528
James Smart895427b2017-02-12 13:52:30 -080010529 spin_unlock_irq(&phba->hbalock);
10530}
10531
10532/**
10533 * lpfc_sli_queue_init - Queue initialization function
10534 * @phba: Pointer to HBA context object.
10535 *
10536 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10537 * ring. This function also initializes ring indices of each ring.
10538 * This function is called during the initialization of the SLI
10539 * interface of an HBA.
10540 * This function is called with no lock held and always returns
10541 * 1.
10542 **/
10543void
10544lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010545{
10546 struct lpfc_sli *psli;
10547 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010548 int i;
dea31012005-04-17 16:05:31 -050010549
10550 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010551 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010552 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010553 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010554 /* Initialize list headers for txq and txcmplq as double linked lists */
10555 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010556 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010557 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010558 pring->sli.sli3.next_cmdidx = 0;
10559 pring->sli.sli3.local_getidx = 0;
10560 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010561 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010562 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010563 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010564 pring->flag = 0;
10565 INIT_LIST_HEAD(&pring->txq);
10566 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010567 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010568 }
James Smart2e0fef82007-06-17 19:56:36 -050010569 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010570}
10571
James Smarte59058c2008-08-24 21:49:00 -040010572/**
James Smart04c68492009-05-22 14:52:52 -040010573 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10574 * @phba: Pointer to HBA context object.
10575 *
10576 * This routine flushes the mailbox command subsystem. It will unconditionally
10577 * flush all the mailbox commands in the three possible stages in the mailbox
10578 * command sub-system: pending mailbox command queue; the outstanding mailbox
10579 * command; and completed mailbox command queue. It is caller's responsibility
10580 * to make sure that the driver is in the proper state to flush the mailbox
10581 * command sub-system. Namely, the posting of mailbox commands into the
10582 * pending mailbox command queue from the various clients must be stopped;
10583 * either the HBA is in a state that it will never works on the outstanding
10584 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10585 * mailbox command has been completed.
10586 **/
10587static void
10588lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10589{
10590 LIST_HEAD(completions);
10591 struct lpfc_sli *psli = &phba->sli;
10592 LPFC_MBOXQ_t *pmb;
10593 unsigned long iflag;
10594
James Smart523128e2018-09-10 10:30:46 -070010595 /* Disable softirqs, including timers from obtaining phba->hbalock */
10596 local_bh_disable();
10597
James Smart04c68492009-05-22 14:52:52 -040010598 /* Flush all the mailbox commands in the mbox system */
10599 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart523128e2018-09-10 10:30:46 -070010600
James Smart04c68492009-05-22 14:52:52 -040010601 /* The pending mailbox command queue */
10602 list_splice_init(&phba->sli.mboxq, &completions);
10603 /* The outstanding active mailbox command */
10604 if (psli->mbox_active) {
10605 list_add_tail(&psli->mbox_active->list, &completions);
10606 psli->mbox_active = NULL;
10607 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10608 }
10609 /* The completed mailbox command queue */
10610 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10611 spin_unlock_irqrestore(&phba->hbalock, iflag);
10612
James Smart523128e2018-09-10 10:30:46 -070010613 /* Enable softirqs again, done with phba->hbalock */
10614 local_bh_enable();
10615
James Smart04c68492009-05-22 14:52:52 -040010616 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10617 while (!list_empty(&completions)) {
10618 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10619 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10620 if (pmb->mbox_cmpl)
10621 pmb->mbox_cmpl(phba, pmb);
10622 }
10623}
10624
10625/**
James Smart3621a712009-04-06 18:47:14 -040010626 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010627 * @vport: Pointer to virtual port object.
10628 *
10629 * lpfc_sli_host_down is called to clean up the resources
10630 * associated with a vport before destroying virtual
10631 * port data structures.
10632 * This function does following operations:
10633 * - Free discovery resources associated with this virtual
10634 * port.
10635 * - Free iocbs associated with this virtual port in
10636 * the txq.
10637 * - Send abort for all iocb commands associated with this
10638 * vport in txcmplq.
10639 *
10640 * This function is called with no lock held and always returns 1.
10641 **/
dea31012005-04-17 16:05:31 -050010642int
James Smart92d7f7b2007-06-17 19:56:38 -050010643lpfc_sli_host_down(struct lpfc_vport *vport)
10644{
James Smart858c9f62007-06-17 19:56:39 -050010645 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010646 struct lpfc_hba *phba = vport->phba;
10647 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010648 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010649 struct lpfc_sli_ring *pring;
10650 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010651 int i;
10652 unsigned long flags = 0;
10653 uint16_t prev_pring_flag;
10654
10655 lpfc_cleanup_discovery_resources(vport);
10656
10657 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010658
James Smart895427b2017-02-12 13:52:30 -080010659 /*
10660 * Error everything on the txq since these iocbs
10661 * have not been given to the FW yet.
10662 * Also issue ABTS for everything on the txcmplq
10663 */
10664 if (phba->sli_rev != LPFC_SLI_REV4) {
10665 for (i = 0; i < psli->num_rings; i++) {
10666 pring = &psli->sli3_ring[i];
10667 prev_pring_flag = pring->flag;
10668 /* Only slow rings */
10669 if (pring->ringno == LPFC_ELS_RING) {
10670 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10671 /* Set the lpfc data pending flag */
10672 set_bit(LPFC_DATA_READY, &phba->data_flags);
10673 }
10674 list_for_each_entry_safe(iocb, next_iocb,
10675 &pring->txq, list) {
10676 if (iocb->vport != vport)
10677 continue;
10678 list_move_tail(&iocb->list, &completions);
10679 }
10680 list_for_each_entry_safe(iocb, next_iocb,
10681 &pring->txcmplq, list) {
10682 if (iocb->vport != vport)
10683 continue;
10684 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10685 }
10686 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010687 }
James Smart895427b2017-02-12 13:52:30 -080010688 } else {
10689 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10690 pring = qp->pring;
10691 if (!pring)
10692 continue;
10693 if (pring == phba->sli4_hba.els_wq->pring) {
10694 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10695 /* Set the lpfc data pending flag */
10696 set_bit(LPFC_DATA_READY, &phba->data_flags);
10697 }
10698 prev_pring_flag = pring->flag;
10699 spin_lock_irq(&pring->ring_lock);
10700 list_for_each_entry_safe(iocb, next_iocb,
10701 &pring->txq, list) {
10702 if (iocb->vport != vport)
10703 continue;
10704 list_move_tail(&iocb->list, &completions);
10705 }
10706 spin_unlock_irq(&pring->ring_lock);
10707 list_for_each_entry_safe(iocb, next_iocb,
10708 &pring->txcmplq, list) {
10709 if (iocb->vport != vport)
10710 continue;
10711 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10712 }
10713 pring->flag = prev_pring_flag;
10714 }
James Smart92d7f7b2007-06-17 19:56:38 -050010715 }
James Smart92d7f7b2007-06-17 19:56:38 -050010716 spin_unlock_irqrestore(&phba->hbalock, flags);
10717
James Smarta257bf92009-04-06 18:48:10 -040010718 /* Cancel all the IOCBs from the completions list */
10719 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10720 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010721 return 1;
10722}
10723
James Smarte59058c2008-08-24 21:49:00 -040010724/**
James Smart3621a712009-04-06 18:47:14 -040010725 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010726 * @phba: Pointer to HBA context object.
10727 *
10728 * This function cleans up all iocb, buffers, mailbox commands
10729 * while shutting down the HBA. This function is called with no
10730 * lock held and always returns 1.
10731 * This function does the following to cleanup driver resources:
10732 * - Free discovery resources for each virtual port
10733 * - Cleanup any pending fabric iocbs
10734 * - Iterate through the iocb txq and free each entry
10735 * in the list.
10736 * - Free up any buffer posted to the HBA
10737 * - Free mailbox commands in the mailbox queue.
10738 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010739int
James Smart2e0fef82007-06-17 19:56:36 -050010740lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010741{
James Smart2534ba72007-04-25 09:52:20 -040010742 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010743 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010744 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010745 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010746 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010747 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010748 int i;
10749
10750 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010751 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010752
dea31012005-04-17 16:05:31 -050010753 lpfc_hba_down_prep(phba);
10754
James Smart523128e2018-09-10 10:30:46 -070010755 /* Disable softirqs, including timers from obtaining phba->hbalock */
10756 local_bh_disable();
10757
James Smart92d7f7b2007-06-17 19:56:38 -050010758 lpfc_fabric_abort_hba(phba);
10759
James Smart2e0fef82007-06-17 19:56:36 -050010760 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010761
James Smart895427b2017-02-12 13:52:30 -080010762 /*
10763 * Error everything on the txq since these iocbs
10764 * have not been given to the FW yet.
10765 */
10766 if (phba->sli_rev != LPFC_SLI_REV4) {
10767 for (i = 0; i < psli->num_rings; i++) {
10768 pring = &psli->sli3_ring[i];
10769 /* Only slow rings */
10770 if (pring->ringno == LPFC_ELS_RING) {
10771 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10772 /* Set the lpfc data pending flag */
10773 set_bit(LPFC_DATA_READY, &phba->data_flags);
10774 }
10775 list_splice_init(&pring->txq, &completions);
10776 }
10777 } else {
10778 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10779 pring = qp->pring;
10780 if (!pring)
10781 continue;
10782 spin_lock_irq(&pring->ring_lock);
10783 list_splice_init(&pring->txq, &completions);
10784 spin_unlock_irq(&pring->ring_lock);
10785 if (pring == phba->sli4_hba.els_wq->pring) {
10786 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10787 /* Set the lpfc data pending flag */
10788 set_bit(LPFC_DATA_READY, &phba->data_flags);
10789 }
10790 }
dea31012005-04-17 16:05:31 -050010791 }
James Smart2e0fef82007-06-17 19:56:36 -050010792 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010793
James Smarta257bf92009-04-06 18:48:10 -040010794 /* Cancel all the IOCBs from the completions list */
10795 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10796 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010797
James Smart0ff10d42008-01-11 01:52:36 -050010798 spin_lock_irqsave(&phba->hbalock, flags);
10799 list_splice_init(&phba->elsbuf, &completions);
10800 phba->elsbuf_cnt = 0;
10801 phba->elsbuf_prev_cnt = 0;
10802 spin_unlock_irqrestore(&phba->hbalock, flags);
10803
10804 while (!list_empty(&completions)) {
10805 list_remove_head(&completions, buf_ptr,
10806 struct lpfc_dmabuf, list);
10807 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10808 kfree(buf_ptr);
10809 }
10810
James Smart523128e2018-09-10 10:30:46 -070010811 /* Enable softirqs again, done with phba->hbalock */
10812 local_bh_enable();
10813
dea31012005-04-17 16:05:31 -050010814 /* Return any active mbox cmds */
10815 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010816
James Smartda0436e2009-05-22 14:51:39 -040010817 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010818 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010819 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010820
James Smartda0436e2009-05-22 14:51:39 -040010821 return 1;
10822}
James Smart92d7f7b2007-06-17 19:56:38 -050010823
James Smartda0436e2009-05-22 14:51:39 -040010824/**
James Smart3621a712009-04-06 18:47:14 -040010825 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010826 * @srcp: Source memory pointer.
10827 * @destp: Destination memory pointer.
10828 * @cnt: Number of words required to be copied.
10829 *
10830 * This function is used for copying data between driver memory
10831 * and the SLI memory. This function also changes the endianness
10832 * of each word if native endianness is different from SLI
10833 * endianness. This function can be called with or without
10834 * lock.
10835 **/
dea31012005-04-17 16:05:31 -050010836void
10837lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10838{
10839 uint32_t *src = srcp;
10840 uint32_t *dest = destp;
10841 uint32_t ldata;
10842 int i;
10843
10844 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10845 ldata = *src;
10846 ldata = le32_to_cpu(ldata);
10847 *dest = ldata;
10848 src++;
10849 dest++;
10850 }
10851}
10852
James Smarte59058c2008-08-24 21:49:00 -040010853
10854/**
James Smarta0c87cb2009-07-19 10:01:10 -040010855 * lpfc_sli_bemem_bcopy - SLI memory copy function
10856 * @srcp: Source memory pointer.
10857 * @destp: Destination memory pointer.
10858 * @cnt: Number of words required to be copied.
10859 *
10860 * This function is used for copying data between a data structure
10861 * with big endian representation to local endianness.
10862 * This function can be called with or without lock.
10863 **/
10864void
10865lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10866{
10867 uint32_t *src = srcp;
10868 uint32_t *dest = destp;
10869 uint32_t ldata;
10870 int i;
10871
10872 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10873 ldata = *src;
10874 ldata = be32_to_cpu(ldata);
10875 *dest = ldata;
10876 src++;
10877 dest++;
10878 }
10879}
10880
10881/**
James Smart3621a712009-04-06 18:47:14 -040010882 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010883 * @phba: Pointer to HBA context object.
10884 * @pring: Pointer to driver SLI ring object.
10885 * @mp: Pointer to driver buffer object.
10886 *
10887 * This function is called with no lock held.
10888 * It always return zero after adding the buffer to the postbufq
10889 * buffer list.
10890 **/
dea31012005-04-17 16:05:31 -050010891int
James Smart2e0fef82007-06-17 19:56:36 -050010892lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10893 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010894{
10895 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10896 later */
James Smart2e0fef82007-06-17 19:56:36 -050010897 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010898 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010899 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010900 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010901 return 0;
10902}
10903
James Smarte59058c2008-08-24 21:49:00 -040010904/**
James Smart3621a712009-04-06 18:47:14 -040010905 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010906 * @phba: Pointer to HBA context object.
10907 *
10908 * When HBQ is enabled, buffers are searched based on tags. This function
10909 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10910 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10911 * does not conflict with tags of buffer posted for unsolicited events.
10912 * The function returns the allocated tag. The function is called with
10913 * no locks held.
10914 **/
James Smart76bb24e2007-10-27 13:38:00 -040010915uint32_t
10916lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10917{
10918 spin_lock_irq(&phba->hbalock);
10919 phba->buffer_tag_count++;
10920 /*
10921 * Always set the QUE_BUFTAG_BIT to distiguish between
10922 * a tag assigned by HBQ.
10923 */
10924 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10925 spin_unlock_irq(&phba->hbalock);
10926 return phba->buffer_tag_count;
10927}
10928
James Smarte59058c2008-08-24 21:49:00 -040010929/**
James Smart3621a712009-04-06 18:47:14 -040010930 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010931 * @phba: Pointer to HBA context object.
10932 * @pring: Pointer to driver SLI ring object.
10933 * @tag: Buffer tag.
10934 *
10935 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10936 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10937 * iocb is posted to the response ring with the tag of the buffer.
10938 * This function searches the pring->postbufq list using the tag
10939 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10940 * iocb. If the buffer is found then lpfc_dmabuf object of the
10941 * buffer is returned to the caller else NULL is returned.
10942 * This function is called with no lock held.
10943 **/
James Smart76bb24e2007-10-27 13:38:00 -040010944struct lpfc_dmabuf *
10945lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10946 uint32_t tag)
10947{
10948 struct lpfc_dmabuf *mp, *next_mp;
10949 struct list_head *slp = &pring->postbufq;
10950
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010951 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010952 spin_lock_irq(&phba->hbalock);
10953 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10954 if (mp->buffer_tag == tag) {
10955 list_del_init(&mp->list);
10956 pring->postbufq_cnt--;
10957 spin_unlock_irq(&phba->hbalock);
10958 return mp;
10959 }
10960 }
10961
10962 spin_unlock_irq(&phba->hbalock);
10963 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010964 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010965 "ring %d Data x%lx x%p x%p x%x\n",
10966 pring->ringno, (unsigned long) tag,
10967 slp->next, slp->prev, pring->postbufq_cnt);
10968
10969 return NULL;
10970}
dea31012005-04-17 16:05:31 -050010971
James Smarte59058c2008-08-24 21:49:00 -040010972/**
James Smart3621a712009-04-06 18:47:14 -040010973 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010974 * @phba: Pointer to HBA context object.
10975 * @pring: Pointer to driver SLI ring object.
10976 * @phys: DMA address of the buffer.
10977 *
10978 * This function searches the buffer list using the dma_address
10979 * of unsolicited event to find the driver's lpfc_dmabuf object
10980 * corresponding to the dma_address. The function returns the
10981 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10982 * This function is called by the ct and els unsolicited event
10983 * handlers to get the buffer associated with the unsolicited
10984 * event.
10985 *
10986 * This function is called with no lock held.
10987 **/
dea31012005-04-17 16:05:31 -050010988struct lpfc_dmabuf *
10989lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10990 dma_addr_t phys)
10991{
10992 struct lpfc_dmabuf *mp, *next_mp;
10993 struct list_head *slp = &pring->postbufq;
10994
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010995 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010996 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010997 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10998 if (mp->phys == phys) {
10999 list_del_init(&mp->list);
11000 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050011001 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011002 return mp;
11003 }
11004 }
11005
James Smart2e0fef82007-06-17 19:56:36 -050011006 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011007 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040011008 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050011009 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011010 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050011011 slp->next, slp->prev, pring->postbufq_cnt);
11012 return NULL;
11013}
11014
James Smarte59058c2008-08-24 21:49:00 -040011015/**
James Smart3621a712009-04-06 18:47:14 -040011016 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040011017 * @phba: Pointer to HBA context object.
11018 * @cmdiocb: Pointer to driver command iocb object.
11019 * @rspiocb: Pointer to driver response iocb object.
11020 *
11021 * This function is the completion handler for the abort iocbs for
11022 * ELS commands. This function is called from the ELS ring event
11023 * handler with no lock held. This function frees memory resources
11024 * associated with the abort iocb.
11025 **/
dea31012005-04-17 16:05:31 -050011026static void
James Smart2e0fef82007-06-17 19:56:36 -050011027lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11028 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050011029{
James Smart2e0fef82007-06-17 19:56:36 -050011030 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040011031 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050011032 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040011033
11034 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050011035
11036 /*
11037 * Assume that the port already completed and returned, or
11038 * will return the iocb. Just Log the message.
11039 */
James Smart2680eea2007-04-25 09:52:55 -040011040 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11041 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11042
James Smart2e0fef82007-06-17 19:56:36 -050011043 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040011044 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smartfaa832e2018-07-31 17:23:18 -070011045 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11046 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11047 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11048 spin_unlock_irq(&phba->hbalock);
11049 goto release_iocb;
11050 }
James Smart45ed1192009-10-02 15:17:02 -040011051 if (abort_iotag != 0 &&
11052 abort_iotag <= phba->sli.last_iotag)
11053 abort_iocb =
11054 phba->sli.iocbq_lookup[abort_iotag];
11055 } else
11056 /* For sli4 the abort_tag is the XRI,
11057 * so the abort routine puts the iotag of the iocb
11058 * being aborted in the context field of the abort
11059 * IOCB.
11060 */
11061 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040011062
James Smart2a9bf3d2010-06-07 15:24:45 -040011063 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
11064 "0327 Cannot abort els iocb %p "
11065 "with tag %x context %x, abort status %x, "
11066 "abort code %x\n",
11067 abort_iocb, abort_iotag, abort_context,
11068 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040011069
James Smartff78d8f2011-12-13 13:21:35 -050011070 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040011071 }
James Smartfaa832e2018-07-31 17:23:18 -070011072release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050011073 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050011074 return;
11075}
11076
James Smarte59058c2008-08-24 21:49:00 -040011077/**
James Smart3621a712009-04-06 18:47:14 -040011078 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040011079 * @phba: Pointer to HBA context object.
11080 * @cmdiocb: Pointer to driver command iocb object.
11081 * @rspiocb: Pointer to driver response iocb object.
11082 *
11083 * The function is called from SLI ring event handler with no
11084 * lock held. This function is the completion handler for ELS commands
11085 * which are aborted. The function frees memory resources used for
11086 * the aborted ELS commands.
11087 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011088static void
11089lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11090 struct lpfc_iocbq *rspiocb)
11091{
11092 IOCB_t *irsp = &rspiocb->iocb;
11093
11094 /* ELS cmd tag <ulpIoTag> completes */
11095 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040011096 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050011097 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011098 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050011099 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050011100 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11101 lpfc_ct_free_iocb(phba, cmdiocb);
11102 else
11103 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050011104 return;
11105}
11106
James Smarte59058c2008-08-24 21:49:00 -040011107/**
James Smart5af5eee2010-10-22 11:06:38 -040011108 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040011109 * @phba: Pointer to HBA context object.
11110 * @pring: Pointer to driver SLI ring object.
11111 * @cmdiocb: Pointer to driver command iocb object.
11112 *
James Smart5af5eee2010-10-22 11:06:38 -040011113 * This function issues an abort iocb for the provided command iocb down to
11114 * the port. Other than the case the outstanding command iocb is an abort
11115 * request, this function issues abort out unconditionally. This function is
11116 * called with hbalock held. The function returns 0 when it fails due to
11117 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040011118 **/
James Smart5af5eee2010-10-22 11:06:38 -040011119static int
11120lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050011121 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050011122{
James Smart2e0fef82007-06-17 19:56:36 -050011123 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011124 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050011125 IOCB_t *icmd = NULL;
11126 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011127 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040011128 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070011129 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040011130
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011131 lockdep_assert_held(&phba->hbalock);
11132
James Smart92d7f7b2007-06-17 19:56:38 -050011133 /*
11134 * There are certain command types we don't want to abort. And we
11135 * don't want to abort commands that are already in the process of
11136 * being aborted.
James Smart07951072007-04-25 09:51:38 -040011137 */
11138 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050011139 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050011140 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11141 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040011142 return 0;
11143
dea31012005-04-17 16:05:31 -050011144 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050011145 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011146 if (abtsiocbp == NULL)
11147 return 0;
dea31012005-04-17 16:05:31 -050011148
James Smart07951072007-04-25 09:51:38 -040011149 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050011150 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040011151 */
11152 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11153
dea31012005-04-17 16:05:31 -050011154 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040011155 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11156 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040011157 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040011158 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040011159 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070011160 } else {
James Smartda0436e2009-05-22 14:51:39 -040011161 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070011162 if (pring->ringno == LPFC_ELS_RING) {
11163 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11164 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11165 }
11166 }
dea31012005-04-17 16:05:31 -050011167 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040011168 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050011169
James Smart5ffc2662009-11-18 15:39:44 -050011170 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011171 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011172 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
11173 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011174 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11175 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011176
James Smart2e0fef82007-06-17 19:56:36 -050011177 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040011178 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11179 else
11180 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11181
11182 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080011183 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040011184
James Smarte8b62012007-08-02 11:10:09 -040011185 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11186 "0339 Abort xri x%x, original iotag x%x, "
11187 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040011188 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040011189 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040011190 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040011191
11192 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011193 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11194 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040011195 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040011196 /* Note: both hbalock and ring_lock need to be set here */
11197 spin_lock_irqsave(&pring->ring_lock, iflags);
11198 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11199 abtsiocbp, 0);
11200 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11201 } else {
11202 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11203 abtsiocbp, 0);
11204 }
James Smart07951072007-04-25 09:51:38 -040011205
James Smartd7c255b2008-08-24 21:50:00 -040011206 if (retval)
11207 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040011208
11209 /*
11210 * Caller to this routine should check for IOCB_ERROR
11211 * and handle it properly. This routine no longer removes
11212 * iocb off txcmplq and call compl in case of IOCB_ERROR.
11213 */
11214 return retval;
11215}
11216
11217/**
11218 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11219 * @phba: Pointer to HBA context object.
11220 * @pring: Pointer to driver SLI ring object.
11221 * @cmdiocb: Pointer to driver command iocb object.
11222 *
11223 * This function issues an abort iocb for the provided command iocb. In case
11224 * of unloading, the abort iocb will not be issued to commands on the ELS
11225 * ring. Instead, the callback function shall be changed to those commands
11226 * so that nothing happens when them finishes. This function is called with
11227 * hbalock held. The function returns 0 when the command iocb is an abort
11228 * request.
11229 **/
11230int
11231lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11232 struct lpfc_iocbq *cmdiocb)
11233{
11234 struct lpfc_vport *vport = cmdiocb->vport;
11235 int retval = IOCB_ERROR;
11236 IOCB_t *icmd = NULL;
11237
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011238 lockdep_assert_held(&phba->hbalock);
11239
James Smart5af5eee2010-10-22 11:06:38 -040011240 /*
11241 * There are certain command types we don't want to abort. And we
11242 * don't want to abort commands that are already in the process of
11243 * being aborted.
11244 */
11245 icmd = &cmdiocb->iocb;
11246 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11247 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11248 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11249 return 0;
11250
Dick Kennedy1234a6d2017-09-29 17:34:29 -070011251 if (!pring) {
11252 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11253 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11254 else
11255 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11256 goto abort_iotag_exit;
11257 }
11258
James Smart5af5eee2010-10-22 11:06:38 -040011259 /*
11260 * If we're unloading, don't abort iocb on the ELS ring, but change
11261 * the callback so that nothing happens when it finishes.
11262 */
11263 if ((vport->load_flag & FC_UNLOADING) &&
11264 (pring->ringno == LPFC_ELS_RING)) {
11265 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11266 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11267 else
11268 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11269 goto abort_iotag_exit;
11270 }
11271
11272 /* Now, we try to issue the abort to the cmdiocb out */
11273 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
11274
James Smart07951072007-04-25 09:51:38 -040011275abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050011276 /*
11277 * Caller to this routine should check for IOCB_ERROR
11278 * and handle it properly. This routine no longer removes
11279 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040011280 */
James Smart2e0fef82007-06-17 19:56:36 -050011281 return retval;
dea31012005-04-17 16:05:31 -050011282}
11283
James Smarte59058c2008-08-24 21:49:00 -040011284/**
James Smart895427b2017-02-12 13:52:30 -080011285 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
11286 * @phba: Pointer to HBA context object.
11287 * @pring: Pointer to driver SLI ring object.
11288 * @cmdiocb: Pointer to driver command iocb object.
11289 *
11290 * This function issues an abort iocb for the provided command iocb down to
11291 * the port. Other than the case the outstanding command iocb is an abort
11292 * request, this function issues abort out unconditionally. This function is
11293 * called with hbalock held. The function returns 0 when it fails due to
11294 * memory allocation failure or when the command iocb is an abort request.
11295 **/
11296static int
11297lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11298 struct lpfc_iocbq *cmdiocb)
11299{
11300 struct lpfc_vport *vport = cmdiocb->vport;
11301 struct lpfc_iocbq *abtsiocbp;
James Smart205e8242018-03-05 12:04:03 -080011302 union lpfc_wqe128 *abts_wqe;
James Smart895427b2017-02-12 13:52:30 -080011303 int retval;
James Smart1fbf9742019-01-28 11:14:26 -080011304 int idx = cmdiocb->hba_wqidx;
James Smart895427b2017-02-12 13:52:30 -080011305
11306 /*
11307 * There are certain command types we don't want to abort. And we
11308 * don't want to abort commands that are already in the process of
11309 * being aborted.
11310 */
11311 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
11312 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
11313 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11314 return 0;
11315
11316 /* issue ABTS for this io based on iotag */
11317 abtsiocbp = __lpfc_sli_get_iocbq(phba);
11318 if (abtsiocbp == NULL)
11319 return 0;
11320
11321 /* This signals the response to set the correct status
11322 * before calling the completion handler
11323 */
11324 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11325
11326 /* Complete prepping the abort wqe and issue to the FW. */
11327 abts_wqe = &abtsiocbp->wqe;
James Smart1c368332018-11-29 16:09:37 -080011328
11329 /* Clear any stale WQE contents */
11330 memset(abts_wqe, 0, sizeof(union lpfc_wqe));
James Smart895427b2017-02-12 13:52:30 -080011331 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
11332
James Smart895427b2017-02-12 13:52:30 -080011333 /* word 7 */
James Smart895427b2017-02-12 13:52:30 -080011334 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
11335 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
11336 cmdiocb->iocb.ulpClass);
11337
11338 /* word 8 - tell the FW to abort the IO associated with this
11339 * outstanding exchange ID.
11340 */
11341 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
11342
11343 /* word 9 - this is the iotag for the abts_wqe completion. */
11344 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
11345 abtsiocbp->iotag);
11346
11347 /* word 10 */
James Smart895427b2017-02-12 13:52:30 -080011348 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
11349 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
11350
11351 /* word 11 */
11352 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11353 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
11354 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
11355
11356 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11357 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
11358 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080011359 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart1fbf9742019-01-28 11:14:26 -080011360 retval = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[idx],
11361 abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070011362 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080011363 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11364 "6147 Failed abts issue_wqe with status x%x "
11365 "for oxid x%x\n",
11366 retval, cmdiocb->sli4_xritag);
11367 lpfc_sli_release_iocbq(phba, abtsiocbp);
11368 return retval;
11369 }
11370
11371 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11372 "6148 Drv Abort NVME Request Issued for "
11373 "ox_id x%x on reqtag x%x\n",
11374 cmdiocb->sli4_xritag,
11375 abtsiocbp->iotag);
11376
11377 return retval;
11378}
11379
11380/**
James Smart5af5eee2010-10-22 11:06:38 -040011381 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11382 * @phba: pointer to lpfc HBA data structure.
11383 *
11384 * This routine will abort all pending and outstanding iocbs to an HBA.
11385 **/
11386void
11387lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11388{
11389 struct lpfc_sli *psli = &phba->sli;
11390 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011391 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011392 int i;
11393
James Smart895427b2017-02-12 13:52:30 -080011394 if (phba->sli_rev != LPFC_SLI_REV4) {
11395 for (i = 0; i < psli->num_rings; i++) {
11396 pring = &psli->sli3_ring[i];
11397 lpfc_sli_abort_iocb_ring(phba, pring);
11398 }
11399 return;
11400 }
11401 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11402 pring = qp->pring;
11403 if (!pring)
11404 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011405 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011406 }
11407}
11408
11409/**
James Smart3621a712009-04-06 18:47:14 -040011410 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011411 * @iocbq: Pointer to driver iocb object.
11412 * @vport: Pointer to driver virtual port object.
11413 * @tgt_id: SCSI ID of the target.
11414 * @lun_id: LUN ID of the scsi device.
11415 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11416 *
James Smart3621a712009-04-06 18:47:14 -040011417 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011418 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11419 * 0 if the filtering criteria is met for the given iocb and will return
11420 * 1 if the filtering criteria is not met.
11421 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11422 * given iocb is for the SCSI device specified by vport, tgt_id and
11423 * lun_id parameter.
11424 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11425 * given iocb is for the SCSI target specified by vport and tgt_id
11426 * parameters.
11427 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11428 * given iocb is for the SCSI host associated with the given vport.
11429 * This function is called with no locks held.
11430 **/
dea31012005-04-17 16:05:31 -050011431static int
James Smart51ef4c22007-08-02 11:10:31 -040011432lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11433 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011434 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011435{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011436 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011437 int rc = 1;
11438
James Smartb0e83012018-06-26 08:24:29 -070011439 if (iocbq->vport != vport)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011440 return rc;
11441
James Smartb0e83012018-06-26 08:24:29 -070011442 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
11443 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
James Smart51ef4c22007-08-02 11:10:31 -040011444 return rc;
11445
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011446 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011447
James Smart495a7142008-06-14 22:52:59 -040011448 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011449 return rc;
11450
11451 switch (ctx_cmd) {
11452 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070011453 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011454 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11455 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011456 rc = 0;
11457 break;
11458 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070011459 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011460 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011461 rc = 0;
11462 break;
dea31012005-04-17 16:05:31 -050011463 case LPFC_CTX_HOST:
11464 rc = 0;
11465 break;
11466 default:
11467 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011468 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011469 break;
11470 }
11471
11472 return rc;
11473}
11474
James Smarte59058c2008-08-24 21:49:00 -040011475/**
James Smart3621a712009-04-06 18:47:14 -040011476 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011477 * @vport: Pointer to virtual port.
11478 * @tgt_id: SCSI ID of the target.
11479 * @lun_id: LUN ID of the scsi device.
11480 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11481 *
11482 * This function returns number of FCP commands pending for the vport.
11483 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11484 * commands pending on the vport associated with SCSI device specified
11485 * by tgt_id and lun_id parameters.
11486 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11487 * commands pending on the vport associated with SCSI target specified
11488 * by tgt_id parameter.
11489 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11490 * commands pending on the vport.
11491 * This function returns the number of iocbs which satisfy the filter.
11492 * This function is called without any lock held.
11493 **/
dea31012005-04-17 16:05:31 -050011494int
James Smart51ef4c22007-08-02 11:10:31 -040011495lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11496 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011497{
James Smart51ef4c22007-08-02 11:10:31 -040011498 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011499 struct lpfc_iocbq *iocbq;
11500 int sum, i;
dea31012005-04-17 16:05:31 -050011501
Johannes Thumshirn31979002016-07-18 16:06:03 +020011502 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011503 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11504 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011505
James Smart51ef4c22007-08-02 11:10:31 -040011506 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11507 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011508 sum++;
dea31012005-04-17 16:05:31 -050011509 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011510 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011511
dea31012005-04-17 16:05:31 -050011512 return sum;
11513}
11514
James Smarte59058c2008-08-24 21:49:00 -040011515/**
James Smart3621a712009-04-06 18:47:14 -040011516 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011517 * @phba: Pointer to HBA context object
11518 * @cmdiocb: Pointer to command iocb object.
11519 * @rspiocb: Pointer to response iocb object.
11520 *
11521 * This function is called when an aborted FCP iocb completes. This
11522 * function is called by the ring event handler with no lock held.
11523 * This function frees the iocb.
11524 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011525void
James Smart2e0fef82007-06-17 19:56:36 -050011526lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11527 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011528{
James Smartcb69f7d2011-12-13 13:21:57 -050011529 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011530 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011531 "original iotag x%x, abort cmd iotag x%x "
11532 "status 0x%x, reason 0x%x\n",
11533 cmdiocb->iocb.un.acxri.abortContextTag,
11534 cmdiocb->iocb.un.acxri.abortIoTag,
11535 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11536 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011537 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011538 return;
11539}
11540
James Smarte59058c2008-08-24 21:49:00 -040011541/**
James Smart3621a712009-04-06 18:47:14 -040011542 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011543 * @vport: Pointer to virtual port.
11544 * @pring: Pointer to driver SLI ring object.
11545 * @tgt_id: SCSI ID of the target.
11546 * @lun_id: LUN ID of the scsi device.
11547 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11548 *
11549 * This function sends an abort command for every SCSI command
11550 * associated with the given virtual port pending on the ring
11551 * filtered by lpfc_sli_validate_fcp_iocb function.
11552 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11553 * FCP iocbs associated with lun specified by tgt_id and lun_id
11554 * parameters
11555 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11556 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11557 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11558 * FCP iocbs associated with virtual port.
11559 * This function returns number of iocbs it failed to abort.
11560 * This function is called with no locks held.
11561 **/
dea31012005-04-17 16:05:31 -050011562int
James Smart51ef4c22007-08-02 11:10:31 -040011563lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11564 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011565{
James Smart51ef4c22007-08-02 11:10:31 -040011566 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011567 struct lpfc_iocbq *iocbq;
11568 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011569 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011570 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011571 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011572 int i;
dea31012005-04-17 16:05:31 -050011573
James Smartb0e83012018-06-26 08:24:29 -070011574 /* all I/Os are in process of being flushed */
11575 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH)
11576 return errcnt;
11577
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011578 for (i = 1; i <= phba->sli.last_iotag; i++) {
11579 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011580
James Smart51ef4c22007-08-02 11:10:31 -040011581 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011582 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011583 continue;
11584
James Smartafbd8d82013-09-06 12:22:13 -040011585 /*
11586 * If the iocbq is already being aborted, don't take a second
11587 * action, but do count it.
11588 */
11589 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11590 continue;
11591
dea31012005-04-17 16:05:31 -050011592 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011593 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011594 if (abtsiocb == NULL) {
11595 errcnt++;
11596 continue;
11597 }
dea31012005-04-17 16:05:31 -050011598
James Smartafbd8d82013-09-06 12:22:13 -040011599 /* indicate the IO is being aborted by the driver. */
11600 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11601
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011602 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011603 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11604 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011605 if (phba->sli_rev == LPFC_SLI_REV4)
11606 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11607 else
11608 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011609 abtsiocb->iocb.ulpLe = 1;
11610 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011611 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011612
James Smart5ffc2662009-11-18 15:39:44 -050011613 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011614 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011615 if (iocbq->iocb_flag & LPFC_IO_FCP)
11616 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011617 if (iocbq->iocb_flag & LPFC_IO_FOF)
11618 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011619
James Smart2e0fef82007-06-17 19:56:36 -050011620 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011621 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11622 else
11623 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11624
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011625 /* Setup callback routine and issue the command. */
11626 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011627 if (phba->sli_rev == LPFC_SLI_REV4) {
11628 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11629 if (!pring_s4)
11630 continue;
11631 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11632 abtsiocb, 0);
11633 } else
11634 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11635 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011636 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011637 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011638 errcnt++;
11639 continue;
11640 }
11641 }
11642
11643 return errcnt;
11644}
11645
James Smarte59058c2008-08-24 21:49:00 -040011646/**
James Smart98912dda2014-04-04 13:52:31 -040011647 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11648 * @vport: Pointer to virtual port.
11649 * @pring: Pointer to driver SLI ring object.
11650 * @tgt_id: SCSI ID of the target.
11651 * @lun_id: LUN ID of the scsi device.
11652 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11653 *
11654 * This function sends an abort command for every SCSI command
11655 * associated with the given virtual port pending on the ring
11656 * filtered by lpfc_sli_validate_fcp_iocb function.
11657 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11658 * FCP iocbs associated with lun specified by tgt_id and lun_id
11659 * parameters
11660 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11661 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11662 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11663 * FCP iocbs associated with virtual port.
11664 * This function returns number of iocbs it aborted .
11665 * This function is called with no locks held right after a taskmgmt
11666 * command is sent.
11667 **/
11668int
11669lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11670 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11671{
11672 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011673 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011674 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011675 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011676 struct lpfc_iocbq *iocbq;
11677 IOCB_t *icmd;
11678 int sum, i, ret_val;
11679 unsigned long iflags;
11680 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011681
James Smart59c68ea2018-04-09 14:24:25 -070011682 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011683
11684 /* all I/Os are in process of being flushed */
11685 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070011686 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011687 return 0;
11688 }
11689 sum = 0;
11690
11691 for (i = 1; i <= phba->sli.last_iotag; i++) {
11692 iocbq = phba->sli.iocbq_lookup[i];
11693
11694 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11695 cmd) != 0)
11696 continue;
11697
11698 /*
11699 * If the iocbq is already being aborted, don't take a second
11700 * action, but do count it.
11701 */
11702 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11703 continue;
11704
11705 /* issue ABTS for this IOCB based on iotag */
11706 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11707 if (abtsiocbq == NULL)
11708 continue;
11709
11710 icmd = &iocbq->iocb;
11711 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11712 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11713 if (phba->sli_rev == LPFC_SLI_REV4)
11714 abtsiocbq->iocb.un.acxri.abortIoTag =
11715 iocbq->sli4_xritag;
11716 else
11717 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11718 abtsiocbq->iocb.ulpLe = 1;
11719 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11720 abtsiocbq->vport = vport;
11721
11722 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011723 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011724 if (iocbq->iocb_flag & LPFC_IO_FCP)
11725 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011726 if (iocbq->iocb_flag & LPFC_IO_FOF)
11727 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011728
James Smart8c50d252014-09-03 12:58:16 -040011729 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11730 ndlp = lpfc_cmd->rdata->pnode;
11731
11732 if (lpfc_is_link_up(phba) &&
11733 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011734 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11735 else
11736 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11737
11738 /* Setup callback routine and issue the command. */
11739 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11740
11741 /*
11742 * Indicate the IO is being aborted by the driver and set
11743 * the caller's flag into the aborted IO.
11744 */
11745 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11746
11747 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart59c68ea2018-04-09 14:24:25 -070011748 pring_s4 = lpfc_sli4_calc_ring(phba, abtsiocbq);
11749 if (!pring_s4)
James Smart895427b2017-02-12 13:52:30 -080011750 continue;
James Smart98912dda2014-04-04 13:52:31 -040011751 /* Note: both hbalock and ring_lock must be set here */
James Smart59c68ea2018-04-09 14:24:25 -070011752 spin_lock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011753 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11754 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070011755 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011756 } else {
11757 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11758 abtsiocbq, 0);
11759 }
11760
11761
11762 if (ret_val == IOCB_ERROR)
11763 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11764 else
11765 sum++;
11766 }
James Smart59c68ea2018-04-09 14:24:25 -070011767 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011768 return sum;
11769}
11770
11771/**
James Smart3621a712009-04-06 18:47:14 -040011772 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011773 * @phba: Pointer to HBA context object.
11774 * @cmdiocbq: Pointer to command iocb.
11775 * @rspiocbq: Pointer to response iocb.
11776 *
11777 * This function is the completion handler for iocbs issued using
11778 * lpfc_sli_issue_iocb_wait function. This function is called by the
11779 * ring event handler function without any lock held. This function
11780 * can be called from both worker thread context and interrupt
11781 * context. This function also can be called from other thread which
11782 * cleans up the SLI layer objects.
11783 * This function copy the contents of the response iocb to the
11784 * response iocb memory object provided by the caller of
11785 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11786 * sleeps for the iocb completion.
11787 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011788static void
11789lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11790 struct lpfc_iocbq *cmdiocbq,
11791 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011792{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011793 wait_queue_head_t *pdone_q;
11794 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011795 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011796
James Smart2e0fef82007-06-17 19:56:36 -050011797 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011798 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11799
11800 /*
11801 * A time out has occurred for the iocb. If a time out
11802 * completion handler has been supplied, call it. Otherwise,
11803 * just free the iocbq.
11804 */
11805
11806 spin_unlock_irqrestore(&phba->hbalock, iflags);
11807 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11808 cmdiocbq->wait_iocb_cmpl = NULL;
11809 if (cmdiocbq->iocb_cmpl)
11810 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11811 else
11812 lpfc_sli_release_iocbq(phba, cmdiocbq);
11813 return;
11814 }
11815
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011816 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11817 if (cmdiocbq->context2 && rspiocbq)
11818 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11819 &rspiocbq->iocb, sizeof(IOCB_t));
11820
James Smart0f65ff62010-02-26 14:14:23 -050011821 /* Set the exchange busy flag for task management commands */
11822 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11823 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11824 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11825 cur_iocbq);
11826 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11827 }
11828
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011829 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011830 if (pdone_q)
11831 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011832 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011833 return;
11834}
11835
James Smarte59058c2008-08-24 21:49:00 -040011836/**
James Smartd11e31d2009-06-10 17:23:06 -040011837 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11838 * @phba: Pointer to HBA context object..
11839 * @piocbq: Pointer to command iocb.
11840 * @flag: Flag to test.
11841 *
11842 * This routine grabs the hbalock and then test the iocb_flag to
11843 * see if the passed in flag is set.
11844 * Returns:
11845 * 1 if flag is set.
11846 * 0 if flag is not set.
11847 **/
11848static int
11849lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11850 struct lpfc_iocbq *piocbq, uint32_t flag)
11851{
11852 unsigned long iflags;
11853 int ret;
11854
11855 spin_lock_irqsave(&phba->hbalock, iflags);
11856 ret = piocbq->iocb_flag & flag;
11857 spin_unlock_irqrestore(&phba->hbalock, iflags);
11858 return ret;
11859
11860}
11861
11862/**
James Smart3621a712009-04-06 18:47:14 -040011863 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011864 * @phba: Pointer to HBA context object..
11865 * @pring: Pointer to sli ring.
11866 * @piocb: Pointer to command iocb.
11867 * @prspiocbq: Pointer to response iocb.
11868 * @timeout: Timeout in number of seconds.
11869 *
11870 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011871 * iocb to complete. The iocb_cmpl field of the shall be used
11872 * to handle iocbs which time out. If the field is NULL, the
11873 * function shall free the iocbq structure. If more clean up is
11874 * needed, the caller is expected to provide a completion function
11875 * that will provide the needed clean up. If the iocb command is
11876 * not completed within timeout seconds, the function will either
11877 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11878 * completion function set in the iocb_cmpl field and then return
11879 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11880 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011881 * The function waits for the iocb completion using an
11882 * non-interruptible wait.
11883 * This function will sleep while waiting for iocb completion.
11884 * So, this function should not be called from any context which
11885 * does not allow sleeping. Due to the same reason, this function
11886 * cannot be called with interrupt disabled.
11887 * This function assumes that the iocb completions occur while
11888 * this function sleep. So, this function cannot be called from
11889 * the thread which process iocb completion for this ring.
11890 * This function clears the iocb_flag of the iocb object before
11891 * issuing the iocb and the iocb completion handler sets this
11892 * flag and wakes this thread when the iocb completes.
11893 * The contents of the response iocb will be copied to prspiocbq
11894 * by the completion handler when the command completes.
11895 * This function returns IOCB_SUCCESS when success.
11896 * This function is called with no lock held.
11897 **/
dea31012005-04-17 16:05:31 -050011898int
James Smart2e0fef82007-06-17 19:56:36 -050011899lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011900 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011901 struct lpfc_iocbq *piocb,
11902 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011903 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011904{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011905 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011906 long timeleft, timeout_req = 0;
11907 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011908 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011909 struct lpfc_iocbq *iocb;
11910 int txq_cnt = 0;
11911 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011912 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011913 unsigned long iflags;
11914 bool iocb_completed = true;
11915
James Smart895427b2017-02-12 13:52:30 -080011916 if (phba->sli_rev >= LPFC_SLI_REV4)
11917 pring = lpfc_sli4_calc_ring(phba, piocb);
11918 else
11919 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011920 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011921 * If the caller has provided a response iocbq buffer, then context2
11922 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011923 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011924 if (prspiocbq) {
11925 if (piocb->context2)
11926 return IOCB_ERROR;
11927 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011928 }
11929
James Smart5a0916b2013-07-15 18:31:42 -040011930 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011931 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11932 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011933 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011934
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011935 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011936 if (lpfc_readl(phba->HCregaddr, &creg_val))
11937 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011938 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11939 writel(creg_val, phba->HCregaddr);
11940 readl(phba->HCregaddr); /* flush */
11941 }
11942
James Smart2a9bf3d2010-06-07 15:24:45 -040011943 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11944 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011945 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011946 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011947 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011948 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011949 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011950 spin_lock_irqsave(&phba->hbalock, iflags);
11951 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011952
James Smart5a0916b2013-07-15 18:31:42 -040011953 /*
11954 * IOCB timed out. Inform the wake iocb wait
11955 * completion function and set local status
11956 */
11957
11958 iocb_completed = false;
11959 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11960 }
11961 spin_unlock_irqrestore(&phba->hbalock, iflags);
11962 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011963 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011964 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011965 /* Note: we are not indicating if the IOCB has a success
11966 * status or not - that's for the caller to check.
11967 * IOCB_SUCCESS means just that the command was sent and
11968 * completed. Not that it completed successfully.
11969 * */
James Smart7054a602007-04-25 09:52:34 -040011970 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011971 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011972 "0338 IOCB wait timeout error - no "
11973 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011974 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011975 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011976 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011977 "0330 IOCB wake NOT set, "
11978 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011979 timeout, (timeleft / jiffies));
11980 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011981 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011982 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011983 if (phba->cfg_log_verbose & LOG_SLI) {
11984 list_for_each_entry(iocb, &pring->txq, list) {
11985 txq_cnt++;
11986 }
11987 list_for_each_entry(iocb, &pring->txcmplq, list) {
11988 txcmplq_cnt++;
11989 }
11990 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11991 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11992 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11993 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011994 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011995 } else {
11996 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011997 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011998 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011999 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050012000 }
12001
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012002 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050012003 if (lpfc_readl(phba->HCregaddr, &creg_val))
12004 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012005 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
12006 writel(creg_val, phba->HCregaddr);
12007 readl(phba->HCregaddr); /* flush */
12008 }
12009
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012010 if (prspiocbq)
12011 piocb->context2 = NULL;
12012
12013 piocb->context_un.wait_queue = NULL;
12014 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050012015 return retval;
12016}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012017
James Smarte59058c2008-08-24 21:49:00 -040012018/**
James Smart3621a712009-04-06 18:47:14 -040012019 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040012020 * @phba: Pointer to HBA context object.
12021 * @pmboxq: Pointer to driver mailbox object.
12022 * @timeout: Timeout in number of seconds.
12023 *
12024 * This function issues the mailbox to firmware and waits for the
12025 * mailbox command to complete. If the mailbox command is not
12026 * completed within timeout seconds, it returns MBX_TIMEOUT.
12027 * The function waits for the mailbox completion using an
12028 * interruptible wait. If the thread is woken up due to a
12029 * signal, MBX_TIMEOUT error is returned to the caller. Caller
12030 * should not free the mailbox resources, if this function returns
12031 * MBX_TIMEOUT.
12032 * This function will sleep while waiting for mailbox completion.
12033 * So, this function should not be called from any context which
12034 * does not allow sleeping. Due to the same reason, this function
12035 * cannot be called with interrupt disabled.
12036 * This function assumes that the mailbox completion occurs while
12037 * this function sleep. So, this function cannot be called from
12038 * the worker thread which processes mailbox completion.
12039 * This function is called in the context of HBA management
12040 * applications.
12041 * This function returns MBX_SUCCESS when successful.
12042 * This function is called with no lock held.
12043 **/
dea31012005-04-17 16:05:31 -050012044int
James Smart2e0fef82007-06-17 19:56:36 -050012045lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050012046 uint32_t timeout)
12047{
James Smarte29d74f2018-03-05 12:04:07 -080012048 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050012049 int retval;
James Smart858c9f62007-06-17 19:56:39 -050012050 unsigned long flag;
dea31012005-04-17 16:05:31 -050012051
James Smart495a7142008-06-14 22:52:59 -040012052 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050012053 /* setup wake call as IOCB callback */
12054 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050012055
James Smarte29d74f2018-03-05 12:04:07 -080012056 /* setup context3 field to pass wait_queue pointer to wake function */
12057 init_completion(&mbox_done);
12058 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050012059 /* now issue the command */
12060 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050012061 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080012062 wait_for_completion_timeout(&mbox_done,
12063 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040012064
James Smart858c9f62007-06-17 19:56:39 -050012065 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080012066 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040012067 /*
12068 * if LPFC_MBX_WAKE flag is set the mailbox is completed
12069 * else do not free the resources.
12070 */
James Smartd7c47992010-06-08 18:31:54 -040012071 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050012072 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040012073 } else {
James Smart7054a602007-04-25 09:52:34 -040012074 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050012075 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12076 }
12077 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050012078 }
dea31012005-04-17 16:05:31 -050012079 return retval;
12080}
12081
James Smarte59058c2008-08-24 21:49:00 -040012082/**
James Smart3772a992009-05-22 14:50:54 -040012083 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040012084 * @phba: Pointer to HBA context.
12085 *
James Smart3772a992009-05-22 14:50:54 -040012086 * This function is called to shutdown the driver's mailbox sub-system.
12087 * It first marks the mailbox sub-system is in a block state to prevent
12088 * the asynchronous mailbox command from issued off the pending mailbox
12089 * command queue. If the mailbox command sub-system shutdown is due to
12090 * HBA error conditions such as EEH or ERATT, this routine shall invoke
12091 * the mailbox sub-system flush routine to forcefully bring down the
12092 * mailbox sub-system. Otherwise, if it is due to normal condition (such
12093 * as with offline or HBA function reset), this routine will wait for the
12094 * outstanding mailbox command to complete before invoking the mailbox
12095 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040012096 **/
James Smart3772a992009-05-22 14:50:54 -040012097void
James Smart618a5232012-06-12 13:54:36 -040012098lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040012099{
James Smart3772a992009-05-22 14:50:54 -040012100 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040012101 unsigned long timeout;
12102
James Smart618a5232012-06-12 13:54:36 -040012103 if (mbx_action == LPFC_MBX_NO_WAIT) {
12104 /* delay 100ms for port state */
12105 msleep(100);
12106 lpfc_sli_mbox_sys_flush(phba);
12107 return;
12108 }
James Smarta183a152011-10-10 21:32:43 -040012109 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050012110
James Smart523128e2018-09-10 10:30:46 -070012111 /* Disable softirqs, including timers from obtaining phba->hbalock */
12112 local_bh_disable();
12113
James Smart3772a992009-05-22 14:50:54 -040012114 spin_lock_irq(&phba->hbalock);
12115 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040012116
12117 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040012118 /* Determine how long we might wait for the active mailbox
12119 * command to be gracefully completed by firmware.
12120 */
James Smarta183a152011-10-10 21:32:43 -040012121 if (phba->sli.mbox_active)
12122 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12123 phba->sli.mbox_active) *
12124 1000) + jiffies;
12125 spin_unlock_irq(&phba->hbalock);
12126
James Smart523128e2018-09-10 10:30:46 -070012127 /* Enable softirqs again, done with phba->hbalock */
12128 local_bh_enable();
12129
James Smart3772a992009-05-22 14:50:54 -040012130 while (phba->sli.mbox_active) {
12131 /* Check active mailbox complete status every 2ms */
12132 msleep(2);
12133 if (time_after(jiffies, timeout))
12134 /* Timeout, let the mailbox flush routine to
12135 * forcefully release active mailbox command
12136 */
12137 break;
12138 }
James Smart523128e2018-09-10 10:30:46 -070012139 } else {
James Smartd7069f02012-03-01 22:36:29 -050012140 spin_unlock_irq(&phba->hbalock);
12141
James Smart523128e2018-09-10 10:30:46 -070012142 /* Enable softirqs again, done with phba->hbalock */
12143 local_bh_enable();
12144 }
12145
James Smart3772a992009-05-22 14:50:54 -040012146 lpfc_sli_mbox_sys_flush(phba);
12147}
12148
12149/**
12150 * lpfc_sli_eratt_read - read sli-3 error attention events
12151 * @phba: Pointer to HBA context.
12152 *
12153 * This function is called to read the SLI3 device error attention registers
12154 * for possible error attention events. The caller must hold the hostlock
12155 * with spin_lock_irq().
12156 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012157 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040012158 * Register and returns 0 otherwise.
12159 **/
12160static int
12161lpfc_sli_eratt_read(struct lpfc_hba *phba)
12162{
James Smarted957682007-06-17 19:56:37 -050012163 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040012164
James Smart3772a992009-05-22 14:50:54 -040012165 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050012166 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12167 goto unplug_err;
12168
James Smart3772a992009-05-22 14:50:54 -040012169 if (ha_copy & HA_ERATT) {
12170 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050012171 if (lpfc_sli_read_hs(phba))
12172 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040012173
James Smart3772a992009-05-22 14:50:54 -040012174 /* Check if there is a deferred error condition is active */
12175 if ((HS_FFER1 & phba->work_hs) &&
12176 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012177 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040012178 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040012179 /* Clear all interrupt enable conditions */
12180 writel(0, phba->HCregaddr);
12181 readl(phba->HCregaddr);
12182 }
12183
12184 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040012185 phba->work_ha |= HA_ERATT;
12186 /* Indicate polling handles this ERATT */
12187 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012188 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012189 }
James Smart3772a992009-05-22 14:50:54 -040012190 return 0;
James Smart9940b972011-03-11 16:06:12 -050012191
12192unplug_err:
12193 /* Set the driver HS work bitmap */
12194 phba->work_hs |= UNPLUG_ERR;
12195 /* Set the driver HA work bitmap */
12196 phba->work_ha |= HA_ERATT;
12197 /* Indicate polling handles this ERATT */
12198 phba->hba_flag |= HBA_ERATT_HANDLED;
12199 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012200}
12201
James Smarte59058c2008-08-24 21:49:00 -040012202/**
James Smartda0436e2009-05-22 14:51:39 -040012203 * lpfc_sli4_eratt_read - read sli-4 error attention events
12204 * @phba: Pointer to HBA context.
12205 *
12206 * This function is called to read the SLI4 device error attention registers
12207 * for possible error attention events. The caller must hold the hostlock
12208 * with spin_lock_irq().
12209 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012210 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040012211 * Register and returns 0 otherwise.
12212 **/
12213static int
12214lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12215{
12216 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050012217 uint32_t if_type, portsmphr;
12218 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040012219
James Smart2fcee4b2010-12-15 17:57:46 -050012220 /*
12221 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040012222 * registers for error attention. This can be changed later.
12223 */
James Smart2fcee4b2010-12-15 17:57:46 -050012224 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12225 switch (if_type) {
12226 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050012227 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12228 &uerr_sta_lo) ||
12229 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12230 &uerr_sta_hi)) {
12231 phba->work_hs |= UNPLUG_ERR;
12232 phba->work_ha |= HA_ERATT;
12233 phba->hba_flag |= HBA_ERATT_HANDLED;
12234 return 1;
12235 }
James Smart2fcee4b2010-12-15 17:57:46 -050012236 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12237 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12238 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12239 "1423 HBA Unrecoverable error: "
12240 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12241 "ue_mask_lo_reg=0x%x, "
12242 "ue_mask_hi_reg=0x%x\n",
12243 uerr_sta_lo, uerr_sta_hi,
12244 phba->sli4_hba.ue_mask_lo,
12245 phba->sli4_hba.ue_mask_hi);
12246 phba->work_status[0] = uerr_sta_lo;
12247 phba->work_status[1] = uerr_sta_hi;
12248 phba->work_ha |= HA_ERATT;
12249 phba->hba_flag |= HBA_ERATT_HANDLED;
12250 return 1;
12251 }
12252 break;
12253 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080012254 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050012255 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12256 &portstat_reg.word0) ||
12257 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12258 &portsmphr)){
12259 phba->work_hs |= UNPLUG_ERR;
12260 phba->work_ha |= HA_ERATT;
12261 phba->hba_flag |= HBA_ERATT_HANDLED;
12262 return 1;
12263 }
James Smart2fcee4b2010-12-15 17:57:46 -050012264 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12265 phba->work_status[0] =
12266 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12267 phba->work_status[1] =
12268 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12269 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050012270 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050012271 "port status reg 0x%x, "
12272 "port smphr reg 0x%x, "
12273 "error 1=0x%x, error 2=0x%x\n",
12274 portstat_reg.word0,
12275 portsmphr,
12276 phba->work_status[0],
12277 phba->work_status[1]);
12278 phba->work_ha |= HA_ERATT;
12279 phba->hba_flag |= HBA_ERATT_HANDLED;
12280 return 1;
12281 }
12282 break;
12283 case LPFC_SLI_INTF_IF_TYPE_1:
12284 default:
James Smarta747c9c2009-11-18 15:41:10 -050012285 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050012286 "2886 HBA Error Attention on unsupported "
12287 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050012288 return 1;
James Smartda0436e2009-05-22 14:51:39 -040012289 }
James Smart2fcee4b2010-12-15 17:57:46 -050012290
James Smartda0436e2009-05-22 14:51:39 -040012291 return 0;
12292}
12293
12294/**
James Smart3621a712009-04-06 18:47:14 -040012295 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040012296 * @phba: Pointer to HBA context.
12297 *
James Smart3772a992009-05-22 14:50:54 -040012298 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040012299 * error attention register bit for error attention events.
12300 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012301 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040012302 * Register and returns 0 otherwise.
12303 **/
12304int
12305lpfc_sli_check_eratt(struct lpfc_hba *phba)
12306{
12307 uint32_t ha_copy;
12308
12309 /* If somebody is waiting to handle an eratt, don't process it
12310 * here. The brdkill function will do this.
12311 */
12312 if (phba->link_flag & LS_IGNORE_ERATT)
12313 return 0;
12314
12315 /* Check if interrupt handler handles this ERATT */
12316 spin_lock_irq(&phba->hbalock);
12317 if (phba->hba_flag & HBA_ERATT_HANDLED) {
12318 /* Interrupt handler has handled ERATT */
12319 spin_unlock_irq(&phba->hbalock);
12320 return 0;
12321 }
12322
James Smarta257bf92009-04-06 18:48:10 -040012323 /*
12324 * If there is deferred error attention, do not check for error
12325 * attention
12326 */
12327 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12328 spin_unlock_irq(&phba->hbalock);
12329 return 0;
12330 }
12331
James Smart3772a992009-05-22 14:50:54 -040012332 /* If PCI channel is offline, don't process it */
12333 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040012334 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012335 return 0;
12336 }
12337
12338 switch (phba->sli_rev) {
12339 case LPFC_SLI_REV2:
12340 case LPFC_SLI_REV3:
12341 /* Read chip Host Attention (HA) register */
12342 ha_copy = lpfc_sli_eratt_read(phba);
12343 break;
James Smartda0436e2009-05-22 14:51:39 -040012344 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050012345 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040012346 ha_copy = lpfc_sli4_eratt_read(phba);
12347 break;
James Smart3772a992009-05-22 14:50:54 -040012348 default:
12349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12350 "0299 Invalid SLI revision (%d)\n",
12351 phba->sli_rev);
12352 ha_copy = 0;
12353 break;
James Smart93996272008-08-24 21:50:30 -040012354 }
12355 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012356
12357 return ha_copy;
12358}
12359
12360/**
12361 * lpfc_intr_state_check - Check device state for interrupt handling
12362 * @phba: Pointer to HBA context.
12363 *
12364 * This inline routine checks whether a device or its PCI slot is in a state
12365 * that the interrupt should be handled.
12366 *
12367 * This function returns 0 if the device or the PCI slot is in a state that
12368 * interrupt should be handled, otherwise -EIO.
12369 */
12370static inline int
12371lpfc_intr_state_check(struct lpfc_hba *phba)
12372{
12373 /* If the pci channel is offline, ignore all the interrupts */
12374 if (unlikely(pci_channel_offline(phba->pcidev)))
12375 return -EIO;
12376
12377 /* Update device level interrupt statistics */
12378 phba->sli.slistat.sli_intr++;
12379
12380 /* Ignore all interrupts during initialization. */
12381 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12382 return -EIO;
12383
James Smart93996272008-08-24 21:50:30 -040012384 return 0;
12385}
12386
12387/**
James Smart3772a992009-05-22 14:50:54 -040012388 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012389 * @irq: Interrupt number.
12390 * @dev_id: The device context pointer.
12391 *
James Smart93996272008-08-24 21:50:30 -040012392 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012393 * service routine when device with SLI-3 interface spec is enabled with
12394 * MSI-X multi-message interrupt mode and there are slow-path events in
12395 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12396 * interrupt mode, this function is called as part of the device-level
12397 * interrupt handler. When the PCI slot is in error recovery or the HBA
12398 * is undergoing initialization, the interrupt handler will not process
12399 * the interrupt. The link attention and ELS ring attention events are
12400 * handled by the worker thread. The interrupt handler signals the worker
12401 * thread and returns for these events. This function is called without
12402 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012403 * structures.
12404 *
12405 * This function returns IRQ_HANDLED when interrupt is handled else it
12406 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012407 **/
dea31012005-04-17 16:05:31 -050012408irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012409lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012410{
James Smart2e0fef82007-06-17 19:56:36 -050012411 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012412 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012413 uint32_t work_ha_copy;
12414 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012415 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012416 uint32_t control;
12417
James Smart92d7f7b2007-06-17 19:56:38 -050012418 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012419 struct lpfc_vport *vport;
12420 struct lpfc_nodelist *ndlp;
12421 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012422 LPFC_MBOXQ_t *pmb;
12423 int rc;
12424
dea31012005-04-17 16:05:31 -050012425 /*
12426 * Get the driver's phba structure from the dev_id and
12427 * assume the HBA is not interrupting.
12428 */
James Smart93996272008-08-24 21:50:30 -040012429 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012430
12431 if (unlikely(!phba))
12432 return IRQ_NONE;
12433
dea31012005-04-17 16:05:31 -050012434 /*
James Smart93996272008-08-24 21:50:30 -040012435 * Stuff needs to be attented to when this function is invoked as an
12436 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012437 */
James Smart93996272008-08-24 21:50:30 -040012438 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012439 /* Check device state for handling interrupt */
12440 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012441 return IRQ_NONE;
12442 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012443 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012444 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12445 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012446 /* If somebody is waiting to handle an eratt don't process it
12447 * here. The brdkill function will do this.
12448 */
12449 if (phba->link_flag & LS_IGNORE_ERATT)
12450 ha_copy &= ~HA_ERATT;
12451 /* Check the need for handling ERATT in interrupt handler */
12452 if (ha_copy & HA_ERATT) {
12453 if (phba->hba_flag & HBA_ERATT_HANDLED)
12454 /* ERATT polling has handled ERATT */
12455 ha_copy &= ~HA_ERATT;
12456 else
12457 /* Indicate interrupt handler handles ERATT */
12458 phba->hba_flag |= HBA_ERATT_HANDLED;
12459 }
James Smarta257bf92009-04-06 18:48:10 -040012460
12461 /*
12462 * If there is deferred error attention, do not check for any
12463 * interrupt.
12464 */
12465 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012466 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012467 return IRQ_NONE;
12468 }
12469
James Smart93996272008-08-24 21:50:30 -040012470 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012471 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12472 goto unplug_error;
12473
James Smarta747c9c2009-11-18 15:41:10 -050012474 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12475 HC_LAINT_ENA | HC_ERINT_ENA),
12476 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012477 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12478 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012479 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012480 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012481 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012482 } else
12483 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012484
dea31012005-04-17 16:05:31 -050012485 work_ha_copy = ha_copy & phba->work_ha_mask;
12486
James Smart93996272008-08-24 21:50:30 -040012487 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012488 if (work_ha_copy & HA_LATT) {
12489 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12490 /*
12491 * Turn off Link Attention interrupts
12492 * until CLEAR_LA done
12493 */
James Smart5b75da22008-12-04 22:39:35 -050012494 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012495 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012496 if (lpfc_readl(phba->HCregaddr, &control))
12497 goto unplug_error;
dea31012005-04-17 16:05:31 -050012498 control &= ~HC_LAINT_ENA;
12499 writel(control, phba->HCregaddr);
12500 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012501 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012502 }
12503 else
12504 work_ha_copy &= ~HA_LATT;
12505 }
12506
James Smart93996272008-08-24 21:50:30 -040012507 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012508 /*
12509 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12510 * the only slow ring.
12511 */
12512 status = (work_ha_copy &
12513 (HA_RXMASK << (4*LPFC_ELS_RING)));
12514 status >>= (4*LPFC_ELS_RING);
12515 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012516 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012517 if (lpfc_readl(phba->HCregaddr, &control))
12518 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012519
12520 lpfc_debugfs_slow_ring_trc(phba,
12521 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12522 control, status,
12523 (uint32_t)phba->sli.slistat.sli_intr);
12524
James Smart858c9f62007-06-17 19:56:39 -050012525 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012526 lpfc_debugfs_slow_ring_trc(phba,
12527 "ISR Disable ring:"
12528 "pwork:x%x hawork:x%x wait:x%x",
12529 phba->work_ha, work_ha_copy,
12530 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012531 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012532
James Smart858c9f62007-06-17 19:56:39 -050012533 control &=
12534 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012535 writel(control, phba->HCregaddr);
12536 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012537 }
James Smarta58cbd52007-08-02 11:09:43 -040012538 else {
12539 lpfc_debugfs_slow_ring_trc(phba,
12540 "ISR slow ring: pwork:"
12541 "x%x hawork:x%x wait:x%x",
12542 phba->work_ha, work_ha_copy,
12543 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012544 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012545 }
James Smart5b75da22008-12-04 22:39:35 -050012546 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012547 }
12548 }
James Smart5b75da22008-12-04 22:39:35 -050012549 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012550 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012551 if (lpfc_sli_read_hs(phba))
12552 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012553 /*
12554 * Check if there is a deferred error condition
12555 * is active
12556 */
12557 if ((HS_FFER1 & phba->work_hs) &&
12558 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012559 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12560 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012561 phba->hba_flag |= DEFER_ERATT;
12562 /* Clear all interrupt enable conditions */
12563 writel(0, phba->HCregaddr);
12564 readl(phba->HCregaddr);
12565 }
12566 }
12567
James Smart93996272008-08-24 21:50:30 -040012568 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012569 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012570 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012571 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012572 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012573
12574 /* First check out the status word */
12575 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12576 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012577 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012578 /*
12579 * Stray Mailbox Interrupt, mbxCommand <cmd>
12580 * mbxStatus <status>
12581 */
James Smart09372822008-01-11 01:52:54 -050012582 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012583 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012584 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012585 "Interrupt mbxCommand x%x "
12586 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012587 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012588 pmbox->mbxCommand,
12589 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012590 /* clear mailbox attention bit */
12591 work_ha_copy &= ~HA_MBATT;
12592 } else {
James Smart97eab632008-04-07 10:16:05 -040012593 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012594 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012595 phba->last_completion_time = jiffies;
12596 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012597 if (pmb->mbox_cmpl) {
12598 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12599 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012600 if (pmb->out_ext_byte_len &&
James Smart3e1f0712018-11-29 16:09:29 -080012601 pmb->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -040012602 lpfc_sli_pcimem_bcopy(
12603 phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -080012604 pmb->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -040012605 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012606 }
James Smart09372822008-01-11 01:52:54 -050012607 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12608 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12609
12610 lpfc_debugfs_disc_trc(vport,
12611 LPFC_DISC_TRC_MBOX_VPORT,
12612 "MBOX dflt rpi: : "
12613 "status:x%x rpi:x%x",
12614 (uint32_t)pmbox->mbxStatus,
12615 pmbox->un.varWords[0], 0);
12616
12617 if (!pmbox->mbxStatus) {
12618 mp = (struct lpfc_dmabuf *)
James Smart3e1f0712018-11-29 16:09:29 -080012619 (pmb->ctx_buf);
James Smart09372822008-01-11 01:52:54 -050012620 ndlp = (struct lpfc_nodelist *)
James Smart3e1f0712018-11-29 16:09:29 -080012621 pmb->ctx_ndlp;
James Smart09372822008-01-11 01:52:54 -050012622
12623 /* Reg_LOGIN of dflt RPI was
12624 * successful. new lets get
12625 * rid of the RPI using the
12626 * same mbox buffer.
12627 */
12628 lpfc_unreg_login(phba,
12629 vport->vpi,
12630 pmbox->un.varWords[0],
12631 pmb);
12632 pmb->mbox_cmpl =
12633 lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080012634 pmb->ctx_buf = mp;
12635 pmb->ctx_ndlp = ndlp;
James Smart09372822008-01-11 01:52:54 -050012636 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012637 rc = lpfc_sli_issue_mbox(phba,
12638 pmb,
12639 MBX_NOWAIT);
12640 if (rc != MBX_BUSY)
12641 lpfc_printf_log(phba,
12642 KERN_ERR,
12643 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012644 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012645 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012646 if (rc != MBX_NOT_FINISHED)
12647 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012648 }
12649 }
James Smart5b75da22008-12-04 22:39:35 -050012650 spin_lock_irqsave(
12651 &phba->pport->work_port_lock,
12652 iflag);
James Smart09372822008-01-11 01:52:54 -050012653 phba->pport->work_port_events &=
12654 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012655 spin_unlock_irqrestore(
12656 &phba->pport->work_port_lock,
12657 iflag);
James Smart09372822008-01-11 01:52:54 -050012658 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012659 }
James Smart97eab632008-04-07 10:16:05 -040012660 } else
James Smart5b75da22008-12-04 22:39:35 -050012661 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012662
James Smart92d7f7b2007-06-17 19:56:38 -050012663 if ((work_ha_copy & HA_MBATT) &&
12664 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012665send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012666 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012667 do {
12668 rc = lpfc_sli_issue_mbox(phba, NULL,
12669 MBX_NOWAIT);
12670 } while (rc == MBX_NOT_FINISHED);
12671 if (rc != MBX_SUCCESS)
12672 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12673 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012674 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012675 }
12676
James Smart5b75da22008-12-04 22:39:35 -050012677 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012678 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012679 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012680 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012681 }
James Smart93996272008-08-24 21:50:30 -040012682 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012683unplug_error:
12684 spin_unlock_irqrestore(&phba->hbalock, iflag);
12685 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012686
James Smart3772a992009-05-22 14:50:54 -040012687} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012688
12689/**
James Smart3772a992009-05-22 14:50:54 -040012690 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012691 * @irq: Interrupt number.
12692 * @dev_id: The device context pointer.
12693 *
12694 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012695 * service routine when device with SLI-3 interface spec is enabled with
12696 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12697 * ring event in the HBA. However, when the device is enabled with either
12698 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12699 * device-level interrupt handler. When the PCI slot is in error recovery
12700 * or the HBA is undergoing initialization, the interrupt handler will not
12701 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12702 * the intrrupt context. This function is called without any lock held.
12703 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012704 *
12705 * This function returns IRQ_HANDLED when interrupt is handled else it
12706 * returns IRQ_NONE.
12707 **/
12708irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012709lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012710{
12711 struct lpfc_hba *phba;
12712 uint32_t ha_copy;
12713 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012714 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012715 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012716
12717 /* Get the driver's phba structure from the dev_id and
12718 * assume the HBA is not interrupting.
12719 */
12720 phba = (struct lpfc_hba *) dev_id;
12721
12722 if (unlikely(!phba))
12723 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012724
12725 /*
James Smart93996272008-08-24 21:50:30 -040012726 * Stuff needs to be attented to when this function is invoked as an
12727 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012728 */
James Smart93996272008-08-24 21:50:30 -040012729 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012730 /* Check device state for handling interrupt */
12731 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012732 return IRQ_NONE;
12733 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012734 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12735 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012736 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012737 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012738 /*
12739 * If there is deferred error attention, do not check for
12740 * any interrupt.
12741 */
12742 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012743 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012744 return IRQ_NONE;
12745 }
James Smart93996272008-08-24 21:50:30 -040012746 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12747 phba->HAregaddr);
12748 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012749 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012750 } else
12751 ha_copy = phba->ha_copy;
12752
12753 /*
12754 * Process all events on FCP ring. Take the optimized path for FCP IO.
12755 */
12756 ha_copy &= ~(phba->work_ha_mask);
12757
12758 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012759 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012760 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012761 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012762 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012763
12764 if (phba->cfg_multi_ring_support == 2) {
12765 /*
James Smart93996272008-08-24 21:50:30 -040012766 * Process all events on extra ring. Take the optimized path
12767 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012768 */
James Smart93996272008-08-24 21:50:30 -040012769 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012770 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012771 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012772 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012773 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012774 status);
12775 }
12776 }
dea31012005-04-17 16:05:31 -050012777 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012778} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012779
James Smart93996272008-08-24 21:50:30 -040012780/**
James Smart3772a992009-05-22 14:50:54 -040012781 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012782 * @irq: Interrupt number.
12783 * @dev_id: The device context pointer.
12784 *
James Smart3772a992009-05-22 14:50:54 -040012785 * This function is the HBA device-level interrupt handler to device with
12786 * SLI-3 interface spec, called from the PCI layer when either MSI or
12787 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12788 * requires driver attention. This function invokes the slow-path interrupt
12789 * attention handling function and fast-path interrupt attention handling
12790 * function in turn to process the relevant HBA attention events. This
12791 * function is called without any lock held. It gets the hbalock to access
12792 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012793 *
12794 * This function returns IRQ_HANDLED when interrupt is handled, else it
12795 * returns IRQ_NONE.
12796 **/
12797irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012798lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012799{
12800 struct lpfc_hba *phba;
12801 irqreturn_t sp_irq_rc, fp_irq_rc;
12802 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012803 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012804
12805 /*
12806 * Get the driver's phba structure from the dev_id and
12807 * assume the HBA is not interrupting.
12808 */
12809 phba = (struct lpfc_hba *) dev_id;
12810
12811 if (unlikely(!phba))
12812 return IRQ_NONE;
12813
James Smart3772a992009-05-22 14:50:54 -040012814 /* Check device state for handling interrupt */
12815 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012816 return IRQ_NONE;
12817
12818 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012819 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12820 spin_unlock(&phba->hbalock);
12821 return IRQ_HANDLED;
12822 }
12823
James Smart93996272008-08-24 21:50:30 -040012824 if (unlikely(!phba->ha_copy)) {
12825 spin_unlock(&phba->hbalock);
12826 return IRQ_NONE;
12827 } else if (phba->ha_copy & HA_ERATT) {
12828 if (phba->hba_flag & HBA_ERATT_HANDLED)
12829 /* ERATT polling has handled ERATT */
12830 phba->ha_copy &= ~HA_ERATT;
12831 else
12832 /* Indicate interrupt handler handles ERATT */
12833 phba->hba_flag |= HBA_ERATT_HANDLED;
12834 }
12835
James Smarta257bf92009-04-06 18:48:10 -040012836 /*
12837 * If there is deferred error attention, do not check for any interrupt.
12838 */
12839 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012840 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012841 return IRQ_NONE;
12842 }
12843
James Smart93996272008-08-24 21:50:30 -040012844 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012845 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12846 spin_unlock(&phba->hbalock);
12847 return IRQ_HANDLED;
12848 }
James Smarta747c9c2009-11-18 15:41:10 -050012849 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12850 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12851 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012852 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012853 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012854 readl(phba->HAregaddr); /* flush */
12855 spin_unlock(&phba->hbalock);
12856
12857 /*
12858 * Invokes slow-path host attention interrupt handling as appropriate.
12859 */
12860
12861 /* status of events with mailbox and link attention */
12862 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12863
12864 /* status of events with ELS ring */
12865 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12866 status2 >>= (4*LPFC_ELS_RING);
12867
12868 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012869 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012870 else
12871 sp_irq_rc = IRQ_NONE;
12872
12873 /*
12874 * Invoke fast-path host attention interrupt handling as appropriate.
12875 */
12876
12877 /* status of events with FCP ring */
12878 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12879 status1 >>= (4*LPFC_FCP_RING);
12880
12881 /* status of events with extra ring */
12882 if (phba->cfg_multi_ring_support == 2) {
12883 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12884 status2 >>= (4*LPFC_EXTRA_RING);
12885 } else
12886 status2 = 0;
12887
12888 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012889 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012890 else
12891 fp_irq_rc = IRQ_NONE;
12892
12893 /* Return device-level interrupt handling status */
12894 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012895} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012896
12897/**
James Smart4f774512009-05-22 14:52:35 -040012898 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12899 * @phba: pointer to lpfc hba data structure.
12900 *
12901 * This routine is invoked by the worker thread to process all the pending
12902 * SLI4 els abort xri events.
12903 **/
12904void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12905{
12906 struct lpfc_cq_event *cq_event;
12907
12908 /* First, declare the els xri abort event has been handled */
12909 spin_lock_irq(&phba->hbalock);
12910 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12911 spin_unlock_irq(&phba->hbalock);
12912 /* Now, handle all the els xri abort events */
12913 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12914 /* Get the first event from the head of the event queue */
12915 spin_lock_irq(&phba->hbalock);
12916 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12917 cq_event, struct lpfc_cq_event, list);
12918 spin_unlock_irq(&phba->hbalock);
12919 /* Notify aborted XRI for ELS work queue */
12920 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12921 /* Free the event processed back to the free pool */
12922 lpfc_sli4_cq_event_release(phba, cq_event);
12923 }
12924}
12925
James Smart341af102010-01-26 23:07:37 -050012926/**
12927 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12928 * @phba: pointer to lpfc hba data structure
12929 * @pIocbIn: pointer to the rspiocbq
12930 * @pIocbOut: pointer to the cmdiocbq
12931 * @wcqe: pointer to the complete wcqe
12932 *
12933 * This routine transfers the fields of a command iocbq to a response iocbq
12934 * by copying all the IOCB fields from command iocbq and transferring the
12935 * completion status information from the complete wcqe.
12936 **/
James Smart4f774512009-05-22 14:52:35 -040012937static void
James Smart341af102010-01-26 23:07:37 -050012938lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12939 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012940 struct lpfc_iocbq *pIocbOut,
12941 struct lpfc_wcqe_complete *wcqe)
12942{
James Smartaf227412013-10-10 12:23:10 -040012943 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012944 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012945 uint32_t status, max_response;
12946 struct lpfc_dmabuf *dmabuf;
12947 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012948 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12949
12950 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12951 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012952 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012953 status = bf_get(lpfc_wcqe_c_status, wcqe);
12954 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012955 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12956 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12957 pIocbIn->iocb.un.fcpi.fcpi_parm =
12958 pIocbOut->iocb.un.fcpi.fcpi_parm -
12959 wcqe->total_data_placed;
12960 else
12961 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012962 else {
James Smart4f774512009-05-22 14:52:35 -040012963 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012964 switch (pIocbOut->iocb.ulpCommand) {
12965 case CMD_ELS_REQUEST64_CR:
12966 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12967 bpl = (struct ulp_bde64 *)dmabuf->virt;
12968 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12969 max_response = bde.tus.f.bdeSize;
12970 break;
12971 case CMD_GEN_REQUEST64_CR:
12972 max_response = 0;
12973 if (!pIocbOut->context3)
12974 break;
12975 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12976 sizeof(struct ulp_bde64);
12977 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12978 bpl = (struct ulp_bde64 *)dmabuf->virt;
12979 for (i = 0; i < numBdes; i++) {
12980 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12981 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12982 max_response += bde.tus.f.bdeSize;
12983 }
12984 break;
12985 default:
12986 max_response = wcqe->total_data_placed;
12987 break;
12988 }
12989 if (max_response < wcqe->total_data_placed)
12990 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12991 else
12992 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12993 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012994 }
James Smart341af102010-01-26 23:07:37 -050012995
James Smartacd68592012-01-18 16:25:09 -050012996 /* Convert BG errors for completion status */
12997 if (status == CQE_STATUS_DI_ERROR) {
12998 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12999
13000 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
13001 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
13002 else
13003 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
13004
13005 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
13006 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
13007 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13008 BGS_GUARD_ERR_MASK;
13009 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
13010 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13011 BGS_APPTAG_ERR_MASK;
13012 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
13013 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13014 BGS_REFTAG_ERR_MASK;
13015
13016 /* Check to see if there was any good data before the error */
13017 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
13018 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13019 BGS_HI_WATER_MARK_PRESENT_MASK;
13020 pIocbIn->iocb.unsli3.sli3_bg.bghm =
13021 wcqe->total_data_placed;
13022 }
13023
13024 /*
13025 * Set ALL the error bits to indicate we don't know what
13026 * type of error it is.
13027 */
13028 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
13029 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13030 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
13031 BGS_GUARD_ERR_MASK);
13032 }
13033
James Smart341af102010-01-26 23:07:37 -050013034 /* Pick up HBA exchange busy condition */
13035 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
13036 spin_lock_irqsave(&phba->hbalock, iflags);
13037 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
13038 spin_unlock_irqrestore(&phba->hbalock, iflags);
13039 }
James Smart4f774512009-05-22 14:52:35 -040013040}
13041
13042/**
James Smart45ed1192009-10-02 15:17:02 -040013043 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
13044 * @phba: Pointer to HBA context object.
13045 * @wcqe: Pointer to work-queue completion queue entry.
13046 *
13047 * This routine handles an ELS work-queue completion event and construct
13048 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
13049 * discovery engine to handle.
13050 *
13051 * Return: Pointer to the receive IOCBQ, NULL otherwise.
13052 **/
13053static struct lpfc_iocbq *
13054lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
13055 struct lpfc_iocbq *irspiocbq)
13056{
James Smart895427b2017-02-12 13:52:30 -080013057 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040013058 struct lpfc_iocbq *cmdiocbq;
13059 struct lpfc_wcqe_complete *wcqe;
13060 unsigned long iflags;
13061
James Smart895427b2017-02-12 13:52:30 -080013062 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070013063 if (unlikely(!pring))
13064 return NULL;
James Smart895427b2017-02-12 13:52:30 -080013065
James Smart45ed1192009-10-02 15:17:02 -040013066 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040013067 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040013068 pring->stats.iocb_event++;
13069 /* Look up the ELS command IOCB and create pseudo response IOCB */
13070 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13071 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040013072 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070013073 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040013074 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13075 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070013076 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
13077 wcqe->word0, wcqe->total_data_placed,
13078 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040013079 lpfc_sli_release_iocbq(phba, irspiocbq);
13080 return NULL;
13081 }
13082
Dick Kennedy401bb412017-09-29 17:34:28 -070013083 /* Put the iocb back on the txcmplq */
13084 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13085 spin_unlock_irqrestore(&pring->ring_lock, iflags);
13086
James Smart45ed1192009-10-02 15:17:02 -040013087 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013088 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040013089
13090 return irspiocbq;
13091}
13092
James Smart8a5ca102017-11-20 16:00:30 -080013093inline struct lpfc_cq_event *
13094lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13095{
13096 struct lpfc_cq_event *cq_event;
13097
13098 /* Allocate a new internal CQ_EVENT entry */
13099 cq_event = lpfc_sli4_cq_event_alloc(phba);
13100 if (!cq_event) {
13101 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13102 "0602 Failed to alloc CQ_EVENT entry\n");
13103 return NULL;
13104 }
13105
13106 /* Move the CQE into the event */
13107 memcpy(&cq_event->cqe, entry, size);
13108 return cq_event;
13109}
13110
James Smart45ed1192009-10-02 15:17:02 -040013111/**
James Smart04c68492009-05-22 14:52:52 -040013112 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
13113 * @phba: Pointer to HBA context object.
13114 * @cqe: Pointer to mailbox completion queue entry.
13115 *
13116 * This routine process a mailbox completion queue entry with asynchrous
13117 * event.
13118 *
13119 * Return: true if work posted to worker thread, otherwise false.
13120 **/
13121static bool
13122lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13123{
13124 struct lpfc_cq_event *cq_event;
13125 unsigned long iflags;
13126
13127 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13128 "0392 Async Event: word0:x%x, word1:x%x, "
13129 "word2:x%x, word3:x%x\n", mcqe->word0,
13130 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13131
James Smart8a5ca102017-11-20 16:00:30 -080013132 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13133 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040013134 return false;
James Smart04c68492009-05-22 14:52:52 -040013135 spin_lock_irqsave(&phba->hbalock, iflags);
13136 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13137 /* Set the async event flag */
13138 phba->hba_flag |= ASYNC_EVENT;
13139 spin_unlock_irqrestore(&phba->hbalock, iflags);
13140
13141 return true;
13142}
13143
13144/**
13145 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13146 * @phba: Pointer to HBA context object.
13147 * @cqe: Pointer to mailbox completion queue entry.
13148 *
13149 * This routine process a mailbox completion queue entry with mailbox
13150 * completion event.
13151 *
13152 * Return: true if work posted to worker thread, otherwise false.
13153 **/
13154static bool
13155lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13156{
13157 uint32_t mcqe_status;
13158 MAILBOX_t *mbox, *pmbox;
13159 struct lpfc_mqe *mqe;
13160 struct lpfc_vport *vport;
13161 struct lpfc_nodelist *ndlp;
13162 struct lpfc_dmabuf *mp;
13163 unsigned long iflags;
13164 LPFC_MBOXQ_t *pmb;
13165 bool workposted = false;
13166 int rc;
13167
13168 /* If not a mailbox complete MCQE, out by checking mailbox consume */
13169 if (!bf_get(lpfc_trailer_completed, mcqe))
13170 goto out_no_mqe_complete;
13171
13172 /* Get the reference to the active mbox command */
13173 spin_lock_irqsave(&phba->hbalock, iflags);
13174 pmb = phba->sli.mbox_active;
13175 if (unlikely(!pmb)) {
13176 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13177 "1832 No pending MBOX command to handle\n");
13178 spin_unlock_irqrestore(&phba->hbalock, iflags);
13179 goto out_no_mqe_complete;
13180 }
13181 spin_unlock_irqrestore(&phba->hbalock, iflags);
13182 mqe = &pmb->u.mqe;
13183 pmbox = (MAILBOX_t *)&pmb->u.mqe;
13184 mbox = phba->mbox;
13185 vport = pmb->vport;
13186
13187 /* Reset heartbeat timer */
13188 phba->last_completion_time = jiffies;
13189 del_timer(&phba->sli.mbox_tmo);
13190
13191 /* Move mbox data to caller's mailbox region, do endian swapping */
13192 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070013193 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040013194
James Smart73d91e52011-10-10 21:32:10 -040013195 /*
13196 * For mcqe errors, conditionally move a modified error code to
13197 * the mbox so that the error will not be missed.
13198 */
13199 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13200 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13201 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13202 bf_set(lpfc_mqe_status, mqe,
13203 (LPFC_MBX_ERROR_RANGE | mcqe_status));
13204 }
James Smart04c68492009-05-22 14:52:52 -040013205 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13206 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13207 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13208 "MBOX dflt rpi: status:x%x rpi:x%x",
13209 mcqe_status,
13210 pmbox->un.varWords[0], 0);
13211 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
James Smart3e1f0712018-11-29 16:09:29 -080013212 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13213 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart04c68492009-05-22 14:52:52 -040013214 /* Reg_LOGIN of dflt RPI was successful. Now lets get
13215 * RID of the PPI using the same mbox buffer.
13216 */
13217 lpfc_unreg_login(phba, vport->vpi,
13218 pmbox->un.varWords[0], pmb);
13219 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080013220 pmb->ctx_buf = mp;
13221 pmb->ctx_ndlp = ndlp;
James Smart04c68492009-05-22 14:52:52 -040013222 pmb->vport = vport;
13223 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13224 if (rc != MBX_BUSY)
13225 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
13226 LOG_SLI, "0385 rc should "
13227 "have been MBX_BUSY\n");
13228 if (rc != MBX_NOT_FINISHED)
13229 goto send_current_mbox;
13230 }
13231 }
13232 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13233 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13234 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13235
13236 /* There is mailbox completion work to do */
13237 spin_lock_irqsave(&phba->hbalock, iflags);
13238 __lpfc_mbox_cmpl_put(phba, pmb);
13239 phba->work_ha |= HA_MBATT;
13240 spin_unlock_irqrestore(&phba->hbalock, iflags);
13241 workposted = true;
13242
13243send_current_mbox:
13244 spin_lock_irqsave(&phba->hbalock, iflags);
13245 /* Release the mailbox command posting token */
13246 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13247 /* Setting active mailbox pointer need to be in sync to flag clear */
13248 phba->sli.mbox_active = NULL;
13249 spin_unlock_irqrestore(&phba->hbalock, iflags);
13250 /* Wake up worker thread to post the next pending mailbox command */
13251 lpfc_worker_wake_up(phba);
13252out_no_mqe_complete:
13253 if (bf_get(lpfc_trailer_consumed, mcqe))
13254 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13255 return workposted;
13256}
13257
13258/**
13259 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13260 * @phba: Pointer to HBA context object.
13261 * @cqe: Pointer to mailbox completion queue entry.
13262 *
13263 * This routine process a mailbox completion queue entry, it invokes the
13264 * proper mailbox complete handling or asynchrous event handling routine
13265 * according to the MCQE's async bit.
13266 *
13267 * Return: true if work posted to worker thread, otherwise false.
13268 **/
13269static bool
13270lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
13271{
13272 struct lpfc_mcqe mcqe;
13273 bool workposted;
13274
13275 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013276 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040013277
13278 /* Invoke the proper event handling routine */
13279 if (!bf_get(lpfc_trailer_async, &mcqe))
13280 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13281 else
13282 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13283 return workposted;
13284}
13285
13286/**
James Smart4f774512009-05-22 14:52:35 -040013287 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13288 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040013289 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040013290 * @wcqe: Pointer to work-queue completion queue entry.
13291 *
13292 * This routine handles an ELS work-queue completion event.
13293 *
13294 * Return: true if work posted to worker thread, otherwise false.
13295 **/
13296static bool
James Smart2a76a282012-08-03 12:35:54 -040013297lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013298 struct lpfc_wcqe_complete *wcqe)
13299{
James Smart4f774512009-05-22 14:52:35 -040013300 struct lpfc_iocbq *irspiocbq;
13301 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040013302 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050013303 int txq_cnt = 0;
13304 int txcmplq_cnt = 0;
13305 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040013306
James Smart11f0e342018-05-04 20:37:57 -070013307 /* Check for response status */
13308 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13309 /* Log the error status */
13310 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13311 "0357 ELS CQE error: status=x%x: "
13312 "CQE: %08x %08x %08x %08x\n",
13313 bf_get(lpfc_wcqe_c_status, wcqe),
13314 wcqe->word0, wcqe->total_data_placed,
13315 wcqe->parameter, wcqe->word3);
13316 }
13317
James Smart45ed1192009-10-02 15:17:02 -040013318 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040013319 irspiocbq = lpfc_sli_get_iocbq(phba);
13320 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013321 if (!list_empty(&pring->txq))
13322 txq_cnt++;
13323 if (!list_empty(&pring->txcmplq))
13324 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040013325 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040013326 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13327 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050013328 txq_cnt, phba->iocb_cnt,
13329 fcp_txcmplq_cnt,
13330 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040013331 return false;
James Smart4f774512009-05-22 14:52:35 -040013332 }
James Smart4f774512009-05-22 14:52:35 -040013333
James Smart45ed1192009-10-02 15:17:02 -040013334 /* Save off the slow-path queue event for work thread to process */
13335 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040013336 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040013337 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013338 &phba->sli4_hba.sp_queue_event);
13339 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013340 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013341
James Smart45ed1192009-10-02 15:17:02 -040013342 return true;
James Smart4f774512009-05-22 14:52:35 -040013343}
13344
13345/**
13346 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13347 * @phba: Pointer to HBA context object.
13348 * @wcqe: Pointer to work-queue completion queue entry.
13349 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013350 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013351 * proper WQ release routine to the slow-path WQ.
13352 **/
13353static void
13354lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13355 struct lpfc_wcqe_release *wcqe)
13356{
James Smart2e90f4b2011-12-13 13:22:37 -050013357 /* sanity check on queue memory */
13358 if (unlikely(!phba->sli4_hba.els_wq))
13359 return;
James Smart4f774512009-05-22 14:52:35 -040013360 /* Check for the slow-path ELS work queue */
13361 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13362 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13363 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13364 else
13365 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13366 "2579 Slow-path wqe consume event carries "
13367 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13368 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13369 phba->sli4_hba.els_wq->queue_id);
13370}
13371
13372/**
13373 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13374 * @phba: Pointer to HBA context object.
13375 * @cq: Pointer to a WQ completion queue.
13376 * @wcqe: Pointer to work-queue completion queue entry.
13377 *
13378 * This routine handles an XRI abort event.
13379 *
13380 * Return: true if work posted to worker thread, otherwise false.
13381 **/
13382static bool
13383lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13384 struct lpfc_queue *cq,
13385 struct sli4_wcqe_xri_aborted *wcqe)
13386{
13387 bool workposted = false;
13388 struct lpfc_cq_event *cq_event;
13389 unsigned long iflags;
13390
James Smart4f774512009-05-22 14:52:35 -040013391 switch (cq->subtype) {
13392 case LPFC_FCP:
James Smart5e5b5112019-01-28 11:14:22 -080013393 lpfc_sli4_fcp_xri_aborted(phba, wcqe, cq->hdwq);
13394 workposted = false;
James Smart4f774512009-05-22 14:52:35 -040013395 break;
James Smart422c4cb2017-11-20 16:00:32 -080013396 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013397 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013398 cq_event = lpfc_cq_event_setup(
13399 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13400 if (!cq_event)
13401 return false;
James Smart5e5b5112019-01-28 11:14:22 -080013402 cq_event->hdwq = cq->hdwq;
James Smart4f774512009-05-22 14:52:35 -040013403 spin_lock_irqsave(&phba->hbalock, iflags);
13404 list_add_tail(&cq_event->list,
13405 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13406 /* Set the els xri abort event flag */
13407 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13408 spin_unlock_irqrestore(&phba->hbalock, iflags);
13409 workposted = true;
13410 break;
James Smart318083a2017-03-04 09:30:30 -080013411 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080013412 /* Notify aborted XRI for NVME work queue */
13413 if (phba->nvmet_support)
13414 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13415 else
James Smart5e5b5112019-01-28 11:14:22 -080013416 lpfc_sli4_nvme_xri_aborted(phba, wcqe, cq->hdwq);
James Smart8a5ca102017-11-20 16:00:30 -080013417
13418 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080013419 break;
James Smart4f774512009-05-22 14:52:35 -040013420 default:
13421 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013422 "0603 Invalid CQ subtype %d: "
13423 "%08x %08x %08x %08x\n",
13424 cq->subtype, wcqe->word0, wcqe->parameter,
13425 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013426 workposted = false;
13427 break;
13428 }
13429 return workposted;
13430}
13431
James Smarte817e5d2018-12-13 15:17:53 -080013432#define FC_RCTL_MDS_DIAGS 0xF4
13433
James Smart4f774512009-05-22 14:52:35 -040013434/**
James Smart4d9ab992009-10-02 15:16:39 -040013435 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013436 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013437 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013438 *
James Smart4d9ab992009-10-02 15:16:39 -040013439 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013440 *
13441 * Return: true if work posted to worker thread, otherwise false.
13442 **/
13443static bool
James Smart4d9ab992009-10-02 15:16:39 -040013444lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13445{
13446 bool workposted = false;
James Smarte817e5d2018-12-13 15:17:53 -080013447 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013448 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13449 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013450 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013451 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013452 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013453 unsigned long iflags;
13454
James Smart2e90f4b2011-12-13 13:22:37 -050013455 /* sanity check on queue memory */
13456 if (unlikely(!hrq) || unlikely(!drq))
13457 return workposted;
13458
James Smart7851fe22011-07-22 18:36:52 -040013459 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13460 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13461 else
13462 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13463 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013464 goto out;
13465
13466 status = bf_get(lpfc_rcqe_status, rcqe);
13467 switch (status) {
13468 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13469 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13470 "2537 Receive Frame Truncated!!\n");
13471 case FC_STATUS_RQ_SUCCESS:
13472 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013473 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013474 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13475 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013476 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013477 spin_unlock_irqrestore(&phba->hbalock, iflags);
13478 goto out;
13479 }
James Smartb84daac2012-08-03 12:35:13 -040013480 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013481 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013482 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013483
James Smarte817e5d2018-12-13 15:17:53 -080013484 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13485
13486 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
13487 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
13488 spin_unlock_irqrestore(&phba->hbalock, iflags);
13489 /* Handle MDS Loopback frames */
13490 lpfc_sli4_handle_mds_loopback(phba->pport, dma_buf);
13491 break;
13492 }
13493
13494 /* save off the frame for the work thread to process */
James Smart4d9ab992009-10-02 15:16:39 -040013495 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013496 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013497 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013498 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013499 spin_unlock_irqrestore(&phba->hbalock, iflags);
13500 workposted = true;
13501 break;
James Smart4d9ab992009-10-02 15:16:39 -040013502 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013503 if (phba->nvmet_support) {
13504 tgtp = phba->targetport->private;
13505 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13506 "6402 RQE Error x%x, posted %d err_cnt "
13507 "%d: %x %x %x\n",
13508 status, hrq->RQ_buf_posted,
13509 hrq->RQ_no_posted_buf,
13510 atomic_read(&tgtp->rcv_fcp_cmd_in),
13511 atomic_read(&tgtp->rcv_fcp_cmd_out),
13512 atomic_read(&tgtp->xmt_fcp_release));
13513 }
13514 /* fallthrough */
13515
13516 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013517 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013518 /* Post more buffers if possible */
13519 spin_lock_irqsave(&phba->hbalock, iflags);
13520 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13521 spin_unlock_irqrestore(&phba->hbalock, iflags);
13522 workposted = true;
13523 break;
13524 }
13525out:
13526 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013527}
13528
13529/**
13530 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13531 * @phba: Pointer to HBA context object.
13532 * @cq: Pointer to the completion queue.
13533 * @wcqe: Pointer to a completion queue entry.
13534 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013535 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013536 * entry.
13537 *
13538 * Return: true if work posted to worker thread, otherwise false.
13539 **/
13540static bool
13541lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013542 struct lpfc_cqe *cqe)
13543{
James Smart45ed1192009-10-02 15:17:02 -040013544 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013545 bool workposted = false;
13546
13547 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013548 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013549
13550 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013551 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013552 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013553 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013554 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013555 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013556 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013557 break;
13558 case CQE_CODE_RELEASE_WQE:
13559 /* Process the WQ release event */
13560 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013561 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013562 break;
13563 case CQE_CODE_XRI_ABORTED:
13564 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013565 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013566 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013567 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013568 break;
James Smart4d9ab992009-10-02 15:16:39 -040013569 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013570 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013571 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013572 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013573 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013574 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013575 break;
James Smart4f774512009-05-22 14:52:35 -040013576 default:
13577 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13578 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013579 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013580 break;
13581 }
13582 return workposted;
13583}
13584
13585/**
James Smart4f774512009-05-22 14:52:35 -040013586 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13587 * @phba: Pointer to HBA context object.
13588 * @eqe: Pointer to fast-path event queue entry.
13589 *
13590 * This routine process a event queue entry from the slow-path event queue.
13591 * It will check the MajorCode and MinorCode to determine this is for a
13592 * completion event on a completion queue, if not, an error shall be logged
13593 * and just return. Otherwise, it will get to the corresponding completion
13594 * queue and process all the entries on that completion queue, rearm the
13595 * completion queue, and then return.
13596 *
13597 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013598static void
James Smart67d12732012-08-03 12:36:13 -040013599lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13600 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013601{
James Smart67d12732012-08-03 12:36:13 -040013602 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013603 uint16_t cqid;
13604
James Smart4f774512009-05-22 14:52:35 -040013605 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013606 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013607
James Smart4f774512009-05-22 14:52:35 -040013608 list_for_each_entry(childq, &speq->child_list, list) {
13609 if (childq->queue_id == cqid) {
13610 cq = childq;
13611 break;
13612 }
13613 }
13614 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013615 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13616 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13617 "0365 Slow-path CQ identifier "
13618 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013619 return;
James Smart4f774512009-05-22 14:52:35 -040013620 }
13621
James Smart895427b2017-02-12 13:52:30 -080013622 /* Save EQ associated with this CQ */
13623 cq->assoc_qp = speq;
13624
Dick Kennedyf485c182017-09-29 17:34:34 -070013625 if (!queue_work(phba->wq, &cq->spwork))
13626 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13627 "0390 Cannot schedule soft IRQ "
13628 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13629 cqid, cq->queue_id, smp_processor_id());
13630}
13631
13632/**
13633 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13634 * @phba: Pointer to HBA context object.
13635 *
13636 * This routine process a event queue entry from the slow-path event queue.
13637 * It will check the MajorCode and MinorCode to determine this is for a
13638 * completion event on a completion queue, if not, an error shall be logged
13639 * and just return. Otherwise, it will get to the corresponding completion
13640 * queue and process all the entries on that completion queue, rearm the
13641 * completion queue, and then return.
13642 *
13643 **/
13644static void
13645lpfc_sli4_sp_process_cq(struct work_struct *work)
13646{
13647 struct lpfc_queue *cq =
13648 container_of(work, struct lpfc_queue, spwork);
13649 struct lpfc_hba *phba = cq->phba;
13650 struct lpfc_cqe *cqe;
13651 bool workposted = false;
13652 int ccount = 0;
13653
James Smart4f774512009-05-22 14:52:35 -040013654 /* Process all the entries to the CQ */
13655 switch (cq->type) {
13656 case LPFC_MCQ:
13657 while ((cqe = lpfc_sli4_cq_get(cq))) {
13658 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013659 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013660 break;
James Smartb84daac2012-08-03 12:35:13 -040013661 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013662 }
13663 break;
13664 case LPFC_WCQ:
13665 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013666 if (cq->subtype == LPFC_FCP ||
13667 cq->subtype == LPFC_NVME) {
13668#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13669 if (phba->ktime_on)
13670 cq->isr_timestamp = ktime_get_ns();
13671 else
13672 cq->isr_timestamp = 0;
13673#endif
James Smart895427b2017-02-12 13:52:30 -080013674 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013675 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013676 } else {
James Smart05580562011-05-24 11:40:48 -040013677 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13678 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013679 }
Dick Kennedyf485c182017-09-29 17:34:34 -070013680 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013681 break;
James Smart4f774512009-05-22 14:52:35 -040013682 }
James Smartb84daac2012-08-03 12:35:13 -040013683
13684 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013685 if (ccount > cq->CQ_max_cqe)
13686 cq->CQ_max_cqe = ccount;
James Smart4f774512009-05-22 14:52:35 -040013687 break;
13688 default:
13689 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13690 "0370 Invalid completion queue type (%d)\n",
13691 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013692 return;
James Smart4f774512009-05-22 14:52:35 -040013693 }
13694
13695 /* Catch the no cq entry condition, log an error */
Dick Kennedyf485c182017-09-29 17:34:34 -070013696 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013697 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13698 "0371 No entry from the CQ: identifier "
13699 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13700
13701 /* In any case, flash and re-arm the RCQ */
James Smartb71413d2018-02-22 08:18:40 -080013702 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013703
13704 /* wake up worker thread if there are works to be done */
13705 if (workposted)
13706 lpfc_worker_wake_up(phba);
13707}
13708
13709/**
13710 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013711 * @phba: Pointer to HBA context object.
13712 * @cq: Pointer to associated CQ
13713 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013714 *
13715 * This routine process a fast-path work queue completion entry from fast-path
13716 * event queue for FCP command response completion.
13717 **/
13718static void
James Smart2a76a282012-08-03 12:35:54 -040013719lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013720 struct lpfc_wcqe_complete *wcqe)
13721{
James Smart2a76a282012-08-03 12:35:54 -040013722 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013723 struct lpfc_iocbq *cmdiocbq;
13724 struct lpfc_iocbq irspiocbq;
13725 unsigned long iflags;
13726
James Smart4f774512009-05-22 14:52:35 -040013727 /* Check for response status */
13728 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13729 /* If resource errors reported from HBA, reduce queue
13730 * depth of the SCSI device.
13731 */
James Smarte3d2b802012-08-14 14:25:43 -040013732 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13733 IOSTAT_LOCAL_REJECT)) &&
13734 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13735 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013736 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013737
James Smart4f774512009-05-22 14:52:35 -040013738 /* Log the error status */
James Smart11f0e342018-05-04 20:37:57 -070013739 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13740 "0373 FCP CQE error: status=x%x: "
13741 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013742 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070013743 wcqe->word0, wcqe->total_data_placed,
13744 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013745 }
13746
13747 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013748 spin_lock_irqsave(&pring->ring_lock, iflags);
13749 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013750 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13751 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013752 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013753 if (unlikely(!cmdiocbq)) {
13754 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13755 "0374 FCP complete with no corresponding "
13756 "cmdiocb: iotag (%d)\n",
13757 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13758 return;
13759 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013760#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13761 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13762#endif
James Smart895427b2017-02-12 13:52:30 -080013763 if (cmdiocbq->iocb_cmpl == NULL) {
13764 if (cmdiocbq->wqe_cmpl) {
13765 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13766 spin_lock_irqsave(&phba->hbalock, iflags);
13767 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13768 spin_unlock_irqrestore(&phba->hbalock, iflags);
13769 }
13770
13771 /* Pass the cmd_iocb and the wcqe to the upper layer */
13772 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13773 return;
13774 }
James Smart4f774512009-05-22 14:52:35 -040013775 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13776 "0375 FCP cmdiocb not callback function "
13777 "iotag: (%d)\n",
13778 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13779 return;
13780 }
13781
13782 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013783 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013784
James Smart0f65ff62010-02-26 14:14:23 -050013785 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13786 spin_lock_irqsave(&phba->hbalock, iflags);
13787 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13788 spin_unlock_irqrestore(&phba->hbalock, iflags);
13789 }
13790
James Smart4f774512009-05-22 14:52:35 -040013791 /* Pass the cmd_iocb and the rsp state to the upper layer */
13792 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13793}
13794
13795/**
13796 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13797 * @phba: Pointer to HBA context object.
13798 * @cq: Pointer to completion queue.
13799 * @wcqe: Pointer to work-queue completion queue entry.
13800 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013801 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013802 * proper WQ release routine to the slow-path WQ.
13803 **/
13804static void
13805lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13806 struct lpfc_wcqe_release *wcqe)
13807{
13808 struct lpfc_queue *childwq;
13809 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013810 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013811
13812 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013813 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013814 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013815 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013816 lpfc_sli4_wq_release(childwq,
13817 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013818 if (childwq->q_flag & HBA_NVMET_WQFULL)
13819 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013820 wqid_matched = true;
13821 break;
13822 }
13823 }
13824 /* Report warning log message if no match found */
13825 if (wqid_matched != true)
13826 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13827 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013828 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013829}
13830
13831/**
James Smart2d7dbc42017-02-12 13:52:35 -080013832 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13833 * @phba: Pointer to HBA context object.
13834 * @rcqe: Pointer to receive-queue completion queue entry.
13835 *
13836 * This routine process a receive-queue completion queue entry.
13837 *
13838 * Return: true if work posted to worker thread, otherwise false.
13839 **/
13840static bool
13841lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13842 struct lpfc_rcqe *rcqe)
13843{
13844 bool workposted = false;
13845 struct lpfc_queue *hrq;
13846 struct lpfc_queue *drq;
13847 struct rqb_dmabuf *dma_buf;
13848 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013849 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013850 uint32_t status, rq_id;
13851 unsigned long iflags;
13852 uint32_t fctl, idx;
13853
13854 if ((phba->nvmet_support == 0) ||
13855 (phba->sli4_hba.nvmet_cqset == NULL))
13856 return workposted;
13857
13858 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13859 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13860 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13861
13862 /* sanity check on queue memory */
13863 if (unlikely(!hrq) || unlikely(!drq))
13864 return workposted;
13865
13866 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13867 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13868 else
13869 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13870
13871 if ((phba->nvmet_support == 0) ||
13872 (rq_id != hrq->queue_id))
13873 return workposted;
13874
13875 status = bf_get(lpfc_rcqe_status, rcqe);
13876 switch (status) {
13877 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13878 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13879 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013880 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013881 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013882 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013883 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013884 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13885 if (!dma_buf) {
13886 hrq->RQ_no_buf_found++;
13887 spin_unlock_irqrestore(&phba->hbalock, iflags);
13888 goto out;
13889 }
13890 spin_unlock_irqrestore(&phba->hbalock, iflags);
13891 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013892 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013893 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13894
13895 /* Just some basic sanity checks on FCP Command frame */
13896 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13897 fc_hdr->fh_f_ctl[1] << 8 |
13898 fc_hdr->fh_f_ctl[2]);
13899 if (((fctl &
13900 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13901 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13902 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13903 goto drop;
13904
13905 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13906 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013907 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070013908 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013909 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013910 return false;
13911 }
13912drop:
13913 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13914 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013915 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013916 if (phba->nvmet_support) {
13917 tgtp = phba->targetport->private;
13918 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13919 "6401 RQE Error x%x, posted %d err_cnt "
13920 "%d: %x %x %x\n",
13921 status, hrq->RQ_buf_posted,
13922 hrq->RQ_no_posted_buf,
13923 atomic_read(&tgtp->rcv_fcp_cmd_in),
13924 atomic_read(&tgtp->rcv_fcp_cmd_out),
13925 atomic_read(&tgtp->xmt_fcp_release));
13926 }
13927 /* fallthrough */
13928
13929 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013930 hrq->RQ_no_posted_buf++;
13931 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013932 break;
13933 }
13934out:
13935 return workposted;
13936}
13937
13938/**
James Smart895427b2017-02-12 13:52:30 -080013939 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013940 * @cq: Pointer to the completion queue.
13941 * @eqe: Pointer to fast-path completion queue entry.
13942 *
13943 * This routine process a fast-path work queue completion entry from fast-path
13944 * event queue for FCP command response completion.
13945 **/
13946static int
James Smart895427b2017-02-12 13:52:30 -080013947lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013948 struct lpfc_cqe *cqe)
13949{
13950 struct lpfc_wcqe_release wcqe;
13951 bool workposted = false;
13952
13953 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013954 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013955
13956 /* Check and process for different type of WCQE and dispatch */
13957 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13958 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013959 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013960 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013961 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013962 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013963 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13964 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13965 (struct lpfc_wcqe_complete *)&wcqe);
13966 if (cq->subtype == LPFC_NVME_LS)
13967 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013968 (struct lpfc_wcqe_complete *)&wcqe);
13969 break;
13970 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013971 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013972 /* Process the WQ release event */
13973 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13974 (struct lpfc_wcqe_release *)&wcqe);
13975 break;
13976 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013977 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013978 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013979 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013980 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13981 (struct sli4_wcqe_xri_aborted *)&wcqe);
13982 break;
James Smart895427b2017-02-12 13:52:30 -080013983 case CQE_CODE_RECEIVE_V1:
13984 case CQE_CODE_RECEIVE:
13985 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013986 if (cq->subtype == LPFC_NVMET) {
13987 workposted = lpfc_sli4_nvmet_handle_rcqe(
13988 phba, cq, (struct lpfc_rcqe *)&wcqe);
13989 }
James Smart895427b2017-02-12 13:52:30 -080013990 break;
James Smart4f774512009-05-22 14:52:35 -040013991 default:
13992 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013993 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013994 bf_get(lpfc_wcqe_c_code, &wcqe));
13995 break;
13996 }
13997 return workposted;
13998}
13999
14000/**
James Smart67d12732012-08-03 12:36:13 -040014001 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040014002 * @phba: Pointer to HBA context object.
14003 * @eqe: Pointer to fast-path event queue entry.
14004 *
14005 * This routine process a event queue entry from the fast-path event queue.
14006 * It will check the MajorCode and MinorCode to determine this is for a
14007 * completion event on a completion queue, if not, an error shall be logged
14008 * and just return. Otherwise, it will get to the corresponding completion
14009 * queue and process all the entries on the completion queue, rearm the
14010 * completion queue, and then return.
14011 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070014012static void
James Smart67d12732012-08-03 12:36:13 -040014013lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14014 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040014015{
James Smart895427b2017-02-12 13:52:30 -080014016 struct lpfc_queue *cq = NULL;
James Smart2d7dbc42017-02-12 13:52:35 -080014017 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040014018
James Smartcb5172e2010-03-15 11:25:07 -040014019 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040014020 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040014021 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040014022 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040014023 bf_get_le32(lpfc_eqe_major_code, eqe),
14024 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070014025 return;
James Smart4f774512009-05-22 14:52:35 -040014026 }
14027
James Smart67d12732012-08-03 12:36:13 -040014028 /* Get the reference to the corresponding CQ */
14029 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14030
James Smartcdb42be2019-01-28 11:14:21 -080014031 /* First check for NVME/SCSI completion */
James Smart5e5b5112019-01-28 11:14:22 -080014032 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
14033 (cqid == phba->sli4_hba.hdwq[qidx].nvme_cq_map)) {
James Smartcdb42be2019-01-28 11:14:21 -080014034 /* Process NVME / NVMET command completion */
14035 cq = phba->sli4_hba.hdwq[qidx].nvme_cq;
14036 goto process_cq;
14037 }
14038
14039 if (cqid == phba->sli4_hba.hdwq[qidx].fcp_cq_map) {
14040 /* Process FCP command completion */
14041 cq = phba->sli4_hba.hdwq[qidx].fcp_cq;
14042 goto process_cq;
14043 }
14044
14045 /* Next check for NVMET completion */
James Smart2d7dbc42017-02-12 13:52:35 -080014046 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14047 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14048 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14049 /* Process NVMET unsol rcv */
14050 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14051 goto process_cq;
14052 }
14053 }
14054
James Smart895427b2017-02-12 13:52:30 -080014055 if (phba->sli4_hba.nvmels_cq &&
14056 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14057 /* Process NVME unsol rcv */
14058 cq = phba->sli4_hba.nvmels_cq;
14059 }
14060
14061 /* Otherwise this is a Slow path event */
14062 if (cq == NULL) {
James Smartcdb42be2019-01-28 11:14:21 -080014063 lpfc_sli4_sp_handle_eqe(phba, eqe,
14064 phba->sli4_hba.hdwq[qidx].hba_eq);
Dick Kennedyf485c182017-09-29 17:34:34 -070014065 return;
James Smart67d12732012-08-03 12:36:13 -040014066 }
14067
James Smart895427b2017-02-12 13:52:30 -080014068process_cq:
James Smart4f774512009-05-22 14:52:35 -040014069 if (unlikely(cqid != cq->queue_id)) {
14070 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14071 "0368 Miss-matched fast-path completion "
14072 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14073 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070014074 return;
James Smart4f774512009-05-22 14:52:35 -040014075 }
14076
James Smart895427b2017-02-12 13:52:30 -080014077 /* Save EQ associated with this CQ */
James Smartcdb42be2019-01-28 11:14:21 -080014078 cq->assoc_qp = phba->sli4_hba.hdwq[qidx].hba_eq;
James Smart895427b2017-02-12 13:52:30 -080014079
Dick Kennedyf485c182017-09-29 17:34:34 -070014080 if (!queue_work(phba->wq, &cq->irqwork))
14081 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14082 "0363 Cannot schedule soft IRQ "
14083 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14084 cqid, cq->queue_id, smp_processor_id());
14085}
14086
14087/**
14088 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14089 * @phba: Pointer to HBA context object.
14090 * @eqe: Pointer to fast-path event queue entry.
14091 *
14092 * This routine process a event queue entry from the fast-path event queue.
14093 * It will check the MajorCode and MinorCode to determine this is for a
14094 * completion event on a completion queue, if not, an error shall be logged
14095 * and just return. Otherwise, it will get to the corresponding completion
14096 * queue and process all the entries on the completion queue, rearm the
14097 * completion queue, and then return.
14098 **/
14099static void
14100lpfc_sli4_hba_process_cq(struct work_struct *work)
14101{
14102 struct lpfc_queue *cq =
14103 container_of(work, struct lpfc_queue, irqwork);
14104 struct lpfc_hba *phba = cq->phba;
14105 struct lpfc_cqe *cqe;
14106 bool workposted = false;
14107 int ccount = 0;
14108
James Smart4f774512009-05-22 14:52:35 -040014109 /* Process all the entries to the CQ */
14110 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070014111#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
14112 if (phba->ktime_on)
14113 cq->isr_timestamp = ktime_get_ns();
14114 else
14115 cq->isr_timestamp = 0;
14116#endif
James Smart895427b2017-02-12 13:52:30 -080014117 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070014118 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070014119 break;
James Smart4f774512009-05-22 14:52:35 -040014120 }
14121
James Smartb84daac2012-08-03 12:35:13 -040014122 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070014123 if (ccount > cq->CQ_max_cqe)
14124 cq->CQ_max_cqe = ccount;
14125 cq->assoc_qp->EQ_cqe_cnt += ccount;
James Smartb84daac2012-08-03 12:35:13 -040014126
James Smart4f774512009-05-22 14:52:35 -040014127 /* Catch the no cq entry condition */
Dick Kennedyf485c182017-09-29 17:34:34 -070014128 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040014129 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14130 "0369 No entry from fast-path completion "
14131 "queue fcpcqid=%d\n", cq->queue_id);
14132
14133 /* In any case, flash and re-arm the CQ */
James Smartb71413d2018-02-22 08:18:40 -080014134 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014135
14136 /* wake up worker thread if there are works to be done */
14137 if (workposted)
14138 lpfc_worker_wake_up(phba);
14139}
14140
14141static void
14142lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
14143{
14144 struct lpfc_eqe *eqe;
14145
14146 /* walk all the EQ entries and drop on the floor */
14147 while ((eqe = lpfc_sli4_eq_get(eq)))
14148 ;
14149
14150 /* Clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -080014151 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014152}
14153
James Smart1ba981f2014-02-20 09:56:45 -050014154
14155/**
James Smart67d12732012-08-03 12:36:13 -040014156 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040014157 * @irq: Interrupt number.
14158 * @dev_id: The device context pointer.
14159 *
14160 * This function is directly called from the PCI layer as an interrupt
14161 * service routine when device with SLI-4 interface spec is enabled with
14162 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14163 * ring event in the HBA. However, when the device is enabled with either
14164 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14165 * device-level interrupt handler. When the PCI slot is in error recovery
14166 * or the HBA is undergoing initialization, the interrupt handler will not
14167 * process the interrupt. The SCSI FCP fast-path ring event are handled in
14168 * the intrrupt context. This function is called without any lock held.
14169 * It gets the hbalock to access and update SLI data structures. Note that,
14170 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14171 * equal to that of FCP CQ index.
14172 *
James Smart67d12732012-08-03 12:36:13 -040014173 * The link attention and ELS ring attention events are handled
14174 * by the worker thread. The interrupt handler signals the worker thread
14175 * and returns for these events. This function is called without any lock
14176 * held. It gets the hbalock to access and update SLI data structures.
14177 *
James Smart4f774512009-05-22 14:52:35 -040014178 * This function returns IRQ_HANDLED when interrupt is handled else it
14179 * returns IRQ_NONE.
14180 **/
14181irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040014182lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040014183{
14184 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080014185 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040014186 struct lpfc_queue *fpeq;
14187 struct lpfc_eqe *eqe;
14188 unsigned long iflag;
14189 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080014190 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040014191
14192 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014193 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14194 phba = hba_eq_hdl->phba;
14195 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014196
14197 if (unlikely(!phba))
14198 return IRQ_NONE;
James Smartcdb42be2019-01-28 11:14:21 -080014199 if (unlikely(!phba->sli4_hba.hdwq))
James Smart5350d872011-10-10 21:33:49 -040014200 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014201
14202 /* Get to the EQ struct associated with this vector */
James Smartcdb42be2019-01-28 11:14:21 -080014203 fpeq = phba->sli4_hba.hdwq[hba_eqidx].hba_eq;
James Smart2e90f4b2011-12-13 13:22:37 -050014204 if (unlikely(!fpeq))
14205 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014206
James Smartba20c852012-08-03 12:36:52 -040014207 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080014208 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartb71413d2018-02-22 08:18:40 -080014209 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -040014210 else {
James Smart895427b2017-02-12 13:52:30 -080014211 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040014212 return IRQ_NONE;
14213 }
14214 }
14215
James Smart4f774512009-05-22 14:52:35 -040014216 /* Check device state for handling interrupt */
14217 if (unlikely(lpfc_intr_state_check(phba))) {
14218 /* Check again for link_state with lock held */
14219 spin_lock_irqsave(&phba->hbalock, iflag);
14220 if (phba->link_state < LPFC_LINK_DOWN)
14221 /* Flush, clear interrupt, and rearm the EQ */
14222 lpfc_sli4_eq_flush(phba, fpeq);
14223 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040014224 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014225 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040014226 return IRQ_NONE;
14227 }
14228
14229 /*
14230 * Process all the event on FCP fast-path EQ
14231 */
14232 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
Dick Kennedyf485c182017-09-29 17:34:34 -070014233 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14234 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070014235 break;
James Smartb84daac2012-08-03 12:35:13 -040014236 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040014237 }
14238
James Smartb84daac2012-08-03 12:35:13 -040014239 /* Track the max number of EQEs processed in 1 intr */
14240 if (ecount > fpeq->EQ_max_eqe)
14241 fpeq->EQ_max_eqe = ecount;
14242
James Smart4f774512009-05-22 14:52:35 -040014243 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080014244 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014245
14246 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014247 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040014248
14249 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080014250 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040014251 return IRQ_NONE;
14252 }
14253
James Smart4f774512009-05-22 14:52:35 -040014254 if (phba->intr_type == MSIX)
14255 /* MSI-X treated interrupt served as no EQ share INT */
14256 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14257 "0358 MSI-X interrupt with no EQE\n");
14258 else
14259 /* Non MSI-X treated on interrupt as EQ share INT */
14260 return IRQ_NONE;
14261 }
14262
James Smartba20c852012-08-03 12:36:52 -040014263 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014264 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14265
James Smart4f774512009-05-22 14:52:35 -040014266 return IRQ_HANDLED;
14267} /* lpfc_sli4_fp_intr_handler */
14268
14269/**
14270 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14271 * @irq: Interrupt number.
14272 * @dev_id: The device context pointer.
14273 *
14274 * This function is the device-level interrupt handler to device with SLI-4
14275 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14276 * interrupt mode is enabled and there is an event in the HBA which requires
14277 * driver attention. This function invokes the slow-path interrupt attention
14278 * handling function and fast-path interrupt attention handling function in
14279 * turn to process the relevant HBA attention events. This function is called
14280 * without any lock held. It gets the hbalock to access and update SLI data
14281 * structures.
14282 *
14283 * This function returns IRQ_HANDLED when interrupt is handled, else it
14284 * returns IRQ_NONE.
14285 **/
14286irqreturn_t
14287lpfc_sli4_intr_handler(int irq, void *dev_id)
14288{
14289 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014290 irqreturn_t hba_irq_rc;
14291 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014292 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014293
14294 /* Get the driver's phba structure from the dev_id */
14295 phba = (struct lpfc_hba *)dev_id;
14296
14297 if (unlikely(!phba))
14298 return IRQ_NONE;
14299
14300 /*
James Smart4f774512009-05-22 14:52:35 -040014301 * Invoke fast-path host attention interrupt handling as appropriate.
14302 */
James Smartcdb42be2019-01-28 11:14:21 -080014303 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014304 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014305 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014306 if (hba_irq_rc == IRQ_HANDLED)
14307 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014308 }
14309
James Smart67d12732012-08-03 12:36:13 -040014310 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014311} /* lpfc_sli4_intr_handler */
14312
14313/**
14314 * lpfc_sli4_queue_free - free a queue structure and associated memory
14315 * @queue: The queue structure to free.
14316 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014317 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014318 * the host resident queue. This function must be called after destroying the
14319 * queue on the HBA.
14320 **/
14321void
14322lpfc_sli4_queue_free(struct lpfc_queue *queue)
14323{
14324 struct lpfc_dmabuf *dmabuf;
14325
14326 if (!queue)
14327 return;
14328
14329 while (!list_empty(&queue->page_list)) {
14330 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14331 list);
James Smart81b96ed2017-11-20 16:00:29 -080014332 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014333 dmabuf->virt, dmabuf->phys);
14334 kfree(dmabuf);
14335 }
James Smart895427b2017-02-12 13:52:30 -080014336 if (queue->rqbp) {
14337 lpfc_free_rq_buffer(queue->phba, queue);
14338 kfree(queue->rqbp);
14339 }
James Smartd1f525a2017-04-21 16:04:55 -070014340
14341 if (!list_empty(&queue->wq_list))
14342 list_del(&queue->wq_list);
14343
James Smart4f774512009-05-22 14:52:35 -040014344 kfree(queue);
14345 return;
14346}
14347
14348/**
14349 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14350 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014351 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014352 * @entry_size: The size of each queue entry for this queue.
14353 * @entry count: The number of entries that this queue will handle.
14354 *
14355 * This function allocates a queue structure and the DMAable memory used for
14356 * the host resident queue. This function must be called before creating the
14357 * queue on the HBA.
14358 **/
14359struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014360lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14361 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040014362{
14363 struct lpfc_queue *queue;
14364 struct lpfc_dmabuf *dmabuf;
14365 int x, total_qe_count;
14366 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040014367 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040014368
James Smartcb5172e2010-03-15 11:25:07 -040014369 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014370 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014371
James Smart4f774512009-05-22 14:52:35 -040014372 queue = kzalloc(sizeof(struct lpfc_queue) +
14373 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14374 if (!queue)
14375 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040014376 queue->page_count = (ALIGN(entry_size * entry_count,
14377 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014378
14379 /* If needed, Adjust page count to match the max the adapter supports */
Ewan D. Milne4e87eb22018-12-13 15:25:16 -050014380 if (phba->sli4_hba.pc_sli4_params.wqpcnt &&
14381 (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt))
James Smart895427b2017-02-12 13:52:30 -080014382 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14383
James Smart4f774512009-05-22 14:52:35 -040014384 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014385 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014386 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014387 INIT_LIST_HEAD(&queue->page_list);
14388 INIT_LIST_HEAD(&queue->child_list);
James Smart81b96ed2017-11-20 16:00:29 -080014389
14390 /* Set queue parameters now. If the system cannot provide memory
14391 * resources, the free routine needs to know what was allocated.
14392 */
14393 queue->entry_size = entry_size;
14394 queue->entry_count = entry_count;
14395 queue->page_size = hw_page_size;
14396 queue->phba = phba;
14397
James Smart4f774512009-05-22 14:52:35 -040014398 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14399 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14400 if (!dmabuf)
14401 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040014402 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14403 hw_page_size, &dmabuf->phys,
14404 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014405 if (!dmabuf->virt) {
14406 kfree(dmabuf);
14407 goto out_fail;
14408 }
14409 dmabuf->buffer_tag = x;
14410 list_add_tail(&dmabuf->list, &queue->page_list);
14411 /* initialize queue's entry array */
14412 dma_pointer = dmabuf->virt;
14413 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040014414 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040014415 total_qe_count++, dma_pointer += entry_size) {
14416 queue->qe[total_qe_count].address = dma_pointer;
14417 }
14418 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014419 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14420 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014421
James Smart64eb4dc2017-05-15 15:20:49 -070014422 /* entry_repost will be set during q creation */
14423
James Smart4f774512009-05-22 14:52:35 -040014424 return queue;
14425out_fail:
14426 lpfc_sli4_queue_free(queue);
14427 return NULL;
14428}
14429
14430/**
James Smart962bc512013-01-03 15:44:00 -050014431 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14432 * @phba: HBA structure that indicates port to create a queue on.
14433 * @pci_barset: PCI BAR set flag.
14434 *
14435 * This function shall perform iomap of the specified PCI BAR address to host
14436 * memory address if not already done so and return it. The returned host
14437 * memory address can be NULL.
14438 */
14439static void __iomem *
14440lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14441{
James Smart962bc512013-01-03 15:44:00 -050014442 if (!phba->pcidev)
14443 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014444
14445 switch (pci_barset) {
14446 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014447 return phba->pci_bar0_memmap_p;
14448 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014449 return phba->pci_bar2_memmap_p;
14450 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014451 return phba->pci_bar4_memmap_p;
14452 default:
14453 break;
14454 }
14455 return NULL;
14456}
14457
14458/**
James Smart895427b2017-02-12 13:52:30 -080014459 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040014460 * @phba: HBA structure that indicates port to create a queue on.
14461 * @startq: The starting FCP EQ to modify
14462 *
14463 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080014464 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14465 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040014466 *
14467 * The @phba struct is used to send mailbox command to HBA. The @startq
14468 * is used to get the starting FCP EQ to change.
14469 * This function is asynchronous and will wait for the mailbox
14470 * command to finish before continuing.
14471 *
14472 * On success this function will return a zero. If unable to allocate enough
14473 * memory this function will return -ENOMEM. If the queue create mailbox command
14474 * fails this function will return -ENXIO.
14475 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014476int
James Smart0cf07f842017-06-01 21:07:10 -070014477lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14478 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014479{
14480 struct lpfc_mbx_modify_eq_delay *eq_delay;
14481 LPFC_MBOXQ_t *mbox;
14482 struct lpfc_queue *eq;
14483 int cnt, rc, length, status = 0;
14484 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014485 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014486 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014487 union lpfc_sli4_cfg_shdr *shdr;
14488 uint16_t dmult;
14489
James Smartcdb42be2019-01-28 11:14:21 -080014490 if (startq >= phba->cfg_hdw_queue)
James Smart173edbb2012-06-12 13:54:50 -040014491 return 0;
14492
14493 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14494 if (!mbox)
14495 return -ENOMEM;
14496 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14497 sizeof(struct lpfc_sli4_cfg_mhdr));
14498 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14499 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14500 length, LPFC_SLI4_MBX_EMBED);
14501 eq_delay = &mbox->u.mqe.un.eq_delay;
14502
14503 /* Calculate delay multiper from maximum interrupt per second */
James Smartcdb42be2019-01-28 11:14:21 -080014504 result = imax / phba->cfg_hdw_queue;
James Smart895427b2017-02-12 13:52:30 -080014505 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014506 dmult = 0;
14507 else
14508 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014509 if (dmult > LPFC_DMULT_MAX)
14510 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014511
14512 cnt = 0;
James Smartcdb42be2019-01-28 11:14:21 -080014513 for (qidx = startq; qidx < phba->cfg_hdw_queue; qidx++) {
14514 eq = phba->sli4_hba.hdwq[qidx].hba_eq;
James Smart173edbb2012-06-12 13:54:50 -040014515 if (!eq)
14516 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014517 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014518 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14519 eq_delay->u.request.eq[cnt].phase = 0;
14520 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14521 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014522
14523 /* q_mode is only used for auto_imax */
14524 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14525 /* Use EQ Delay Register method for q_mode */
14526
14527 /* Convert for EQ Delay register */
14528 val = phba->cfg_fcp_imax;
14529 if (val) {
14530 /* First, interrupts per sec per EQ */
James Smartcdb42be2019-01-28 11:14:21 -080014531 val = phba->cfg_fcp_imax / phba->cfg_hdw_queue;
James Smart0cf07f842017-06-01 21:07:10 -070014532
14533 /* us delay between each interrupt */
14534 val = LPFC_SEC_TO_USEC / val;
14535 }
14536 eq->q_mode = val;
14537 } else {
14538 eq->q_mode = imax;
14539 }
14540
14541 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014542 break;
14543 }
14544 eq_delay->u.request.num_eq = cnt;
14545
14546 mbox->vport = phba->pport;
14547 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014548 mbox->ctx_buf = NULL;
14549 mbox->ctx_ndlp = NULL;
James Smart173edbb2012-06-12 13:54:50 -040014550 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14551 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14552 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14553 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14554 if (shdr_status || shdr_add_status || rc) {
14555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14556 "2512 MODIFY_EQ_DELAY mailbox failed with "
14557 "status x%x add_status x%x, mbx status x%x\n",
14558 shdr_status, shdr_add_status, rc);
14559 status = -ENXIO;
14560 }
14561 mempool_free(mbox, phba->mbox_mem_pool);
14562 return status;
14563}
14564
14565/**
James Smart4f774512009-05-22 14:52:35 -040014566 * lpfc_eq_create - Create an Event Queue on the HBA
14567 * @phba: HBA structure that indicates port to create a queue on.
14568 * @eq: The queue structure to use to create the event queue.
14569 * @imax: The maximum interrupt per second limit.
14570 *
14571 * This function creates an event queue, as detailed in @eq, on a port,
14572 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14573 *
14574 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14575 * is used to get the entry count and entry size that are necessary to
14576 * determine the number of pages to allocate and use for this queue. This
14577 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14578 * event queue. This function is asynchronous and will wait for the mailbox
14579 * command to finish before continuing.
14580 *
14581 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014582 * memory this function will return -ENOMEM. If the queue create mailbox command
14583 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014584 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014585int
James Smartee020062012-09-29 11:28:52 -040014586lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014587{
14588 struct lpfc_mbx_eq_create *eq_create;
14589 LPFC_MBOXQ_t *mbox;
14590 int rc, length, status = 0;
14591 struct lpfc_dmabuf *dmabuf;
14592 uint32_t shdr_status, shdr_add_status;
14593 union lpfc_sli4_cfg_shdr *shdr;
14594 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014595 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14596
James Smart2e90f4b2011-12-13 13:22:37 -050014597 /* sanity check on queue memory */
14598 if (!eq)
14599 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014600 if (!phba->sli4_hba.pc_sli4_params.supported)
14601 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014602
14603 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14604 if (!mbox)
14605 return -ENOMEM;
14606 length = (sizeof(struct lpfc_mbx_eq_create) -
14607 sizeof(struct lpfc_sli4_cfg_mhdr));
14608 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14609 LPFC_MBOX_OPCODE_EQ_CREATE,
14610 length, LPFC_SLI4_MBX_EMBED);
14611 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014612 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014613 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14614 eq->page_count);
14615 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14616 LPFC_EQE_SIZE);
14617 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014618
14619 /* Use version 2 of CREATE_EQ if eqav is set */
14620 if (phba->sli4_hba.pc_sli4_params.eqav) {
14621 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14622 LPFC_Q_CREATE_VERSION_2);
14623 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14624 phba->sli4_hba.pc_sli4_params.eqav);
14625 }
14626
James Smart2c9c5a02015-04-07 15:07:15 -040014627 /* don't setup delay multiplier using EQ_CREATE */
14628 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014629 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14630 dmult);
14631 switch (eq->entry_count) {
14632 default:
14633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14634 "0360 Unsupported EQ count. (%d)\n",
14635 eq->entry_count);
14636 if (eq->entry_count < 256)
14637 return -EINVAL;
14638 /* otherwise default to smallest count (drop through) */
14639 case 256:
14640 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14641 LPFC_EQ_CNT_256);
14642 break;
14643 case 512:
14644 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14645 LPFC_EQ_CNT_512);
14646 break;
14647 case 1024:
14648 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14649 LPFC_EQ_CNT_1024);
14650 break;
14651 case 2048:
14652 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14653 LPFC_EQ_CNT_2048);
14654 break;
14655 case 4096:
14656 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14657 LPFC_EQ_CNT_4096);
14658 break;
14659 }
14660 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014661 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014662 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14663 putPaddrLow(dmabuf->phys);
14664 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14665 putPaddrHigh(dmabuf->phys);
14666 }
14667 mbox->vport = phba->pport;
14668 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080014669 mbox->ctx_buf = NULL;
14670 mbox->ctx_ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -040014671 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040014672 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14673 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14674 if (shdr_status || shdr_add_status || rc) {
14675 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14676 "2500 EQ_CREATE mailbox failed with "
14677 "status x%x add_status x%x, mbx status x%x\n",
14678 shdr_status, shdr_add_status, rc);
14679 status = -ENXIO;
14680 }
14681 eq->type = LPFC_EQ;
14682 eq->subtype = LPFC_NONE;
14683 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14684 if (eq->queue_id == 0xFFFF)
14685 status = -ENXIO;
14686 eq->host_index = 0;
14687 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014688 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014689
James Smart8fa38512009-07-19 10:01:03 -040014690 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014691 return status;
14692}
14693
14694/**
14695 * lpfc_cq_create - Create a Completion Queue on the HBA
14696 * @phba: HBA structure that indicates port to create a queue on.
14697 * @cq: The queue structure to use to create the completion queue.
14698 * @eq: The event queue to bind this completion queue to.
14699 *
14700 * This function creates a completion queue, as detailed in @wq, on a port,
14701 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14702 *
14703 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14704 * is used to get the entry count and entry size that are necessary to
14705 * determine the number of pages to allocate and use for this queue. The @eq
14706 * is used to indicate which event queue to bind this completion queue to. This
14707 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14708 * completion queue. This function is asynchronous and will wait for the mailbox
14709 * command to finish before continuing.
14710 *
14711 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014712 * memory this function will return -ENOMEM. If the queue create mailbox command
14713 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014714 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014715int
James Smart4f774512009-05-22 14:52:35 -040014716lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14717 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14718{
14719 struct lpfc_mbx_cq_create *cq_create;
14720 struct lpfc_dmabuf *dmabuf;
14721 LPFC_MBOXQ_t *mbox;
14722 int rc, length, status = 0;
14723 uint32_t shdr_status, shdr_add_status;
14724 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014725
James Smart2e90f4b2011-12-13 13:22:37 -050014726 /* sanity check on queue memory */
14727 if (!cq || !eq)
14728 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014729
James Smart4f774512009-05-22 14:52:35 -040014730 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14731 if (!mbox)
14732 return -ENOMEM;
14733 length = (sizeof(struct lpfc_mbx_cq_create) -
14734 sizeof(struct lpfc_sli4_cfg_mhdr));
14735 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14736 LPFC_MBOX_OPCODE_CQ_CREATE,
14737 length, LPFC_SLI4_MBX_EMBED);
14738 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014739 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014740 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14741 cq->page_count);
14742 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14743 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014744 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14745 phba->sli4_hba.pc_sli4_params.cqv);
14746 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014747 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14748 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014749 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14750 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080014751 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14752 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050014753 } else {
14754 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14755 eq->queue_id);
14756 }
James Smart4f774512009-05-22 14:52:35 -040014757 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014758 case 2048:
14759 case 4096:
14760 if (phba->sli4_hba.pc_sli4_params.cqv ==
14761 LPFC_Q_CREATE_VERSION_2) {
14762 cq_create->u.request.context.lpfc_cq_context_count =
14763 cq->entry_count;
14764 bf_set(lpfc_cq_context_count,
14765 &cq_create->u.request.context,
14766 LPFC_CQ_CNT_WORD7);
14767 break;
14768 }
14769 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014770 default:
14771 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014772 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014773 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014774 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014775 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014776 if (cq->entry_count < 256) {
14777 status = -EINVAL;
14778 goto out;
14779 }
James Smart4f774512009-05-22 14:52:35 -040014780 /* otherwise default to smallest count (drop through) */
14781 case 256:
14782 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14783 LPFC_CQ_CNT_256);
14784 break;
14785 case 512:
14786 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14787 LPFC_CQ_CNT_512);
14788 break;
14789 case 1024:
14790 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14791 LPFC_CQ_CNT_1024);
14792 break;
14793 }
14794 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014795 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014796 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14797 putPaddrLow(dmabuf->phys);
14798 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14799 putPaddrHigh(dmabuf->phys);
14800 }
14801 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14802
14803 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014804 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14805 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14806 if (shdr_status || shdr_add_status || rc) {
14807 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14808 "2501 CQ_CREATE mailbox failed with "
14809 "status x%x add_status x%x, mbx status x%x\n",
14810 shdr_status, shdr_add_status, rc);
14811 status = -ENXIO;
14812 goto out;
14813 }
14814 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14815 if (cq->queue_id == 0xFFFF) {
14816 status = -ENXIO;
14817 goto out;
14818 }
14819 /* link the cq onto the parent eq child list */
14820 list_add_tail(&cq->list, &eq->child_list);
14821 /* Set up completion queue's type and subtype */
14822 cq->type = type;
14823 cq->subtype = subtype;
14824 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014825 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014826 cq->host_index = 0;
14827 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014828 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014829
James Smart8fa38512009-07-19 10:01:03 -040014830out:
14831 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014832 return status;
14833}
14834
14835/**
James Smart2d7dbc42017-02-12 13:52:35 -080014836 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14837 * @phba: HBA structure that indicates port to create a queue on.
14838 * @cqp: The queue structure array to use to create the completion queues.
James Smartcdb42be2019-01-28 11:14:21 -080014839 * @hdwq: The hardware queue array with the EQ to bind completion queues to.
James Smart2d7dbc42017-02-12 13:52:35 -080014840 *
14841 * This function creates a set of completion queue, s to support MRQ
14842 * as detailed in @cqp, on a port,
14843 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14844 *
14845 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14846 * is used to get the entry count and entry size that are necessary to
14847 * determine the number of pages to allocate and use for this queue. The @eq
14848 * is used to indicate which event queue to bind this completion queue to. This
14849 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14850 * completion queue. This function is asynchronous and will wait for the mailbox
14851 * command to finish before continuing.
14852 *
14853 * On success this function will return a zero. If unable to allocate enough
14854 * memory this function will return -ENOMEM. If the queue create mailbox command
14855 * fails this function will return -ENXIO.
14856 **/
14857int
14858lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
James Smartcdb42be2019-01-28 11:14:21 -080014859 struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
14860 uint32_t subtype)
James Smart2d7dbc42017-02-12 13:52:35 -080014861{
14862 struct lpfc_queue *cq;
14863 struct lpfc_queue *eq;
14864 struct lpfc_mbx_cq_create_set *cq_set;
14865 struct lpfc_dmabuf *dmabuf;
14866 LPFC_MBOXQ_t *mbox;
14867 int rc, length, alloclen, status = 0;
14868 int cnt, idx, numcq, page_idx = 0;
14869 uint32_t shdr_status, shdr_add_status;
14870 union lpfc_sli4_cfg_shdr *shdr;
14871 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14872
14873 /* sanity check on queue memory */
14874 numcq = phba->cfg_nvmet_mrq;
James Smartcdb42be2019-01-28 11:14:21 -080014875 if (!cqp || !hdwq || !numcq)
James Smart2d7dbc42017-02-12 13:52:35 -080014876 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014877
14878 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14879 if (!mbox)
14880 return -ENOMEM;
14881
14882 length = sizeof(struct lpfc_mbx_cq_create_set);
14883 length += ((numcq * cqp[0]->page_count) *
14884 sizeof(struct dma_address));
14885 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14886 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14887 LPFC_SLI4_MBX_NEMBED);
14888 if (alloclen < length) {
14889 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14890 "3098 Allocated DMA memory size (%d) is "
14891 "less than the requested DMA memory size "
14892 "(%d)\n", alloclen, length);
14893 status = -ENOMEM;
14894 goto out;
14895 }
14896 cq_set = mbox->sge_array->addr[0];
14897 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14898 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14899
14900 for (idx = 0; idx < numcq; idx++) {
14901 cq = cqp[idx];
James Smartcdb42be2019-01-28 11:14:21 -080014902 eq = hdwq[idx].hba_eq;
James Smart2d7dbc42017-02-12 13:52:35 -080014903 if (!cq || !eq) {
14904 status = -ENOMEM;
14905 goto out;
14906 }
James Smart81b96ed2017-11-20 16:00:29 -080014907 if (!phba->sli4_hba.pc_sli4_params.supported)
14908 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014909
14910 switch (idx) {
14911 case 0:
14912 bf_set(lpfc_mbx_cq_create_set_page_size,
14913 &cq_set->u.request,
14914 (hw_page_size / SLI4_PAGE_SIZE));
14915 bf_set(lpfc_mbx_cq_create_set_num_pages,
14916 &cq_set->u.request, cq->page_count);
14917 bf_set(lpfc_mbx_cq_create_set_evt,
14918 &cq_set->u.request, 1);
14919 bf_set(lpfc_mbx_cq_create_set_valid,
14920 &cq_set->u.request, 1);
14921 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14922 &cq_set->u.request, 0);
14923 bf_set(lpfc_mbx_cq_create_set_num_cq,
14924 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080014925 bf_set(lpfc_mbx_cq_create_set_autovalid,
14926 &cq_set->u.request,
14927 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080014928 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014929 case 2048:
14930 case 4096:
14931 if (phba->sli4_hba.pc_sli4_params.cqv ==
14932 LPFC_Q_CREATE_VERSION_2) {
14933 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14934 &cq_set->u.request,
14935 cq->entry_count);
14936 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14937 &cq_set->u.request,
14938 LPFC_CQ_CNT_WORD7);
14939 break;
14940 }
14941 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080014942 default:
14943 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14944 "3118 Bad CQ count. (%d)\n",
14945 cq->entry_count);
14946 if (cq->entry_count < 256) {
14947 status = -EINVAL;
14948 goto out;
14949 }
14950 /* otherwise default to smallest (drop thru) */
14951 case 256:
14952 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14953 &cq_set->u.request, LPFC_CQ_CNT_256);
14954 break;
14955 case 512:
14956 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14957 &cq_set->u.request, LPFC_CQ_CNT_512);
14958 break;
14959 case 1024:
14960 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14961 &cq_set->u.request, LPFC_CQ_CNT_1024);
14962 break;
14963 }
14964 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14965 &cq_set->u.request, eq->queue_id);
14966 break;
14967 case 1:
14968 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14969 &cq_set->u.request, eq->queue_id);
14970 break;
14971 case 2:
14972 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14973 &cq_set->u.request, eq->queue_id);
14974 break;
14975 case 3:
14976 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14977 &cq_set->u.request, eq->queue_id);
14978 break;
14979 case 4:
14980 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14981 &cq_set->u.request, eq->queue_id);
14982 break;
14983 case 5:
14984 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14985 &cq_set->u.request, eq->queue_id);
14986 break;
14987 case 6:
14988 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14989 &cq_set->u.request, eq->queue_id);
14990 break;
14991 case 7:
14992 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14993 &cq_set->u.request, eq->queue_id);
14994 break;
14995 case 8:
14996 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14997 &cq_set->u.request, eq->queue_id);
14998 break;
14999 case 9:
15000 bf_set(lpfc_mbx_cq_create_set_eq_id9,
15001 &cq_set->u.request, eq->queue_id);
15002 break;
15003 case 10:
15004 bf_set(lpfc_mbx_cq_create_set_eq_id10,
15005 &cq_set->u.request, eq->queue_id);
15006 break;
15007 case 11:
15008 bf_set(lpfc_mbx_cq_create_set_eq_id11,
15009 &cq_set->u.request, eq->queue_id);
15010 break;
15011 case 12:
15012 bf_set(lpfc_mbx_cq_create_set_eq_id12,
15013 &cq_set->u.request, eq->queue_id);
15014 break;
15015 case 13:
15016 bf_set(lpfc_mbx_cq_create_set_eq_id13,
15017 &cq_set->u.request, eq->queue_id);
15018 break;
15019 case 14:
15020 bf_set(lpfc_mbx_cq_create_set_eq_id14,
15021 &cq_set->u.request, eq->queue_id);
15022 break;
15023 case 15:
15024 bf_set(lpfc_mbx_cq_create_set_eq_id15,
15025 &cq_set->u.request, eq->queue_id);
15026 break;
15027 }
15028
15029 /* link the cq onto the parent eq child list */
15030 list_add_tail(&cq->list, &eq->child_list);
15031 /* Set up completion queue's type and subtype */
15032 cq->type = type;
15033 cq->subtype = subtype;
15034 cq->assoc_qid = eq->queue_id;
15035 cq->host_index = 0;
15036 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070015037 cq->entry_repost = LPFC_CQ_REPOST;
James Smart81b96ed2017-11-20 16:00:29 -080015038 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080015039
15040 rc = 0;
15041 list_for_each_entry(dmabuf, &cq->page_list, list) {
15042 memset(dmabuf->virt, 0, hw_page_size);
15043 cnt = page_idx + dmabuf->buffer_tag;
15044 cq_set->u.request.page[cnt].addr_lo =
15045 putPaddrLow(dmabuf->phys);
15046 cq_set->u.request.page[cnt].addr_hi =
15047 putPaddrHigh(dmabuf->phys);
15048 rc++;
15049 }
15050 page_idx += rc;
15051 }
15052
15053 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15054
15055 /* The IOCTL status is embedded in the mailbox subheader. */
15056 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15057 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15058 if (shdr_status || shdr_add_status || rc) {
15059 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15060 "3119 CQ_CREATE_SET mailbox failed with "
15061 "status x%x add_status x%x, mbx status x%x\n",
15062 shdr_status, shdr_add_status, rc);
15063 status = -ENXIO;
15064 goto out;
15065 }
15066 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15067 if (rc == 0xFFFF) {
15068 status = -ENXIO;
15069 goto out;
15070 }
15071
15072 for (idx = 0; idx < numcq; idx++) {
15073 cq = cqp[idx];
15074 cq->queue_id = rc + idx;
15075 }
15076
15077out:
15078 lpfc_sli4_mbox_cmd_free(phba, mbox);
15079 return status;
15080}
15081
15082/**
James Smartb19a0612010-04-06 14:48:51 -040015083 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040015084 * @phba: HBA structure that indicates port to create a queue on.
15085 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040015086 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15087 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040015088 *
James Smartb19a0612010-04-06 14:48:51 -040015089 * This function provides failback (fb) functionality when the
15090 * mq_create_ext fails on older FW generations. It's purpose is identical
15091 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040015092 *
James Smartb19a0612010-04-06 14:48:51 -040015093 * This routine cannot fail as all attributes were previously accessed and
15094 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040015095 **/
James Smartb19a0612010-04-06 14:48:51 -040015096static void
15097lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15098 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040015099{
15100 struct lpfc_mbx_mq_create *mq_create;
15101 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040015102 int length;
James Smart04c68492009-05-22 14:52:52 -040015103
James Smart04c68492009-05-22 14:52:52 -040015104 length = (sizeof(struct lpfc_mbx_mq_create) -
15105 sizeof(struct lpfc_sli4_cfg_mhdr));
15106 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15107 LPFC_MBOX_OPCODE_MQ_CREATE,
15108 length, LPFC_SLI4_MBX_EMBED);
15109 mq_create = &mbox->u.mqe.un.mq_create;
15110 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040015111 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040015112 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040015113 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040015114 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15115 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040015116 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015117 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15118 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040015119 break;
15120 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015121 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15122 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040015123 break;
15124 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015125 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15126 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040015127 break;
15128 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015129 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15130 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040015131 break;
15132 }
15133 list_for_each_entry(dmabuf, &mq->page_list, list) {
15134 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040015135 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040015136 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040015137 putPaddrHigh(dmabuf->phys);
15138 }
15139}
15140
15141/**
15142 * lpfc_mq_create - Create a mailbox Queue on the HBA
15143 * @phba: HBA structure that indicates port to create a queue on.
15144 * @mq: The queue structure to use to create the mailbox queue.
15145 * @cq: The completion queue to associate with this cq.
15146 * @subtype: The queue's subtype.
15147 *
15148 * This function creates a mailbox queue, as detailed in @mq, on a port,
15149 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15150 *
15151 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15152 * is used to get the entry count and entry size that are necessary to
15153 * determine the number of pages to allocate and use for this queue. This
15154 * function will send the MQ_CREATE mailbox command to the HBA to setup the
15155 * mailbox queue. This function is asynchronous and will wait for the mailbox
15156 * command to finish before continuing.
15157 *
15158 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015159 * memory this function will return -ENOMEM. If the queue create mailbox command
15160 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040015161 **/
15162int32_t
15163lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15164 struct lpfc_queue *cq, uint32_t subtype)
15165{
15166 struct lpfc_mbx_mq_create *mq_create;
15167 struct lpfc_mbx_mq_create_ext *mq_create_ext;
15168 struct lpfc_dmabuf *dmabuf;
15169 LPFC_MBOXQ_t *mbox;
15170 int rc, length, status = 0;
15171 uint32_t shdr_status, shdr_add_status;
15172 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015173 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040015174
James Smart2e90f4b2011-12-13 13:22:37 -050015175 /* sanity check on queue memory */
15176 if (!mq || !cq)
15177 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015178 if (!phba->sli4_hba.pc_sli4_params.supported)
15179 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040015180
15181 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15182 if (!mbox)
15183 return -ENOMEM;
15184 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15185 sizeof(struct lpfc_sli4_cfg_mhdr));
15186 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15187 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15188 length, LPFC_SLI4_MBX_EMBED);
15189
15190 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015191 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015192 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15193 &mq_create_ext->u.request, mq->page_count);
15194 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15195 &mq_create_ext->u.request, 1);
15196 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015197 &mq_create_ext->u.request, 1);
15198 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15199 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015200 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15201 &mq_create_ext->u.request, 1);
15202 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15203 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015204 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015205 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15206 phba->sli4_hba.pc_sli4_params.mqv);
15207 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15208 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15209 cq->queue_id);
15210 else
15211 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15212 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015213 switch (mq->entry_count) {
15214 default:
15215 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15216 "0362 Unsupported MQ count. (%d)\n",
15217 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015218 if (mq->entry_count < 16) {
15219 status = -EINVAL;
15220 goto out;
15221 }
James Smartb19a0612010-04-06 14:48:51 -040015222 /* otherwise default to smallest count (drop through) */
15223 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015224 bf_set(lpfc_mq_context_ring_size,
15225 &mq_create_ext->u.request.context,
15226 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015227 break;
15228 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015229 bf_set(lpfc_mq_context_ring_size,
15230 &mq_create_ext->u.request.context,
15231 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015232 break;
15233 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015234 bf_set(lpfc_mq_context_ring_size,
15235 &mq_create_ext->u.request.context,
15236 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015237 break;
15238 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015239 bf_set(lpfc_mq_context_ring_size,
15240 &mq_create_ext->u.request.context,
15241 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015242 break;
15243 }
15244 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015245 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015246 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15247 putPaddrLow(dmabuf->phys);
15248 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015249 putPaddrHigh(dmabuf->phys);
15250 }
15251 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015252 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15253 &mq_create_ext->u.response);
15254 if (rc != MBX_SUCCESS) {
15255 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15256 "2795 MQ_CREATE_EXT failed with "
15257 "status x%x. Failback to MQ_CREATE.\n",
15258 rc);
15259 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15260 mq_create = &mbox->u.mqe.un.mq_create;
15261 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15262 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15263 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15264 &mq_create->u.response);
15265 }
15266
James Smart04c68492009-05-22 14:52:52 -040015267 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015268 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15269 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15270 if (shdr_status || shdr_add_status || rc) {
15271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15272 "2502 MQ_CREATE mailbox failed with "
15273 "status x%x add_status x%x, mbx status x%x\n",
15274 shdr_status, shdr_add_status, rc);
15275 status = -ENXIO;
15276 goto out;
15277 }
James Smart04c68492009-05-22 14:52:52 -040015278 if (mq->queue_id == 0xFFFF) {
15279 status = -ENXIO;
15280 goto out;
15281 }
15282 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015283 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015284 mq->subtype = subtype;
15285 mq->host_index = 0;
15286 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070015287 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040015288
15289 /* link the mq onto the parent cq child list */
15290 list_add_tail(&mq->list, &cq->child_list);
15291out:
James Smart8fa38512009-07-19 10:01:03 -040015292 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015293 return status;
15294}
15295
15296/**
James Smart4f774512009-05-22 14:52:35 -040015297 * lpfc_wq_create - Create a Work Queue on the HBA
15298 * @phba: HBA structure that indicates port to create a queue on.
15299 * @wq: The queue structure to use to create the work queue.
15300 * @cq: The completion queue to bind this work queue to.
15301 * @subtype: The subtype of the work queue indicating its functionality.
15302 *
15303 * This function creates a work queue, as detailed in @wq, on a port, described
15304 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15305 *
15306 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15307 * is used to get the entry count and entry size that are necessary to
15308 * determine the number of pages to allocate and use for this queue. The @cq
15309 * is used to indicate which completion queue to bind this work queue to. This
15310 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15311 * work queue. This function is asynchronous and will wait for the mailbox
15312 * command to finish before continuing.
15313 *
15314 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015315 * memory this function will return -ENOMEM. If the queue create mailbox command
15316 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015317 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015318int
James Smart4f774512009-05-22 14:52:35 -040015319lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15320 struct lpfc_queue *cq, uint32_t subtype)
15321{
15322 struct lpfc_mbx_wq_create *wq_create;
15323 struct lpfc_dmabuf *dmabuf;
15324 LPFC_MBOXQ_t *mbox;
15325 int rc, length, status = 0;
15326 uint32_t shdr_status, shdr_add_status;
15327 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015328 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015329 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015330 void __iomem *bar_memmap_p;
15331 uint32_t db_offset;
15332 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015333 uint8_t dpp_barset;
15334 uint32_t dpp_offset;
15335 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015336 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015337
James Smart2e90f4b2011-12-13 13:22:37 -050015338 /* sanity check on queue memory */
15339 if (!wq || !cq)
15340 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015341 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015342 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015343
15344 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15345 if (!mbox)
15346 return -ENOMEM;
15347 length = (sizeof(struct lpfc_mbx_wq_create) -
15348 sizeof(struct lpfc_sli4_cfg_mhdr));
15349 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15350 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15351 length, LPFC_SLI4_MBX_EMBED);
15352 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015353 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015354 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15355 wq->page_count);
15356 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15357 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015358
15359 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015360 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15361 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015362
James Smartc176ffa2018-01-30 15:58:46 -080015363 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15364 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015365 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15366 else
15367 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15368
James Smart0c651872013-07-15 18:33:23 -040015369
James Smart1351e692018-02-22 08:18:43 -080015370 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15371 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15372 else
15373 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15374
15375 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015376 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015377 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15378 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015379 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15380 LPFC_Q_CREATE_VERSION_1);
15381
James Smart5a6f1332011-03-11 16:05:35 -050015382 switch (wq->entry_size) {
15383 default:
15384 case 64:
15385 bf_set(lpfc_mbx_wq_create_wqe_size,
15386 &wq_create->u.request_1,
15387 LPFC_WQ_WQE_SIZE_64);
15388 break;
15389 case 128:
15390 bf_set(lpfc_mbx_wq_create_wqe_size,
15391 &wq_create->u.request_1,
15392 LPFC_WQ_WQE_SIZE_128);
15393 break;
15394 }
James Smart1351e692018-02-22 08:18:43 -080015395 /* Request DPP by default */
15396 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015397 bf_set(lpfc_mbx_wq_create_page_size,
15398 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015399 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015400 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015401 break;
15402 default:
James Smart1351e692018-02-22 08:18:43 -080015403 page = wq_create->u.request.page;
15404 break;
James Smart5a6f1332011-03-11 16:05:35 -050015405 }
James Smart0c651872013-07-15 18:33:23 -040015406
James Smart4f774512009-05-22 14:52:35 -040015407 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015408 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015409 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15410 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015411 }
James Smart962bc512013-01-03 15:44:00 -050015412
15413 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15414 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15415
James Smart4f774512009-05-22 14:52:35 -040015416 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15417 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015418 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15419 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15420 if (shdr_status || shdr_add_status || rc) {
15421 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15422 "2503 WQ_CREATE mailbox failed with "
15423 "status x%x add_status x%x, mbx status x%x\n",
15424 shdr_status, shdr_add_status, rc);
15425 status = -ENXIO;
15426 goto out;
15427 }
James Smart1351e692018-02-22 08:18:43 -080015428
15429 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15430 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15431 &wq_create->u.response);
15432 else
15433 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15434 &wq_create->u.response_1);
15435
James Smart4f774512009-05-22 14:52:35 -040015436 if (wq->queue_id == 0xFFFF) {
15437 status = -ENXIO;
15438 goto out;
15439 }
James Smart1351e692018-02-22 08:18:43 -080015440
15441 wq->db_format = LPFC_DB_LIST_FORMAT;
15442 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15443 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15444 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15445 &wq_create->u.response);
15446 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15447 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15449 "3265 WQ[%d] doorbell format "
15450 "not supported: x%x\n",
15451 wq->queue_id, wq->db_format);
15452 status = -EINVAL;
15453 goto out;
15454 }
15455 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15456 &wq_create->u.response);
15457 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15458 pci_barset);
15459 if (!bar_memmap_p) {
15460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15461 "3263 WQ[%d] failed to memmap "
15462 "pci barset:x%x\n",
15463 wq->queue_id, pci_barset);
15464 status = -ENOMEM;
15465 goto out;
15466 }
15467 db_offset = wq_create->u.response.doorbell_offset;
15468 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15469 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15471 "3252 WQ[%d] doorbell offset "
15472 "not supported: x%x\n",
15473 wq->queue_id, db_offset);
15474 status = -EINVAL;
15475 goto out;
15476 }
15477 wq->db_regaddr = bar_memmap_p + db_offset;
15478 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15479 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15480 "format:x%x\n", wq->queue_id,
15481 pci_barset, db_offset, wq->db_format);
15482 } else
15483 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015484 } else {
James Smart1351e692018-02-22 08:18:43 -080015485 /* Check if DPP was honored by the firmware */
15486 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15487 &wq_create->u.response_1);
15488 if (wq->dpp_enable) {
15489 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15490 &wq_create->u.response_1);
15491 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15492 pci_barset);
15493 if (!bar_memmap_p) {
15494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15495 "3267 WQ[%d] failed to memmap "
15496 "pci barset:x%x\n",
15497 wq->queue_id, pci_barset);
15498 status = -ENOMEM;
15499 goto out;
15500 }
15501 db_offset = wq_create->u.response_1.doorbell_offset;
15502 wq->db_regaddr = bar_memmap_p + db_offset;
15503 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15504 &wq_create->u.response_1);
15505 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15506 &wq_create->u.response_1);
15507 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15508 dpp_barset);
15509 if (!bar_memmap_p) {
15510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15511 "3268 WQ[%d] failed to memmap "
15512 "pci barset:x%x\n",
15513 wq->queue_id, dpp_barset);
15514 status = -ENOMEM;
15515 goto out;
15516 }
15517 dpp_offset = wq_create->u.response_1.dpp_offset;
15518 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15519 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15520 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15521 "dpp_id:x%x dpp_barset:x%x "
15522 "dpp_offset:x%x\n",
15523 wq->queue_id, pci_barset, db_offset,
15524 wq->dpp_id, dpp_barset, dpp_offset);
15525
15526 /* Enable combined writes for DPP aperture */
15527 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15528#ifdef CONFIG_X86
15529 rc = set_memory_wc(pg_addr, 1);
15530 if (rc) {
15531 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15532 "3272 Cannot setup Combined "
15533 "Write on WQ[%d] - disable DPP\n",
15534 wq->queue_id);
15535 phba->cfg_enable_dpp = 0;
15536 }
15537#else
15538 phba->cfg_enable_dpp = 0;
15539#endif
15540 } else
15541 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015542 }
James Smart895427b2017-02-12 13:52:30 -080015543 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15544 if (wq->pring == NULL) {
15545 status = -ENOMEM;
15546 goto out;
15547 }
James Smart4f774512009-05-22 14:52:35 -040015548 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015549 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015550 wq->subtype = subtype;
15551 wq->host_index = 0;
15552 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050015553 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040015554
15555 /* link the wq onto the parent cq child list */
15556 list_add_tail(&wq->list, &cq->child_list);
15557out:
James Smart8fa38512009-07-19 10:01:03 -040015558 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015559 return status;
15560}
15561
15562/**
15563 * lpfc_rq_create - Create a Receive Queue on the HBA
15564 * @phba: HBA structure that indicates port to create a queue on.
15565 * @hrq: The queue structure to use to create the header receive queue.
15566 * @drq: The queue structure to use to create the data receive queue.
15567 * @cq: The completion queue to bind this work queue to.
15568 *
15569 * This function creates a receive buffer queue pair , as detailed in @hrq and
15570 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15571 * to the HBA.
15572 *
15573 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15574 * struct is used to get the entry count that is necessary to determine the
15575 * number of pages to use for this queue. The @cq is used to indicate which
15576 * completion queue to bind received buffers that are posted to these queues to.
15577 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15578 * receive queue pair. This function is asynchronous and will wait for the
15579 * mailbox command to finish before continuing.
15580 *
15581 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015582 * memory this function will return -ENOMEM. If the queue create mailbox command
15583 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015584 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015585int
James Smart4f774512009-05-22 14:52:35 -040015586lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15587 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15588{
15589 struct lpfc_mbx_rq_create *rq_create;
15590 struct lpfc_dmabuf *dmabuf;
15591 LPFC_MBOXQ_t *mbox;
15592 int rc, length, status = 0;
15593 uint32_t shdr_status, shdr_add_status;
15594 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015595 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015596 void __iomem *bar_memmap_p;
15597 uint32_t db_offset;
15598 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015599
James Smart2e90f4b2011-12-13 13:22:37 -050015600 /* sanity check on queue memory */
15601 if (!hrq || !drq || !cq)
15602 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015603 if (!phba->sli4_hba.pc_sli4_params.supported)
15604 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015605
15606 if (hrq->entry_count != drq->entry_count)
15607 return -EINVAL;
15608 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15609 if (!mbox)
15610 return -ENOMEM;
15611 length = (sizeof(struct lpfc_mbx_rq_create) -
15612 sizeof(struct lpfc_sli4_cfg_mhdr));
15613 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15614 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15615 length, LPFC_SLI4_MBX_EMBED);
15616 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015617 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15618 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15619 phba->sli4_hba.pc_sli4_params.rqv);
15620 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15621 bf_set(lpfc_rq_context_rqe_count_1,
15622 &rq_create->u.request.context,
15623 hrq->entry_count);
15624 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015625 bf_set(lpfc_rq_context_rqe_size,
15626 &rq_create->u.request.context,
15627 LPFC_RQE_SIZE_8);
15628 bf_set(lpfc_rq_context_page_size,
15629 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015630 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015631 } else {
15632 switch (hrq->entry_count) {
15633 default:
15634 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15635 "2535 Unsupported RQ count. (%d)\n",
15636 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015637 if (hrq->entry_count < 512) {
15638 status = -EINVAL;
15639 goto out;
15640 }
James Smart5a6f1332011-03-11 16:05:35 -050015641 /* otherwise default to smallest count (drop through) */
15642 case 512:
15643 bf_set(lpfc_rq_context_rqe_count,
15644 &rq_create->u.request.context,
15645 LPFC_RQ_RING_SIZE_512);
15646 break;
15647 case 1024:
15648 bf_set(lpfc_rq_context_rqe_count,
15649 &rq_create->u.request.context,
15650 LPFC_RQ_RING_SIZE_1024);
15651 break;
15652 case 2048:
15653 bf_set(lpfc_rq_context_rqe_count,
15654 &rq_create->u.request.context,
15655 LPFC_RQ_RING_SIZE_2048);
15656 break;
15657 case 4096:
15658 bf_set(lpfc_rq_context_rqe_count,
15659 &rq_create->u.request.context,
15660 LPFC_RQ_RING_SIZE_4096);
15661 break;
15662 }
15663 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15664 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015665 }
15666 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15667 cq->queue_id);
15668 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15669 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015670 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015671 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015672 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15673 putPaddrLow(dmabuf->phys);
15674 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15675 putPaddrHigh(dmabuf->phys);
15676 }
James Smart962bc512013-01-03 15:44:00 -050015677 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15678 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15679
James Smart4f774512009-05-22 14:52:35 -040015680 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15681 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015682 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15683 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15684 if (shdr_status || shdr_add_status || rc) {
15685 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15686 "2504 RQ_CREATE mailbox failed with "
15687 "status x%x add_status x%x, mbx status x%x\n",
15688 shdr_status, shdr_add_status, rc);
15689 status = -ENXIO;
15690 goto out;
15691 }
15692 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15693 if (hrq->queue_id == 0xFFFF) {
15694 status = -ENXIO;
15695 goto out;
15696 }
James Smart962bc512013-01-03 15:44:00 -050015697
15698 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15699 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15700 &rq_create->u.response);
15701 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15702 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15703 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15704 "3262 RQ [%d] doorbell format not "
15705 "supported: x%x\n", hrq->queue_id,
15706 hrq->db_format);
15707 status = -EINVAL;
15708 goto out;
15709 }
15710
15711 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15712 &rq_create->u.response);
15713 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15714 if (!bar_memmap_p) {
15715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15716 "3269 RQ[%d] failed to memmap pci "
15717 "barset:x%x\n", hrq->queue_id,
15718 pci_barset);
15719 status = -ENOMEM;
15720 goto out;
15721 }
15722
15723 db_offset = rq_create->u.response.doorbell_offset;
15724 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15725 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15727 "3270 RQ[%d] doorbell offset not "
15728 "supported: x%x\n", hrq->queue_id,
15729 db_offset);
15730 status = -EINVAL;
15731 goto out;
15732 }
15733 hrq->db_regaddr = bar_memmap_p + db_offset;
15734 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015735 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15736 "format:x%x\n", hrq->queue_id, pci_barset,
15737 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015738 } else {
15739 hrq->db_format = LPFC_DB_RING_FORMAT;
15740 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15741 }
James Smart4f774512009-05-22 14:52:35 -040015742 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015743 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015744 hrq->subtype = subtype;
15745 hrq->host_index = 0;
15746 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015747 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015748
15749 /* now create the data queue */
15750 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15751 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15752 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015753 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15754 phba->sli4_hba.pc_sli4_params.rqv);
15755 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15756 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015757 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015758 if (subtype == LPFC_NVMET)
15759 rq_create->u.request.context.buffer_size =
15760 LPFC_NVMET_DATA_BUF_SIZE;
15761 else
15762 rq_create->u.request.context.buffer_size =
15763 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015764 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15765 LPFC_RQE_SIZE_8);
15766 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15767 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015768 } else {
15769 switch (drq->entry_count) {
15770 default:
15771 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15772 "2536 Unsupported RQ count. (%d)\n",
15773 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015774 if (drq->entry_count < 512) {
15775 status = -EINVAL;
15776 goto out;
15777 }
James Smart5a6f1332011-03-11 16:05:35 -050015778 /* otherwise default to smallest count (drop through) */
15779 case 512:
15780 bf_set(lpfc_rq_context_rqe_count,
15781 &rq_create->u.request.context,
15782 LPFC_RQ_RING_SIZE_512);
15783 break;
15784 case 1024:
15785 bf_set(lpfc_rq_context_rqe_count,
15786 &rq_create->u.request.context,
15787 LPFC_RQ_RING_SIZE_1024);
15788 break;
15789 case 2048:
15790 bf_set(lpfc_rq_context_rqe_count,
15791 &rq_create->u.request.context,
15792 LPFC_RQ_RING_SIZE_2048);
15793 break;
15794 case 4096:
15795 bf_set(lpfc_rq_context_rqe_count,
15796 &rq_create->u.request.context,
15797 LPFC_RQ_RING_SIZE_4096);
15798 break;
15799 }
James Smart3c603be2017-05-15 15:20:44 -070015800 if (subtype == LPFC_NVMET)
15801 bf_set(lpfc_rq_context_buf_size,
15802 &rq_create->u.request.context,
15803 LPFC_NVMET_DATA_BUF_SIZE);
15804 else
15805 bf_set(lpfc_rq_context_buf_size,
15806 &rq_create->u.request.context,
15807 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015808 }
15809 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15810 cq->queue_id);
15811 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15812 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015813 list_for_each_entry(dmabuf, &drq->page_list, list) {
15814 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15815 putPaddrLow(dmabuf->phys);
15816 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15817 putPaddrHigh(dmabuf->phys);
15818 }
James Smart962bc512013-01-03 15:44:00 -050015819 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15820 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015821 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15822 /* The IOCTL status is embedded in the mailbox subheader. */
15823 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15824 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15825 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15826 if (shdr_status || shdr_add_status || rc) {
15827 status = -ENXIO;
15828 goto out;
15829 }
15830 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15831 if (drq->queue_id == 0xFFFF) {
15832 status = -ENXIO;
15833 goto out;
15834 }
15835 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015836 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015837 drq->subtype = subtype;
15838 drq->host_index = 0;
15839 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015840 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015841
15842 /* link the header and data RQs onto the parent cq child list */
15843 list_add_tail(&hrq->list, &cq->child_list);
15844 list_add_tail(&drq->list, &cq->child_list);
15845
15846out:
James Smart8fa38512009-07-19 10:01:03 -040015847 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015848 return status;
15849}
15850
15851/**
James Smart2d7dbc42017-02-12 13:52:35 -080015852 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15853 * @phba: HBA structure that indicates port to create a queue on.
15854 * @hrqp: The queue structure array to use to create the header receive queues.
15855 * @drqp: The queue structure array to use to create the data receive queues.
15856 * @cqp: The completion queue array to bind these receive queues to.
15857 *
15858 * This function creates a receive buffer queue pair , as detailed in @hrq and
15859 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15860 * to the HBA.
15861 *
15862 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15863 * struct is used to get the entry count that is necessary to determine the
15864 * number of pages to use for this queue. The @cq is used to indicate which
15865 * completion queue to bind received buffers that are posted to these queues to.
15866 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15867 * receive queue pair. This function is asynchronous and will wait for the
15868 * mailbox command to finish before continuing.
15869 *
15870 * On success this function will return a zero. If unable to allocate enough
15871 * memory this function will return -ENOMEM. If the queue create mailbox command
15872 * fails this function will return -ENXIO.
15873 **/
15874int
15875lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15876 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15877 uint32_t subtype)
15878{
15879 struct lpfc_queue *hrq, *drq, *cq;
15880 struct lpfc_mbx_rq_create_v2 *rq_create;
15881 struct lpfc_dmabuf *dmabuf;
15882 LPFC_MBOXQ_t *mbox;
15883 int rc, length, alloclen, status = 0;
15884 int cnt, idx, numrq, page_idx = 0;
15885 uint32_t shdr_status, shdr_add_status;
15886 union lpfc_sli4_cfg_shdr *shdr;
15887 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15888
15889 numrq = phba->cfg_nvmet_mrq;
15890 /* sanity check on array memory */
15891 if (!hrqp || !drqp || !cqp || !numrq)
15892 return -ENODEV;
15893 if (!phba->sli4_hba.pc_sli4_params.supported)
15894 hw_page_size = SLI4_PAGE_SIZE;
15895
15896 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15897 if (!mbox)
15898 return -ENOMEM;
15899
15900 length = sizeof(struct lpfc_mbx_rq_create_v2);
15901 length += ((2 * numrq * hrqp[0]->page_count) *
15902 sizeof(struct dma_address));
15903
15904 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15905 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15906 LPFC_SLI4_MBX_NEMBED);
15907 if (alloclen < length) {
15908 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15909 "3099 Allocated DMA memory size (%d) is "
15910 "less than the requested DMA memory size "
15911 "(%d)\n", alloclen, length);
15912 status = -ENOMEM;
15913 goto out;
15914 }
15915
15916
15917
15918 rq_create = mbox->sge_array->addr[0];
15919 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15920
15921 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15922 cnt = 0;
15923
15924 for (idx = 0; idx < numrq; idx++) {
15925 hrq = hrqp[idx];
15926 drq = drqp[idx];
15927 cq = cqp[idx];
15928
James Smart2d7dbc42017-02-12 13:52:35 -080015929 /* sanity check on queue memory */
15930 if (!hrq || !drq || !cq) {
15931 status = -ENODEV;
15932 goto out;
15933 }
15934
James Smart7aabe842017-03-04 09:30:22 -080015935 if (hrq->entry_count != drq->entry_count) {
15936 status = -EINVAL;
15937 goto out;
15938 }
15939
James Smart2d7dbc42017-02-12 13:52:35 -080015940 if (idx == 0) {
15941 bf_set(lpfc_mbx_rq_create_num_pages,
15942 &rq_create->u.request,
15943 hrq->page_count);
15944 bf_set(lpfc_mbx_rq_create_rq_cnt,
15945 &rq_create->u.request, (numrq * 2));
15946 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15947 1);
15948 bf_set(lpfc_rq_context_base_cq,
15949 &rq_create->u.request.context,
15950 cq->queue_id);
15951 bf_set(lpfc_rq_context_data_size,
15952 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015953 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015954 bf_set(lpfc_rq_context_hdr_size,
15955 &rq_create->u.request.context,
15956 LPFC_HDR_BUF_SIZE);
15957 bf_set(lpfc_rq_context_rqe_count_1,
15958 &rq_create->u.request.context,
15959 hrq->entry_count);
15960 bf_set(lpfc_rq_context_rqe_size,
15961 &rq_create->u.request.context,
15962 LPFC_RQE_SIZE_8);
15963 bf_set(lpfc_rq_context_page_size,
15964 &rq_create->u.request.context,
15965 (PAGE_SIZE/SLI4_PAGE_SIZE));
15966 }
15967 rc = 0;
15968 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15969 memset(dmabuf->virt, 0, hw_page_size);
15970 cnt = page_idx + dmabuf->buffer_tag;
15971 rq_create->u.request.page[cnt].addr_lo =
15972 putPaddrLow(dmabuf->phys);
15973 rq_create->u.request.page[cnt].addr_hi =
15974 putPaddrHigh(dmabuf->phys);
15975 rc++;
15976 }
15977 page_idx += rc;
15978
15979 rc = 0;
15980 list_for_each_entry(dmabuf, &drq->page_list, list) {
15981 memset(dmabuf->virt, 0, hw_page_size);
15982 cnt = page_idx + dmabuf->buffer_tag;
15983 rq_create->u.request.page[cnt].addr_lo =
15984 putPaddrLow(dmabuf->phys);
15985 rq_create->u.request.page[cnt].addr_hi =
15986 putPaddrHigh(dmabuf->phys);
15987 rc++;
15988 }
15989 page_idx += rc;
15990
15991 hrq->db_format = LPFC_DB_RING_FORMAT;
15992 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15993 hrq->type = LPFC_HRQ;
15994 hrq->assoc_qid = cq->queue_id;
15995 hrq->subtype = subtype;
15996 hrq->host_index = 0;
15997 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015998 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015999
16000 drq->db_format = LPFC_DB_RING_FORMAT;
16001 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16002 drq->type = LPFC_DRQ;
16003 drq->assoc_qid = cq->queue_id;
16004 drq->subtype = subtype;
16005 drq->host_index = 0;
16006 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070016007 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080016008
16009 list_add_tail(&hrq->list, &cq->child_list);
16010 list_add_tail(&drq->list, &cq->child_list);
16011 }
16012
16013 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16014 /* The IOCTL status is embedded in the mailbox subheader. */
16015 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16016 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16017 if (shdr_status || shdr_add_status || rc) {
16018 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16019 "3120 RQ_CREATE mailbox failed with "
16020 "status x%x add_status x%x, mbx status x%x\n",
16021 shdr_status, shdr_add_status, rc);
16022 status = -ENXIO;
16023 goto out;
16024 }
16025 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16026 if (rc == 0xFFFF) {
16027 status = -ENXIO;
16028 goto out;
16029 }
16030
16031 /* Initialize all RQs with associated queue id */
16032 for (idx = 0; idx < numrq; idx++) {
16033 hrq = hrqp[idx];
16034 hrq->queue_id = rc + (2 * idx);
16035 drq = drqp[idx];
16036 drq->queue_id = rc + (2 * idx) + 1;
16037 }
16038
16039out:
16040 lpfc_sli4_mbox_cmd_free(phba, mbox);
16041 return status;
16042}
16043
16044/**
James Smart4f774512009-05-22 14:52:35 -040016045 * lpfc_eq_destroy - Destroy an event Queue on the HBA
16046 * @eq: The queue structure associated with the queue to destroy.
16047 *
16048 * This function destroys a queue, as detailed in @eq by sending an mailbox
16049 * command, specific to the type of queue, to the HBA.
16050 *
16051 * The @eq struct is used to get the queue ID of the queue to destroy.
16052 *
16053 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016054 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016055 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016056int
James Smart4f774512009-05-22 14:52:35 -040016057lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16058{
16059 LPFC_MBOXQ_t *mbox;
16060 int rc, length, status = 0;
16061 uint32_t shdr_status, shdr_add_status;
16062 union lpfc_sli4_cfg_shdr *shdr;
16063
James Smart2e90f4b2011-12-13 13:22:37 -050016064 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016065 if (!eq)
16066 return -ENODEV;
16067 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16068 if (!mbox)
16069 return -ENOMEM;
16070 length = (sizeof(struct lpfc_mbx_eq_destroy) -
16071 sizeof(struct lpfc_sli4_cfg_mhdr));
16072 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16073 LPFC_MBOX_OPCODE_EQ_DESTROY,
16074 length, LPFC_SLI4_MBX_EMBED);
16075 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16076 eq->queue_id);
16077 mbox->vport = eq->phba->pport;
16078 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16079
16080 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16081 /* The IOCTL status is embedded in the mailbox subheader. */
16082 shdr = (union lpfc_sli4_cfg_shdr *)
16083 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16084 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16085 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16086 if (shdr_status || shdr_add_status || rc) {
16087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16088 "2505 EQ_DESTROY mailbox failed with "
16089 "status x%x add_status x%x, mbx status x%x\n",
16090 shdr_status, shdr_add_status, rc);
16091 status = -ENXIO;
16092 }
16093
16094 /* Remove eq from any list */
16095 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040016096 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016097 return status;
16098}
16099
16100/**
16101 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16102 * @cq: The queue structure associated with the queue to destroy.
16103 *
16104 * This function destroys a queue, as detailed in @cq by sending an mailbox
16105 * command, specific to the type of queue, to the HBA.
16106 *
16107 * The @cq struct is used to get the queue ID of the queue to destroy.
16108 *
16109 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016110 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016111 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016112int
James Smart4f774512009-05-22 14:52:35 -040016113lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16114{
16115 LPFC_MBOXQ_t *mbox;
16116 int rc, length, status = 0;
16117 uint32_t shdr_status, shdr_add_status;
16118 union lpfc_sli4_cfg_shdr *shdr;
16119
James Smart2e90f4b2011-12-13 13:22:37 -050016120 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016121 if (!cq)
16122 return -ENODEV;
16123 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16124 if (!mbox)
16125 return -ENOMEM;
16126 length = (sizeof(struct lpfc_mbx_cq_destroy) -
16127 sizeof(struct lpfc_sli4_cfg_mhdr));
16128 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16129 LPFC_MBOX_OPCODE_CQ_DESTROY,
16130 length, LPFC_SLI4_MBX_EMBED);
16131 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16132 cq->queue_id);
16133 mbox->vport = cq->phba->pport;
16134 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16135 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16136 /* The IOCTL status is embedded in the mailbox subheader. */
16137 shdr = (union lpfc_sli4_cfg_shdr *)
16138 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16139 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16140 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16141 if (shdr_status || shdr_add_status || rc) {
16142 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16143 "2506 CQ_DESTROY mailbox failed with "
16144 "status x%x add_status x%x, mbx status x%x\n",
16145 shdr_status, shdr_add_status, rc);
16146 status = -ENXIO;
16147 }
16148 /* Remove cq from any list */
16149 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040016150 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016151 return status;
16152}
16153
16154/**
James Smart04c68492009-05-22 14:52:52 -040016155 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16156 * @qm: The queue structure associated with the queue to destroy.
16157 *
16158 * This function destroys a queue, as detailed in @mq by sending an mailbox
16159 * command, specific to the type of queue, to the HBA.
16160 *
16161 * The @mq struct is used to get the queue ID of the queue to destroy.
16162 *
16163 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016164 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040016165 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016166int
James Smart04c68492009-05-22 14:52:52 -040016167lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16168{
16169 LPFC_MBOXQ_t *mbox;
16170 int rc, length, status = 0;
16171 uint32_t shdr_status, shdr_add_status;
16172 union lpfc_sli4_cfg_shdr *shdr;
16173
James Smart2e90f4b2011-12-13 13:22:37 -050016174 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040016175 if (!mq)
16176 return -ENODEV;
16177 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16178 if (!mbox)
16179 return -ENOMEM;
16180 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16181 sizeof(struct lpfc_sli4_cfg_mhdr));
16182 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16183 LPFC_MBOX_OPCODE_MQ_DESTROY,
16184 length, LPFC_SLI4_MBX_EMBED);
16185 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16186 mq->queue_id);
16187 mbox->vport = mq->phba->pport;
16188 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16189 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16190 /* The IOCTL status is embedded in the mailbox subheader. */
16191 shdr = (union lpfc_sli4_cfg_shdr *)
16192 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16193 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16194 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16195 if (shdr_status || shdr_add_status || rc) {
16196 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16197 "2507 MQ_DESTROY mailbox failed with "
16198 "status x%x add_status x%x, mbx status x%x\n",
16199 shdr_status, shdr_add_status, rc);
16200 status = -ENXIO;
16201 }
16202 /* Remove mq from any list */
16203 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016204 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016205 return status;
16206}
16207
16208/**
James Smart4f774512009-05-22 14:52:35 -040016209 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16210 * @wq: The queue structure associated with the queue to destroy.
16211 *
16212 * This function destroys a queue, as detailed in @wq by sending an mailbox
16213 * command, specific to the type of queue, to the HBA.
16214 *
16215 * The @wq struct is used to get the queue ID of the queue to destroy.
16216 *
16217 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016218 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016219 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016220int
James Smart4f774512009-05-22 14:52:35 -040016221lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16222{
16223 LPFC_MBOXQ_t *mbox;
16224 int rc, length, status = 0;
16225 uint32_t shdr_status, shdr_add_status;
16226 union lpfc_sli4_cfg_shdr *shdr;
16227
James Smart2e90f4b2011-12-13 13:22:37 -050016228 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016229 if (!wq)
16230 return -ENODEV;
16231 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16232 if (!mbox)
16233 return -ENOMEM;
16234 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16235 sizeof(struct lpfc_sli4_cfg_mhdr));
16236 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16237 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16238 length, LPFC_SLI4_MBX_EMBED);
16239 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16240 wq->queue_id);
16241 mbox->vport = wq->phba->pport;
16242 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16243 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16244 shdr = (union lpfc_sli4_cfg_shdr *)
16245 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16246 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16247 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16248 if (shdr_status || shdr_add_status || rc) {
16249 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16250 "2508 WQ_DESTROY mailbox failed with "
16251 "status x%x add_status x%x, mbx status x%x\n",
16252 shdr_status, shdr_add_status, rc);
16253 status = -ENXIO;
16254 }
16255 /* Remove wq from any list */
16256 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016257 kfree(wq->pring);
16258 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016259 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016260 return status;
16261}
16262
16263/**
16264 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16265 * @rq: The queue structure associated with the queue to destroy.
16266 *
16267 * This function destroys a queue, as detailed in @rq by sending an mailbox
16268 * command, specific to the type of queue, to the HBA.
16269 *
16270 * The @rq struct is used to get the queue ID of the queue to destroy.
16271 *
16272 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016273 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016274 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016275int
James Smart4f774512009-05-22 14:52:35 -040016276lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16277 struct lpfc_queue *drq)
16278{
16279 LPFC_MBOXQ_t *mbox;
16280 int rc, length, status = 0;
16281 uint32_t shdr_status, shdr_add_status;
16282 union lpfc_sli4_cfg_shdr *shdr;
16283
James Smart2e90f4b2011-12-13 13:22:37 -050016284 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016285 if (!hrq || !drq)
16286 return -ENODEV;
16287 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16288 if (!mbox)
16289 return -ENOMEM;
16290 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016291 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016292 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16293 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16294 length, LPFC_SLI4_MBX_EMBED);
16295 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16296 hrq->queue_id);
16297 mbox->vport = hrq->phba->pport;
16298 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16299 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16300 /* The IOCTL status is embedded in the mailbox subheader. */
16301 shdr = (union lpfc_sli4_cfg_shdr *)
16302 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16303 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16304 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16305 if (shdr_status || shdr_add_status || rc) {
16306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16307 "2509 RQ_DESTROY mailbox failed with "
16308 "status x%x add_status x%x, mbx status x%x\n",
16309 shdr_status, shdr_add_status, rc);
16310 if (rc != MBX_TIMEOUT)
16311 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16312 return -ENXIO;
16313 }
16314 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16315 drq->queue_id);
16316 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16317 shdr = (union lpfc_sli4_cfg_shdr *)
16318 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16319 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16320 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16321 if (shdr_status || shdr_add_status || rc) {
16322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16323 "2510 RQ_DESTROY mailbox failed with "
16324 "status x%x add_status x%x, mbx status x%x\n",
16325 shdr_status, shdr_add_status, rc);
16326 status = -ENXIO;
16327 }
16328 list_del_init(&hrq->list);
16329 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016330 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016331 return status;
16332}
16333
16334/**
16335 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16336 * @phba: The virtual port for which this call being executed.
16337 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16338 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16339 * @xritag: the xritag that ties this io to the SGL pages.
16340 *
16341 * This routine will post the sgl pages for the IO that has the xritag
16342 * that is in the iocbq structure. The xritag is assigned during iocbq
16343 * creation and persists for as long as the driver is loaded.
16344 * if the caller has fewer than 256 scatter gather segments to map then
16345 * pdma_phys_addr1 should be 0.
16346 * If the caller needs to map more than 256 scatter gather segment then
16347 * pdma_phys_addr1 should be a valid physical address.
16348 * physical address for SGLs must be 64 byte aligned.
16349 * If you are going to map 2 SGL's then the first one must have 256 entries
16350 * the second sgl can have between 1 and 256 entries.
16351 *
16352 * Return codes:
16353 * 0 - Success
16354 * -ENXIO, -ENOMEM - Failure
16355 **/
16356int
16357lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16358 dma_addr_t pdma_phys_addr0,
16359 dma_addr_t pdma_phys_addr1,
16360 uint16_t xritag)
16361{
16362 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16363 LPFC_MBOXQ_t *mbox;
16364 int rc;
16365 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016366 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016367 union lpfc_sli4_cfg_shdr *shdr;
16368
16369 if (xritag == NO_XRI) {
16370 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16371 "0364 Invalid param:\n");
16372 return -EINVAL;
16373 }
16374
16375 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16376 if (!mbox)
16377 return -ENOMEM;
16378
16379 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16380 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16381 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016382 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016383
16384 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16385 &mbox->u.mqe.un.post_sgl_pages;
16386 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16387 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16388
16389 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16390 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16391 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16392 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16393
16394 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16395 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16396 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16397 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16398 if (!phba->sli4_hba.intr_enable)
16399 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016400 else {
James Smarta183a152011-10-10 21:32:43 -040016401 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016402 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16403 }
James Smart4f774512009-05-22 14:52:35 -040016404 /* The IOCTL status is embedded in the mailbox subheader. */
16405 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16406 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16407 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16408 if (rc != MBX_TIMEOUT)
16409 mempool_free(mbox, phba->mbox_mem_pool);
16410 if (shdr_status || shdr_add_status || rc) {
16411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16412 "2511 POST_SGL mailbox failed with "
16413 "status x%x add_status x%x, mbx status x%x\n",
16414 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016415 }
16416 return 0;
16417}
James Smart4f774512009-05-22 14:52:35 -040016418
16419/**
James Smart88a2cfb2011-07-22 18:36:33 -040016420 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016421 * @phba: pointer to lpfc hba data structure.
16422 *
16423 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016424 * HBA consistent with the SLI-4 interface spec. This routine
16425 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16426 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016427 *
James Smart88a2cfb2011-07-22 18:36:33 -040016428 * Returns
16429 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16430 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16431 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016432static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016433lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16434{
16435 unsigned long xri;
16436
16437 /*
16438 * Fetch the next logical xri. Because this index is logical,
16439 * the driver starts at 0 each time.
16440 */
16441 spin_lock_irq(&phba->hbalock);
16442 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16443 phba->sli4_hba.max_cfg_param.max_xri, 0);
16444 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16445 spin_unlock_irq(&phba->hbalock);
16446 return NO_XRI;
16447 } else {
16448 set_bit(xri, phba->sli4_hba.xri_bmask);
16449 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016450 }
James Smart6d368e52011-05-24 11:44:12 -040016451 spin_unlock_irq(&phba->hbalock);
16452 return xri;
16453}
16454
16455/**
16456 * lpfc_sli4_free_xri - Release an xri for reuse.
16457 * @phba: pointer to lpfc hba data structure.
16458 *
16459 * This routine is invoked to release an xri to the pool of
16460 * available rpis maintained by the driver.
16461 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016462static void
James Smart6d368e52011-05-24 11:44:12 -040016463__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16464{
16465 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016466 phba->sli4_hba.max_cfg_param.xri_used--;
16467 }
16468}
16469
16470/**
16471 * lpfc_sli4_free_xri - Release an xri for reuse.
16472 * @phba: pointer to lpfc hba data structure.
16473 *
16474 * This routine is invoked to release an xri to the pool of
16475 * available rpis maintained by the driver.
16476 **/
16477void
16478lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16479{
16480 spin_lock_irq(&phba->hbalock);
16481 __lpfc_sli4_free_xri(phba, xri);
16482 spin_unlock_irq(&phba->hbalock);
16483}
16484
16485/**
James Smart4f774512009-05-22 14:52:35 -040016486 * lpfc_sli4_next_xritag - Get an xritag for the io
16487 * @phba: Pointer to HBA context object.
16488 *
16489 * This function gets an xritag for the iocb. If there is no unused xritag
16490 * it will return 0xffff.
16491 * The function returns the allocated xritag if successful, else returns zero.
16492 * Zero is not a valid xritag.
16493 * The caller is not required to hold any lock.
16494 **/
16495uint16_t
16496lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16497{
James Smart6d368e52011-05-24 11:44:12 -040016498 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016499
James Smart6d368e52011-05-24 11:44:12 -040016500 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016501 if (xri_index == NO_XRI)
16502 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16503 "2004 Failed to allocate XRI.last XRITAG is %d"
16504 " Max XRI is %d, Used XRI is %d\n",
16505 xri_index,
16506 phba->sli4_hba.max_cfg_param.max_xri,
16507 phba->sli4_hba.max_cfg_param.xri_used);
16508 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016509}
16510
16511/**
James Smart895427b2017-02-12 13:52:30 -080016512 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016513 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016514 * @post_sgl_list: pointer to els sgl entry list.
16515 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016516 *
16517 * This routine is invoked to post a block of driver's sgl pages to the
16518 * HBA using non-embedded mailbox command. No Lock is held. This routine
16519 * is only called when the driver is loading and after all IO has been
16520 * stopped.
16521 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016522static int
James Smart895427b2017-02-12 13:52:30 -080016523lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016524 struct list_head *post_sgl_list,
16525 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016526{
James Smart8a9d2e82012-05-09 21:16:12 -040016527 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016528 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16529 struct sgl_page_pairs *sgl_pg_pairs;
16530 void *viraddr;
16531 LPFC_MBOXQ_t *mbox;
16532 uint32_t reqlen, alloclen, pg_pairs;
16533 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016534 uint16_t xritag_start = 0;
16535 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016536 uint32_t shdr_status, shdr_add_status;
16537 union lpfc_sli4_cfg_shdr *shdr;
16538
James Smart895427b2017-02-12 13:52:30 -080016539 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016540 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016541 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016543 "2559 Block sgl registration required DMA "
16544 "size (%d) great than a page\n", reqlen);
16545 return -ENOMEM;
16546 }
James Smart895427b2017-02-12 13:52:30 -080016547
James Smart4f774512009-05-22 14:52:35 -040016548 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016549 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016550 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016551
16552 /* Allocate DMA memory and set up the non-embedded mailbox command */
16553 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16554 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16555 LPFC_SLI4_MBX_NEMBED);
16556
16557 if (alloclen < reqlen) {
16558 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16559 "0285 Allocated DMA memory size (%d) is "
16560 "less than the requested DMA memory "
16561 "size (%d)\n", alloclen, reqlen);
16562 lpfc_sli4_mbox_cmd_free(phba, mbox);
16563 return -ENOMEM;
16564 }
James Smart4f774512009-05-22 14:52:35 -040016565 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016566 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016567 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16568 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16569
James Smart8a9d2e82012-05-09 21:16:12 -040016570 pg_pairs = 0;
16571 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016572 /* Set up the sge entry */
16573 sgl_pg_pairs->sgl_pg0_addr_lo =
16574 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16575 sgl_pg_pairs->sgl_pg0_addr_hi =
16576 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16577 sgl_pg_pairs->sgl_pg1_addr_lo =
16578 cpu_to_le32(putPaddrLow(0));
16579 sgl_pg_pairs->sgl_pg1_addr_hi =
16580 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016581
James Smart4f774512009-05-22 14:52:35 -040016582 /* Keep the first xritag on the list */
16583 if (pg_pairs == 0)
16584 xritag_start = sglq_entry->sli4_xritag;
16585 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016586 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016587 }
James Smart6d368e52011-05-24 11:44:12 -040016588
16589 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016590 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016591 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016592 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016593
James Smart4f774512009-05-22 14:52:35 -040016594 if (!phba->sli4_hba.intr_enable)
16595 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16596 else {
James Smarta183a152011-10-10 21:32:43 -040016597 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016598 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16599 }
16600 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16601 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16602 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16603 if (rc != MBX_TIMEOUT)
16604 lpfc_sli4_mbox_cmd_free(phba, mbox);
16605 if (shdr_status || shdr_add_status || rc) {
16606 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16607 "2513 POST_SGL_BLOCK mailbox command failed "
16608 "status x%x add_status x%x mbx status x%x\n",
16609 shdr_status, shdr_add_status, rc);
16610 rc = -ENXIO;
16611 }
16612 return rc;
16613}
16614
16615/**
James Smart5e5b5112019-01-28 11:14:22 -080016616 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
James Smart4f774512009-05-22 14:52:35 -040016617 * @phba: pointer to lpfc hba data structure.
James Smart0794d602019-01-28 11:14:19 -080016618 * @nblist: pointer to nvme buffer list.
James Smart4f774512009-05-22 14:52:35 -040016619 * @count: number of scsi buffers on the list.
16620 *
16621 * This routine is invoked to post a block of @count scsi sgl pages from a
James Smart0794d602019-01-28 11:14:19 -080016622 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
James Smart4f774512009-05-22 14:52:35 -040016623 * No Lock is held.
16624 *
16625 **/
James Smart0794d602019-01-28 11:14:19 -080016626static int
James Smart5e5b5112019-01-28 11:14:22 -080016627lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
16628 int count)
James Smart4f774512009-05-22 14:52:35 -040016629{
James Smart0794d602019-01-28 11:14:19 -080016630 struct lpfc_nvme_buf *lpfc_ncmd;
James Smart4f774512009-05-22 14:52:35 -040016631 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16632 struct sgl_page_pairs *sgl_pg_pairs;
16633 void *viraddr;
16634 LPFC_MBOXQ_t *mbox;
16635 uint32_t reqlen, alloclen, pg_pairs;
16636 uint32_t mbox_tmo;
16637 uint16_t xritag_start = 0;
16638 int rc = 0;
16639 uint32_t shdr_status, shdr_add_status;
16640 dma_addr_t pdma_phys_bpl1;
16641 union lpfc_sli4_cfg_shdr *shdr;
16642
16643 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016644 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016645 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016646 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016647 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016648 "6118 Block sgl registration required DMA "
James Smart4f774512009-05-22 14:52:35 -040016649 "size (%d) great than a page\n", reqlen);
16650 return -ENOMEM;
16651 }
16652 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16653 if (!mbox) {
16654 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016655 "6119 Failed to allocate mbox cmd memory\n");
James Smart4f774512009-05-22 14:52:35 -040016656 return -ENOMEM;
16657 }
16658
16659 /* Allocate DMA memory and set up the non-embedded mailbox command */
16660 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
James Smart0794d602019-01-28 11:14:19 -080016661 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16662 reqlen, LPFC_SLI4_MBX_NEMBED);
James Smart4f774512009-05-22 14:52:35 -040016663
16664 if (alloclen < reqlen) {
16665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080016666 "6120 Allocated DMA memory size (%d) is "
James Smart4f774512009-05-22 14:52:35 -040016667 "less than the requested DMA memory "
16668 "size (%d)\n", alloclen, reqlen);
16669 lpfc_sli4_mbox_cmd_free(phba, mbox);
16670 return -ENOMEM;
16671 }
James Smart6d368e52011-05-24 11:44:12 -040016672
James Smart4f774512009-05-22 14:52:35 -040016673 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016674 viraddr = mbox->sge_array->addr[0];
16675
16676 /* Set up the SGL pages in the non-embedded DMA pages */
16677 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16678 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16679
16680 pg_pairs = 0;
James Smart0794d602019-01-28 11:14:19 -080016681 list_for_each_entry(lpfc_ncmd, nblist, list) {
James Smart4f774512009-05-22 14:52:35 -040016682 /* Set up the sge entry */
16683 sgl_pg_pairs->sgl_pg0_addr_lo =
James Smart0794d602019-01-28 11:14:19 -080016684 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040016685 sgl_pg_pairs->sgl_pg0_addr_hi =
James Smart0794d602019-01-28 11:14:19 -080016686 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040016687 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
James Smart0794d602019-01-28 11:14:19 -080016688 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
16689 SGL_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040016690 else
16691 pdma_phys_bpl1 = 0;
16692 sgl_pg_pairs->sgl_pg1_addr_lo =
16693 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16694 sgl_pg_pairs->sgl_pg1_addr_hi =
16695 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16696 /* Keep the first xritag on the list */
16697 if (pg_pairs == 0)
James Smart0794d602019-01-28 11:14:19 -080016698 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040016699 sgl_pg_pairs++;
16700 pg_pairs++;
16701 }
16702 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16703 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16704 /* Perform endian conversion if necessary */
16705 sgl->word0 = cpu_to_le32(sgl->word0);
16706
James Smart0794d602019-01-28 11:14:19 -080016707 if (!phba->sli4_hba.intr_enable) {
James Smart4f774512009-05-22 14:52:35 -040016708 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart0794d602019-01-28 11:14:19 -080016709 } else {
James Smarta183a152011-10-10 21:32:43 -040016710 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016711 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16712 }
James Smart0794d602019-01-28 11:14:19 -080016713 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016714 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16715 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16716 if (rc != MBX_TIMEOUT)
16717 lpfc_sli4_mbox_cmd_free(phba, mbox);
16718 if (shdr_status || shdr_add_status || rc) {
16719 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -080016720 "6125 POST_SGL_BLOCK mailbox command failed "
James Smart4f774512009-05-22 14:52:35 -040016721 "status x%x add_status x%x mbx status x%x\n",
16722 shdr_status, shdr_add_status, rc);
16723 rc = -ENXIO;
16724 }
16725 return rc;
16726}
16727
16728/**
James Smart5e5b5112019-01-28 11:14:22 -080016729 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
James Smart0794d602019-01-28 11:14:19 -080016730 * @phba: pointer to lpfc hba data structure.
16731 * @post_nblist: pointer to the nvme buffer list.
16732 *
16733 * This routine walks a list of nvme buffers that was passed in. It attempts
16734 * to construct blocks of nvme buffer sgls which contains contiguous xris and
16735 * uses the non-embedded SGL block post mailbox commands to post to the port.
16736 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
16737 * embedded SGL post mailbox command for posting. The @post_nblist passed in
16738 * must be local list, thus no lock is needed when manipulate the list.
16739 *
16740 * Returns: 0 = failure, non-zero number of successfully posted buffers.
16741 **/
16742int
James Smart5e5b5112019-01-28 11:14:22 -080016743lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
16744 struct list_head *post_nblist, int sb_count)
James Smart0794d602019-01-28 11:14:19 -080016745{
16746 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
16747 int status, sgl_size;
16748 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
16749 dma_addr_t pdma_phys_sgl1;
16750 int last_xritag = NO_XRI;
16751 int cur_xritag;
James Smart0794d602019-01-28 11:14:19 -080016752 LIST_HEAD(prep_nblist);
16753 LIST_HEAD(blck_nblist);
16754 LIST_HEAD(nvme_nblist);
16755
16756 /* sanity check */
16757 if (sb_count <= 0)
16758 return -EINVAL;
16759
16760 sgl_size = phba->cfg_sg_dma_buf_size;
16761 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
16762 list_del_init(&lpfc_ncmd->list);
16763 block_cnt++;
16764 if ((last_xritag != NO_XRI) &&
16765 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
16766 /* a hole in xri block, form a sgl posting block */
16767 list_splice_init(&prep_nblist, &blck_nblist);
16768 post_cnt = block_cnt - 1;
16769 /* prepare list for next posting block */
16770 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16771 block_cnt = 1;
16772 } else {
16773 /* prepare list for next posting block */
16774 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16775 /* enough sgls for non-embed sgl mbox command */
16776 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
16777 list_splice_init(&prep_nblist, &blck_nblist);
16778 post_cnt = block_cnt;
16779 block_cnt = 0;
16780 }
16781 }
16782 num_posting++;
16783 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16784
16785 /* end of repost sgl list condition for NVME buffers */
16786 if (num_posting == sb_count) {
16787 if (post_cnt == 0) {
16788 /* last sgl posting block */
16789 list_splice_init(&prep_nblist, &blck_nblist);
16790 post_cnt = block_cnt;
16791 } else if (block_cnt == 1) {
16792 /* last single sgl with non-contiguous xri */
16793 if (sgl_size > SGL_PAGE_SIZE)
16794 pdma_phys_sgl1 =
16795 lpfc_ncmd->dma_phys_sgl +
16796 SGL_PAGE_SIZE;
16797 else
16798 pdma_phys_sgl1 = 0;
16799 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16800 status = lpfc_sli4_post_sgl(
16801 phba, lpfc_ncmd->dma_phys_sgl,
16802 pdma_phys_sgl1, cur_xritag);
16803 if (status) {
16804 /* failure, put on abort nvme list */
16805 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
16806 } else {
16807 /* success, put on NVME buffer list */
16808 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
16809 lpfc_ncmd->status = IOSTAT_SUCCESS;
16810 num_posted++;
16811 }
16812 /* success, put on NVME buffer sgl list */
16813 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16814 }
16815 }
16816
16817 /* continue until a nembed page worth of sgls */
16818 if (post_cnt == 0)
16819 continue;
16820
16821 /* post block of NVME buffer list sgls */
James Smart5e5b5112019-01-28 11:14:22 -080016822 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
16823 post_cnt);
James Smart0794d602019-01-28 11:14:19 -080016824
16825 /* don't reset xirtag due to hole in xri block */
16826 if (block_cnt == 0)
16827 last_xritag = NO_XRI;
16828
16829 /* reset NVME buffer post count for next round of posting */
16830 post_cnt = 0;
16831
16832 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
16833 while (!list_empty(&blck_nblist)) {
16834 list_remove_head(&blck_nblist, lpfc_ncmd,
16835 struct lpfc_nvme_buf, list);
16836 if (status) {
16837 /* failure, put on abort nvme list */
16838 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
16839 } else {
16840 /* success, put on NVME buffer list */
16841 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
16842 lpfc_ncmd->status = IOSTAT_SUCCESS;
16843 num_posted++;
16844 }
16845 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16846 }
16847 }
16848 /* Push NVME buffers with sgl posted to the available list */
James Smart5e5b5112019-01-28 11:14:22 -080016849 lpfc_io_buf_replenish(phba, &nvme_nblist);
16850
James Smart0794d602019-01-28 11:14:19 -080016851 return num_posted;
16852}
16853
16854/**
James Smart4f774512009-05-22 14:52:35 -040016855 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16856 * @phba: pointer to lpfc_hba struct that the frame was received on
16857 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16858 *
16859 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16860 * valid type of frame that the LPFC driver will handle. This function will
16861 * return a zero if the frame is a valid frame or a non zero value when the
16862 * frame does not pass the check.
16863 **/
16864static int
16865lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16866{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016867 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016868 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016869 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016870
16871 switch (fc_hdr->fh_r_ctl) {
16872 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16873 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16874 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16875 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16876 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16877 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16878 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16879 case FC_RCTL_DD_CMD_STATUS: /* command status */
16880 case FC_RCTL_ELS_REQ: /* extended link services request */
16881 case FC_RCTL_ELS_REP: /* extended link services reply */
16882 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16883 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16884 case FC_RCTL_BA_NOP: /* basic link service NOP */
16885 case FC_RCTL_BA_ABTS: /* basic link service abort */
16886 case FC_RCTL_BA_RMC: /* remove connection */
16887 case FC_RCTL_BA_ACC: /* basic accept */
16888 case FC_RCTL_BA_RJT: /* basic reject */
16889 case FC_RCTL_BA_PRMT:
16890 case FC_RCTL_ACK_1: /* acknowledge_1 */
16891 case FC_RCTL_ACK_0: /* acknowledge_0 */
16892 case FC_RCTL_P_RJT: /* port reject */
16893 case FC_RCTL_F_RJT: /* fabric reject */
16894 case FC_RCTL_P_BSY: /* port busy */
16895 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16896 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16897 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016898 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016899 case FC_RCTL_END: /* end */
16900 break;
16901 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16902 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16903 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16904 return lpfc_fc_frame_check(phba, fc_hdr);
16905 default:
16906 goto drop;
16907 }
James Smartae9e28f2017-05-15 15:20:51 -070016908
James Smart4f774512009-05-22 14:52:35 -040016909 switch (fc_hdr->fh_type) {
16910 case FC_TYPE_BLS:
16911 case FC_TYPE_ELS:
16912 case FC_TYPE_FCP:
16913 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016914 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040016915 break;
16916 case FC_TYPE_IP:
16917 case FC_TYPE_ILS:
16918 default:
16919 goto drop;
16920 }
James Smart546fc852011-03-11 16:06:29 -050016921
James Smart4f774512009-05-22 14:52:35 -040016922 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016923 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016924 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016925 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16926 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16927 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16928 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16929 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016930 return 0;
16931drop:
16932 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016933 "2539 Dropped frame rctl:x%x type:x%x\n",
16934 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016935 return 1;
16936}
16937
16938/**
16939 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16940 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16941 *
16942 * This function processes the FC header to retrieve the VFI from the VF
16943 * header, if one exists. This function will return the VFI if one exists
16944 * or 0 if no VSAN Header exists.
16945 **/
16946static uint32_t
16947lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16948{
16949 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16950
16951 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16952 return 0;
16953 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16954}
16955
16956/**
16957 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16958 * @phba: Pointer to the HBA structure to search for the vport on
16959 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16960 * @fcfi: The FC Fabric ID that the frame came from
16961 *
16962 * This function searches the @phba for a vport that matches the content of the
16963 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16964 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16965 * returns the matching vport pointer or NULL if unable to match frame to a
16966 * vport.
16967 **/
16968static struct lpfc_vport *
16969lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016970 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016971{
16972 struct lpfc_vport **vports;
16973 struct lpfc_vport *vport = NULL;
16974 int i;
James Smart939723a2012-05-09 21:19:03 -040016975
James Smartbf086112011-08-21 21:48:13 -040016976 if (did == Fabric_DID)
16977 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016978 if ((phba->pport->fc_flag & FC_PT2PT) &&
16979 !(phba->link_state == LPFC_HBA_READY))
16980 return phba->pport;
16981
James Smart4f774512009-05-22 14:52:35 -040016982 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016983 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016984 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16985 if (phba->fcf.fcfi == fcfi &&
16986 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16987 vports[i]->fc_myDID == did) {
16988 vport = vports[i];
16989 break;
16990 }
16991 }
James Smart895427b2017-02-12 13:52:30 -080016992 }
James Smart4f774512009-05-22 14:52:35 -040016993 lpfc_destroy_vport_work_array(phba, vports);
16994 return vport;
16995}
16996
16997/**
James Smart45ed1192009-10-02 15:17:02 -040016998 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16999 * @vport: The vport to work on.
17000 *
17001 * This function updates the receive sequence time stamp for this vport. The
17002 * receive sequence time stamp indicates the time that the last frame of the
17003 * the sequence that has been idle for the longest amount of time was received.
17004 * the driver uses this time stamp to indicate if any received sequences have
17005 * timed out.
17006 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017007static void
James Smart45ed1192009-10-02 15:17:02 -040017008lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17009{
17010 struct lpfc_dmabuf *h_buf;
17011 struct hbq_dmabuf *dmabuf = NULL;
17012
17013 /* get the oldest sequence on the rcv list */
17014 h_buf = list_get_first(&vport->rcv_buffer_list,
17015 struct lpfc_dmabuf, list);
17016 if (!h_buf)
17017 return;
17018 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17019 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17020}
17021
17022/**
17023 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17024 * @vport: The vport that the received sequences were sent to.
17025 *
17026 * This function cleans up all outstanding received sequences. This is called
17027 * by the driver when a link event or user action invalidates all the received
17028 * sequences.
17029 **/
17030void
17031lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17032{
17033 struct lpfc_dmabuf *h_buf, *hnext;
17034 struct lpfc_dmabuf *d_buf, *dnext;
17035 struct hbq_dmabuf *dmabuf = NULL;
17036
17037 /* start with the oldest sequence on the rcv list */
17038 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17039 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17040 list_del_init(&dmabuf->hbuf.list);
17041 list_for_each_entry_safe(d_buf, dnext,
17042 &dmabuf->dbuf.list, list) {
17043 list_del_init(&d_buf->list);
17044 lpfc_in_buf_free(vport->phba, d_buf);
17045 }
17046 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17047 }
17048}
17049
17050/**
17051 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17052 * @vport: The vport that the received sequences were sent to.
17053 *
17054 * This function determines whether any received sequences have timed out by
17055 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17056 * indicates that there is at least one timed out sequence this routine will
17057 * go through the received sequences one at a time from most inactive to most
17058 * active to determine which ones need to be cleaned up. Once it has determined
17059 * that a sequence needs to be cleaned up it will simply free up the resources
17060 * without sending an abort.
17061 **/
17062void
17063lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17064{
17065 struct lpfc_dmabuf *h_buf, *hnext;
17066 struct lpfc_dmabuf *d_buf, *dnext;
17067 struct hbq_dmabuf *dmabuf = NULL;
17068 unsigned long timeout;
17069 int abort_count = 0;
17070
17071 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17072 vport->rcv_buffer_time_stamp);
17073 if (list_empty(&vport->rcv_buffer_list) ||
17074 time_before(jiffies, timeout))
17075 return;
17076 /* start with the oldest sequence on the rcv list */
17077 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17078 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17079 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17080 dmabuf->time_stamp);
17081 if (time_before(jiffies, timeout))
17082 break;
17083 abort_count++;
17084 list_del_init(&dmabuf->hbuf.list);
17085 list_for_each_entry_safe(d_buf, dnext,
17086 &dmabuf->dbuf.list, list) {
17087 list_del_init(&d_buf->list);
17088 lpfc_in_buf_free(vport->phba, d_buf);
17089 }
17090 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17091 }
17092 if (abort_count)
17093 lpfc_update_rcv_time_stamp(vport);
17094}
17095
17096/**
James Smart4f774512009-05-22 14:52:35 -040017097 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17098 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17099 *
17100 * This function searches through the existing incomplete sequences that have
17101 * been sent to this @vport. If the frame matches one of the incomplete
17102 * sequences then the dbuf in the @dmabuf is added to the list of frames that
17103 * make up that sequence. If no sequence is found that matches this frame then
17104 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17105 * This function returns a pointer to the first dmabuf in the sequence list that
17106 * the frame was linked to.
17107 **/
17108static struct hbq_dmabuf *
17109lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17110{
17111 struct fc_frame_header *new_hdr;
17112 struct fc_frame_header *temp_hdr;
17113 struct lpfc_dmabuf *d_buf;
17114 struct lpfc_dmabuf *h_buf;
17115 struct hbq_dmabuf *seq_dmabuf = NULL;
17116 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050017117 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040017118
James Smart4d9ab992009-10-02 15:16:39 -040017119 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017120 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040017121 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050017122
James Smart4f774512009-05-22 14:52:35 -040017123 /* Use the hdr_buf to find the sequence that this frame belongs to */
17124 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17125 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17126 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17127 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17128 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17129 continue;
17130 /* found a pending sequence that matches this frame */
17131 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17132 break;
17133 }
17134 if (!seq_dmabuf) {
17135 /*
17136 * This indicates first frame received for this sequence.
17137 * Queue the buffer on the vport's rcv_buffer_list.
17138 */
17139 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040017140 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017141 return dmabuf;
17142 }
17143 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050017144 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17145 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040017146 list_del_init(&seq_dmabuf->hbuf.list);
17147 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17148 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017149 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017150 return dmabuf;
17151 }
James Smart45ed1192009-10-02 15:17:02 -040017152 /* move this sequence to the tail to indicate a young sequence */
17153 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17154 seq_dmabuf->time_stamp = jiffies;
17155 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050017156 if (list_empty(&seq_dmabuf->dbuf.list)) {
17157 temp_hdr = dmabuf->hbuf.virt;
17158 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17159 return seq_dmabuf;
17160 }
James Smart4f774512009-05-22 14:52:35 -040017161 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050017162 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17163 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040017164 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17165 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17166 /*
17167 * If the frame's sequence count is greater than the frame on
17168 * the list then insert the frame right after this frame
17169 */
James Smarteeead812009-12-21 17:01:23 -050017170 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17171 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040017172 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050017173 found = 1;
17174 break;
James Smart4f774512009-05-22 14:52:35 -040017175 }
James Smart4360ca92015-12-16 18:12:04 -050017176
17177 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17178 break;
17179 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040017180 }
James Smart4360ca92015-12-16 18:12:04 -050017181
17182 if (found)
17183 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017184 return NULL;
17185}
17186
17187/**
James Smart6669f9b2009-10-02 15:16:45 -040017188 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17189 * @vport: pointer to a vitural port
17190 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17191 *
17192 * This function tries to abort from the partially assembed sequence, described
17193 * by the information from basic abbort @dmabuf. It checks to see whether such
17194 * partially assembled sequence held by the driver. If so, it shall free up all
17195 * the frames from the partially assembled sequence.
17196 *
17197 * Return
17198 * true -- if there is matching partially assembled sequence present and all
17199 * the frames freed with the sequence;
17200 * false -- if there is no matching partially assembled sequence present so
17201 * nothing got aborted in the lower layer driver
17202 **/
17203static bool
17204lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17205 struct hbq_dmabuf *dmabuf)
17206{
17207 struct fc_frame_header *new_hdr;
17208 struct fc_frame_header *temp_hdr;
17209 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17210 struct hbq_dmabuf *seq_dmabuf = NULL;
17211
17212 /* Use the hdr_buf to find the sequence that matches this frame */
17213 INIT_LIST_HEAD(&dmabuf->dbuf.list);
17214 INIT_LIST_HEAD(&dmabuf->hbuf.list);
17215 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17216 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17217 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17218 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17219 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17220 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17221 continue;
17222 /* found a pending sequence that matches this frame */
17223 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17224 break;
17225 }
17226
17227 /* Free up all the frames from the partially assembled sequence */
17228 if (seq_dmabuf) {
17229 list_for_each_entry_safe(d_buf, n_buf,
17230 &seq_dmabuf->dbuf.list, list) {
17231 list_del_init(&d_buf->list);
17232 lpfc_in_buf_free(vport->phba, d_buf);
17233 }
17234 return true;
17235 }
17236 return false;
17237}
17238
17239/**
James Smart6dd9e312013-01-03 15:43:37 -050017240 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
17241 * @vport: pointer to a vitural port
17242 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17243 *
17244 * This function tries to abort from the assembed sequence from upper level
17245 * protocol, described by the information from basic abbort @dmabuf. It
17246 * checks to see whether such pending context exists at upper level protocol.
17247 * If so, it shall clean up the pending context.
17248 *
17249 * Return
17250 * true -- if there is matching pending context of the sequence cleaned
17251 * at ulp;
17252 * false -- if there is no matching pending context of the sequence present
17253 * at ulp.
17254 **/
17255static bool
17256lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17257{
17258 struct lpfc_hba *phba = vport->phba;
17259 int handled;
17260
17261 /* Accepting abort at ulp with SLI4 only */
17262 if (phba->sli_rev < LPFC_SLI_REV4)
17263 return false;
17264
17265 /* Register all caring upper level protocols to attend abort */
17266 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
17267 if (handled)
17268 return true;
17269
17270 return false;
17271}
17272
17273/**
James Smart546fc852011-03-11 16:06:29 -050017274 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040017275 * @phba: Pointer to HBA context object.
17276 * @cmd_iocbq: pointer to the command iocbq structure.
17277 * @rsp_iocbq: pointer to the response iocbq structure.
17278 *
James Smart546fc852011-03-11 16:06:29 -050017279 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040017280 * event. It properly releases the memory allocated to the sequence abort
17281 * accept iocb.
17282 **/
17283static void
James Smart546fc852011-03-11 16:06:29 -050017284lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040017285 struct lpfc_iocbq *cmd_iocbq,
17286 struct lpfc_iocbq *rsp_iocbq)
17287{
James Smart6dd9e312013-01-03 15:43:37 -050017288 struct lpfc_nodelist *ndlp;
17289
17290 if (cmd_iocbq) {
17291 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17292 lpfc_nlp_put(ndlp);
17293 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017294 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050017295 }
James Smart6b5151f2012-01-18 16:24:06 -050017296
17297 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17298 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17299 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17300 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
17301 rsp_iocbq->iocb.ulpStatus,
17302 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040017303}
17304
17305/**
James Smart6d368e52011-05-24 11:44:12 -040017306 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17307 * @phba: Pointer to HBA context object.
17308 * @xri: xri id in transaction.
17309 *
17310 * This function validates the xri maps to the known range of XRIs allocated an
17311 * used by the driver.
17312 **/
James Smart7851fe22011-07-22 18:36:52 -040017313uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017314lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17315 uint16_t xri)
17316{
James Smarta2fc4aef2014-09-03 12:57:55 -040017317 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040017318
17319 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17320 if (xri == phba->sli4_hba.xri_ids[i])
17321 return i;
17322 }
17323 return NO_XRI;
17324}
17325
James Smart6d368e52011-05-24 11:44:12 -040017326/**
James Smart546fc852011-03-11 16:06:29 -050017327 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017328 * @phba: Pointer to HBA context object.
17329 * @fc_hdr: pointer to a FC frame header.
17330 *
James Smart546fc852011-03-11 16:06:29 -050017331 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017332 * event after aborting the sequence handling.
17333 **/
James Smart86c67372017-04-21 16:05:04 -070017334void
James Smart6dd9e312013-01-03 15:43:37 -050017335lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17336 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040017337{
James Smart6dd9e312013-01-03 15:43:37 -050017338 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017339 struct lpfc_iocbq *ctiocb = NULL;
17340 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017341 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017342 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017343 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017344 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017345
17346 if (!lpfc_is_link_up(phba))
17347 return;
17348
17349 sid = sli4_sid_from_fc_hdr(fc_hdr);
17350 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017351 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017352
James Smart6dd9e312013-01-03 15:43:37 -050017353 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017354 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017355 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017356 if (!ndlp) {
17357 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17358 "1268 Failed to allocate ndlp for "
17359 "oxid:x%x SID:x%x\n", oxid, sid);
17360 return;
17361 }
James Smart6dd9e312013-01-03 15:43:37 -050017362 /* Put ndlp onto pport node list */
17363 lpfc_enqueue_node(vport, ndlp);
17364 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17365 /* re-setup ndlp without removing from node list */
17366 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17367 if (!ndlp) {
17368 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17369 "3275 Failed to active ndlp found "
17370 "for oxid:x%x SID:x%x\n", oxid, sid);
17371 return;
17372 }
James Smart6669f9b2009-10-02 15:16:45 -040017373 }
17374
James Smart546fc852011-03-11 16:06:29 -050017375 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017376 ctiocb = lpfc_sli_get_iocbq(phba);
17377 if (!ctiocb)
17378 return;
17379
James Smart5ffc2662009-11-18 15:39:44 -050017380 /* Extract the F_CTL field from FC_HDR */
17381 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17382
James Smart6669f9b2009-10-02 15:16:45 -040017383 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017384 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017385 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017386 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17387 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17388 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17389
17390 /* Fill in the rest of iocb fields */
17391 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17392 icmd->ulpBdeCount = 0;
17393 icmd->ulpLe = 1;
17394 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017395 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017396 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017397
James Smart6669f9b2009-10-02 15:16:45 -040017398 ctiocb->iocb_cmpl = NULL;
17399 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017400 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017401 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017402 ctiocb->sli4_xritag = NO_XRI;
17403
James Smartee0f4fe2012-05-09 21:19:14 -040017404 if (fctl & FC_FC_EX_CTX)
17405 /* Exchange responder sent the abort so we
17406 * own the oxid.
17407 */
17408 xri = oxid;
17409 else
17410 xri = rxid;
17411 lxri = lpfc_sli4_xri_inrange(phba, xri);
17412 if (lxri != NO_XRI)
17413 lpfc_set_rrq_active(phba, ndlp, lxri,
17414 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017415 /* For BA_ABTS from exchange responder, if the logical xri with
17416 * the oxid maps to the FCP XRI range, the port no longer has
17417 * that exchange context, send a BLS_RJT. Override the IOCB for
17418 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017419 */
James Smart6dd9e312013-01-03 15:43:37 -050017420 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017421 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050017422 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17423 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17424 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17425 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17426 }
17427
17428 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17429 * the driver no longer has that exchange, send a BLS_RJT. Override
17430 * the IOCB for a BA_RJT.
17431 */
17432 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017433 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17434 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17435 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17436 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17437 }
James Smart6669f9b2009-10-02 15:16:45 -040017438
James Smart5ffc2662009-11-18 15:39:44 -050017439 if (fctl & FC_FC_EX_CTX) {
17440 /* ABTS sent by responder to CT exchange, construction
17441 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17442 * field and RX_ID from ABTS for RX_ID field.
17443 */
James Smart546fc852011-03-11 16:06:29 -050017444 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017445 } else {
17446 /* ABTS sent by initiator to CT exchange, construction
17447 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017448 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017449 */
James Smart546fc852011-03-11 16:06:29 -050017450 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017451 }
James Smartf09c3ac2012-03-01 22:33:29 -050017452 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017453 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017454
James Smart546fc852011-03-11 16:06:29 -050017455 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017456 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17457 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17458 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017459
17460 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17461 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017462 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17463 "2925 Failed to issue CT ABTS RSP x%x on "
17464 "xri x%x, Data x%x\n",
17465 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17466 phba->link_state);
17467 lpfc_nlp_put(ndlp);
17468 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017469 lpfc_sli_release_iocbq(phba, ctiocb);
17470 }
James Smart6669f9b2009-10-02 15:16:45 -040017471}
17472
17473/**
17474 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17475 * @vport: Pointer to the vport on which this sequence was received
17476 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17477 *
17478 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17479 * receive sequence is only partially assembed by the driver, it shall abort
17480 * the partially assembled frames for the sequence. Otherwise, if the
17481 * unsolicited receive sequence has been completely assembled and passed to
17482 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17483 * unsolicited sequence has been aborted. After that, it will issue a basic
17484 * accept to accept the abort.
17485 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017486static void
James Smart6669f9b2009-10-02 15:16:45 -040017487lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17488 struct hbq_dmabuf *dmabuf)
17489{
17490 struct lpfc_hba *phba = vport->phba;
17491 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017492 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017493 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017494
James Smart6669f9b2009-10-02 15:16:45 -040017495 /* Make a copy of fc_hdr before the dmabuf being released */
17496 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017497 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017498
James Smart5ffc2662009-11-18 15:39:44 -050017499 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017500 /* ABTS by responder to exchange, no cleanup needed */
17501 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017502 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017503 /* ABTS by initiator to exchange, need to do cleanup */
17504 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17505 if (aborted == false)
17506 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017507 }
James Smart6dd9e312013-01-03 15:43:37 -050017508 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17509
James Smart86c67372017-04-21 16:05:04 -070017510 if (phba->nvmet_support) {
17511 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17512 return;
17513 }
17514
James Smart6dd9e312013-01-03 15:43:37 -050017515 /* Respond with BA_ACC or BA_RJT accordingly */
17516 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017517}
17518
17519/**
James Smart4f774512009-05-22 14:52:35 -040017520 * lpfc_seq_complete - Indicates if a sequence is complete
17521 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17522 *
17523 * This function checks the sequence, starting with the frame described by
17524 * @dmabuf, to see if all the frames associated with this sequence are present.
17525 * the frames associated with this sequence are linked to the @dmabuf using the
17526 * dbuf list. This function looks for two major things. 1) That the first frame
17527 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17528 * set. 3) That there are no holes in the sequence count. The function will
17529 * return 1 when the sequence is complete, otherwise it will return 0.
17530 **/
17531static int
17532lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17533{
17534 struct fc_frame_header *hdr;
17535 struct lpfc_dmabuf *d_buf;
17536 struct hbq_dmabuf *seq_dmabuf;
17537 uint32_t fctl;
17538 int seq_count = 0;
17539
17540 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17541 /* make sure first fame of sequence has a sequence count of zero */
17542 if (hdr->fh_seq_cnt != seq_count)
17543 return 0;
17544 fctl = (hdr->fh_f_ctl[0] << 16 |
17545 hdr->fh_f_ctl[1] << 8 |
17546 hdr->fh_f_ctl[2]);
17547 /* If last frame of sequence we can return success. */
17548 if (fctl & FC_FC_END_SEQ)
17549 return 1;
17550 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17551 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17552 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17553 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017554 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017555 return 0;
17556 fctl = (hdr->fh_f_ctl[0] << 16 |
17557 hdr->fh_f_ctl[1] << 8 |
17558 hdr->fh_f_ctl[2]);
17559 /* If last frame of sequence we can return success. */
17560 if (fctl & FC_FC_END_SEQ)
17561 return 1;
17562 }
17563 return 0;
17564}
17565
17566/**
17567 * lpfc_prep_seq - Prep sequence for ULP processing
17568 * @vport: Pointer to the vport on which this sequence was received
17569 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17570 *
17571 * This function takes a sequence, described by a list of frames, and creates
17572 * a list of iocbq structures to describe the sequence. This iocbq list will be
17573 * used to issue to the generic unsolicited sequence handler. This routine
17574 * returns a pointer to the first iocbq in the list. If the function is unable
17575 * to allocate an iocbq then it throw out the received frames that were not
17576 * able to be described and return a pointer to the first iocbq. If unable to
17577 * allocate any iocbqs (including the first) this function will return NULL.
17578 **/
17579static struct lpfc_iocbq *
17580lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17581{
James Smart7851fe22011-07-22 18:36:52 -040017582 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017583 struct lpfc_dmabuf *d_buf, *n_buf;
17584 struct lpfc_iocbq *first_iocbq, *iocbq;
17585 struct fc_frame_header *fc_hdr;
17586 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017587 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017588 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017589
17590 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17591 /* remove from receive buffer list */
17592 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017593 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017594 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017595 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017596 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017597 /* Get an iocbq struct to fill in. */
17598 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17599 if (first_iocbq) {
17600 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017601 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017602 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017603 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017604
17605 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17606 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17607 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17608 first_iocbq->iocb.un.rcvels.parmRo =
17609 sli4_did_from_fc_hdr(fc_hdr);
17610 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17611 } else
17612 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017613 first_iocbq->iocb.ulpContext = NO_XRI;
17614 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17615 be16_to_cpu(fc_hdr->fh_ox_id);
17616 /* iocbq is prepped for internal consumption. Physical vpi. */
17617 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17618 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017619 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017620 tot_len = bf_get(lpfc_rcqe_length,
17621 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17622
James Smart4f774512009-05-22 14:52:35 -040017623 first_iocbq->context2 = &seq_dmabuf->dbuf;
17624 first_iocbq->context3 = NULL;
17625 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017626 if (tot_len > LPFC_DATA_BUF_SIZE)
17627 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017628 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017629 else
17630 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17631
James Smart4f774512009-05-22 14:52:35 -040017632 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017633
James Smart7851fe22011-07-22 18:36:52 -040017634 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017635 }
17636 iocbq = first_iocbq;
17637 /*
17638 * Each IOCBq can have two Buffers assigned, so go through the list
17639 * of buffers for this sequence and save two buffers in each IOCBq
17640 */
17641 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17642 if (!iocbq) {
17643 lpfc_in_buf_free(vport->phba, d_buf);
17644 continue;
17645 }
17646 if (!iocbq->context3) {
17647 iocbq->context3 = d_buf;
17648 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017649 /* We need to get the size out of the right CQE */
17650 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17651 len = bf_get(lpfc_rcqe_length,
17652 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017653 pbde = (struct ulp_bde64 *)
17654 &iocbq->iocb.unsli3.sli3Words[4];
17655 if (len > LPFC_DATA_BUF_SIZE)
17656 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17657 else
17658 pbde->tus.f.bdeSize = len;
17659
James Smart7851fe22011-07-22 18:36:52 -040017660 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17661 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017662 } else {
17663 iocbq = lpfc_sli_get_iocbq(vport->phba);
17664 if (!iocbq) {
17665 if (first_iocbq) {
17666 first_iocbq->iocb.ulpStatus =
17667 IOSTAT_FCP_RSP_ERROR;
17668 first_iocbq->iocb.un.ulpWord[4] =
17669 IOERR_NO_RESOURCES;
17670 }
17671 lpfc_in_buf_free(vport->phba, d_buf);
17672 continue;
17673 }
James Smart7851fe22011-07-22 18:36:52 -040017674 /* We need to get the size out of the right CQE */
17675 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17676 len = bf_get(lpfc_rcqe_length,
17677 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017678 iocbq->context2 = d_buf;
17679 iocbq->context3 = NULL;
17680 iocbq->iocb.ulpBdeCount = 1;
17681 if (len > LPFC_DATA_BUF_SIZE)
17682 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17683 LPFC_DATA_BUF_SIZE;
17684 else
17685 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17686
James Smart7851fe22011-07-22 18:36:52 -040017687 tot_len += len;
17688 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17689
James Smart4f774512009-05-22 14:52:35 -040017690 iocbq->iocb.un.rcvels.remoteID = sid;
17691 list_add_tail(&iocbq->list, &first_iocbq->list);
17692 }
17693 }
17694 return first_iocbq;
17695}
17696
James Smart6669f9b2009-10-02 15:16:45 -040017697static void
17698lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17699 struct hbq_dmabuf *seq_dmabuf)
17700{
17701 struct fc_frame_header *fc_hdr;
17702 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17703 struct lpfc_hba *phba = vport->phba;
17704
17705 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17706 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17707 if (!iocbq) {
17708 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17709 "2707 Ring %d handler: Failed to allocate "
17710 "iocb Rctl x%x Type x%x received\n",
17711 LPFC_ELS_RING,
17712 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17713 return;
17714 }
17715 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017716 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017717 iocbq, fc_hdr->fh_r_ctl,
17718 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017719 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017720 "2540 Ring %d handler: unexpected Rctl "
17721 "x%x Type x%x received\n",
17722 LPFC_ELS_RING,
17723 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17724
17725 /* Free iocb created in lpfc_prep_seq */
17726 list_for_each_entry_safe(curr_iocb, next_iocb,
17727 &iocbq->list, list) {
17728 list_del_init(&curr_iocb->list);
17729 lpfc_sli_release_iocbq(phba, curr_iocb);
17730 }
17731 lpfc_sli_release_iocbq(phba, iocbq);
17732}
17733
James Smartae9e28f2017-05-15 15:20:51 -070017734static void
17735lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17736 struct lpfc_iocbq *rspiocb)
17737{
17738 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17739
17740 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017741 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017742 kfree(pcmd);
17743 lpfc_sli_release_iocbq(phba, cmdiocb);
James Smarte817e5d2018-12-13 15:17:53 -080017744 lpfc_drain_txq(phba);
James Smartae9e28f2017-05-15 15:20:51 -070017745}
17746
17747static void
17748lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17749 struct hbq_dmabuf *dmabuf)
17750{
17751 struct fc_frame_header *fc_hdr;
17752 struct lpfc_hba *phba = vport->phba;
17753 struct lpfc_iocbq *iocbq = NULL;
17754 union lpfc_wqe *wqe;
17755 struct lpfc_dmabuf *pcmd = NULL;
17756 uint32_t frame_len;
17757 int rc;
James Smarte817e5d2018-12-13 15:17:53 -080017758 unsigned long iflags;
James Smartae9e28f2017-05-15 15:20:51 -070017759
17760 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17761 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17762
17763 /* Send the received frame back */
17764 iocbq = lpfc_sli_get_iocbq(phba);
James Smarte817e5d2018-12-13 15:17:53 -080017765 if (!iocbq) {
17766 /* Queue cq event and wakeup worker thread to process it */
17767 spin_lock_irqsave(&phba->hbalock, iflags);
17768 list_add_tail(&dmabuf->cq_event.list,
17769 &phba->sli4_hba.sp_queue_event);
17770 phba->hba_flag |= HBA_SP_QUEUE_EVT;
17771 spin_unlock_irqrestore(&phba->hbalock, iflags);
17772 lpfc_worker_wake_up(phba);
17773 return;
17774 }
James Smartae9e28f2017-05-15 15:20:51 -070017775
17776 /* Allocate buffer for command payload */
17777 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17778 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017779 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017780 &pcmd->phys);
17781 if (!pcmd || !pcmd->virt)
17782 goto exit;
17783
17784 INIT_LIST_HEAD(&pcmd->list);
17785
17786 /* copyin the payload */
17787 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17788
17789 /* fill in BDE's for command */
17790 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17791 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17792 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17793 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17794
17795 iocbq->context2 = pcmd;
17796 iocbq->vport = vport;
17797 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17798 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17799
17800 /*
17801 * Setup rest of the iocb as though it were a WQE
17802 * Build the SEND_FRAME WQE
17803 */
17804 wqe = (union lpfc_wqe *)&iocbq->iocb;
17805
17806 wqe->send_frame.frame_len = frame_len;
17807 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17808 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17809 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17810 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17811 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17812 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17813
17814 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17815 iocbq->iocb.ulpLe = 1;
17816 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17817 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17818 if (rc == IOCB_ERROR)
17819 goto exit;
17820
17821 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17822 return;
17823
17824exit:
17825 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17826 "2023 Unable to process MDS loopback frame\n");
17827 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017828 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017829 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017830 if (iocbq)
17831 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017832 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17833}
17834
James Smart4f774512009-05-22 14:52:35 -040017835/**
17836 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17837 * @phba: Pointer to HBA context object.
17838 *
17839 * This function is called with no lock held. This function processes all
17840 * the received buffers and gives it to upper layers when a received buffer
17841 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017842 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017843 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17844 * appropriate receive function when the final frame in a sequence is received.
17845 **/
James Smart4d9ab992009-10-02 15:16:39 -040017846void
17847lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17848 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017849{
James Smart4d9ab992009-10-02 15:16:39 -040017850 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017851 struct fc_frame_header *fc_hdr;
17852 struct lpfc_vport *vport;
17853 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017854 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017855
James Smart4f774512009-05-22 14:52:35 -040017856 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017857 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017858
James Smarte817e5d2018-12-13 15:17:53 -080017859 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
17860 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
17861 vport = phba->pport;
17862 /* Handle MDS Loopback frames */
17863 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17864 return;
17865 }
17866
James Smart4d9ab992009-10-02 15:16:39 -040017867 /* check to see if this a valid type of frame */
17868 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17869 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17870 return;
17871 }
James Smart2ea259e2017-02-12 13:52:27 -080017872
James Smart7851fe22011-07-22 18:36:52 -040017873 if ((bf_get(lpfc_cqe_code,
17874 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17875 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17876 &dmabuf->cq_event.cqe.rcqe_cmpl);
17877 else
17878 fcfi = bf_get(lpfc_rcqe_fcf_id,
17879 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017880
James Smart895427b2017-02-12 13:52:30 -080017881 /* d_id this frame is directed to */
17882 did = sli4_did_from_fc_hdr(fc_hdr);
17883
17884 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017885 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017886 /* throw out the frame */
17887 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17888 return;
17889 }
James Smart939723a2012-05-09 21:19:03 -040017890
James Smart939723a2012-05-09 21:19:03 -040017891 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17892 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17893 (did != Fabric_DID)) {
17894 /*
17895 * Throw out the frame if we are not pt2pt.
17896 * The pt2pt protocol allows for discovery frames
17897 * to be received without a registered VPI.
17898 */
17899 if (!(vport->fc_flag & FC_PT2PT) ||
17900 (phba->link_state == LPFC_HBA_READY)) {
17901 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17902 return;
17903 }
17904 }
17905
James Smart6669f9b2009-10-02 15:16:45 -040017906 /* Handle the basic abort sequence (BA_ABTS) event */
17907 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17908 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17909 return;
17910 }
17911
James Smart4d9ab992009-10-02 15:16:39 -040017912 /* Link this frame */
17913 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17914 if (!seq_dmabuf) {
17915 /* unable to add frame to vport - throw it out */
17916 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17917 return;
17918 }
17919 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017920 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017921 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017922
James Smart6669f9b2009-10-02 15:16:45 -040017923 /* Send the complete sequence to the upper layer protocol */
17924 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017925}
James Smart6fb120a2009-05-22 14:52:59 -040017926
17927/**
17928 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17929 * @phba: pointer to lpfc hba data structure.
17930 *
17931 * This routine is invoked to post rpi header templates to the
17932 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017933 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17934 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017935 *
17936 * This routine does not require any locks. It's usage is expected
17937 * to be driver load or reset recovery when the driver is
17938 * sequential.
17939 *
17940 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017941 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017942 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017943 * When this error occurs, the driver is not guaranteed
17944 * to have any rpi regions posted to the device and
17945 * must either attempt to repost the regions or take a
17946 * fatal error.
17947 **/
17948int
17949lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17950{
17951 struct lpfc_rpi_hdr *rpi_page;
17952 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017953 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017954
James Smart6d368e52011-05-24 11:44:12 -040017955 /* SLI4 ports that support extents do not require RPI headers. */
17956 if (!phba->sli4_hba.rpi_hdrs_in_use)
17957 goto exit;
17958 if (phba->sli4_hba.extents_in_use)
17959 return -EIO;
17960
James Smart6fb120a2009-05-22 14:52:59 -040017961 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017962 /*
17963 * Assign the rpi headers a physical rpi only if the driver
17964 * has not initialized those resources. A port reset only
17965 * needs the headers posted.
17966 */
17967 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17968 LPFC_RPI_RSRC_RDY)
17969 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17970
James Smart6fb120a2009-05-22 14:52:59 -040017971 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17972 if (rc != MBX_SUCCESS) {
17973 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17974 "2008 Error %d posting all rpi "
17975 "headers\n", rc);
17976 rc = -EIO;
17977 break;
17978 }
17979 }
17980
James Smart6d368e52011-05-24 11:44:12 -040017981 exit:
17982 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17983 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017984 return rc;
17985}
17986
17987/**
17988 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17989 * @phba: pointer to lpfc hba data structure.
17990 * @rpi_page: pointer to the rpi memory region.
17991 *
17992 * This routine is invoked to post a single rpi header to the
17993 * HBA consistent with the SLI-4 interface spec. This memory region
17994 * maps up to 64 rpi context regions.
17995 *
17996 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017997 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017998 * -ENOMEM - No available memory
17999 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040018000 **/
18001int
18002lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18003{
18004 LPFC_MBOXQ_t *mboxq;
18005 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18006 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018007 uint32_t shdr_status, shdr_add_status;
18008 union lpfc_sli4_cfg_shdr *shdr;
18009
James Smart6d368e52011-05-24 11:44:12 -040018010 /* SLI4 ports that support extents do not require RPI headers. */
18011 if (!phba->sli4_hba.rpi_hdrs_in_use)
18012 return rc;
18013 if (phba->sli4_hba.extents_in_use)
18014 return -EIO;
18015
James Smart6fb120a2009-05-22 14:52:59 -040018016 /* The port is notified of the header region via a mailbox command. */
18017 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18018 if (!mboxq) {
18019 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18020 "2001 Unable to allocate memory for issuing "
18021 "SLI_CONFIG_SPECIAL mailbox command\n");
18022 return -ENOMEM;
18023 }
18024
18025 /* Post all rpi memory regions to the port. */
18026 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040018027 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18028 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18029 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050018030 sizeof(struct lpfc_sli4_cfg_mhdr),
18031 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040018032
18033
18034 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040018035 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18036 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040018037 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18038 hdr_tmpl, rpi_page->page_count);
18039
James Smart6fb120a2009-05-22 14:52:59 -040018040 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18041 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040018042 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040018043 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18044 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18045 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18046 if (rc != MBX_TIMEOUT)
18047 mempool_free(mboxq, phba->mbox_mem_pool);
18048 if (shdr_status || shdr_add_status || rc) {
18049 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18050 "2514 POST_RPI_HDR mailbox failed with "
18051 "status x%x add_status x%x, mbx status x%x\n",
18052 shdr_status, shdr_add_status, rc);
18053 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070018054 } else {
18055 /*
18056 * The next_rpi stores the next logical module-64 rpi value used
18057 * to post physical rpis in subsequent rpi postings.
18058 */
18059 spin_lock_irq(&phba->hbalock);
18060 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18061 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040018062 }
18063 return rc;
18064}
18065
18066/**
18067 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18068 * @phba: pointer to lpfc hba data structure.
18069 *
18070 * This routine is invoked to post rpi header templates to the
18071 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018072 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18073 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018074 *
18075 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018076 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040018077 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
18078 **/
18079int
18080lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18081{
James Smart6d368e52011-05-24 11:44:12 -040018082 unsigned long rpi;
18083 uint16_t max_rpi, rpi_limit;
18084 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018085 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040018086 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040018087
James Smart6fb120a2009-05-22 14:52:59 -040018088 /*
James Smart6d368e52011-05-24 11:44:12 -040018089 * Fetch the next logical rpi. Because this index is logical,
18090 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040018091 */
James Smart4902b382013-10-10 12:20:35 -040018092 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040018093 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18094 rpi_limit = phba->sli4_hba.next_rpi;
18095
James Smart6d368e52011-05-24 11:44:12 -040018096 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18097 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040018098 rpi = LPFC_RPI_ALLOC_ERROR;
18099 else {
18100 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18101 phba->sli4_hba.max_cfg_param.rpi_used++;
18102 phba->sli4_hba.rpi_count++;
18103 }
James Smartbe6bb942015-04-07 15:07:22 -040018104 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18105 "0001 rpi:%x max:%x lim:%x\n",
18106 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040018107
18108 /*
18109 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040018110 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040018111 */
18112 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18113 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040018114 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018115 return rpi;
18116 }
18117
18118 /*
James Smart6d368e52011-05-24 11:44:12 -040018119 * RPI header postings are not required for SLI4 ports capable of
18120 * extents.
18121 */
18122 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040018123 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040018124 return rpi;
18125 }
18126
18127 /*
James Smart6fb120a2009-05-22 14:52:59 -040018128 * If the driver is running low on rpi resources, allocate another
18129 * page now. Note that the next_rpi value is used because
18130 * it represents how many are actually in use whereas max_rpi notes
18131 * how many are supported max by the device.
18132 */
James Smart6d368e52011-05-24 11:44:12 -040018133 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040018134 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018135 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18136 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18137 if (!rpi_hdr) {
18138 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18139 "2002 Error Could not grow rpi "
18140 "count\n");
18141 } else {
James Smart6d368e52011-05-24 11:44:12 -040018142 lrpi = rpi_hdr->start_rpi;
18143 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040018144 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18145 }
18146 }
18147
18148 return rpi;
18149}
18150
18151/**
18152 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18153 * @phba: pointer to lpfc hba data structure.
18154 *
18155 * This routine is invoked to release an rpi to the pool of
18156 * available rpis maintained by the driver.
18157 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018158static void
James Smartd7c47992010-06-08 18:31:54 -040018159__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18160{
18161 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18162 phba->sli4_hba.rpi_count--;
18163 phba->sli4_hba.max_cfg_param.rpi_used--;
18164 }
18165}
18166
18167/**
18168 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18169 * @phba: pointer to lpfc hba data structure.
18170 *
18171 * This routine is invoked to release an rpi to the pool of
18172 * available rpis maintained by the driver.
18173 **/
18174void
James Smart6fb120a2009-05-22 14:52:59 -040018175lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18176{
18177 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040018178 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040018179 spin_unlock_irq(&phba->hbalock);
18180}
18181
18182/**
18183 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18184 * @phba: pointer to lpfc hba data structure.
18185 *
18186 * This routine is invoked to remove the memory region that
18187 * provided rpi via a bitmask.
18188 **/
18189void
18190lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18191{
18192 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040018193 kfree(phba->sli4_hba.rpi_ids);
18194 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040018195}
18196
18197/**
18198 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18199 * @phba: pointer to lpfc hba data structure.
18200 *
18201 * This routine is invoked to remove the memory region that
18202 * provided rpi via a bitmask.
18203 **/
18204int
James Smart6b5151f2012-01-18 16:24:06 -050018205lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18206 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040018207{
18208 LPFC_MBOXQ_t *mboxq;
18209 struct lpfc_hba *phba = ndlp->phba;
18210 int rc;
18211
18212 /* The port is notified of the header region via a mailbox command. */
18213 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18214 if (!mboxq)
18215 return -ENOMEM;
18216
18217 /* Post all rpi memory regions to the port. */
18218 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050018219 if (cmpl) {
18220 mboxq->mbox_cmpl = cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080018221 mboxq->ctx_buf = arg;
18222 mboxq->ctx_ndlp = ndlp;
James Smart72859902012-01-18 16:25:38 -050018223 } else
18224 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050018225 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040018226 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18227 if (rc == MBX_NOT_FINISHED) {
18228 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18229 "2010 Resume RPI Mailbox failed "
18230 "status %d, mbxStatus x%x\n", rc,
18231 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18232 mempool_free(mboxq, phba->mbox_mem_pool);
18233 return -EIO;
18234 }
18235 return 0;
18236}
18237
18238/**
18239 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050018240 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040018241 *
James Smart76a95d72010-11-20 23:11:48 -050018242 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040018243 *
18244 * Returns:
18245 * 0 success
18246 * -Evalue otherwise
18247 **/
18248int
James Smart76a95d72010-11-20 23:11:48 -050018249lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040018250{
18251 LPFC_MBOXQ_t *mboxq;
18252 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040018253 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040018254 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050018255 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040018256 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18257 if (!mboxq)
18258 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050018259 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040018260 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040018261 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040018262 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050018263 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040018264 "2022 INIT VPI Mailbox failed "
18265 "status %d, mbxStatus x%x\n", rc,
18266 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040018267 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040018268 }
James Smart6a9c52c2009-10-02 15:16:51 -040018269 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050018270 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040018271
18272 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040018273}
18274
18275/**
18276 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
18277 * @phba: pointer to lpfc hba data structure.
18278 * @mboxq: Pointer to mailbox object.
18279 *
18280 * This routine is invoked to manually add a single FCF record. The caller
18281 * must pass a completely initialized FCF_Record. This routine takes
18282 * care of the nonembedded mailbox operations.
18283 **/
18284static void
18285lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
18286{
18287 void *virt_addr;
18288 union lpfc_sli4_cfg_shdr *shdr;
18289 uint32_t shdr_status, shdr_add_status;
18290
18291 virt_addr = mboxq->sge_array->addr[0];
18292 /* The IOCTL status is embedded in the mailbox subheader. */
18293 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18294 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18295 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18296
18297 if ((shdr_status || shdr_add_status) &&
18298 (shdr_status != STATUS_FCF_IN_USE))
18299 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18300 "2558 ADD_FCF_RECORD mailbox failed with "
18301 "status x%x add_status x%x\n",
18302 shdr_status, shdr_add_status);
18303
18304 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18305}
18306
18307/**
18308 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18309 * @phba: pointer to lpfc hba data structure.
18310 * @fcf_record: pointer to the initialized fcf record to add.
18311 *
18312 * This routine is invoked to manually add a single FCF record. The caller
18313 * must pass a completely initialized FCF_Record. This routine takes
18314 * care of the nonembedded mailbox operations.
18315 **/
18316int
18317lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18318{
18319 int rc = 0;
18320 LPFC_MBOXQ_t *mboxq;
18321 uint8_t *bytep;
18322 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040018323 struct lpfc_mbx_sge sge;
18324 uint32_t alloc_len, req_len;
18325 uint32_t fcfindex;
18326
18327 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18328 if (!mboxq) {
18329 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18330 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18331 return -ENOMEM;
18332 }
18333
18334 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18335 sizeof(uint32_t);
18336
18337 /* Allocate DMA memory and set up the non-embedded mailbox command */
18338 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18339 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18340 req_len, LPFC_SLI4_MBX_NEMBED);
18341 if (alloc_len < req_len) {
18342 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18343 "2523 Allocated DMA memory size (x%x) is "
18344 "less than the requested DMA memory "
18345 "size (x%x)\n", alloc_len, req_len);
18346 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18347 return -ENOMEM;
18348 }
18349
18350 /*
18351 * Get the first SGE entry from the non-embedded DMA memory. This
18352 * routine only uses a single SGE.
18353 */
18354 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018355 virt_addr = mboxq->sge_array->addr[0];
18356 /*
18357 * Configure the FCF record for FCFI 0. This is the driver's
18358 * hardcoded default and gets used in nonFIP mode.
18359 */
18360 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18361 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18362 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18363
18364 /*
18365 * Copy the fcf_index and the FCF Record Data. The data starts after
18366 * the FCoE header plus word10. The data copy needs to be endian
18367 * correct.
18368 */
18369 bytep += sizeof(uint32_t);
18370 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18371 mboxq->vport = phba->pport;
18372 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18373 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18374 if (rc == MBX_NOT_FINISHED) {
18375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18376 "2515 ADD_FCF_RECORD mailbox failed with "
18377 "status 0x%x\n", rc);
18378 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18379 rc = -EIO;
18380 } else
18381 rc = 0;
18382
18383 return rc;
18384}
18385
18386/**
18387 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18388 * @phba: pointer to lpfc hba data structure.
18389 * @fcf_record: pointer to the fcf record to write the default data.
18390 * @fcf_index: FCF table entry index.
18391 *
18392 * This routine is invoked to build the driver's default FCF record. The
18393 * values used are hardcoded. This routine handles memory initialization.
18394 *
18395 **/
18396void
18397lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18398 struct fcf_record *fcf_record,
18399 uint16_t fcf_index)
18400{
18401 memset(fcf_record, 0, sizeof(struct fcf_record));
18402 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18403 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18404 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18405 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18406 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18407 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18408 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18409 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18410 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18411 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18412 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18413 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18414 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018415 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018416 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18417 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18418 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18419 /* Set the VLAN bit map */
18420 if (phba->valid_vlan) {
18421 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18422 = 1 << (phba->vlan_id % 8);
18423 }
18424}
18425
18426/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018427 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018428 * @phba: pointer to lpfc hba data structure.
18429 * @fcf_index: FCF table entry offset.
18430 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018431 * This routine is invoked to scan the entire FCF table by reading FCF
18432 * record and processing it one at a time starting from the @fcf_index
18433 * for initial FCF discovery or fast FCF failover rediscovery.
18434 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018435 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018436 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018437 **/
18438int
James Smart0c9ab6f2010-02-26 14:15:57 -050018439lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018440{
18441 int rc = 0, error;
18442 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018443
James Smart32b97932009-07-19 10:01:21 -040018444 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018445 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018446 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18447 if (!mboxq) {
18448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18449 "2000 Failed to allocate mbox for "
18450 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018451 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018452 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018453 }
James Smartecfd03c2010-02-12 14:41:27 -050018454 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018455 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018456 if (rc) {
18457 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018458 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018459 }
James Smartecfd03c2010-02-12 14:41:27 -050018460 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018461 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018462 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018463
18464 spin_lock_irq(&phba->hbalock);
18465 phba->hba_flag |= FCF_TS_INPROG;
18466 spin_unlock_irq(&phba->hbalock);
18467
James Smart6fb120a2009-05-22 14:52:59 -040018468 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018469 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018470 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018471 else {
James Smart38b92ef2010-08-04 16:11:39 -040018472 /* Reset eligible FCF count for new scan */
18473 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018474 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018475 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018476 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018477fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018478 if (error) {
18479 if (mboxq)
18480 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018481 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018482 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018483 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018484 spin_unlock_irq(&phba->hbalock);
18485 }
James Smart6fb120a2009-05-22 14:52:59 -040018486 return error;
18487}
James Smarta0c87cb2009-07-19 10:01:10 -040018488
18489/**
James Smarta93ff372010-10-22 11:06:08 -040018490 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018491 * @phba: pointer to lpfc hba data structure.
18492 * @fcf_index: FCF table entry offset.
18493 *
18494 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018495 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018496 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018497 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018498 * otherwise.
18499 **/
18500int
18501lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18502{
18503 int rc = 0, error;
18504 LPFC_MBOXQ_t *mboxq;
18505
18506 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18507 if (!mboxq) {
18508 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18509 "2763 Failed to allocate mbox for "
18510 "READ_FCF cmd\n");
18511 error = -ENOMEM;
18512 goto fail_fcf_read;
18513 }
18514 /* Construct the read FCF record mailbox command */
18515 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18516 if (rc) {
18517 error = -EINVAL;
18518 goto fail_fcf_read;
18519 }
18520 /* Issue the mailbox command asynchronously */
18521 mboxq->vport = phba->pport;
18522 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18523 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18524 if (rc == MBX_NOT_FINISHED)
18525 error = -EIO;
18526 else
18527 error = 0;
18528
18529fail_fcf_read:
18530 if (error && mboxq)
18531 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18532 return error;
18533}
18534
18535/**
18536 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18537 * @phba: pointer to lpfc hba data structure.
18538 * @fcf_index: FCF table entry offset.
18539 *
18540 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018541 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018542 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018543 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018544 * otherwise.
18545 **/
18546int
18547lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18548{
18549 int rc = 0, error;
18550 LPFC_MBOXQ_t *mboxq;
18551
18552 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18553 if (!mboxq) {
18554 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18555 "2758 Failed to allocate mbox for "
18556 "READ_FCF cmd\n");
18557 error = -ENOMEM;
18558 goto fail_fcf_read;
18559 }
18560 /* Construct the read FCF record mailbox command */
18561 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18562 if (rc) {
18563 error = -EINVAL;
18564 goto fail_fcf_read;
18565 }
18566 /* Issue the mailbox command asynchronously */
18567 mboxq->vport = phba->pport;
18568 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18569 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18570 if (rc == MBX_NOT_FINISHED)
18571 error = -EIO;
18572 else
18573 error = 0;
18574
18575fail_fcf_read:
18576 if (error && mboxq)
18577 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18578 return error;
18579}
18580
18581/**
James Smartf5cb5302015-12-16 18:11:52 -050018582 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018583 * phba pointer to the lpfc_hba struct for this port.
18584 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18585 * routine when the rr_bmask is empty. The FCF indecies are put into the
18586 * rr_bmask based on their priority level. Starting from the highest priority
18587 * to the lowest. The most likely FCF candidate will be in the highest
18588 * priority group. When this routine is called it searches the fcf_pri list for
18589 * next lowest priority group and repopulates the rr_bmask with only those
18590 * fcf_indexes.
18591 * returns:
18592 * 1=success 0=failure
18593 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018594static int
James Smart7d791df2011-07-22 18:37:52 -040018595lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18596{
18597 uint16_t next_fcf_pri;
18598 uint16_t last_index;
18599 struct lpfc_fcf_pri *fcf_pri;
18600 int rc;
18601 int ret = 0;
18602
18603 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18604 LPFC_SLI4_FCF_TBL_INDX_MAX);
18605 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18606 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018607
18608 /* Verify the priority list has 2 or more entries */
18609 spin_lock_irq(&phba->hbalock);
18610 if (list_empty(&phba->fcf.fcf_pri_list) ||
18611 list_is_singular(&phba->fcf.fcf_pri_list)) {
18612 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018613 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18614 "3061 Last IDX %d\n", last_index);
18615 return 0; /* Empty rr list */
18616 }
James Smart25626692013-03-01 16:36:54 -050018617 spin_unlock_irq(&phba->hbalock);
18618
James Smart7d791df2011-07-22 18:37:52 -040018619 next_fcf_pri = 0;
18620 /*
18621 * Clear the rr_bmask and set all of the bits that are at this
18622 * priority.
18623 */
18624 memset(phba->fcf.fcf_rr_bmask, 0,
18625 sizeof(*phba->fcf.fcf_rr_bmask));
18626 spin_lock_irq(&phba->hbalock);
18627 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18628 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18629 continue;
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 /*
18646 * if next_fcf_pri was not set above and the list is not empty then
18647 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018648 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018649 */
18650 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18651 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18652 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18653 /*
18654 * the 1st priority that has not FLOGI failed
18655 * will be the highest.
18656 */
18657 if (!next_fcf_pri)
18658 next_fcf_pri = fcf_pri->fcf_rec.priority;
18659 spin_unlock_irq(&phba->hbalock);
18660 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18661 rc = lpfc_sli4_fcf_rr_index_set(phba,
18662 fcf_pri->fcf_rec.fcf_index);
18663 if (rc)
18664 return 0;
18665 }
18666 spin_lock_irq(&phba->hbalock);
18667 }
18668 } else
18669 ret = 1;
18670 spin_unlock_irq(&phba->hbalock);
18671
18672 return ret;
18673}
18674/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018675 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18676 * @phba: pointer to lpfc hba data structure.
18677 *
18678 * This routine is to get the next eligible FCF record index in a round
18679 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018680 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018681 * shall be returned, otherwise, the next eligible FCF record's index
18682 * shall be returned.
18683 **/
18684uint16_t
18685lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18686{
18687 uint16_t next_fcf_index;
18688
James Smart421c6622013-01-03 15:44:16 -050018689initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018690 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018691 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18692
James Smart7d791df2011-07-22 18:37:52 -040018693next_priority:
James Smart421c6622013-01-03 15:44:16 -050018694 /* Determine the next fcf index to check */
18695 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018696 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18697 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018698 next_fcf_index);
18699
James Smart0c9ab6f2010-02-26 14:15:57 -050018700 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018701 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18702 /*
18703 * If we have wrapped then we need to clear the bits that
18704 * have been tested so that we can detect when we should
18705 * change the priority level.
18706 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018707 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18708 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018709 }
18710
James Smart0c9ab6f2010-02-26 14:15:57 -050018711
James Smart3804dc82010-07-14 15:31:37 -040018712 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018713 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18714 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18715 /*
18716 * If next fcf index is not found check if there are lower
18717 * Priority level fcf's in the fcf_priority list.
18718 * Set up the rr_bmask with all of the avaiable fcf bits
18719 * at that level and continue the selection process.
18720 */
18721 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018722 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018723 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18724 "2844 No roundrobin failover FCF available\n");
James Smart036cad12018-10-23 13:41:06 -070018725
18726 return LPFC_FCOE_FCF_NEXT_NONE;
James Smart3804dc82010-07-14 15:31:37 -040018727 }
18728
James Smart7d791df2011-07-22 18:37:52 -040018729 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18730 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018731 LPFC_FCF_FLOGI_FAILED) {
18732 if (list_is_singular(&phba->fcf.fcf_pri_list))
18733 return LPFC_FCOE_FCF_NEXT_NONE;
18734
James Smart7d791df2011-07-22 18:37:52 -040018735 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018736 }
James Smart7d791df2011-07-22 18:37:52 -040018737
James Smart3804dc82010-07-14 15:31:37 -040018738 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018739 "2845 Get next roundrobin failover FCF (x%x)\n",
18740 next_fcf_index);
18741
James Smart0c9ab6f2010-02-26 14:15:57 -050018742 return next_fcf_index;
18743}
18744
18745/**
18746 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18747 * @phba: pointer to lpfc hba data structure.
18748 *
18749 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018750 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018751 * does not go beyond the range of the driver allocated bmask dimension
18752 * before setting the bit.
18753 *
18754 * Returns 0 if the index bit successfully set, otherwise, it returns
18755 * -EINVAL.
18756 **/
18757int
18758lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18759{
18760 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18761 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018762 "2610 FCF (x%x) reached driver's book "
18763 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018764 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18765 return -EINVAL;
18766 }
18767 /* Set the eligible FCF record index bmask */
18768 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18769
James Smart3804dc82010-07-14 15:31:37 -040018770 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018771 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018772 "bmask\n", fcf_index);
18773
James Smart0c9ab6f2010-02-26 14:15:57 -050018774 return 0;
18775}
18776
18777/**
James Smart3804dc82010-07-14 15:31:37 -040018778 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018779 * @phba: pointer to lpfc hba data structure.
18780 *
18781 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018782 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018783 * does not go beyond the range of the driver allocated bmask dimension
18784 * before clearing the bit.
18785 **/
18786void
18787lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18788{
James Smart9a803a72013-09-06 12:17:56 -040018789 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018790 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18791 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018792 "2762 FCF (x%x) reached driver's book "
18793 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018794 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18795 return;
18796 }
18797 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018798 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018799 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18800 list) {
James Smart7d791df2011-07-22 18:37:52 -040018801 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18802 list_del_init(&fcf_pri->list);
18803 break;
18804 }
18805 }
18806 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018807 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018808
18809 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018810 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018811 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018812}
18813
18814/**
James Smartecfd03c2010-02-12 14:41:27 -050018815 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18816 * @phba: pointer to lpfc hba data structure.
18817 *
18818 * This routine is the completion routine for the rediscover FCF table mailbox
18819 * command. If the mailbox command returned failure, it will try to stop the
18820 * FCF rediscover wait timer.
18821 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018822static void
James Smartecfd03c2010-02-12 14:41:27 -050018823lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18824{
18825 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18826 uint32_t shdr_status, shdr_add_status;
18827
18828 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18829
18830 shdr_status = bf_get(lpfc_mbox_hdr_status,
18831 &redisc_fcf->header.cfg_shdr.response);
18832 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18833 &redisc_fcf->header.cfg_shdr.response);
18834 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018835 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018836 "2746 Requesting for FCF rediscovery failed "
18837 "status x%x add_status x%x\n",
18838 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018839 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018840 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018841 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018842 spin_unlock_irq(&phba->hbalock);
18843 /*
18844 * CVL event triggered FCF rediscover request failed,
18845 * last resort to re-try current registered FCF entry.
18846 */
18847 lpfc_retry_pport_discovery(phba);
18848 } else {
18849 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018850 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018851 spin_unlock_irq(&phba->hbalock);
18852 /*
18853 * DEAD FCF event triggered FCF rediscover request
18854 * failed, last resort to fail over as a link down
18855 * to FCF registration.
18856 */
18857 lpfc_sli4_fcf_dead_failthrough(phba);
18858 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018859 } else {
18860 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018861 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018862 /*
18863 * Start FCF rediscovery wait timer for pending FCF
18864 * before rescan FCF record table.
18865 */
18866 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018867 }
James Smartecfd03c2010-02-12 14:41:27 -050018868
18869 mempool_free(mbox, phba->mbox_mem_pool);
18870}
18871
18872/**
James Smart3804dc82010-07-14 15:31:37 -040018873 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018874 * @phba: pointer to lpfc hba data structure.
18875 *
18876 * This routine is invoked to request for rediscovery of the entire FCF table
18877 * by the port.
18878 **/
18879int
18880lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18881{
18882 LPFC_MBOXQ_t *mbox;
18883 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18884 int rc, length;
18885
James Smart0c9ab6f2010-02-26 14:15:57 -050018886 /* Cancel retry delay timers to all vports before FCF rediscover */
18887 lpfc_cancel_all_vport_retry_delay_timer(phba);
18888
James Smartecfd03c2010-02-12 14:41:27 -050018889 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18890 if (!mbox) {
18891 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18892 "2745 Failed to allocate mbox for "
18893 "requesting FCF rediscover.\n");
18894 return -ENOMEM;
18895 }
18896
18897 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18898 sizeof(struct lpfc_sli4_cfg_mhdr));
18899 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18900 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18901 length, LPFC_SLI4_MBX_EMBED);
18902
18903 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18904 /* Set count to 0 for invalidating the entire FCF database */
18905 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18906
18907 /* Issue the mailbox command asynchronously */
18908 mbox->vport = phba->pport;
18909 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18910 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18911
18912 if (rc == MBX_NOT_FINISHED) {
18913 mempool_free(mbox, phba->mbox_mem_pool);
18914 return -EIO;
18915 }
18916 return 0;
18917}
18918
18919/**
James Smartfc2b9892010-02-26 14:15:29 -050018920 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18921 * @phba: pointer to lpfc hba data structure.
18922 *
18923 * This function is the failover routine as a last resort to the FCF DEAD
18924 * event when driver failed to perform fast FCF failover.
18925 **/
18926void
18927lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18928{
18929 uint32_t link_state;
18930
18931 /*
18932 * Last resort as FCF DEAD event failover will treat this as
18933 * a link down, but save the link state because we don't want
18934 * it to be changed to Link Down unless it is already down.
18935 */
18936 link_state = phba->link_state;
18937 lpfc_linkdown(phba);
18938 phba->link_state = link_state;
18939
18940 /* Unregister FCF if no devices connected to it */
18941 lpfc_unregister_unused_fcf(phba);
18942}
18943
18944/**
James Smart026abb82011-12-13 13:20:45 -050018945 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018946 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018947 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018948 *
James Smart026abb82011-12-13 13:20:45 -050018949 * This function gets SLI3 port configure region 23 data through memory dump
18950 * mailbox command. When it successfully retrieves data, the size of the data
18951 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018952 **/
James Smart026abb82011-12-13 13:20:45 -050018953static uint32_t
18954lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018955{
18956 LPFC_MBOXQ_t *pmb = NULL;
18957 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018958 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018959 int rc;
18960
James Smart026abb82011-12-13 13:20:45 -050018961 if (!rgn23_data)
18962 return 0;
18963
James Smarta0c87cb2009-07-19 10:01:10 -040018964 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18965 if (!pmb) {
18966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018967 "2600 failed to allocate mailbox memory\n");
18968 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018969 }
18970 mb = &pmb->u.mb;
18971
James Smarta0c87cb2009-07-19 10:01:10 -040018972 do {
18973 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18974 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18975
18976 if (rc != MBX_SUCCESS) {
18977 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018978 "2601 failed to read config "
18979 "region 23, rc 0x%x Status 0x%x\n",
18980 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018981 mb->un.varDmp.word_cnt = 0;
18982 }
18983 /*
18984 * dump mem may return a zero when finished or we got a
18985 * mailbox error, either way we are done.
18986 */
18987 if (mb->un.varDmp.word_cnt == 0)
18988 break;
18989 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18990 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18991
18992 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018993 rgn23_data + offset,
18994 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018995 offset += mb->un.varDmp.word_cnt;
18996 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18997
James Smart026abb82011-12-13 13:20:45 -050018998 mempool_free(pmb, phba->mbox_mem_pool);
18999 return offset;
19000}
19001
19002/**
19003 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19004 * @phba: pointer to lpfc hba data structure.
19005 * @rgn23_data: pointer to configure region 23 data.
19006 *
19007 * This function gets SLI4 port configure region 23 data through memory dump
19008 * mailbox command. When it successfully retrieves data, the size of the data
19009 * will be returned, otherwise, 0 will be returned.
19010 **/
19011static uint32_t
19012lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19013{
19014 LPFC_MBOXQ_t *mboxq = NULL;
19015 struct lpfc_dmabuf *mp = NULL;
19016 struct lpfc_mqe *mqe;
19017 uint32_t data_length = 0;
19018 int rc;
19019
19020 if (!rgn23_data)
19021 return 0;
19022
19023 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19024 if (!mboxq) {
19025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19026 "3105 failed to allocate mailbox memory\n");
19027 return 0;
19028 }
19029
19030 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19031 goto out;
19032 mqe = &mboxq->u.mqe;
James Smart3e1f0712018-11-29 16:09:29 -080019033 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smart026abb82011-12-13 13:20:45 -050019034 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19035 if (rc)
19036 goto out;
19037 data_length = mqe->un.mb_words[5];
19038 if (data_length == 0)
19039 goto out;
19040 if (data_length > DMP_RGN23_SIZE) {
19041 data_length = 0;
19042 goto out;
19043 }
19044 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19045out:
19046 mempool_free(mboxq, phba->mbox_mem_pool);
19047 if (mp) {
19048 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19049 kfree(mp);
19050 }
19051 return data_length;
19052}
19053
19054/**
19055 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19056 * @phba: pointer to lpfc hba data structure.
19057 *
19058 * This function read region 23 and parse TLV for port status to
19059 * decide if the user disaled the port. If the TLV indicates the
19060 * port is disabled, the hba_flag is set accordingly.
19061 **/
19062void
19063lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19064{
19065 uint8_t *rgn23_data = NULL;
19066 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19067 uint32_t offset = 0;
19068
19069 /* Get adapter Region 23 data */
19070 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19071 if (!rgn23_data)
19072 goto out;
19073
19074 if (phba->sli_rev < LPFC_SLI_REV4)
19075 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19076 else {
19077 if_type = bf_get(lpfc_sli_intf_if_type,
19078 &phba->sli4_hba.sli_intf);
19079 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19080 goto out;
19081 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19082 }
James Smarta0c87cb2009-07-19 10:01:10 -040019083
19084 if (!data_size)
19085 goto out;
19086
19087 /* Check the region signature first */
19088 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19089 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19090 "2619 Config region 23 has bad signature\n");
19091 goto out;
19092 }
19093 offset += 4;
19094
19095 /* Check the data structure version */
19096 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19097 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19098 "2620 Config region 23 has bad version\n");
19099 goto out;
19100 }
19101 offset += 4;
19102
19103 /* Parse TLV entries in the region */
19104 while (offset < data_size) {
19105 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19106 break;
19107 /*
19108 * If the TLV is not driver specific TLV or driver id is
19109 * not linux driver id, skip the record.
19110 */
19111 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19112 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19113 (rgn23_data[offset + 3] != 0)) {
19114 offset += rgn23_data[offset + 1] * 4 + 4;
19115 continue;
19116 }
19117
19118 /* Driver found a driver specific TLV in the config region */
19119 sub_tlv_len = rgn23_data[offset + 1] * 4;
19120 offset += 4;
19121 tlv_offset = 0;
19122
19123 /*
19124 * Search for configured port state sub-TLV.
19125 */
19126 while ((offset < data_size) &&
19127 (tlv_offset < sub_tlv_len)) {
19128 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19129 offset += 4;
19130 tlv_offset += 4;
19131 break;
19132 }
19133 if (rgn23_data[offset] != PORT_STE_TYPE) {
19134 offset += rgn23_data[offset + 1] * 4 + 4;
19135 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19136 continue;
19137 }
19138
19139 /* This HBA contains PORT_STE configured */
19140 if (!rgn23_data[offset + 2])
19141 phba->hba_flag |= LINK_DISABLED;
19142
19143 goto out;
19144 }
19145 }
James Smart026abb82011-12-13 13:20:45 -050019146
James Smarta0c87cb2009-07-19 10:01:10 -040019147out:
James Smarta0c87cb2009-07-19 10:01:10 -040019148 kfree(rgn23_data);
19149 return;
19150}
James Smart695a8142010-01-26 23:08:03 -050019151
19152/**
James Smart52d52442011-05-24 11:42:45 -040019153 * lpfc_wr_object - write an object to the firmware
19154 * @phba: HBA structure that indicates port to create a queue on.
19155 * @dmabuf_list: list of dmabufs to write to the port.
19156 * @size: the total byte value of the objects to write to the port.
19157 * @offset: the current offset to be used to start the transfer.
19158 *
19159 * This routine will create a wr_object mailbox command to send to the port.
19160 * the mailbox command will be constructed using the dma buffers described in
19161 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19162 * BDEs that the imbedded mailbox can support. The @offset variable will be
19163 * used to indicate the starting offset of the transfer and will also return
19164 * the offset after the write object mailbox has completed. @size is used to
19165 * determine the end of the object and whether the eof bit should be set.
19166 *
19167 * Return 0 is successful and offset will contain the the new offset to use
19168 * for the next write.
19169 * Return negative value for error cases.
19170 **/
19171int
19172lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19173 uint32_t size, uint32_t *offset)
19174{
19175 struct lpfc_mbx_wr_object *wr_object;
19176 LPFC_MBOXQ_t *mbox;
19177 int rc = 0, i = 0;
James Smart50212672018-12-13 15:17:57 -080019178 uint32_t shdr_status, shdr_add_status, shdr_change_status;
James Smart52d52442011-05-24 11:42:45 -040019179 uint32_t mbox_tmo;
James Smart52d52442011-05-24 11:42:45 -040019180 struct lpfc_dmabuf *dmabuf;
19181 uint32_t written = 0;
James Smart50212672018-12-13 15:17:57 -080019182 bool check_change_status = false;
James Smart52d52442011-05-24 11:42:45 -040019183
19184 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19185 if (!mbox)
19186 return -ENOMEM;
19187
19188 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19189 LPFC_MBOX_OPCODE_WRITE_OBJECT,
19190 sizeof(struct lpfc_mbx_wr_object) -
19191 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19192
19193 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19194 wr_object->u.request.write_offset = *offset;
19195 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19196 wr_object->u.request.object_name[0] =
19197 cpu_to_le32(wr_object->u.request.object_name[0]);
19198 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19199 list_for_each_entry(dmabuf, dmabuf_list, list) {
19200 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19201 break;
19202 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19203 wr_object->u.request.bde[i].addrHigh =
19204 putPaddrHigh(dmabuf->phys);
19205 if (written + SLI4_PAGE_SIZE >= size) {
19206 wr_object->u.request.bde[i].tus.f.bdeSize =
19207 (size - written);
19208 written += (size - written);
19209 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
James Smart50212672018-12-13 15:17:57 -080019210 bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
19211 check_change_status = true;
James Smart52d52442011-05-24 11:42:45 -040019212 } else {
19213 wr_object->u.request.bde[i].tus.f.bdeSize =
19214 SLI4_PAGE_SIZE;
19215 written += SLI4_PAGE_SIZE;
19216 }
19217 i++;
19218 }
19219 wr_object->u.request.bde_count = i;
19220 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19221 if (!phba->sli4_hba.intr_enable)
19222 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19223 else {
James Smarta183a152011-10-10 21:32:43 -040019224 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040019225 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19226 }
19227 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart50212672018-12-13 15:17:57 -080019228 shdr_status = bf_get(lpfc_mbox_hdr_status,
19229 &wr_object->header.cfg_shdr.response);
19230 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19231 &wr_object->header.cfg_shdr.response);
19232 if (check_change_status) {
19233 shdr_change_status = bf_get(lpfc_wr_object_change_status,
19234 &wr_object->u.response);
19235 switch (shdr_change_status) {
19236 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
19237 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19238 "3198 Firmware write complete: System "
19239 "reboot required to instantiate\n");
19240 break;
19241 case (LPFC_CHANGE_STATUS_FW_RESET):
19242 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19243 "3199 Firmware write complete: Firmware"
19244 " reset required to instantiate\n");
19245 break;
19246 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
19247 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19248 "3200 Firmware write complete: Port "
19249 "Migration or PCI Reset required to "
19250 "instantiate\n");
19251 break;
19252 case (LPFC_CHANGE_STATUS_PCI_RESET):
19253 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19254 "3201 Firmware write complete: PCI "
19255 "Reset required to instantiate\n");
19256 break;
19257 default:
19258 break;
19259 }
19260 }
James Smart52d52442011-05-24 11:42:45 -040019261 if (rc != MBX_TIMEOUT)
19262 mempool_free(mbox, phba->mbox_mem_pool);
19263 if (shdr_status || shdr_add_status || rc) {
19264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19265 "3025 Write Object mailbox failed with "
19266 "status x%x add_status x%x, mbx status x%x\n",
19267 shdr_status, shdr_add_status, rc);
19268 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080019269 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040019270 } else
19271 *offset += wr_object->u.response.actual_write_length;
19272 return rc;
19273}
19274
19275/**
James Smart695a8142010-01-26 23:08:03 -050019276 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
19277 * @vport: pointer to vport data structure.
19278 *
19279 * This function iterate through the mailboxq and clean up all REG_LOGIN
19280 * and REG_VPI mailbox commands associated with the vport. This function
19281 * is called when driver want to restart discovery of the vport due to
19282 * a Clear Virtual Link event.
19283 **/
19284void
19285lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
19286{
19287 struct lpfc_hba *phba = vport->phba;
19288 LPFC_MBOXQ_t *mb, *nextmb;
19289 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040019290 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040019291 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040019292 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040019293 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050019294 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050019295
James Smartd439d282010-09-29 11:18:45 -040019296 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050019297 spin_lock_irq(&phba->hbalock);
19298 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
19299 if (mb->vport != vport)
19300 continue;
19301
19302 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19303 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19304 continue;
19305
James Smartd439d282010-09-29 11:18:45 -040019306 list_del(&mb->list);
19307 list_add_tail(&mb->list, &mbox_cmd_list);
19308 }
19309 /* Clean up active mailbox command with the vport */
19310 mb = phba->sli.mbox_active;
19311 if (mb && (mb->vport == vport)) {
19312 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
19313 (mb->u.mb.mbxCommand == MBX_REG_VPI))
19314 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19315 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019316 act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -040019317 /* Put reference count for delayed processing */
19318 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19319 /* Unregister the RPI when mailbox complete */
19320 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19321 }
19322 }
James Smart63e801c2010-11-20 23:14:19 -050019323 /* Cleanup any mailbox completions which are not yet processed */
19324 do {
19325 restart_loop = 0;
19326 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19327 /*
19328 * If this mailox is already processed or it is
19329 * for another vport ignore it.
19330 */
19331 if ((mb->vport != vport) ||
19332 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19333 continue;
19334
19335 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19336 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19337 continue;
19338
19339 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19340 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019341 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smart63e801c2010-11-20 23:14:19 -050019342 /* Unregister the RPI when mailbox complete */
19343 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19344 restart_loop = 1;
19345 spin_unlock_irq(&phba->hbalock);
19346 spin_lock(shost->host_lock);
19347 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19348 spin_unlock(shost->host_lock);
19349 spin_lock_irq(&phba->hbalock);
19350 break;
19351 }
19352 }
19353 } while (restart_loop);
19354
James Smartd439d282010-09-29 11:18:45 -040019355 spin_unlock_irq(&phba->hbalock);
19356
19357 /* Release the cleaned-up mailbox commands */
19358 while (!list_empty(&mbox_cmd_list)) {
19359 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050019360 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019361 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
James Smart695a8142010-01-26 23:08:03 -050019362 if (mp) {
19363 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19364 kfree(mp);
19365 }
James Smart3e1f0712018-11-29 16:09:29 -080019366 mb->ctx_buf = NULL;
19367 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
19368 mb->ctx_ndlp = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019369 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019370 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019371 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019372 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019373 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019374 }
James Smart695a8142010-01-26 23:08:03 -050019375 }
James Smart695a8142010-01-26 23:08:03 -050019376 mempool_free(mb, phba->mbox_mem_pool);
19377 }
James Smartd439d282010-09-29 11:18:45 -040019378
19379 /* Release the ndlp with the cleaned-up active mailbox command */
19380 if (act_mbx_ndlp) {
19381 spin_lock(shost->host_lock);
19382 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19383 spin_unlock(shost->host_lock);
19384 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019385 }
James Smart695a8142010-01-26 23:08:03 -050019386}
19387
James Smart2a9bf3d2010-06-07 15:24:45 -040019388/**
19389 * lpfc_drain_txq - Drain the txq
19390 * @phba: Pointer to HBA context object.
19391 *
19392 * This function attempt to submit IOCBs on the txq
19393 * to the adapter. For SLI4 adapters, the txq contains
19394 * ELS IOCBs that have been deferred because the there
19395 * are no SGLs. This congestion can occur with large
19396 * vport counts during node discovery.
19397 **/
19398
19399uint32_t
19400lpfc_drain_txq(struct lpfc_hba *phba)
19401{
19402 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019403 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019404 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019405 unsigned long iflags = 0;
19406 char *fail_msg = NULL;
19407 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019408 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019409 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070019410 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040019411
James Smartdc19e3b2018-05-24 21:08:57 -070019412 if (phba->link_flag & LS_MDS_LOOPBACK) {
19413 /* MDS WQE are posted only to first WQ*/
James Smartcdb42be2019-01-28 11:14:21 -080019414 wq = phba->sli4_hba.hdwq[0].fcp_wq;
James Smartdc19e3b2018-05-24 21:08:57 -070019415 if (unlikely(!wq))
19416 return 0;
19417 pring = wq->pring;
19418 } else {
19419 wq = phba->sli4_hba.els_wq;
19420 if (unlikely(!wq))
19421 return 0;
19422 pring = lpfc_phba_elsring(phba);
19423 }
19424
19425 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019426 return 0;
James Smart895427b2017-02-12 13:52:30 -080019427
James Smart398d81c2013-05-31 17:04:19 -040019428 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019429 list_for_each_entry(piocbq, &pring->txq, list) {
19430 txq_cnt++;
19431 }
19432
19433 if (txq_cnt > pring->txq_max)
19434 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019435
James Smart398d81c2013-05-31 17:04:19 -040019436 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019437
James Smart0e9bb8d2013-03-01 16:35:12 -050019438 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019439 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019440
James Smart19ca7602010-11-20 23:11:55 -050019441 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019442 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019443 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019444 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19445 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019446 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019447 break;
19448 }
James Smart895427b2017-02-12 13:52:30 -080019449 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019450 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019451 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019452 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019453 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019454 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019455 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019456
19457 /* The xri and iocb resources secured,
19458 * attempt to issue request
19459 */
James Smart6d368e52011-05-24 11:44:12 -040019460 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019461 piocbq->sli4_xritag = sglq->sli4_xritag;
19462 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19463 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019464 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019465 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070019466 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019467 fail_msg = " - Wq is full";
19468 else
19469 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19470
19471 if (fail_msg) {
19472 /* Failed means we can't issue and need to cancel */
19473 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19474 "2822 IOCB failed %s iotag 0x%x "
19475 "xri 0x%x\n",
19476 fail_msg,
19477 piocbq->iotag, piocbq->sli4_xritag);
19478 list_add_tail(&piocbq->list, &completions);
19479 }
James Smart398d81c2013-05-31 17:04:19 -040019480 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019481 }
19482
James Smart2a9bf3d2010-06-07 15:24:45 -040019483 /* Cancel all the IOCBs that cannot be issued */
19484 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19485 IOERR_SLI_ABORTED);
19486
James Smart0e9bb8d2013-03-01 16:35:12 -050019487 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019488}
James Smart895427b2017-02-12 13:52:30 -080019489
19490/**
19491 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19492 * @phba: Pointer to HBA context object.
19493 * @pwqe: Pointer to command WQE.
19494 * @sglq: Pointer to the scatter gather queue object.
19495 *
19496 * This routine converts the bpl or bde that is in the WQE
19497 * to a sgl list for the sli4 hardware. The physical address
19498 * of the bpl/bde is converted back to a virtual address.
19499 * If the WQE contains a BPL then the list of BDE's is
19500 * converted to sli4_sge's. If the WQE contains a single
19501 * BDE then it is converted to a single sli_sge.
19502 * The WQE is still in cpu endianness so the contents of
19503 * the bpl can be used without byte swapping.
19504 *
19505 * Returns valid XRI = Success, NO_XRI = Failure.
19506 */
19507static uint16_t
19508lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19509 struct lpfc_sglq *sglq)
19510{
19511 uint16_t xritag = NO_XRI;
19512 struct ulp_bde64 *bpl = NULL;
19513 struct ulp_bde64 bde;
19514 struct sli4_sge *sgl = NULL;
19515 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080019516 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080019517 int numBdes = 0;
19518 int i = 0;
19519 uint32_t offset = 0; /* accumulated offset in the sg request list */
19520 int inbound = 0; /* number of sg reply entries inbound from firmware */
19521 uint32_t cmd;
19522
19523 if (!pwqeq || !sglq)
19524 return xritag;
19525
19526 sgl = (struct sli4_sge *)sglq->sgl;
19527 wqe = &pwqeq->wqe;
19528 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19529
19530 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19531 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19532 return sglq->sli4_xritag;
19533 numBdes = pwqeq->rsvd2;
19534 if (numBdes) {
19535 /* The addrHigh and addrLow fields within the WQE
19536 * have not been byteswapped yet so there is no
19537 * need to swap them back.
19538 */
19539 if (pwqeq->context3)
19540 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19541 else
19542 return xritag;
19543
19544 bpl = (struct ulp_bde64 *)dmabuf->virt;
19545 if (!bpl)
19546 return xritag;
19547
19548 for (i = 0; i < numBdes; i++) {
19549 /* Should already be byte swapped. */
19550 sgl->addr_hi = bpl->addrHigh;
19551 sgl->addr_lo = bpl->addrLow;
19552
19553 sgl->word2 = le32_to_cpu(sgl->word2);
19554 if ((i+1) == numBdes)
19555 bf_set(lpfc_sli4_sge_last, sgl, 1);
19556 else
19557 bf_set(lpfc_sli4_sge_last, sgl, 0);
19558 /* swap the size field back to the cpu so we
19559 * can assign it to the sgl.
19560 */
19561 bde.tus.w = le32_to_cpu(bpl->tus.w);
19562 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19563 /* The offsets in the sgl need to be accumulated
19564 * separately for the request and reply lists.
19565 * The request is always first, the reply follows.
19566 */
19567 switch (cmd) {
19568 case CMD_GEN_REQUEST64_WQE:
19569 /* add up the reply sg entries */
19570 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19571 inbound++;
19572 /* first inbound? reset the offset */
19573 if (inbound == 1)
19574 offset = 0;
19575 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19576 bf_set(lpfc_sli4_sge_type, sgl,
19577 LPFC_SGE_TYPE_DATA);
19578 offset += bde.tus.f.bdeSize;
19579 break;
19580 case CMD_FCP_TRSP64_WQE:
19581 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19582 bf_set(lpfc_sli4_sge_type, sgl,
19583 LPFC_SGE_TYPE_DATA);
19584 break;
19585 case CMD_FCP_TSEND64_WQE:
19586 case CMD_FCP_TRECEIVE64_WQE:
19587 bf_set(lpfc_sli4_sge_type, sgl,
19588 bpl->tus.f.bdeFlags);
19589 if (i < 3)
19590 offset = 0;
19591 else
19592 offset += bde.tus.f.bdeSize;
19593 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19594 break;
19595 }
19596 sgl->word2 = cpu_to_le32(sgl->word2);
19597 bpl++;
19598 sgl++;
19599 }
19600 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19601 /* The addrHigh and addrLow fields of the BDE have not
19602 * been byteswapped yet so they need to be swapped
19603 * before putting them in the sgl.
19604 */
19605 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19606 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19607 sgl->word2 = le32_to_cpu(sgl->word2);
19608 bf_set(lpfc_sli4_sge_last, sgl, 1);
19609 sgl->word2 = cpu_to_le32(sgl->word2);
19610 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19611 }
19612 return sglq->sli4_xritag;
19613}
19614
19615/**
19616 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19617 * @phba: Pointer to HBA context object.
19618 * @ring_number: Base sli ring number
19619 * @pwqe: Pointer to command WQE.
19620 **/
19621int
James Smart1fbf9742019-01-28 11:14:26 -080019622lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
James Smart895427b2017-02-12 13:52:30 -080019623 struct lpfc_iocbq *pwqe)
19624{
James Smart205e8242018-03-05 12:04:03 -080019625 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019626 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019627 struct lpfc_queue *wq;
19628 struct lpfc_sglq *sglq;
19629 struct lpfc_sli_ring *pring;
19630 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019631 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019632
19633 /* NVME_LS and NVME_LS ABTS requests. */
19634 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19635 pring = phba->sli4_hba.nvmels_wq->pring;
19636 spin_lock_irqsave(&pring->ring_lock, iflags);
19637 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19638 if (!sglq) {
19639 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19640 return WQE_BUSY;
19641 }
19642 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19643 pwqe->sli4_xritag = sglq->sli4_xritag;
19644 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19645 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19646 return WQE_ERROR;
19647 }
19648 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19649 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019650 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19651 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019652 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019653 return ret;
James Smart895427b2017-02-12 13:52:30 -080019654 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019655
James Smart895427b2017-02-12 13:52:30 -080019656 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19657 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19658 return 0;
19659 }
19660
19661 /* NVME_FCREQ and NVME_ABTS requests */
19662 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19663 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smart1fbf9742019-01-28 11:14:26 -080019664 wq = qp->nvme_wq;
19665 pring = wq->pring;
19666
19667 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->nvme_cq_map);
James Smart895427b2017-02-12 13:52:30 -080019668
19669 spin_lock_irqsave(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019670 ret = lpfc_sli4_wq_put(wq, wqe);
19671 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019672 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019673 return ret;
James Smart895427b2017-02-12 13:52:30 -080019674 }
19675 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19676 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19677 return 0;
19678 }
19679
James Smartf358dd02017-02-12 13:52:34 -080019680 /* NVMET requests */
19681 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19682 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smart1fbf9742019-01-28 11:14:26 -080019683 wq = qp->nvme_wq;
19684 pring = wq->pring;
James Smartf358dd02017-02-12 13:52:34 -080019685
James Smartf358dd02017-02-12 13:52:34 -080019686 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019687 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019688 if (pwqe->sli4_xritag == NO_XRI) {
19689 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19690 pwqe->sli4_xritag = sglq->sli4_xritag;
19691 }
19692 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19693 pwqe->sli4_xritag);
James Smart1fbf9742019-01-28 11:14:26 -080019694 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->nvme_cq_map);
19695
19696 spin_lock_irqsave(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019697 ret = lpfc_sli4_wq_put(wq, wqe);
19698 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019699 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019700 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019701 }
19702 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19703 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19704 return 0;
19705 }
James Smart895427b2017-02-12 13:52:30 -080019706 return WQE_ERROR;
19707}