blob: b9e5cd79931a21293a58e18125b2e2f650d47a73 [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
dea31012005-04-17 16:05:31 -05002459 mp = (struct lpfc_dmabuf *) (pmb->context1);
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) {
2494 ndlp = (struct lpfc_nodelist *)pmb->context2;
2495 lpfc_nlp_put(ndlp);
2496 pmb->context2 = NULL;
2497 }
2498
James Smartdcf2a4e2010-09-29 11:18:53 -04002499 /* Check security permission status on INIT_LINK mailbox command */
2500 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2501 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2502 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2503 "2860 SLI authentication is required "
2504 "for INIT_LINK but has not done yet\n");
2505
James Smart04c68492009-05-22 14:52:52 -04002506 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2507 lpfc_sli4_mbox_cmd_free(phba, pmb);
2508 else
2509 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002510}
James Smartbe6bb942015-04-07 15:07:22 -04002511 /**
2512 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2513 * @phba: Pointer to HBA context object.
2514 * @pmb: Pointer to mailbox object.
2515 *
2516 * This function is the unreg rpi mailbox completion handler. It
2517 * frees the memory resources associated with the completed mailbox
2518 * command. An additional refrenece is put on the ndlp to prevent
2519 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2520 * the unreg mailbox command completes, this routine puts the
2521 * reference back.
2522 *
2523 **/
2524void
2525lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2526{
2527 struct lpfc_vport *vport = pmb->vport;
2528 struct lpfc_nodelist *ndlp;
2529
2530 ndlp = pmb->context1;
2531 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2532 if (phba->sli_rev == LPFC_SLI_REV4 &&
2533 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002534 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002535 LPFC_SLI_INTF_IF_TYPE_2)) {
2536 if (ndlp) {
2537 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2538 "0010 UNREG_LOGIN vpi:%x "
2539 "rpi:%x DID:%x map:%x %p\n",
2540 vport->vpi, ndlp->nlp_rpi,
2541 ndlp->nlp_DID,
2542 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002543 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002544 lpfc_nlp_put(ndlp);
2545 }
2546 }
2547 }
2548
2549 mempool_free(pmb, phba->mbox_mem_pool);
2550}
dea31012005-04-17 16:05:31 -05002551
James Smarte59058c2008-08-24 21:49:00 -04002552/**
James Smart3621a712009-04-06 18:47:14 -04002553 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002554 * @phba: Pointer to HBA context object.
2555 *
2556 * This function is called with no lock held. This function processes all
2557 * the completed mailbox commands and gives it to upper layers. The interrupt
2558 * service routine processes mailbox completion interrupt and adds completed
2559 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2560 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2561 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2562 * function returns the mailbox commands to the upper layer by calling the
2563 * completion handler function of each mailbox.
2564 **/
dea31012005-04-17 16:05:31 -05002565int
James Smart2e0fef82007-06-17 19:56:36 -05002566lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002567{
James Smart92d7f7b2007-06-17 19:56:38 -05002568 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002569 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002570 int rc;
2571 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002572
2573 phba->sli.slistat.mbox_event++;
2574
James Smart92d7f7b2007-06-17 19:56:38 -05002575 /* Get all completed mailboxe buffers into the cmplq */
2576 spin_lock_irq(&phba->hbalock);
2577 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2578 spin_unlock_irq(&phba->hbalock);
2579
dea31012005-04-17 16:05:31 -05002580 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002581 do {
2582 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2583 if (pmb == NULL)
2584 break;
2585
James Smart04c68492009-05-22 14:52:52 -04002586 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002587
James Smart858c9f62007-06-17 19:56:39 -05002588 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2589 if (pmb->vport) {
2590 lpfc_debugfs_disc_trc(pmb->vport,
2591 LPFC_DISC_TRC_MBOX_VPORT,
2592 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2593 (uint32_t)pmbox->mbxCommand,
2594 pmbox->un.varWords[0],
2595 pmbox->un.varWords[1]);
2596 }
2597 else {
2598 lpfc_debugfs_disc_trc(phba->pport,
2599 LPFC_DISC_TRC_MBOX,
2600 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2601 (uint32_t)pmbox->mbxCommand,
2602 pmbox->un.varWords[0],
2603 pmbox->un.varWords[1]);
2604 }
2605 }
2606
dea31012005-04-17 16:05:31 -05002607 /*
2608 * It is a fatal error if unknown mbox command completion.
2609 */
2610 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2611 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002612 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002613 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002614 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002615 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002616 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002617 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002618 lpfc_sli_config_mbox_subsys_get(phba,
2619 pmb),
2620 lpfc_sli_config_mbox_opcode_get(phba,
2621 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002622 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002623 phba->work_hs = HS_FFER3;
2624 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002625 continue;
dea31012005-04-17 16:05:31 -05002626 }
2627
dea31012005-04-17 16:05:31 -05002628 if (pmbox->mbxStatus) {
2629 phba->sli.slistat.mbox_stat_err++;
2630 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2631 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002632 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002633 LOG_MBOX | LOG_SLI,
2634 "(%d):0305 Mbox cmd cmpl "
2635 "error - RETRYing Data: x%x "
2636 "(x%x/x%x) x%x x%x x%x\n",
2637 pmb->vport ? pmb->vport->vpi : 0,
2638 pmbox->mbxCommand,
2639 lpfc_sli_config_mbox_subsys_get(phba,
2640 pmb),
2641 lpfc_sli_config_mbox_opcode_get(phba,
2642 pmb),
2643 pmbox->mbxStatus,
2644 pmbox->un.varWords[0],
2645 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002646 pmbox->mbxStatus = 0;
2647 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002648 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002649 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002650 continue;
dea31012005-04-17 16:05:31 -05002651 }
2652 }
2653
2654 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002655 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002656 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002657 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2658 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002659 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002660 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002661 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2662 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002663 pmb->mbox_cmpl,
2664 *((uint32_t *) pmbox),
2665 pmbox->un.varWords[0],
2666 pmbox->un.varWords[1],
2667 pmbox->un.varWords[2],
2668 pmbox->un.varWords[3],
2669 pmbox->un.varWords[4],
2670 pmbox->un.varWords[5],
2671 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002672 pmbox->un.varWords[7],
2673 pmbox->un.varWords[8],
2674 pmbox->un.varWords[9],
2675 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002676
James Smart92d7f7b2007-06-17 19:56:38 -05002677 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002678 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002679 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002680 return 0;
dea31012005-04-17 16:05:31 -05002681}
James Smart92d7f7b2007-06-17 19:56:38 -05002682
James Smarte59058c2008-08-24 21:49:00 -04002683/**
James Smart3621a712009-04-06 18:47:14 -04002684 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002685 * @phba: Pointer to HBA context object.
2686 * @pring: Pointer to driver SLI ring object.
2687 * @tag: buffer tag.
2688 *
2689 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2690 * is set in the tag the buffer is posted for a particular exchange,
2691 * the function will return the buffer without replacing the buffer.
2692 * If the buffer is for unsolicited ELS or CT traffic, this function
2693 * returns the buffer and also posts another buffer to the firmware.
2694 **/
James Smart76bb24e2007-10-27 13:38:00 -04002695static struct lpfc_dmabuf *
2696lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002697 struct lpfc_sli_ring *pring,
2698 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002699{
James Smart9f1e1b52008-12-04 22:39:40 -05002700 struct hbq_dmabuf *hbq_entry;
2701
James Smart76bb24e2007-10-27 13:38:00 -04002702 if (tag & QUE_BUFTAG_BIT)
2703 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002704 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2705 if (!hbq_entry)
2706 return NULL;
2707 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002708}
James Smart57127f12007-10-27 13:37:05 -04002709
James Smart3772a992009-05-22 14:50:54 -04002710/**
2711 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2712 * @phba: Pointer to HBA context object.
2713 * @pring: Pointer to driver SLI ring object.
2714 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2715 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2716 * @fch_type: the type for the first frame of the sequence.
2717 *
2718 * This function is called with no lock held. This function uses the r_ctl and
2719 * type of the received sequence to find the correct callback function to call
2720 * to process the sequence.
2721 **/
2722static int
2723lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2724 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2725 uint32_t fch_type)
2726{
2727 int i;
2728
James Smartf358dd02017-02-12 13:52:34 -08002729 switch (fch_type) {
2730 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002731 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002732 return 1;
2733 default:
2734 break;
2735 }
2736
James Smart3772a992009-05-22 14:50:54 -04002737 /* unSolicited Responses */
2738 if (pring->prt[0].profile) {
2739 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2740 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2741 saveq);
2742 return 1;
2743 }
2744 /* We must search, based on rctl / type
2745 for the right routine */
2746 for (i = 0; i < pring->num_mask; i++) {
2747 if ((pring->prt[i].rctl == fch_r_ctl) &&
2748 (pring->prt[i].type == fch_type)) {
2749 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2750 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2751 (phba, pring, saveq);
2752 return 1;
2753 }
2754 }
2755 return 0;
2756}
James Smarte59058c2008-08-24 21:49:00 -04002757
2758/**
James Smart3621a712009-04-06 18:47:14 -04002759 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002760 * @phba: Pointer to HBA context object.
2761 * @pring: Pointer to driver SLI ring object.
2762 * @saveq: Pointer to the unsolicited iocb.
2763 *
2764 * This function is called with no lock held by the ring event handler
2765 * when there is an unsolicited iocb posted to the response ring by the
2766 * firmware. This function gets the buffer associated with the iocbs
2767 * and calls the event handler for the ring. This function handles both
2768 * qring buffers and hbq buffers.
2769 * When the function returns 1 the caller can free the iocb object otherwise
2770 * upper layer functions will free the iocb objects.
2771 **/
dea31012005-04-17 16:05:31 -05002772static int
2773lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2774 struct lpfc_iocbq *saveq)
2775{
2776 IOCB_t * irsp;
2777 WORD5 * w5p;
2778 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002779 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002780 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002781
dea31012005-04-17 16:05:31 -05002782 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002783
2784 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2785 if (pring->lpfc_sli_rcv_async_status)
2786 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2787 else
2788 lpfc_printf_log(phba,
2789 KERN_WARNING,
2790 LOG_SLI,
2791 "0316 Ring %d handler: unexpected "
2792 "ASYNC_STATUS iocb received evt_code "
2793 "0x%x\n",
2794 pring->ringno,
2795 irsp->un.asyncstat.evt_code);
2796 return 1;
2797 }
2798
James Smart3163f722008-02-08 18:50:25 -05002799 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2800 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2801 if (irsp->ulpBdeCount > 0) {
2802 dmzbuf = lpfc_sli_get_buff(phba, pring,
2803 irsp->un.ulpWord[3]);
2804 lpfc_in_buf_free(phba, dmzbuf);
2805 }
2806
2807 if (irsp->ulpBdeCount > 1) {
2808 dmzbuf = lpfc_sli_get_buff(phba, pring,
2809 irsp->unsli3.sli3Words[3]);
2810 lpfc_in_buf_free(phba, dmzbuf);
2811 }
2812
2813 if (irsp->ulpBdeCount > 2) {
2814 dmzbuf = lpfc_sli_get_buff(phba, pring,
2815 irsp->unsli3.sli3Words[7]);
2816 lpfc_in_buf_free(phba, dmzbuf);
2817 }
2818
2819 return 1;
2820 }
2821
James Smart92d7f7b2007-06-17 19:56:38 -05002822 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002823 if (irsp->ulpBdeCount != 0) {
2824 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002825 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002826 if (!saveq->context2)
2827 lpfc_printf_log(phba,
2828 KERN_ERR,
2829 LOG_SLI,
2830 "0341 Ring %d Cannot find buffer for "
2831 "an unsolicited iocb. tag 0x%x\n",
2832 pring->ringno,
2833 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002834 }
2835 if (irsp->ulpBdeCount == 2) {
2836 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002837 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002838 if (!saveq->context3)
2839 lpfc_printf_log(phba,
2840 KERN_ERR,
2841 LOG_SLI,
2842 "0342 Ring %d Cannot find buffer for an"
2843 " unsolicited iocb. tag 0x%x\n",
2844 pring->ringno,
2845 irsp->unsli3.sli3Words[7]);
2846 }
2847 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002848 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002849 if (irsp->ulpBdeCount != 0) {
2850 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2851 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002852 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002853 lpfc_printf_log(phba,
2854 KERN_ERR,
2855 LOG_SLI,
2856 "0343 Ring %d Cannot find "
2857 "buffer for an unsolicited iocb"
2858 ". tag 0x%x\n", pring->ringno,
2859 irsp->un.ulpWord[3]);
2860 }
2861 if (irsp->ulpBdeCount == 2) {
2862 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2863 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002864 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002865 lpfc_printf_log(phba,
2866 KERN_ERR,
2867 LOG_SLI,
2868 "0344 Ring %d Cannot find "
2869 "buffer for an unsolicited "
2870 "iocb. tag 0x%x\n",
2871 pring->ringno,
2872 irsp->unsli3.sli3Words[7]);
2873 }
2874 }
James Smart92d7f7b2007-06-17 19:56:38 -05002875 }
James Smart9c2face2008-01-11 01:53:18 -05002876 if (irsp->ulpBdeCount != 0 &&
2877 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2878 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2879 int found = 0;
2880
2881 /* search continue save q for same XRI */
2882 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002883 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2884 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002885 list_add_tail(&saveq->list, &iocbq->list);
2886 found = 1;
2887 break;
2888 }
2889 }
2890 if (!found)
2891 list_add_tail(&saveq->clist,
2892 &pring->iocb_continue_saveq);
2893 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2894 list_del_init(&iocbq->clist);
2895 saveq = iocbq;
2896 irsp = &(saveq->iocb);
2897 } else
2898 return 0;
2899 }
2900 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2901 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2902 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002903 Rctl = FC_RCTL_ELS_REQ;
2904 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002905 } else {
2906 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2907 Rctl = w5p->hcsw.Rctl;
2908 Type = w5p->hcsw.Type;
2909
2910 /* Firmware Workaround */
2911 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2912 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2913 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002914 Rctl = FC_RCTL_ELS_REQ;
2915 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002916 w5p->hcsw.Rctl = Rctl;
2917 w5p->hcsw.Type = Type;
2918 }
2919 }
James Smart92d7f7b2007-06-17 19:56:38 -05002920
James Smart3772a992009-05-22 14:50:54 -04002921 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002922 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002923 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002924 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002925 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002926
James Smart92d7f7b2007-06-17 19:56:38 -05002927 return 1;
dea31012005-04-17 16:05:31 -05002928}
2929
James Smarte59058c2008-08-24 21:49:00 -04002930/**
James Smart3621a712009-04-06 18:47:14 -04002931 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002932 * @phba: Pointer to HBA context object.
2933 * @pring: Pointer to driver SLI ring object.
2934 * @prspiocb: Pointer to response iocb object.
2935 *
2936 * This function looks up the iocb_lookup table to get the command iocb
2937 * corresponding to the given response iocb using the iotag of the
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002938 * response iocb. This function is called with the hbalock held
2939 * for sli3 devices or the ring_lock for sli4 devices.
James Smarte59058c2008-08-24 21:49:00 -04002940 * This function returns the command iocb object if it finds the command
2941 * iocb else returns NULL.
2942 **/
dea31012005-04-17 16:05:31 -05002943static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002944lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2945 struct lpfc_sli_ring *pring,
2946 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002947{
dea31012005-04-17 16:05:31 -05002948 struct lpfc_iocbq *cmd_iocb = NULL;
2949 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002950 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002951
James Bottomley604a3e32005-10-29 10:28:33 -05002952 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002953
James Bottomley604a3e32005-10-29 10:28:33 -05002954 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2955 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002956 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002957 /* remove from txcmpl queue list */
2958 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002959 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002960 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002961 }
dea31012005-04-17 16:05:31 -05002962 }
2963
dea31012005-04-17 16:05:31 -05002964 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002965 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002966 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002967 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002968 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002969 return NULL;
2970}
2971
James Smarte59058c2008-08-24 21:49:00 -04002972/**
James Smart3772a992009-05-22 14:50:54 -04002973 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2974 * @phba: Pointer to HBA context object.
2975 * @pring: Pointer to driver SLI ring object.
2976 * @iotag: IOCB tag.
2977 *
2978 * This function looks up the iocb_lookup table to get the command iocb
2979 * corresponding to the given iotag. This function is called with the
2980 * hbalock held.
2981 * This function returns the command iocb object if it finds the command
2982 * iocb else returns NULL.
2983 **/
2984static struct lpfc_iocbq *
2985lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2986 struct lpfc_sli_ring *pring, uint16_t iotag)
2987{
James Smart895427b2017-02-12 13:52:30 -08002988 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002989
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002990 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002991 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2992 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002993 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2994 /* remove from txcmpl queue list */
2995 list_del_init(&cmd_iocb->list);
2996 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002997 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002998 }
James Smart3772a992009-05-22 14:50:54 -04002999 }
James Smart89533e92016-10-13 15:06:15 -07003000
James Smart3772a992009-05-22 14:50:54 -04003001 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08003002 "0372 iotag x%x lookup error: max iotag (x%x) "
3003 "iocb_flag x%x\n",
3004 iotag, phba->sli.last_iotag,
3005 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003006 return NULL;
3007}
3008
3009/**
James Smart3621a712009-04-06 18:47:14 -04003010 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003011 * @phba: Pointer to HBA context object.
3012 * @pring: Pointer to driver SLI ring object.
3013 * @saveq: Pointer to the response iocb to be processed.
3014 *
3015 * This function is called by the ring event handler for non-fcp
3016 * rings when there is a new response iocb in the response ring.
3017 * The caller is not required to hold any locks. This function
3018 * gets the command iocb associated with the response iocb and
3019 * calls the completion handler for the command iocb. If there
3020 * is no completion handler, the function will free the resources
3021 * associated with command iocb. If the response iocb is for
3022 * an already aborted command iocb, the status of the completion
3023 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3024 * This function always returns 1.
3025 **/
dea31012005-04-17 16:05:31 -05003026static int
James Smart2e0fef82007-06-17 19:56:36 -05003027lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003028 struct lpfc_iocbq *saveq)
3029{
James Smart2e0fef82007-06-17 19:56:36 -05003030 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003031 int rc = 1;
3032 unsigned long iflag;
3033
3034 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003035 if (phba->sli_rev == LPFC_SLI_REV4)
3036 spin_lock_irqsave(&pring->ring_lock, iflag);
3037 else
3038 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003039 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003040 if (phba->sli_rev == LPFC_SLI_REV4)
3041 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3042 else
3043 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart2e0fef82007-06-17 19:56:36 -05003044
dea31012005-04-17 16:05:31 -05003045 if (cmdiocbp) {
3046 if (cmdiocbp->iocb_cmpl) {
3047 /*
James Smartea2151b2008-09-07 11:52:10 -04003048 * If an ELS command failed send an event to mgmt
3049 * application.
3050 */
3051 if (saveq->iocb.ulpStatus &&
3052 (pring->ringno == LPFC_ELS_RING) &&
3053 (cmdiocbp->iocb.ulpCommand ==
3054 CMD_ELS_REQUEST64_CR))
3055 lpfc_send_els_failure_event(phba,
3056 cmdiocbp, saveq);
3057
3058 /*
dea31012005-04-17 16:05:31 -05003059 * Post all ELS completions to the worker thread.
3060 * All other are passed to the completion callback.
3061 */
3062 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003063 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3064 (cmdiocbp->iocb_flag &
3065 LPFC_DRIVER_ABORTED)) {
3066 spin_lock_irqsave(&phba->hbalock,
3067 iflag);
James Smart07951072007-04-25 09:51:38 -04003068 cmdiocbp->iocb_flag &=
3069 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003070 spin_unlock_irqrestore(&phba->hbalock,
3071 iflag);
James Smart07951072007-04-25 09:51:38 -04003072 saveq->iocb.ulpStatus =
3073 IOSTAT_LOCAL_REJECT;
3074 saveq->iocb.un.ulpWord[4] =
3075 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003076
3077 /* Firmware could still be in progress
3078 * of DMAing payload, so don't free data
3079 * buffer till after a hbeat.
3080 */
James Smart341af102010-01-26 23:07:37 -05003081 spin_lock_irqsave(&phba->hbalock,
3082 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003083 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003084 spin_unlock_irqrestore(&phba->hbalock,
3085 iflag);
3086 }
James Smart0f65ff62010-02-26 14:14:23 -05003087 if (phba->sli_rev == LPFC_SLI_REV4) {
3088 if (saveq->iocb_flag &
3089 LPFC_EXCHANGE_BUSY) {
3090 /* Set cmdiocb flag for the
3091 * exchange busy so sgl (xri)
3092 * will not be released until
3093 * the abort xri is received
3094 * from hba.
3095 */
3096 spin_lock_irqsave(
3097 &phba->hbalock, iflag);
3098 cmdiocbp->iocb_flag |=
3099 LPFC_EXCHANGE_BUSY;
3100 spin_unlock_irqrestore(
3101 &phba->hbalock, iflag);
3102 }
3103 if (cmdiocbp->iocb_flag &
3104 LPFC_DRIVER_ABORTED) {
3105 /*
3106 * Clear LPFC_DRIVER_ABORTED
3107 * bit in case it was driver
3108 * initiated abort.
3109 */
3110 spin_lock_irqsave(
3111 &phba->hbalock, iflag);
3112 cmdiocbp->iocb_flag &=
3113 ~LPFC_DRIVER_ABORTED;
3114 spin_unlock_irqrestore(
3115 &phba->hbalock, iflag);
3116 cmdiocbp->iocb.ulpStatus =
3117 IOSTAT_LOCAL_REJECT;
3118 cmdiocbp->iocb.un.ulpWord[4] =
3119 IOERR_ABORT_REQUESTED;
3120 /*
3121 * For SLI4, irsiocb contains
3122 * NO_XRI in sli_xritag, it
3123 * shall not affect releasing
3124 * sgl (xri) process.
3125 */
3126 saveq->iocb.ulpStatus =
3127 IOSTAT_LOCAL_REJECT;
3128 saveq->iocb.un.ulpWord[4] =
3129 IOERR_SLI_ABORTED;
3130 spin_lock_irqsave(
3131 &phba->hbalock, iflag);
3132 saveq->iocb_flag |=
3133 LPFC_DELAY_MEM_FREE;
3134 spin_unlock_irqrestore(
3135 &phba->hbalock, iflag);
3136 }
James Smart07951072007-04-25 09:51:38 -04003137 }
dea31012005-04-17 16:05:31 -05003138 }
James Smart2e0fef82007-06-17 19:56:36 -05003139 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003140 } else
3141 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003142 } else {
3143 /*
3144 * Unknown initiating command based on the response iotag.
3145 * This could be the case on the ELS ring because of
3146 * lpfc_els_abort().
3147 */
3148 if (pring->ringno != LPFC_ELS_RING) {
3149 /*
3150 * Ring <ringno> handler: unexpected completion IoTag
3151 * <IoTag>
3152 */
James Smarta257bf92009-04-06 18:48:10 -04003153 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003154 "0322 Ring %d handler: "
3155 "unexpected completion IoTag x%x "
3156 "Data: x%x x%x x%x x%x\n",
3157 pring->ringno,
3158 saveq->iocb.ulpIoTag,
3159 saveq->iocb.ulpStatus,
3160 saveq->iocb.un.ulpWord[4],
3161 saveq->iocb.ulpCommand,
3162 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003163 }
3164 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003165
dea31012005-04-17 16:05:31 -05003166 return rc;
3167}
3168
James Smarte59058c2008-08-24 21:49:00 -04003169/**
James Smart3621a712009-04-06 18:47:14 -04003170 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003171 * @phba: Pointer to HBA context object.
3172 * @pring: Pointer to driver SLI ring object.
3173 *
3174 * This function is called from the iocb ring event handlers when
3175 * put pointer is ahead of the get pointer for a ring. This function signal
3176 * an error attention condition to the worker thread and the worker
3177 * thread will transition the HBA to offline state.
3178 **/
James Smart2e0fef82007-06-17 19:56:36 -05003179static void
3180lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003181{
James Smart34b02dc2008-08-24 21:49:55 -04003182 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003183 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003184 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003185 * rsp ring <portRspMax>
3186 */
3187 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003188 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003189 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003190 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003191 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003192
James Smart2e0fef82007-06-17 19:56:36 -05003193 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003194
3195 /*
3196 * All error attention handlers are posted to
3197 * worker thread
3198 */
3199 phba->work_ha |= HA_ERATT;
3200 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003201
James Smart5e9d9b82008-06-14 22:52:53 -04003202 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003203
3204 return;
3205}
3206
James Smarte59058c2008-08-24 21:49:00 -04003207/**
James Smart3621a712009-04-06 18:47:14 -04003208 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003209 * @ptr: Pointer to address of HBA context object.
3210 *
3211 * This function is invoked by the Error Attention polling timer when the
3212 * timer times out. It will check the SLI Error Attention register for
3213 * possible attention events. If so, it will post an Error Attention event
3214 * and wake up worker thread to process it. Otherwise, it will set up the
3215 * Error Attention polling timer for the next poll.
3216 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003217void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003218{
3219 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003220 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003221 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003222
Kees Cookf22eb4d2017-09-06 20:24:26 -07003223 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003224
James Smartaa6fbb72012-08-03 12:36:03 -04003225 /* Here we will also keep track of interrupts per sec of the hba */
3226 sli_intr = phba->sli.slistat.sli_intr;
3227
3228 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3229 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3230 sli_intr);
3231 else
3232 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3233
James Smart65791f12016-07-06 12:35:56 -07003234 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3235 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003236 phba->sli.slistat.sli_ips = cnt;
3237
3238 phba->sli.slistat.sli_prev_intr = sli_intr;
3239
James Smart93996272008-08-24 21:50:30 -04003240 /* Check chip HA register for error event */
3241 eratt = lpfc_sli_check_eratt(phba);
3242
3243 if (eratt)
3244 /* Tell the worker thread there is work to do */
3245 lpfc_worker_wake_up(phba);
3246 else
3247 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003248 mod_timer(&phba->eratt_poll,
3249 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003250 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003251 return;
3252}
3253
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003254
James Smarte59058c2008-08-24 21:49:00 -04003255/**
James Smart3621a712009-04-06 18:47:14 -04003256 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003257 * @phba: Pointer to HBA context object.
3258 * @pring: Pointer to driver SLI ring object.
3259 * @mask: Host attention register mask for this ring.
3260 *
3261 * This function is called from the interrupt context when there is a ring
3262 * event for the fcp ring. The caller does not hold any lock.
3263 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003264 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003265 * LE bit set. The function will call the completion handler of the command iocb
3266 * if the response iocb indicates a completion for a command iocb or it is
3267 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3268 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003269 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003270 * to check it explicitly.
3271 */
3272int
James Smart2e0fef82007-06-17 19:56:36 -05003273lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3274 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003275{
James Smart34b02dc2008-08-24 21:49:55 -04003276 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003277 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003278 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003279 struct lpfc_iocbq *cmdiocbq = NULL;
3280 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003281 uint32_t status;
3282 uint32_t portRspPut, portRspMax;
3283 int rc = 1;
3284 lpfc_iocb_type type;
3285 unsigned long iflag;
3286 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003287
James Smart2e0fef82007-06-17 19:56:36 -05003288 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003289 pring->stats.iocb_event++;
3290
dea31012005-04-17 16:05:31 -05003291 /*
3292 * The next available response entry should never exceed the maximum
3293 * entries. If it does, treat it as an adapter hardware error.
3294 */
James Smart7e56aa22012-08-03 12:35:34 -04003295 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003296 portRspPut = le32_to_cpu(pgp->rspPutInx);
3297 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003298 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003299 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003300 return 1;
3301 }
James Smart45ed1192009-10-02 15:17:02 -04003302 if (phba->fcp_ring_in_use) {
3303 spin_unlock_irqrestore(&phba->hbalock, iflag);
3304 return 1;
3305 } else
3306 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003307
3308 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003309 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003310 /*
3311 * Fetch an entry off the ring and copy it into a local data
3312 * structure. The copy involves a byte-swap since the
3313 * network byte order and pci byte orders are different.
3314 */
James Smarted957682007-06-17 19:56:37 -05003315 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003316 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003317
James Smart7e56aa22012-08-03 12:35:34 -04003318 if (++pring->sli.sli3.rspidx >= portRspMax)
3319 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003320
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003321 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3322 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003323 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003324 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003325 irsp = &rspiocbq.iocb;
3326
dea31012005-04-17 16:05:31 -05003327 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3328 pring->stats.iocb_rsp++;
3329 rsp_cmpl++;
3330
3331 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003332 /*
3333 * If resource errors reported from HBA, reduce
3334 * queuedepths of the SCSI device.
3335 */
3336 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003337 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3338 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003339 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003340 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003341 spin_lock_irqsave(&phba->hbalock, iflag);
3342 }
3343
dea31012005-04-17 16:05:31 -05003344 /* Rsp ring <ringno> error: IOCB */
3345 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003346 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003347 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003348 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003349 irsp->un.ulpWord[0],
3350 irsp->un.ulpWord[1],
3351 irsp->un.ulpWord[2],
3352 irsp->un.ulpWord[3],
3353 irsp->un.ulpWord[4],
3354 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003355 *(uint32_t *)&irsp->un1,
3356 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003357 }
3358
3359 switch (type) {
3360 case LPFC_ABORT_IOCB:
3361 case LPFC_SOL_IOCB:
3362 /*
3363 * Idle exchange closed via ABTS from port. No iocb
3364 * resources need to be recovered.
3365 */
3366 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003367 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003368 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003369 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003370 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003371 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003372 break;
3373 }
3374
James Bottomley604a3e32005-10-29 10:28:33 -05003375 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3376 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003377 if (unlikely(!cmdiocbq))
3378 break;
3379 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3380 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3381 if (cmdiocbq->iocb_cmpl) {
3382 spin_unlock_irqrestore(&phba->hbalock, iflag);
3383 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3384 &rspiocbq);
3385 spin_lock_irqsave(&phba->hbalock, iflag);
3386 }
dea31012005-04-17 16:05:31 -05003387 break;
James Smarta4bc3372006-12-02 13:34:16 -05003388 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003389 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003390 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003391 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003392 break;
dea31012005-04-17 16:05:31 -05003393 default:
3394 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3395 char adaptermsg[LPFC_MAX_ADPTMSG];
3396 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3397 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3398 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003399 dev_warn(&((phba->pcidev)->dev),
3400 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003401 phba->brd_no, adaptermsg);
3402 } else {
3403 /* Unknown IOCB command */
3404 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003405 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003406 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003407 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003408 irsp->ulpStatus,
3409 irsp->ulpIoTag,
3410 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003411 }
3412 break;
3413 }
3414
3415 /*
3416 * The response IOCB has been processed. Update the ring
3417 * pointer in SLIM. If the port response put pointer has not
3418 * been updated, sync the pgp->rspPutInx and fetch the new port
3419 * response put pointer.
3420 */
James Smart7e56aa22012-08-03 12:35:34 -04003421 writel(pring->sli.sli3.rspidx,
3422 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003423
James Smart7e56aa22012-08-03 12:35:34 -04003424 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003425 portRspPut = le32_to_cpu(pgp->rspPutInx);
3426 }
3427
3428 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3429 pring->stats.iocb_rsp_full++;
3430 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3431 writel(status, phba->CAregaddr);
3432 readl(phba->CAregaddr);
3433 }
3434 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3435 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3436 pring->stats.iocb_cmd_empty++;
3437
3438 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003439 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003440 lpfc_sli_resume_iocb(phba, pring);
3441
3442 if ((pring->lpfc_sli_cmd_available))
3443 (pring->lpfc_sli_cmd_available) (phba, pring);
3444
3445 }
3446
James Smart45ed1192009-10-02 15:17:02 -04003447 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003448 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003449 return rc;
3450}
3451
James Smarte59058c2008-08-24 21:49:00 -04003452/**
James Smart3772a992009-05-22 14:50:54 -04003453 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3454 * @phba: Pointer to HBA context object.
3455 * @pring: Pointer to driver SLI ring object.
3456 * @rspiocbp: Pointer to driver response IOCB object.
3457 *
3458 * This function is called from the worker thread when there is a slow-path
3459 * response IOCB to process. This function chains all the response iocbs until
3460 * seeing the iocb with the LE bit set. The function will call
3461 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3462 * completion of a command iocb. The function will call the
3463 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3464 * The function frees the resources or calls the completion handler if this
3465 * iocb is an abort completion. The function returns NULL when the response
3466 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3467 * this function shall chain the iocb on to the iocb_continueq and return the
3468 * response iocb passed in.
3469 **/
3470static struct lpfc_iocbq *
3471lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3472 struct lpfc_iocbq *rspiocbp)
3473{
3474 struct lpfc_iocbq *saveq;
3475 struct lpfc_iocbq *cmdiocbp;
3476 struct lpfc_iocbq *next_iocb;
3477 IOCB_t *irsp = NULL;
3478 uint32_t free_saveq;
3479 uint8_t iocb_cmd_type;
3480 lpfc_iocb_type type;
3481 unsigned long iflag;
3482 int rc;
3483
3484 spin_lock_irqsave(&phba->hbalock, iflag);
3485 /* First add the response iocb to the countinueq list */
3486 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3487 pring->iocb_continueq_cnt++;
3488
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003489 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003490 irsp = &rspiocbp->iocb;
3491 if (irsp->ulpLe) {
3492 /*
3493 * By default, the driver expects to free all resources
3494 * associated with this iocb completion.
3495 */
3496 free_saveq = 1;
3497 saveq = list_get_first(&pring->iocb_continueq,
3498 struct lpfc_iocbq, list);
3499 irsp = &(saveq->iocb);
3500 list_del_init(&pring->iocb_continueq);
3501 pring->iocb_continueq_cnt = 0;
3502
3503 pring->stats.iocb_rsp++;
3504
3505 /*
3506 * If resource errors reported from HBA, reduce
3507 * queuedepths of the SCSI device.
3508 */
3509 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003510 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3511 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003512 spin_unlock_irqrestore(&phba->hbalock, iflag);
3513 phba->lpfc_rampdown_queue_depth(phba);
3514 spin_lock_irqsave(&phba->hbalock, iflag);
3515 }
3516
3517 if (irsp->ulpStatus) {
3518 /* Rsp ring <ringno> error: IOCB */
3519 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3520 "0328 Rsp Ring %d error: "
3521 "IOCB Data: "
3522 "x%x x%x x%x x%x "
3523 "x%x x%x x%x x%x "
3524 "x%x x%x x%x x%x "
3525 "x%x x%x x%x x%x\n",
3526 pring->ringno,
3527 irsp->un.ulpWord[0],
3528 irsp->un.ulpWord[1],
3529 irsp->un.ulpWord[2],
3530 irsp->un.ulpWord[3],
3531 irsp->un.ulpWord[4],
3532 irsp->un.ulpWord[5],
3533 *(((uint32_t *) irsp) + 6),
3534 *(((uint32_t *) irsp) + 7),
3535 *(((uint32_t *) irsp) + 8),
3536 *(((uint32_t *) irsp) + 9),
3537 *(((uint32_t *) irsp) + 10),
3538 *(((uint32_t *) irsp) + 11),
3539 *(((uint32_t *) irsp) + 12),
3540 *(((uint32_t *) irsp) + 13),
3541 *(((uint32_t *) irsp) + 14),
3542 *(((uint32_t *) irsp) + 15));
3543 }
3544
3545 /*
3546 * Fetch the IOCB command type and call the correct completion
3547 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3548 * get freed back to the lpfc_iocb_list by the discovery
3549 * kernel thread.
3550 */
3551 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3552 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3553 switch (type) {
3554 case LPFC_SOL_IOCB:
3555 spin_unlock_irqrestore(&phba->hbalock, iflag);
3556 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3557 spin_lock_irqsave(&phba->hbalock, iflag);
3558 break;
3559
3560 case LPFC_UNSOL_IOCB:
3561 spin_unlock_irqrestore(&phba->hbalock, iflag);
3562 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3563 spin_lock_irqsave(&phba->hbalock, iflag);
3564 if (!rc)
3565 free_saveq = 0;
3566 break;
3567
3568 case LPFC_ABORT_IOCB:
3569 cmdiocbp = NULL;
3570 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3571 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3572 saveq);
3573 if (cmdiocbp) {
3574 /* Call the specified completion routine */
3575 if (cmdiocbp->iocb_cmpl) {
3576 spin_unlock_irqrestore(&phba->hbalock,
3577 iflag);
3578 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3579 saveq);
3580 spin_lock_irqsave(&phba->hbalock,
3581 iflag);
3582 } else
3583 __lpfc_sli_release_iocbq(phba,
3584 cmdiocbp);
3585 }
3586 break;
3587
3588 case LPFC_UNKNOWN_IOCB:
3589 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3590 char adaptermsg[LPFC_MAX_ADPTMSG];
3591 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3592 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3593 MAX_MSG_DATA);
3594 dev_warn(&((phba->pcidev)->dev),
3595 "lpfc%d: %s\n",
3596 phba->brd_no, adaptermsg);
3597 } else {
3598 /* Unknown IOCB command */
3599 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3600 "0335 Unknown IOCB "
3601 "command Data: x%x "
3602 "x%x x%x x%x\n",
3603 irsp->ulpCommand,
3604 irsp->ulpStatus,
3605 irsp->ulpIoTag,
3606 irsp->ulpContext);
3607 }
3608 break;
3609 }
3610
3611 if (free_saveq) {
3612 list_for_each_entry_safe(rspiocbp, next_iocb,
3613 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003614 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003615 __lpfc_sli_release_iocbq(phba, rspiocbp);
3616 }
3617 __lpfc_sli_release_iocbq(phba, saveq);
3618 }
3619 rspiocbp = NULL;
3620 }
3621 spin_unlock_irqrestore(&phba->hbalock, iflag);
3622 return rspiocbp;
3623}
3624
3625/**
3626 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003627 * @phba: Pointer to HBA context object.
3628 * @pring: Pointer to driver SLI ring object.
3629 * @mask: Host attention register mask for this ring.
3630 *
James Smart3772a992009-05-22 14:50:54 -04003631 * This routine wraps the actual slow_ring event process routine from the
3632 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003633 **/
James Smart3772a992009-05-22 14:50:54 -04003634void
James Smart2e0fef82007-06-17 19:56:36 -05003635lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3636 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003637{
James Smart3772a992009-05-22 14:50:54 -04003638 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3639}
3640
3641/**
3642 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3643 * @phba: Pointer to HBA context object.
3644 * @pring: Pointer to driver SLI ring object.
3645 * @mask: Host attention register mask for this ring.
3646 *
3647 * This function is called from the worker thread when there is a ring event
3648 * for non-fcp rings. The caller does not hold any lock. The function will
3649 * remove each response iocb in the response ring and calls the handle
3650 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3651 **/
3652static void
3653lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3654 struct lpfc_sli_ring *pring, uint32_t mask)
3655{
James Smart34b02dc2008-08-24 21:49:55 -04003656 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003657 IOCB_t *entry;
3658 IOCB_t *irsp = NULL;
3659 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003660 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003661 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003662 uint32_t status;
dea31012005-04-17 16:05:31 -05003663
James Smart34b02dc2008-08-24 21:49:55 -04003664 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003665 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003666 pring->stats.iocb_event++;
3667
dea31012005-04-17 16:05:31 -05003668 /*
3669 * The next available response entry should never exceed the maximum
3670 * entries. If it does, treat it as an adapter hardware error.
3671 */
James Smart7e56aa22012-08-03 12:35:34 -04003672 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003673 portRspPut = le32_to_cpu(pgp->rspPutInx);
3674 if (portRspPut >= portRspMax) {
3675 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003676 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003677 * rsp ring <portRspMax>
3678 */
James Smarted957682007-06-17 19:56:37 -05003679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003680 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003681 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003682 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003683
James Smart2e0fef82007-06-17 19:56:36 -05003684 phba->link_state = LPFC_HBA_ERROR;
3685 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003686
3687 phba->work_hs = HS_FFER3;
3688 lpfc_handle_eratt(phba);
3689
James Smart3772a992009-05-22 14:50:54 -04003690 return;
dea31012005-04-17 16:05:31 -05003691 }
3692
3693 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003694 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003695 /*
3696 * Build a completion list and call the appropriate handler.
3697 * The process is to get the next available response iocb, get
3698 * a free iocb from the list, copy the response data into the
3699 * free iocb, insert to the continuation list, and update the
3700 * next response index to slim. This process makes response
3701 * iocb's in the ring available to DMA as fast as possible but
3702 * pays a penalty for a copy operation. Since the iocb is
3703 * only 32 bytes, this penalty is considered small relative to
3704 * the PCI reads for register values and a slim write. When
3705 * the ulpLe field is set, the entire Command has been
3706 * received.
3707 */
James Smarted957682007-06-17 19:56:37 -05003708 entry = lpfc_resp_iocb(phba, pring);
3709
James Smart858c9f62007-06-17 19:56:39 -05003710 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003711 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003712 if (rspiocbp == NULL) {
3713 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003714 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003715 break;
3716 }
3717
James Smarted957682007-06-17 19:56:37 -05003718 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3719 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003720 irsp = &rspiocbp->iocb;
3721
James Smart7e56aa22012-08-03 12:35:34 -04003722 if (++pring->sli.sli3.rspidx >= portRspMax)
3723 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003724
James Smarta58cbd52007-08-02 11:09:43 -04003725 if (pring->ringno == LPFC_ELS_RING) {
3726 lpfc_debugfs_slow_ring_trc(phba,
3727 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3728 *(((uint32_t *) irsp) + 4),
3729 *(((uint32_t *) irsp) + 6),
3730 *(((uint32_t *) irsp) + 7));
3731 }
3732
James Smart7e56aa22012-08-03 12:35:34 -04003733 writel(pring->sli.sli3.rspidx,
3734 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003735
James Smart3772a992009-05-22 14:50:54 -04003736 spin_unlock_irqrestore(&phba->hbalock, iflag);
3737 /* Handle the response IOCB */
3738 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3739 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003740
3741 /*
3742 * If the port response put pointer has not been updated, sync
3743 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3744 * response put pointer.
3745 */
James Smart7e56aa22012-08-03 12:35:34 -04003746 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003747 portRspPut = le32_to_cpu(pgp->rspPutInx);
3748 }
James Smart7e56aa22012-08-03 12:35:34 -04003749 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003750
James Smart92d7f7b2007-06-17 19:56:38 -05003751 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003752 /* At least one response entry has been freed */
3753 pring->stats.iocb_rsp_full++;
3754 /* SET RxRE_RSP in Chip Att register */
3755 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3756 writel(status, phba->CAregaddr);
3757 readl(phba->CAregaddr); /* flush */
3758 }
3759 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3760 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3761 pring->stats.iocb_cmd_empty++;
3762
3763 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003764 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003765 lpfc_sli_resume_iocb(phba, pring);
3766
3767 if ((pring->lpfc_sli_cmd_available))
3768 (pring->lpfc_sli_cmd_available) (phba, pring);
3769
3770 }
3771
James Smart2e0fef82007-06-17 19:56:36 -05003772 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003773 return;
dea31012005-04-17 16:05:31 -05003774}
3775
James Smarte59058c2008-08-24 21:49:00 -04003776/**
James Smart4f774512009-05-22 14:52:35 -04003777 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3778 * @phba: Pointer to HBA context object.
3779 * @pring: Pointer to driver SLI ring object.
3780 * @mask: Host attention register mask for this ring.
3781 *
3782 * This function is called from the worker thread when there is a pending
3783 * ELS response iocb on the driver internal slow-path response iocb worker
3784 * queue. The caller does not hold any lock. The function will remove each
3785 * response iocb from the response worker queue and calls the handle
3786 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3787 **/
3788static void
3789lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3790 struct lpfc_sli_ring *pring, uint32_t mask)
3791{
3792 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003793 struct hbq_dmabuf *dmabuf;
3794 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003795 unsigned long iflag;
James Smart0ef01a22018-09-10 10:30:45 -07003796 int count = 0;
James Smart4f774512009-05-22 14:52:35 -04003797
James Smart45ed1192009-10-02 15:17:02 -04003798 spin_lock_irqsave(&phba->hbalock, iflag);
3799 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3800 spin_unlock_irqrestore(&phba->hbalock, iflag);
3801 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003802 /* Get the response iocb from the head of work queue */
3803 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003804 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003805 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003806 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003807
3808 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3809 case CQE_CODE_COMPL_WQE:
3810 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3811 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003812 /* Translate ELS WCQE to response IOCBQ */
3813 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3814 irspiocbq);
3815 if (irspiocbq)
3816 lpfc_sli_sp_handle_rspiocb(phba, pring,
3817 irspiocbq);
James Smart0ef01a22018-09-10 10:30:45 -07003818 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003819 break;
3820 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003821 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003822 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3823 cq_event);
3824 lpfc_sli4_handle_received_buffer(phba, dmabuf);
James Smart0ef01a22018-09-10 10:30:45 -07003825 count++;
James Smart4d9ab992009-10-02 15:16:39 -04003826 break;
3827 default:
3828 break;
3829 }
James Smart0ef01a22018-09-10 10:30:45 -07003830
3831 /* Limit the number of events to 64 to avoid soft lockups */
3832 if (count == 64)
3833 break;
James Smart4f774512009-05-22 14:52:35 -04003834 }
3835}
3836
3837/**
James Smart3621a712009-04-06 18:47:14 -04003838 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003839 * @phba: Pointer to HBA context object.
3840 * @pring: Pointer to driver SLI ring object.
3841 *
3842 * This function aborts all iocbs in the given ring and frees all the iocb
3843 * objects in txq. This function issues an abort iocb for all the iocb commands
3844 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3845 * the return of this function. The caller is not required to hold any locks.
3846 **/
James Smart2e0fef82007-06-17 19:56:36 -05003847void
dea31012005-04-17 16:05:31 -05003848lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3849{
James Smart2534ba72007-04-25 09:52:20 -04003850 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003851 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003852
James Smart92d7f7b2007-06-17 19:56:38 -05003853 if (pring->ringno == LPFC_ELS_RING) {
3854 lpfc_fabric_abort_hba(phba);
3855 }
3856
dea31012005-04-17 16:05:31 -05003857 /* Error everything on txq and txcmplq
3858 * First do the txq.
3859 */
James Smartdb55fba2014-04-04 13:52:02 -04003860 if (phba->sli_rev >= LPFC_SLI_REV4) {
3861 spin_lock_irq(&pring->ring_lock);
3862 list_splice_init(&pring->txq, &completions);
3863 pring->txq_cnt = 0;
3864 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003865
James Smartdb55fba2014-04-04 13:52:02 -04003866 spin_lock_irq(&phba->hbalock);
3867 /* Next issue ABTS for everything on the txcmplq */
3868 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3869 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3870 spin_unlock_irq(&phba->hbalock);
3871 } else {
3872 spin_lock_irq(&phba->hbalock);
3873 list_splice_init(&pring->txq, &completions);
3874 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003875
James Smartdb55fba2014-04-04 13:52:02 -04003876 /* Next issue ABTS for everything on the txcmplq */
3877 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3878 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3879 spin_unlock_irq(&phba->hbalock);
3880 }
James Smart2534ba72007-04-25 09:52:20 -04003881
James Smarta257bf92009-04-06 18:48:10 -04003882 /* Cancel all the IOCBs from the completions list */
3883 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3884 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003885}
3886
James Smarte59058c2008-08-24 21:49:00 -04003887/**
James Smart895427b2017-02-12 13:52:30 -08003888 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3889 * @phba: Pointer to HBA context object.
3890 * @pring: Pointer to driver SLI ring object.
3891 *
3892 * This function aborts all iocbs in the given ring and frees all the iocb
3893 * objects in txq. This function issues an abort iocb for all the iocb commands
3894 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3895 * the return of this function. The caller is not required to hold any locks.
3896 **/
3897void
3898lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3899{
3900 LIST_HEAD(completions);
3901 struct lpfc_iocbq *iocb, *next_iocb;
3902
3903 if (pring->ringno == LPFC_ELS_RING)
3904 lpfc_fabric_abort_hba(phba);
3905
3906 spin_lock_irq(&phba->hbalock);
3907 /* Next issue ABTS for everything on the txcmplq */
3908 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3909 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3910 spin_unlock_irq(&phba->hbalock);
3911}
3912
3913
3914/**
James Smartdb55fba2014-04-04 13:52:02 -04003915 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3916 * @phba: Pointer to HBA context object.
3917 * @pring: Pointer to driver SLI ring object.
3918 *
3919 * This function aborts all iocbs in FCP rings and frees all the iocb
3920 * objects in txq. This function issues an abort iocb for all the iocb commands
3921 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3922 * the return of this function. The caller is not required to hold any locks.
3923 **/
3924void
3925lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3926{
3927 struct lpfc_sli *psli = &phba->sli;
3928 struct lpfc_sli_ring *pring;
3929 uint32_t i;
3930
3931 /* Look on all the FCP Rings for the iotag */
3932 if (phba->sli_rev >= LPFC_SLI_REV4) {
3933 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003934 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003935 lpfc_sli_abort_iocb_ring(phba, pring);
3936 }
3937 } else {
James Smart895427b2017-02-12 13:52:30 -08003938 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003939 lpfc_sli_abort_iocb_ring(phba, pring);
3940 }
3941}
3942
James Smart895427b2017-02-12 13:52:30 -08003943/**
3944 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3945 * @phba: Pointer to HBA context object.
3946 *
3947 * This function aborts all wqes in NVME rings. This function issues an
3948 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3949 * the txcmplq is not guaranteed to complete before the return of this
3950 * function. The caller is not required to hold any locks.
3951 **/
3952void
3953lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3954{
3955 struct lpfc_sli_ring *pring;
3956 uint32_t i;
3957
3958 if (phba->sli_rev < LPFC_SLI_REV4)
3959 return;
3960
3961 /* Abort all IO on each NVME ring. */
3962 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3963 pring = phba->sli4_hba.nvme_wq[i]->pring;
3964 lpfc_sli_abort_wqe_ring(phba, pring);
3965 }
3966}
3967
James Smartdb55fba2014-04-04 13:52:02 -04003968
3969/**
James Smart3621a712009-04-06 18:47:14 -04003970 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003971 * @phba: Pointer to HBA context object.
3972 *
3973 * This function flushes all iocbs in the fcp ring and frees all the iocb
3974 * objects in txq and txcmplq. This function will not issue abort iocbs
3975 * for all the iocb commands in txcmplq, they will just be returned with
3976 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3977 * slot has been permanently disabled.
3978 **/
3979void
3980lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3981{
3982 LIST_HEAD(txq);
3983 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003984 struct lpfc_sli *psli = &phba->sli;
3985 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003986 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08003987 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04003988
3989 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003990 /* Indicate the I/O queues are flushed */
3991 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003992 spin_unlock_irq(&phba->hbalock);
3993
James Smartdb55fba2014-04-04 13:52:02 -04003994 /* Look on all the FCP Rings for the iotag */
3995 if (phba->sli_rev >= LPFC_SLI_REV4) {
3996 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003997 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003998
James Smartdb55fba2014-04-04 13:52:02 -04003999 spin_lock_irq(&pring->ring_lock);
4000 /* Retrieve everything on txq */
4001 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004002 list_for_each_entry_safe(piocb, next_iocb,
4003 &pring->txcmplq, list)
4004 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004005 /* Retrieve everything on the txcmplq */
4006 list_splice_init(&pring->txcmplq, &txcmplq);
4007 pring->txq_cnt = 0;
4008 pring->txcmplq_cnt = 0;
4009 spin_unlock_irq(&pring->ring_lock);
4010
4011 /* Flush the txq */
4012 lpfc_sli_cancel_iocbs(phba, &txq,
4013 IOSTAT_LOCAL_REJECT,
4014 IOERR_SLI_DOWN);
4015 /* Flush the txcmpq */
4016 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4017 IOSTAT_LOCAL_REJECT,
4018 IOERR_SLI_DOWN);
4019 }
4020 } else {
James Smart895427b2017-02-12 13:52:30 -08004021 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004022
4023 spin_lock_irq(&phba->hbalock);
4024 /* Retrieve everything on txq */
4025 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004026 list_for_each_entry_safe(piocb, next_iocb,
4027 &pring->txcmplq, list)
4028 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004029 /* Retrieve everything on the txcmplq */
4030 list_splice_init(&pring->txcmplq, &txcmplq);
4031 pring->txq_cnt = 0;
4032 pring->txcmplq_cnt = 0;
4033 spin_unlock_irq(&phba->hbalock);
4034
4035 /* Flush the txq */
4036 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4037 IOERR_SLI_DOWN);
4038 /* Flush the txcmpq */
4039 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4040 IOERR_SLI_DOWN);
4041 }
James Smarta8e497d2008-08-24 21:50:11 -04004042}
4043
4044/**
James Smart895427b2017-02-12 13:52:30 -08004045 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4046 * @phba: Pointer to HBA context object.
4047 *
4048 * This function flushes all wqes in the nvme rings and frees all resources
4049 * in the txcmplq. This function does not issue abort wqes for the IO
4050 * commands in txcmplq, they will just be returned with
4051 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4052 * slot has been permanently disabled.
4053 **/
4054void
4055lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4056{
4057 LIST_HEAD(txcmplq);
4058 struct lpfc_sli_ring *pring;
4059 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004060 struct lpfc_iocbq *piocb, *next_iocb;
James Smart895427b2017-02-12 13:52:30 -08004061
4062 if (phba->sli_rev < LPFC_SLI_REV4)
4063 return;
4064
4065 /* Hint to other driver operations that a flush is in progress. */
4066 spin_lock_irq(&phba->hbalock);
4067 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4068 spin_unlock_irq(&phba->hbalock);
4069
4070 /* Cycle through all NVME rings and complete each IO with
4071 * a local driver reason code. This is a flush so no
4072 * abort exchange to FW.
4073 */
4074 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
4075 pring = phba->sli4_hba.nvme_wq[i]->pring;
4076
James Smart895427b2017-02-12 13:52:30 -08004077 spin_lock_irq(&pring->ring_lock);
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;
4081 /* Retrieve everything on the txcmplq */
James Smart895427b2017-02-12 13:52:30 -08004082 list_splice_init(&pring->txcmplq, &txcmplq);
4083 pring->txcmplq_cnt = 0;
4084 spin_unlock_irq(&pring->ring_lock);
4085
4086 /* Flush the txcmpq &&&PAE */
4087 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4088 IOSTAT_LOCAL_REJECT,
4089 IOERR_SLI_DOWN);
4090 }
4091}
4092
4093/**
James Smart3772a992009-05-22 14:50:54 -04004094 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004095 * @phba: Pointer to HBA context object.
4096 * @mask: Bit mask to be checked.
4097 *
4098 * This function reads the host status register and compares
4099 * with the provided bit mask to check if HBA completed
4100 * the restart. This function will wait in a loop for the
4101 * HBA to complete restart. If the HBA does not restart within
4102 * 15 iterations, the function will reset the HBA again. The
4103 * function returns 1 when HBA fail to restart otherwise returns
4104 * zero.
4105 **/
James Smart3772a992009-05-22 14:50:54 -04004106static int
4107lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004108{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004109 uint32_t status;
4110 int i = 0;
4111 int retval = 0;
dea31012005-04-17 16:05:31 -05004112
Jamie Wellnitz41415862006-02-28 19:25:27 -05004113 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004114 if (lpfc_readl(phba->HSregaddr, &status))
4115 return 1;
dea31012005-04-17 16:05:31 -05004116
Jamie Wellnitz41415862006-02-28 19:25:27 -05004117 /*
4118 * Check status register every 100ms for 5 retries, then every
4119 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4120 * every 2.5 sec for 4.
4121 * Break our of the loop if errors occurred during init.
4122 */
4123 while (((status & mask) != mask) &&
4124 !(status & HS_FFERM) &&
4125 i++ < 20) {
dea31012005-04-17 16:05:31 -05004126
Jamie Wellnitz41415862006-02-28 19:25:27 -05004127 if (i <= 5)
4128 msleep(10);
4129 else if (i <= 10)
4130 msleep(500);
4131 else
4132 msleep(2500);
dea31012005-04-17 16:05:31 -05004133
Jamie Wellnitz41415862006-02-28 19:25:27 -05004134 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004135 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004136 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004137 lpfc_sli_brdrestart(phba);
4138 }
4139 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004140 if (lpfc_readl(phba->HSregaddr, &status)) {
4141 retval = 1;
4142 break;
4143 }
dea31012005-04-17 16:05:31 -05004144 }
dea31012005-04-17 16:05:31 -05004145
Jamie Wellnitz41415862006-02-28 19:25:27 -05004146 /* Check to see if any errors occurred during init */
4147 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004148 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4149 "2751 Adapter failed to restart, "
4150 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4151 status,
4152 readl(phba->MBslimaddr + 0xa8),
4153 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004154 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004155 retval = 1;
4156 }
dea31012005-04-17 16:05:31 -05004157
Jamie Wellnitz41415862006-02-28 19:25:27 -05004158 return retval;
dea31012005-04-17 16:05:31 -05004159}
4160
James Smartda0436e2009-05-22 14:51:39 -04004161/**
4162 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4163 * @phba: Pointer to HBA context object.
4164 * @mask: Bit mask to be checked.
4165 *
4166 * This function checks the host status register to check if HBA is
4167 * ready. This function will wait in a loop for the HBA to be ready
4168 * If the HBA is not ready , the function will will reset the HBA PCI
4169 * function again. The function returns 1 when HBA fail to be ready
4170 * otherwise returns zero.
4171 **/
4172static int
4173lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4174{
4175 uint32_t status;
4176 int retval = 0;
4177
4178 /* Read the HBA Host Status Register */
4179 status = lpfc_sli4_post_status_check(phba);
4180
4181 if (status) {
4182 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4183 lpfc_sli_brdrestart(phba);
4184 status = lpfc_sli4_post_status_check(phba);
4185 }
4186
4187 /* Check to see if any errors occurred during init */
4188 if (status) {
4189 phba->link_state = LPFC_HBA_ERROR;
4190 retval = 1;
4191 } else
4192 phba->sli4_hba.intr_enable = 0;
4193
4194 return retval;
4195}
4196
4197/**
4198 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4199 * @phba: Pointer to HBA context object.
4200 * @mask: Bit mask to be checked.
4201 *
4202 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4203 * from the API jump table function pointer from the lpfc_hba struct.
4204 **/
4205int
4206lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4207{
4208 return phba->lpfc_sli_brdready(phba, mask);
4209}
4210
James Smart92908312006-03-07 15:04:13 -05004211#define BARRIER_TEST_PATTERN (0xdeadbeef)
4212
James Smarte59058c2008-08-24 21:49:00 -04004213/**
James Smart3621a712009-04-06 18:47:14 -04004214 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004215 * @phba: Pointer to HBA context object.
4216 *
James Smart1b511972011-12-13 13:23:09 -05004217 * This function is called before resetting an HBA. This function is called
4218 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004219 **/
James Smart2e0fef82007-06-17 19:56:36 -05004220void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004221{
James Smart65a29c12006-07-06 15:50:50 -04004222 uint32_t __iomem *resp_buf;
4223 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004224 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004225 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004226 int i;
4227 uint8_t hdrtype;
4228
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004229 lockdep_assert_held(&phba->hbalock);
4230
James Smart92908312006-03-07 15:04:13 -05004231 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4232 if (hdrtype != 0x80 ||
4233 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4234 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4235 return;
4236
4237 /*
4238 * Tell the other part of the chip to suspend temporarily all
4239 * its DMA activity.
4240 */
James Smart65a29c12006-07-06 15:50:50 -04004241 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004242
4243 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004244 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4245 return;
James Smart92908312006-03-07 15:04:13 -05004246 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4247 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004248 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004249
James Smart9940b972011-03-11 16:06:12 -05004250 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4251 return;
4252 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004253 /* Clear Chip error bit */
4254 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004255 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004256 }
4257
4258 mbox = 0;
4259 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4260 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4261
4262 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004263 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004264 writel(mbox, mbox_buf);
4265
James Smart9940b972011-03-11 16:06:12 -05004266 for (i = 0; i < 50; i++) {
4267 if (lpfc_readl((resp_buf + 1), &resp_data))
4268 return;
4269 if (resp_data != ~(BARRIER_TEST_PATTERN))
4270 mdelay(1);
4271 else
4272 break;
4273 }
4274 resp_data = 0;
4275 if (lpfc_readl((resp_buf + 1), &resp_data))
4276 return;
4277 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004278 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004279 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004280 goto restore_hc;
4281 else
4282 goto clear_errat;
4283 }
4284
4285 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004286 resp_data = 0;
4287 for (i = 0; i < 500; i++) {
4288 if (lpfc_readl(resp_buf, &resp_data))
4289 return;
4290 if (resp_data != mbox)
4291 mdelay(1);
4292 else
4293 break;
4294 }
James Smart92908312006-03-07 15:04:13 -05004295
4296clear_errat:
4297
James Smart9940b972011-03-11 16:06:12 -05004298 while (++i < 500) {
4299 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4300 return;
4301 if (!(ha_copy & HA_ERATT))
4302 mdelay(1);
4303 else
4304 break;
4305 }
James Smart92908312006-03-07 15:04:13 -05004306
4307 if (readl(phba->HAregaddr) & HA_ERATT) {
4308 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004309 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004310 }
4311
4312restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004313 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004314 writel(hc_copy, phba->HCregaddr);
4315 readl(phba->HCregaddr); /* flush */
4316}
4317
James Smarte59058c2008-08-24 21:49:00 -04004318/**
James Smart3621a712009-04-06 18:47:14 -04004319 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004320 * @phba: Pointer to HBA context object.
4321 *
4322 * This function issues a kill_board mailbox command and waits for
4323 * the error attention interrupt. This function is called for stopping
4324 * the firmware processing. The caller is not required to hold any
4325 * locks. This function calls lpfc_hba_down_post function to free
4326 * any pending commands after the kill. The function will return 1 when it
4327 * fails to kill the board else will return 0.
4328 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004329int
James Smart2e0fef82007-06-17 19:56:36 -05004330lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004331{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004332 struct lpfc_sli *psli;
4333 LPFC_MBOXQ_t *pmb;
4334 uint32_t status;
4335 uint32_t ha_copy;
4336 int retval;
4337 int i = 0;
4338
4339 psli = &phba->sli;
4340
4341 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004342 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004343 "0329 Kill HBA Data: x%x x%x\n",
4344 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004345
James Smart98c9ea52007-10-27 13:37:33 -04004346 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4347 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004348 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004349
4350 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004351 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004352 if (lpfc_readl(phba->HCregaddr, &status)) {
4353 spin_unlock_irq(&phba->hbalock);
4354 mempool_free(pmb, phba->mbox_mem_pool);
4355 return 1;
4356 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004357 status &= ~HC_ERINT_ENA;
4358 writel(status, phba->HCregaddr);
4359 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004360 phba->link_flag |= LS_IGNORE_ERATT;
4361 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004362
4363 lpfc_kill_board(phba, pmb);
4364 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4365 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4366
4367 if (retval != MBX_SUCCESS) {
4368 if (retval != MBX_BUSY)
4369 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004370 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4371 "2752 KILL_BOARD command failed retval %d\n",
4372 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004373 spin_lock_irq(&phba->hbalock);
4374 phba->link_flag &= ~LS_IGNORE_ERATT;
4375 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004376 return 1;
4377 }
4378
James Smartf4b4c682009-05-22 14:53:12 -04004379 spin_lock_irq(&phba->hbalock);
4380 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4381 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004382
Jamie Wellnitz41415862006-02-28 19:25:27 -05004383 mempool_free(pmb, phba->mbox_mem_pool);
4384
4385 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4386 * attention every 100ms for 3 seconds. If we don't get ERATT after
4387 * 3 seconds we still set HBA_ERROR state because the status of the
4388 * board is now undefined.
4389 */
James Smart9940b972011-03-11 16:06:12 -05004390 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4391 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004392 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4393 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004394 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4395 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004396 }
4397
4398 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004399 if (ha_copy & HA_ERATT) {
4400 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004401 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004402 }
James Smart2e0fef82007-06-17 19:56:36 -05004403 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004404 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004405 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004406 phba->link_flag &= ~LS_IGNORE_ERATT;
4407 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004408
Jamie Wellnitz41415862006-02-28 19:25:27 -05004409 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004410 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004411
James Smart2e0fef82007-06-17 19:56:36 -05004412 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004413}
4414
James Smarte59058c2008-08-24 21:49:00 -04004415/**
James Smart3772a992009-05-22 14:50:54 -04004416 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004417 * @phba: Pointer to HBA context object.
4418 *
4419 * This function resets the HBA by writing HC_INITFF to the control
4420 * register. After the HBA resets, this function resets all the iocb ring
4421 * indices. This function disables PCI layer parity checking during
4422 * the reset.
4423 * This function returns 0 always.
4424 * The caller is not required to hold any locks.
4425 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004426int
James Smart2e0fef82007-06-17 19:56:36 -05004427lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004428{
4429 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004430 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004431 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004432 int i;
dea31012005-04-17 16:05:31 -05004433
Jamie Wellnitz41415862006-02-28 19:25:27 -05004434 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004435
Jamie Wellnitz41415862006-02-28 19:25:27 -05004436 /* Reset HBA */
4437 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004438 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004439 (phba->pport) ? phba->pport->port_state : 0,
4440 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004441
4442 /* perform board reset */
4443 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004444 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004445 if (phba->pport) {
4446 phba->pport->fc_myDID = 0;
4447 phba->pport->fc_prevDID = 0;
4448 }
dea31012005-04-17 16:05:31 -05004449
Jamie Wellnitz41415862006-02-28 19:25:27 -05004450 /* Turn off parity checking and serr during the physical reset */
4451 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4452 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4453 (cfg_value &
4454 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4455
James Smart3772a992009-05-22 14:50:54 -04004456 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4457
Jamie Wellnitz41415862006-02-28 19:25:27 -05004458 /* Now toggle INITFF bit in the Host Control Register */
4459 writel(HC_INITFF, phba->HCregaddr);
4460 mdelay(1);
4461 readl(phba->HCregaddr); /* flush */
4462 writel(0, phba->HCregaddr);
4463 readl(phba->HCregaddr); /* flush */
4464
4465 /* Restore PCI cmd register */
4466 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004467
4468 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004469 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004470 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004471 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004472 pring->sli.sli3.rspidx = 0;
4473 pring->sli.sli3.next_cmdidx = 0;
4474 pring->sli.sli3.local_getidx = 0;
4475 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004476 pring->missbufcnt = 0;
4477 }
dea31012005-04-17 16:05:31 -05004478
James Smart2e0fef82007-06-17 19:56:36 -05004479 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004480 return 0;
4481}
4482
James Smarte59058c2008-08-24 21:49:00 -04004483/**
James Smartda0436e2009-05-22 14:51:39 -04004484 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4485 * @phba: Pointer to HBA context object.
4486 *
4487 * This function resets a SLI4 HBA. This function disables PCI layer parity
4488 * checking during resets the device. The caller is not required to hold
4489 * any locks.
4490 *
4491 * This function returns 0 always.
4492 **/
4493int
4494lpfc_sli4_brdreset(struct lpfc_hba *phba)
4495{
4496 struct lpfc_sli *psli = &phba->sli;
4497 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004498 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004499
4500 /* Reset HBA */
4501 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004502 "0295 Reset HBA Data: x%x x%x x%x\n",
4503 phba->pport->port_state, psli->sli_flag,
4504 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004505
4506 /* perform board reset */
4507 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004508 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004509 phba->pport->fc_myDID = 0;
4510 phba->pport->fc_prevDID = 0;
4511
James Smartda0436e2009-05-22 14:51:39 -04004512 spin_lock_irq(&phba->hbalock);
4513 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4514 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004515 spin_unlock_irq(&phba->hbalock);
4516
James Smart02936352014-04-04 13:52:12 -04004517 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4518 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4519 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4520 return rc;
4521 }
4522
James Smartda0436e2009-05-22 14:51:39 -04004523 /* Now physically reset the device */
4524 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4525 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004526
4527 /* Turn off parity checking and serr during the physical reset */
4528 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4529 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4530 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4531
James Smart88318812012-09-29 11:29:29 -04004532 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004533 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004534
James Smartbe858b62010-12-15 17:57:20 -05004535 /* Restore PCI cmd register */
4536 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4537
James Smart27b01b82012-05-09 21:19:44 -04004538 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004539}
4540
4541/**
4542 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004543 * @phba: Pointer to HBA context object.
4544 *
4545 * This function is called in the SLI initialization code path to
4546 * restart the HBA. The caller is not required to hold any lock.
4547 * This function writes MBX_RESTART mailbox command to the SLIM and
4548 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4549 * function to free any pending commands. The function enables
4550 * POST only during the first initialization. The function returns zero.
4551 * The function does not guarantee completion of MBX_RESTART mailbox
4552 * command before the return of this function.
4553 **/
James Smartda0436e2009-05-22 14:51:39 -04004554static int
4555lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004556{
4557 MAILBOX_t *mb;
4558 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004559 volatile uint32_t word0;
4560 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004561 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004562
James Smart2e0fef82007-06-17 19:56:36 -05004563 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004564
James Smart0d878412009-10-02 15:16:56 -04004565 /* Take PCIe device Advanced Error Reporting (AER) state */
4566 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4567
Jamie Wellnitz41415862006-02-28 19:25:27 -05004568 psli = &phba->sli;
4569
4570 /* Restart HBA */
4571 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004572 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004573 (phba->pport) ? phba->pport->port_state : 0,
4574 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004575
4576 word0 = 0;
4577 mb = (MAILBOX_t *) &word0;
4578 mb->mbxCommand = MBX_RESTART;
4579 mb->mbxHc = 1;
4580
James Smart92908312006-03-07 15:04:13 -05004581 lpfc_reset_barrier(phba);
4582
Jamie Wellnitz41415862006-02-28 19:25:27 -05004583 to_slim = phba->MBslimaddr;
4584 writel(*(uint32_t *) mb, to_slim);
4585 readl(to_slim); /* flush */
4586
4587 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004588 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004589 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004590 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004591 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004592 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004593 writel(*(uint32_t *) mb, to_slim);
4594 readl(to_slim); /* flush */
4595
4596 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004597 if (phba->pport)
4598 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004599 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004600 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004601 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004602
James Smart64ba8812006-08-02 15:24:34 -04004603 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004604 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04004605
James Smarteaf15d52008-12-04 22:39:29 -05004606 /* Give the INITFF and Post time to settle. */
4607 mdelay(100);
dea31012005-04-17 16:05:31 -05004608
James Smart0d878412009-10-02 15:16:56 -04004609 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4610 if (hba_aer_enabled)
4611 pci_disable_pcie_error_reporting(phba->pcidev);
4612
Jamie Wellnitz41415862006-02-28 19:25:27 -05004613 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004614
4615 return 0;
4616}
4617
James Smarte59058c2008-08-24 21:49:00 -04004618/**
James Smartda0436e2009-05-22 14:51:39 -04004619 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4620 * @phba: Pointer to HBA context object.
4621 *
4622 * This function is called in the SLI initialization code path to restart
4623 * a SLI4 HBA. The caller is not required to hold any lock.
4624 * At the end of the function, it calls lpfc_hba_down_post function to
4625 * free any pending commands.
4626 **/
4627static int
4628lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4629{
4630 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004631 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004632 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004633
4634 /* Restart HBA */
4635 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4636 "0296 Restart HBA Data: x%x x%x\n",
4637 phba->pport->port_state, psli->sli_flag);
4638
James Smart75baf692010-06-08 18:31:21 -04004639 /* Take PCIe device Advanced Error Reporting (AER) state */
4640 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4641
James Smart27b01b82012-05-09 21:19:44 -04004642 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004643
4644 spin_lock_irq(&phba->hbalock);
4645 phba->pport->stopped = 0;
4646 phba->link_state = LPFC_INIT_START;
4647 phba->hba_flag = 0;
4648 spin_unlock_irq(&phba->hbalock);
4649
4650 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004651 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04004652
James Smart75baf692010-06-08 18:31:21 -04004653 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4654 if (hba_aer_enabled)
4655 pci_disable_pcie_error_reporting(phba->pcidev);
4656
James Smartda0436e2009-05-22 14:51:39 -04004657 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004658 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004659
James Smart27b01b82012-05-09 21:19:44 -04004660 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004661}
4662
4663/**
4664 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4665 * @phba: Pointer to HBA context object.
4666 *
4667 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4668 * API jump table function pointer from the lpfc_hba struct.
4669**/
4670int
4671lpfc_sli_brdrestart(struct lpfc_hba *phba)
4672{
4673 return phba->lpfc_sli_brdrestart(phba);
4674}
4675
4676/**
James Smart3621a712009-04-06 18:47:14 -04004677 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004678 * @phba: Pointer to HBA context object.
4679 *
4680 * This function is called after a HBA restart to wait for successful
4681 * restart of the HBA. Successful restart of the HBA is indicated by
4682 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4683 * iteration, the function will restart the HBA again. The function returns
4684 * zero if HBA successfully restarted else returns negative error code.
4685 **/
James Smart4492b732017-04-27 15:08:26 -07004686int
dea31012005-04-17 16:05:31 -05004687lpfc_sli_chipset_init(struct lpfc_hba *phba)
4688{
4689 uint32_t status, i = 0;
4690
4691 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004692 if (lpfc_readl(phba->HSregaddr, &status))
4693 return -EIO;
dea31012005-04-17 16:05:31 -05004694
4695 /* Check status register to see what current state is */
4696 i = 0;
4697 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4698
James Smartdcf2a4e2010-09-29 11:18:53 -04004699 /* Check every 10ms for 10 retries, then every 100ms for 90
4700 * retries, then every 1 sec for 50 retires for a total of
4701 * ~60 seconds before reset the board again and check every
4702 * 1 sec for 50 retries. The up to 60 seconds before the
4703 * board ready is required by the Falcon FIPS zeroization
4704 * complete, and any reset the board in between shall cause
4705 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004706 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004707 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004708 /* Adapter failed to init, timeout, status reg
4709 <status> */
James Smarted957682007-06-17 19:56:37 -05004710 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004711 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004712 "timeout, status reg x%x, "
4713 "FW Data: A8 x%x AC x%x\n", status,
4714 readl(phba->MBslimaddr + 0xa8),
4715 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004716 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004717 return -ETIMEDOUT;
4718 }
4719
4720 /* Check to see if any errors occurred during init */
4721 if (status & HS_FFERM) {
4722 /* ERROR: During chipset initialization */
4723 /* Adapter failed to init, chipset, status reg
4724 <status> */
James Smarted957682007-06-17 19:56:37 -05004725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004726 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004727 "chipset, status reg x%x, "
4728 "FW Data: A8 x%x AC x%x\n", status,
4729 readl(phba->MBslimaddr + 0xa8),
4730 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004731 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004732 return -EIO;
4733 }
4734
James Smartdcf2a4e2010-09-29 11:18:53 -04004735 if (i <= 10)
dea31012005-04-17 16:05:31 -05004736 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004737 else if (i <= 100)
4738 msleep(100);
4739 else
4740 msleep(1000);
dea31012005-04-17 16:05:31 -05004741
James Smartdcf2a4e2010-09-29 11:18:53 -04004742 if (i == 150) {
4743 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004744 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004745 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004746 }
4747 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004748 if (lpfc_readl(phba->HSregaddr, &status))
4749 return -EIO;
dea31012005-04-17 16:05:31 -05004750 }
4751
4752 /* Check to see if any errors occurred during init */
4753 if (status & HS_FFERM) {
4754 /* ERROR: During chipset initialization */
4755 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004757 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004758 "status reg x%x, "
4759 "FW Data: A8 x%x AC x%x\n", status,
4760 readl(phba->MBslimaddr + 0xa8),
4761 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004762 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004763 return -EIO;
4764 }
4765
4766 /* Clear all interrupt enable conditions */
4767 writel(0, phba->HCregaddr);
4768 readl(phba->HCregaddr); /* flush */
4769
4770 /* setup host attn register */
4771 writel(0xffffffff, phba->HAregaddr);
4772 readl(phba->HAregaddr); /* flush */
4773 return 0;
4774}
4775
James Smarte59058c2008-08-24 21:49:00 -04004776/**
James Smart3621a712009-04-06 18:47:14 -04004777 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004778 *
4779 * This function calculates and returns the number of HBQs required to be
4780 * configured.
4781 **/
James Smart78b2d852007-08-02 11:10:21 -04004782int
James Smarted957682007-06-17 19:56:37 -05004783lpfc_sli_hbq_count(void)
4784{
James Smart92d7f7b2007-06-17 19:56:38 -05004785 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004786}
4787
James Smarte59058c2008-08-24 21:49:00 -04004788/**
James Smart3621a712009-04-06 18:47:14 -04004789 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004790 *
4791 * This function adds the number of hbq entries in every HBQ to get
4792 * the total number of hbq entries required for the HBA and returns
4793 * the total count.
4794 **/
James Smarted957682007-06-17 19:56:37 -05004795static int
4796lpfc_sli_hbq_entry_count(void)
4797{
4798 int hbq_count = lpfc_sli_hbq_count();
4799 int count = 0;
4800 int i;
4801
4802 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004803 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004804 return count;
4805}
4806
James Smarte59058c2008-08-24 21:49:00 -04004807/**
James Smart3621a712009-04-06 18:47:14 -04004808 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004809 *
4810 * This function calculates amount of memory required for all hbq entries
4811 * to be configured and returns the total memory required.
4812 **/
dea31012005-04-17 16:05:31 -05004813int
James Smarted957682007-06-17 19:56:37 -05004814lpfc_sli_hbq_size(void)
4815{
4816 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4817}
4818
James Smarte59058c2008-08-24 21:49:00 -04004819/**
James Smart3621a712009-04-06 18:47:14 -04004820 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004821 * @phba: Pointer to HBA context object.
4822 *
4823 * This function is called during the SLI initialization to configure
4824 * all the HBQs and post buffers to the HBQ. The caller is not
4825 * required to hold any locks. This function will return zero if successful
4826 * else it will return negative error code.
4827 **/
James Smarted957682007-06-17 19:56:37 -05004828static int
4829lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4830{
4831 int hbq_count = lpfc_sli_hbq_count();
4832 LPFC_MBOXQ_t *pmb;
4833 MAILBOX_t *pmbox;
4834 uint32_t hbqno;
4835 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004836
James Smart92d7f7b2007-06-17 19:56:38 -05004837 /* Get a Mailbox buffer to setup mailbox
4838 * commands for HBA initialization
4839 */
James Smarted957682007-06-17 19:56:37 -05004840 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4841
4842 if (!pmb)
4843 return -ENOMEM;
4844
James Smart04c68492009-05-22 14:52:52 -04004845 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004846
4847 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4848 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004849 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004850
4851 hbq_entry_index = 0;
4852 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4853 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4854 phba->hbqs[hbqno].hbqPutIdx = 0;
4855 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4856 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004857 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004858 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4859 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004860 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4861
4862 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4863 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4864 mbxStatus <status>, ring <num> */
4865
4866 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004867 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004868 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004869 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004870 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004871 pmbox->mbxStatus, hbqno);
4872
4873 phba->link_state = LPFC_HBA_ERROR;
4874 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004875 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004876 }
4877 }
4878 phba->hbq_count = hbq_count;
4879
James Smarted957682007-06-17 19:56:37 -05004880 mempool_free(pmb, phba->mbox_mem_pool);
4881
James Smart92d7f7b2007-06-17 19:56:38 -05004882 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004883 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4884 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004885 return 0;
4886}
4887
James Smarte59058c2008-08-24 21:49:00 -04004888/**
James Smart4f774512009-05-22 14:52:35 -04004889 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4890 * @phba: Pointer to HBA context object.
4891 *
4892 * This function is called during the SLI initialization to configure
4893 * all the HBQs and post buffers to the HBQ. The caller is not
4894 * required to hold any locks. This function will return zero if successful
4895 * else it will return negative error code.
4896 **/
4897static int
4898lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4899{
4900 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004901 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4902 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004903 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004904 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004905 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004906 return 0;
4907}
4908
4909/**
James Smart3621a712009-04-06 18:47:14 -04004910 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004911 * @phba: Pointer to HBA context object.
4912 * @sli_mode: sli mode - 2/3
4913 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004914 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004915 * to issue config_port mailbox command. This function restarts the
4916 * HBA firmware and issues a config_port mailbox command to configure
4917 * the SLI interface in the sli mode specified by sli_mode
4918 * variable. The caller is not required to hold any locks.
4919 * The function returns 0 if successful, else returns negative error
4920 * code.
4921 **/
James Smart93996272008-08-24 21:50:30 -04004922int
4923lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004924{
4925 LPFC_MBOXQ_t *pmb;
4926 uint32_t resetcount = 0, rc = 0, done = 0;
4927
4928 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4929 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004930 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004931 return -ENOMEM;
4932 }
4933
James Smarted957682007-06-17 19:56:37 -05004934 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004935 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004936 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004937 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004938 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004939 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004940 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004941 rc = lpfc_sli_chipset_init(phba);
4942 if (rc)
4943 break;
4944
James Smart2e0fef82007-06-17 19:56:36 -05004945 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004946 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004947 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004948 resetcount++;
4949
James Smarted957682007-06-17 19:56:37 -05004950 /* Call pre CONFIG_PORT mailbox command initialization. A
4951 * value of 0 means the call was successful. Any other
4952 * nonzero value is a failure, but if ERESTART is returned,
4953 * the driver may reset the HBA and try again.
4954 */
dea31012005-04-17 16:05:31 -05004955 rc = lpfc_config_port_prep(phba);
4956 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004957 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004958 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004959 } else if (rc)
dea31012005-04-17 16:05:31 -05004960 break;
James Smart6d368e52011-05-24 11:44:12 -04004961
James Smart2e0fef82007-06-17 19:56:36 -05004962 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004963 lpfc_config_port(phba, pmb);
4964 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004965 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4966 LPFC_SLI3_HBQ_ENABLED |
4967 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004968 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004969 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004971 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004972 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004973 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004974 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004975 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004976 spin_unlock_irq(&phba->hbalock);
4977 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004978 } else {
4979 /* Allow asynchronous mailbox command to go through */
4980 spin_lock_irq(&phba->hbalock);
4981 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4982 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004983 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004984
4985 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4986 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4987 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4988 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004989 }
dea31012005-04-17 16:05:31 -05004990 }
James Smarted957682007-06-17 19:56:37 -05004991 if (!done) {
4992 rc = -EINVAL;
4993 goto do_prep_failed;
4994 }
James Smart04c68492009-05-22 14:52:52 -04004995 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4996 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004997 rc = -ENXIO;
4998 goto do_prep_failed;
4999 }
James Smart04c68492009-05-22 14:52:52 -04005000 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04005001 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005002 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5003 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5004 phba->max_vpi : phba->max_vports;
5005
James Smart34b02dc2008-08-24 21:49:55 -04005006 } else
5007 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04005008 phba->fips_level = 0;
5009 phba->fips_spec_rev = 0;
5010 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04005011 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04005012 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
5013 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5014 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5015 "2850 Security Crypto Active. FIPS x%d "
5016 "(Spec Rev: x%d)",
5017 phba->fips_level, phba->fips_spec_rev);
5018 }
5019 if (pmb->u.mb.un.varCfgPort.sec_err) {
5020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5021 "2856 Config Port Security Crypto "
5022 "Error: x%x ",
5023 pmb->u.mb.un.varCfgPort.sec_err);
5024 }
James Smart04c68492009-05-22 14:52:52 -04005025 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005026 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005027 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005028 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005029
5030 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5031 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005032
James Smartf44ac122018-03-05 12:04:08 -08005033 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5034 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5035 phba->cfg_enable_bg = 0;
5036 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smarte2a0a9d2008-12-04 22:40:02 -05005037 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5038 "0443 Adapter did not grant "
5039 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005040 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005041 }
James Smart34b02dc2008-08-24 21:49:55 -04005042 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005043 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005044 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005045 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005046 }
James Smart92d7f7b2007-06-17 19:56:38 -05005047do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005048 mempool_free(pmb, phba->mbox_mem_pool);
5049 return rc;
5050}
5051
James Smarte59058c2008-08-24 21:49:00 -04005052
5053/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005054 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005055 * @phba: Pointer to HBA context object.
5056 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005057 * This function is the main SLI initialization function. This function
5058 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005059 * error attention handler code. Caller is not required to hold any
5060 * locks. This function issues config_port mailbox command to configure
5061 * the SLI, setup iocb rings and HBQ rings. In the end the function
5062 * calls the config_port_post function to issue init_link mailbox
5063 * command and to start the discovery. The function will return zero
5064 * if successful, else it will return negative error code.
5065 **/
James Smarted957682007-06-17 19:56:37 -05005066int
5067lpfc_sli_hba_setup(struct lpfc_hba *phba)
5068{
5069 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005070 int mode = 3, i;
5071 int longs;
James Smarted957682007-06-17 19:56:37 -05005072
James Smart12247e82016-07-06 12:36:09 -07005073 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005074 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005075 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005077 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005078 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005079 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005080 break;
5081 }
James Smarted957682007-06-17 19:56:37 -05005082 mode = 2;
5083 break;
5084 case 0:
5085 case 3:
5086 break;
5087 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005088 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005089 "1819 Unrecognized sli_mode parameter: %d.\n",
5090 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005091
5092 break;
5093 }
James Smartb5c53952016-03-31 14:12:30 -07005094 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005095
James Smart93996272008-08-24 21:50:30 -04005096 rc = lpfc_sli_config_port(phba, mode);
5097
James Smart12247e82016-07-06 12:36:09 -07005098 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005099 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005100 "1820 Unable to select SLI-3. "
5101 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005102 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005103 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005104 else if (rc && mode == 2)
5105 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005106 if (rc)
dea31012005-04-17 16:05:31 -05005107 goto lpfc_sli_hba_setup_error;
5108
James Smart0d878412009-10-02 15:16:56 -04005109 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5110 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5111 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5112 if (!rc) {
5113 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5114 "2709 This device supports "
5115 "Advanced Error Reporting (AER)\n");
5116 spin_lock_irq(&phba->hbalock);
5117 phba->hba_flag |= HBA_AER_ENABLED;
5118 spin_unlock_irq(&phba->hbalock);
5119 } else {
5120 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5121 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005122 "Advanced Error Reporting (AER): %d\n",
5123 rc);
James Smart0d878412009-10-02 15:16:56 -04005124 phba->cfg_aer_support = 0;
5125 }
5126 }
5127
James Smarted957682007-06-17 19:56:37 -05005128 if (phba->sli_rev == 3) {
5129 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5130 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005131 } else {
5132 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5133 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005134 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005135 }
5136
5137 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005138 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5139 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005140 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005141
5142 if (rc)
5143 goto lpfc_sli_hba_setup_error;
5144
James Smart6d368e52011-05-24 11:44:12 -04005145 /* Initialize VPIs. */
5146 if (phba->sli_rev == LPFC_SLI_REV3) {
5147 /*
5148 * The VPI bitmask and physical ID array are allocated
5149 * and initialized once only - at driver load. A port
5150 * reset doesn't need to reinitialize this memory.
5151 */
5152 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5153 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005154 phba->vpi_bmask = kcalloc(longs,
5155 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005156 GFP_KERNEL);
5157 if (!phba->vpi_bmask) {
5158 rc = -ENOMEM;
5159 goto lpfc_sli_hba_setup_error;
5160 }
5161
Kees Cook6396bb22018-06-12 14:03:40 -07005162 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5163 sizeof(uint16_t),
5164 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005165 if (!phba->vpi_ids) {
5166 kfree(phba->vpi_bmask);
5167 rc = -ENOMEM;
5168 goto lpfc_sli_hba_setup_error;
5169 }
5170 for (i = 0; i < phba->max_vpi; i++)
5171 phba->vpi_ids[i] = i;
5172 }
5173 }
5174
James Smart93996272008-08-24 21:50:30 -04005175 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005176 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5177 rc = lpfc_sli_hbq_setup(phba);
5178 if (rc)
5179 goto lpfc_sli_hba_setup_error;
5180 }
James Smart04c68492009-05-22 14:52:52 -04005181 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005182 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005183 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005184
5185 rc = lpfc_config_port_post(phba);
5186 if (rc)
5187 goto lpfc_sli_hba_setup_error;
5188
James Smarted957682007-06-17 19:56:37 -05005189 return rc;
5190
James Smart92d7f7b2007-06-17 19:56:38 -05005191lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005192 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005194 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005195 return rc;
5196}
5197
James Smartda0436e2009-05-22 14:51:39 -04005198/**
5199 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5200 * @phba: Pointer to HBA context object.
5201 * @mboxq: mailbox pointer.
5202 * This function issue a dump mailbox command to read config region
5203 * 23 and parse the records in the region and populate driver
5204 * data structure.
5205 **/
5206static int
James Smartff78d8f2011-12-13 13:21:35 -05005207lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005208{
James Smartff78d8f2011-12-13 13:21:35 -05005209 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005210 struct lpfc_dmabuf *mp;
5211 struct lpfc_mqe *mqe;
5212 uint32_t data_length;
5213 int rc;
5214
5215 /* Program the default value of vlan_id and fc_map */
5216 phba->valid_vlan = 0;
5217 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5218 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5219 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5220
James Smartff78d8f2011-12-13 13:21:35 -05005221 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5222 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005223 return -ENOMEM;
5224
James Smartff78d8f2011-12-13 13:21:35 -05005225 mqe = &mboxq->u.mqe;
5226 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5227 rc = -ENOMEM;
5228 goto out_free_mboxq;
5229 }
5230
James Smartda0436e2009-05-22 14:51:39 -04005231 mp = (struct lpfc_dmabuf *) mboxq->context1;
5232 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5233
5234 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5235 "(%d):2571 Mailbox cmd x%x Status x%x "
5236 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5237 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5238 "CQ: x%x x%x x%x x%x\n",
5239 mboxq->vport ? mboxq->vport->vpi : 0,
5240 bf_get(lpfc_mqe_command, mqe),
5241 bf_get(lpfc_mqe_status, mqe),
5242 mqe->un.mb_words[0], mqe->un.mb_words[1],
5243 mqe->un.mb_words[2], mqe->un.mb_words[3],
5244 mqe->un.mb_words[4], mqe->un.mb_words[5],
5245 mqe->un.mb_words[6], mqe->un.mb_words[7],
5246 mqe->un.mb_words[8], mqe->un.mb_words[9],
5247 mqe->un.mb_words[10], mqe->un.mb_words[11],
5248 mqe->un.mb_words[12], mqe->un.mb_words[13],
5249 mqe->un.mb_words[14], mqe->un.mb_words[15],
5250 mqe->un.mb_words[16], mqe->un.mb_words[50],
5251 mboxq->mcqe.word0,
5252 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5253 mboxq->mcqe.trailer);
5254
5255 if (rc) {
5256 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5257 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005258 rc = -EIO;
5259 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005260 }
5261 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005262 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005263 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5264 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005265 rc = -EIO;
5266 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005267 }
James Smartda0436e2009-05-22 14:51:39 -04005268
5269 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5270 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5271 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005272 rc = 0;
5273
5274out_free_mboxq:
5275 mempool_free(mboxq, phba->mbox_mem_pool);
5276 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005277}
5278
5279/**
5280 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5281 * @phba: pointer to lpfc hba data structure.
5282 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5283 * @vpd: pointer to the memory to hold resulting port vpd data.
5284 * @vpd_size: On input, the number of bytes allocated to @vpd.
5285 * On output, the number of data bytes in @vpd.
5286 *
5287 * This routine executes a READ_REV SLI4 mailbox command. In
5288 * addition, this routine gets the port vpd data.
5289 *
5290 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005291 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005292 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005293 **/
5294static int
5295lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5296 uint8_t *vpd, uint32_t *vpd_size)
5297{
5298 int rc = 0;
5299 uint32_t dma_size;
5300 struct lpfc_dmabuf *dmabuf;
5301 struct lpfc_mqe *mqe;
5302
5303 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5304 if (!dmabuf)
5305 return -ENOMEM;
5306
5307 /*
5308 * Get a DMA buffer for the vpd data resulting from the READ_REV
5309 * mailbox command.
5310 */
5311 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005312 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5313 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005314 if (!dmabuf->virt) {
5315 kfree(dmabuf);
5316 return -ENOMEM;
5317 }
James Smartda0436e2009-05-22 14:51:39 -04005318
5319 /*
5320 * The SLI4 implementation of READ_REV conflicts at word1,
5321 * bits 31:16 and SLI4 adds vpd functionality not present
5322 * in SLI3. This code corrects the conflicts.
5323 */
5324 lpfc_read_rev(phba, mboxq);
5325 mqe = &mboxq->u.mqe;
5326 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5327 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5328 mqe->un.read_rev.word1 &= 0x0000FFFF;
5329 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5330 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5331
5332 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5333 if (rc) {
5334 dma_free_coherent(&phba->pcidev->dev, dma_size,
5335 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005336 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005337 return -EIO;
5338 }
5339
James Smartda0436e2009-05-22 14:51:39 -04005340 /*
5341 * The available vpd length cannot be bigger than the
5342 * DMA buffer passed to the port. Catch the less than
5343 * case and update the caller's size.
5344 */
5345 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5346 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5347
James Smartd7c47992010-06-08 18:31:54 -04005348 memcpy(vpd, dmabuf->virt, *vpd_size);
5349
James Smartda0436e2009-05-22 14:51:39 -04005350 dma_free_coherent(&phba->pcidev->dev, dma_size,
5351 dmabuf->virt, dmabuf->phys);
5352 kfree(dmabuf);
5353 return 0;
5354}
5355
5356/**
James Smartcd1c8302011-10-10 21:33:25 -04005357 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5358 * @phba: pointer to lpfc hba data structure.
5359 *
5360 * This routine retrieves SLI4 device physical port name this PCI function
5361 * is attached to.
5362 *
5363 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005364 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005365 * otherwise - failed to retrieve physical port name
5366 **/
5367static int
5368lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5369{
5370 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005371 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5372 struct lpfc_controller_attribute *cntl_attr;
5373 struct lpfc_mbx_get_port_name *get_port_name;
5374 void *virtaddr = NULL;
5375 uint32_t alloclen, reqlen;
5376 uint32_t shdr_status, shdr_add_status;
5377 union lpfc_sli4_cfg_shdr *shdr;
5378 char cport_name = 0;
5379 int rc;
5380
5381 /* We assume nothing at this point */
5382 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5383 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5384
5385 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5386 if (!mboxq)
5387 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005388 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005389 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5390 lpfc_sli4_read_config(phba);
5391 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5392 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005393
5394 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5395 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5396 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5397 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5398 LPFC_SLI4_MBX_NEMBED);
5399 if (alloclen < reqlen) {
5400 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5401 "3084 Allocated DMA memory size (%d) is "
5402 "less than the requested DMA memory size "
5403 "(%d)\n", alloclen, reqlen);
5404 rc = -ENOMEM;
5405 goto out_free_mboxq;
5406 }
5407 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5408 virtaddr = mboxq->sge_array->addr[0];
5409 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5410 shdr = &mbx_cntl_attr->cfg_shdr;
5411 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5412 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5413 if (shdr_status || shdr_add_status || rc) {
5414 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5415 "3085 Mailbox x%x (x%x/x%x) failed, "
5416 "rc:x%x, status:x%x, add_status:x%x\n",
5417 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5418 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5419 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5420 rc, shdr_status, shdr_add_status);
5421 rc = -ENXIO;
5422 goto out_free_mboxq;
5423 }
5424 cntl_attr = &mbx_cntl_attr->cntl_attr;
5425 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5426 phba->sli4_hba.lnk_info.lnk_tp =
5427 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5428 phba->sli4_hba.lnk_info.lnk_no =
5429 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5430 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5431 "3086 lnk_type:%d, lnk_numb:%d\n",
5432 phba->sli4_hba.lnk_info.lnk_tp,
5433 phba->sli4_hba.lnk_info.lnk_no);
5434
5435retrieve_ppname:
5436 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5437 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5438 sizeof(struct lpfc_mbx_get_port_name) -
5439 sizeof(struct lpfc_sli4_cfg_mhdr),
5440 LPFC_SLI4_MBX_EMBED);
5441 get_port_name = &mboxq->u.mqe.un.get_port_name;
5442 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5443 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5444 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5445 phba->sli4_hba.lnk_info.lnk_tp);
5446 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5447 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5448 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5449 if (shdr_status || shdr_add_status || rc) {
5450 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5451 "3087 Mailbox x%x (x%x/x%x) failed: "
5452 "rc:x%x, status:x%x, add_status:x%x\n",
5453 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5454 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5455 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5456 rc, shdr_status, shdr_add_status);
5457 rc = -ENXIO;
5458 goto out_free_mboxq;
5459 }
5460 switch (phba->sli4_hba.lnk_info.lnk_no) {
5461 case LPFC_LINK_NUMBER_0:
5462 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5463 &get_port_name->u.response);
5464 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5465 break;
5466 case LPFC_LINK_NUMBER_1:
5467 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5468 &get_port_name->u.response);
5469 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5470 break;
5471 case LPFC_LINK_NUMBER_2:
5472 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5473 &get_port_name->u.response);
5474 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5475 break;
5476 case LPFC_LINK_NUMBER_3:
5477 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5478 &get_port_name->u.response);
5479 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5480 break;
5481 default:
5482 break;
5483 }
5484
5485 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5486 phba->Port[0] = cport_name;
5487 phba->Port[1] = '\0';
5488 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5489 "3091 SLI get port name: %s\n", phba->Port);
5490 }
5491
5492out_free_mboxq:
5493 if (rc != MBX_TIMEOUT) {
5494 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5495 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5496 else
5497 mempool_free(mboxq, phba->mbox_mem_pool);
5498 }
5499 return rc;
5500}
5501
5502/**
James Smartda0436e2009-05-22 14:51:39 -04005503 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5504 * @phba: pointer to lpfc hba data structure.
5505 *
5506 * This routine is called to explicitly arm the SLI4 device's completion and
5507 * event queues
5508 **/
5509static void
5510lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5511{
James Smart895427b2017-02-12 13:52:30 -08005512 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005513 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartda0436e2009-05-22 14:51:39 -04005514
James Smartb71413d2018-02-22 08:18:40 -08005515 sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5516 sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5517 if (sli4_hba->nvmels_cq)
5518 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
James Smart895427b2017-02-12 13:52:30 -08005519 LPFC_QUEUE_REARM);
5520
James Smartb71413d2018-02-22 08:18:40 -08005521 if (sli4_hba->fcp_cq)
James Smart895427b2017-02-12 13:52:30 -08005522 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005523 sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
James Smart895427b2017-02-12 13:52:30 -08005524 LPFC_QUEUE_REARM);
5525
James Smartb71413d2018-02-22 08:18:40 -08005526 if (sli4_hba->nvme_cq)
James Smart895427b2017-02-12 13:52:30 -08005527 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005528 sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
James Smart895427b2017-02-12 13:52:30 -08005529 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005530
James Smartf38fa0b2014-04-04 13:52:21 -04005531 if (phba->cfg_fof)
James Smartb71413d2018-02-22 08:18:40 -08005532 sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005533
James Smartb71413d2018-02-22 08:18:40 -08005534 if (sli4_hba->hba_eq)
James Smart895427b2017-02-12 13:52:30 -08005535 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005536 sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5537 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005538
James Smart2d7dbc42017-02-12 13:52:35 -08005539 if (phba->nvmet_support) {
5540 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smartb71413d2018-02-22 08:18:40 -08005541 sli4_hba->sli4_cq_release(
5542 sli4_hba->nvmet_cqset[qidx],
James Smart2d7dbc42017-02-12 13:52:35 -08005543 LPFC_QUEUE_REARM);
5544 }
James Smart2e90f4b2011-12-13 13:22:37 -05005545 }
James Smart1ba981f2014-02-20 09:56:45 -05005546
5547 if (phba->cfg_fof)
James Smartb71413d2018-02-22 08:18:40 -08005548 sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005549}
5550
5551/**
James Smart6d368e52011-05-24 11:44:12 -04005552 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5553 * @phba: Pointer to HBA context object.
5554 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005555 * @extnt_count: buffer to hold port available extent count.
5556 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005557 *
James Smartb76f2dc2011-07-22 18:37:42 -04005558 * This function calls the port and retrievs the number of available
5559 * extents and their size for a particular extent type.
5560 *
5561 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005562 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005563int
James Smart6d368e52011-05-24 11:44:12 -04005564lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5565 uint16_t *extnt_count, uint16_t *extnt_size)
5566{
5567 int rc = 0;
5568 uint32_t length;
5569 uint32_t mbox_tmo;
5570 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5571 LPFC_MBOXQ_t *mbox;
5572
5573 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5574 if (!mbox)
5575 return -ENOMEM;
5576
5577 /* Find out how many extents are available for this resource type */
5578 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5579 sizeof(struct lpfc_sli4_cfg_mhdr));
5580 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5581 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5582 length, LPFC_SLI4_MBX_EMBED);
5583
5584 /* Send an extents count of 0 - the GET doesn't use it. */
5585 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5586 LPFC_SLI4_MBX_EMBED);
5587 if (unlikely(rc)) {
5588 rc = -EIO;
5589 goto err_exit;
5590 }
5591
5592 if (!phba->sli4_hba.intr_enable)
5593 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5594 else {
James Smarta183a152011-10-10 21:32:43 -04005595 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005596 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5597 }
5598 if (unlikely(rc)) {
5599 rc = -EIO;
5600 goto err_exit;
5601 }
5602
5603 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5604 if (bf_get(lpfc_mbox_hdr_status,
5605 &rsrc_info->header.cfg_shdr.response)) {
5606 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5607 "2930 Failed to get resource extents "
5608 "Status 0x%x Add'l Status 0x%x\n",
5609 bf_get(lpfc_mbox_hdr_status,
5610 &rsrc_info->header.cfg_shdr.response),
5611 bf_get(lpfc_mbox_hdr_add_status,
5612 &rsrc_info->header.cfg_shdr.response));
5613 rc = -EIO;
5614 goto err_exit;
5615 }
5616
5617 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5618 &rsrc_info->u.rsp);
5619 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5620 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005621
5622 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5623 "3162 Retrieved extents type-%d from port: count:%d, "
5624 "size:%d\n", type, *extnt_count, *extnt_size);
5625
5626err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005627 mempool_free(mbox, phba->mbox_mem_pool);
5628 return rc;
5629}
5630
5631/**
5632 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5633 * @phba: Pointer to HBA context object.
5634 * @type: The extent type to check.
5635 *
5636 * This function reads the current available extents from the port and checks
5637 * if the extent count or extent size has changed since the last access.
5638 * Callers use this routine post port reset to understand if there is a
5639 * extent reprovisioning requirement.
5640 *
5641 * Returns:
5642 * -Error: error indicates problem.
5643 * 1: Extent count or size has changed.
5644 * 0: No changes.
5645 **/
5646static int
5647lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5648{
5649 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5650 uint16_t size_diff, rsrc_ext_size;
5651 int rc = 0;
5652 struct lpfc_rsrc_blks *rsrc_entry;
5653 struct list_head *rsrc_blk_list = NULL;
5654
5655 size_diff = 0;
5656 curr_ext_cnt = 0;
5657 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5658 &rsrc_ext_cnt,
5659 &rsrc_ext_size);
5660 if (unlikely(rc))
5661 return -EIO;
5662
5663 switch (type) {
5664 case LPFC_RSC_TYPE_FCOE_RPI:
5665 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5666 break;
5667 case LPFC_RSC_TYPE_FCOE_VPI:
5668 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5669 break;
5670 case LPFC_RSC_TYPE_FCOE_XRI:
5671 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5672 break;
5673 case LPFC_RSC_TYPE_FCOE_VFI:
5674 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5675 break;
5676 default:
5677 break;
5678 }
5679
5680 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5681 curr_ext_cnt++;
5682 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5683 size_diff++;
5684 }
5685
5686 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5687 rc = 1;
5688
5689 return rc;
5690}
5691
5692/**
5693 * lpfc_sli4_cfg_post_extnts -
5694 * @phba: Pointer to HBA context object.
5695 * @extnt_cnt - number of available extents.
5696 * @type - the extent type (rpi, xri, vfi, vpi).
5697 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5698 * @mbox - pointer to the caller's allocated mailbox structure.
5699 *
5700 * This function executes the extents allocation request. It also
5701 * takes care of the amount of memory needed to allocate or get the
5702 * allocated extents. It is the caller's responsibility to evaluate
5703 * the response.
5704 *
5705 * Returns:
5706 * -Error: Error value describes the condition found.
5707 * 0: if successful
5708 **/
5709static int
James Smart8a9d2e82012-05-09 21:16:12 -04005710lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005711 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5712{
5713 int rc = 0;
5714 uint32_t req_len;
5715 uint32_t emb_len;
5716 uint32_t alloc_len, mbox_tmo;
5717
5718 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005719 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005720
5721 /*
5722 * Calculate the size of an embedded mailbox. The uint32_t
5723 * accounts for extents-specific word.
5724 */
5725 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5726 sizeof(uint32_t);
5727
5728 /*
5729 * Presume the allocation and response will fit into an embedded
5730 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5731 */
5732 *emb = LPFC_SLI4_MBX_EMBED;
5733 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005734 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005735 sizeof(union lpfc_sli4_cfg_shdr) +
5736 sizeof(uint32_t);
5737 *emb = LPFC_SLI4_MBX_NEMBED;
5738 }
5739
5740 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5741 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5742 req_len, *emb);
5743 if (alloc_len < req_len) {
5744 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005745 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005746 "less than the requested DMA memory "
5747 "size (x%x)\n", alloc_len, req_len);
5748 return -ENOMEM;
5749 }
James Smart8a9d2e82012-05-09 21:16:12 -04005750 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005751 if (unlikely(rc))
5752 return -EIO;
5753
5754 if (!phba->sli4_hba.intr_enable)
5755 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5756 else {
James Smarta183a152011-10-10 21:32:43 -04005757 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005758 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5759 }
5760
5761 if (unlikely(rc))
5762 rc = -EIO;
5763 return rc;
5764}
5765
5766/**
5767 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5768 * @phba: Pointer to HBA context object.
5769 * @type: The resource extent type to allocate.
5770 *
5771 * This function allocates the number of elements for the specified
5772 * resource type.
5773 **/
5774static int
5775lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5776{
5777 bool emb = false;
5778 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5779 uint16_t rsrc_id, rsrc_start, j, k;
5780 uint16_t *ids;
5781 int i, rc;
5782 unsigned long longs;
5783 unsigned long *bmask;
5784 struct lpfc_rsrc_blks *rsrc_blks;
5785 LPFC_MBOXQ_t *mbox;
5786 uint32_t length;
5787 struct lpfc_id_range *id_array = NULL;
5788 void *virtaddr = NULL;
5789 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5790 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5791 struct list_head *ext_blk_list;
5792
5793 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5794 &rsrc_cnt,
5795 &rsrc_size);
5796 if (unlikely(rc))
5797 return -EIO;
5798
5799 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5800 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5801 "3009 No available Resource Extents "
5802 "for resource type 0x%x: Count: 0x%x, "
5803 "Size 0x%x\n", type, rsrc_cnt,
5804 rsrc_size);
5805 return -ENOMEM;
5806 }
5807
James Smart8a9d2e82012-05-09 21:16:12 -04005808 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5809 "2903 Post resource extents type-0x%x: "
5810 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005811
5812 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5813 if (!mbox)
5814 return -ENOMEM;
5815
James Smart8a9d2e82012-05-09 21:16:12 -04005816 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005817 if (unlikely(rc)) {
5818 rc = -EIO;
5819 goto err_exit;
5820 }
5821
5822 /*
5823 * Figure out where the response is located. Then get local pointers
5824 * to the response data. The port does not guarantee to respond to
5825 * all extents counts request so update the local variable with the
5826 * allocated count from the port.
5827 */
5828 if (emb == LPFC_SLI4_MBX_EMBED) {
5829 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5830 id_array = &rsrc_ext->u.rsp.id[0];
5831 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5832 } else {
5833 virtaddr = mbox->sge_array->addr[0];
5834 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5835 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5836 id_array = &n_rsrc->id;
5837 }
5838
5839 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5840 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5841
5842 /*
5843 * Based on the resource size and count, correct the base and max
5844 * resource values.
5845 */
5846 length = sizeof(struct lpfc_rsrc_blks);
5847 switch (type) {
5848 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005849 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005850 sizeof(unsigned long),
5851 GFP_KERNEL);
5852 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5853 rc = -ENOMEM;
5854 goto err_exit;
5855 }
Kees Cook6396bb22018-06-12 14:03:40 -07005856 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005857 sizeof(uint16_t),
5858 GFP_KERNEL);
5859 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5860 kfree(phba->sli4_hba.rpi_bmask);
5861 rc = -ENOMEM;
5862 goto err_exit;
5863 }
5864
5865 /*
5866 * The next_rpi was initialized with the maximum available
5867 * count but the port may allocate a smaller number. Catch
5868 * that case and update the next_rpi.
5869 */
5870 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5871
5872 /* Initialize local ptrs for common extent processing later. */
5873 bmask = phba->sli4_hba.rpi_bmask;
5874 ids = phba->sli4_hba.rpi_ids;
5875 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5876 break;
5877 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005878 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005879 GFP_KERNEL);
5880 if (unlikely(!phba->vpi_bmask)) {
5881 rc = -ENOMEM;
5882 goto err_exit;
5883 }
Kees Cook6396bb22018-06-12 14:03:40 -07005884 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04005885 GFP_KERNEL);
5886 if (unlikely(!phba->vpi_ids)) {
5887 kfree(phba->vpi_bmask);
5888 rc = -ENOMEM;
5889 goto err_exit;
5890 }
5891
5892 /* Initialize local ptrs for common extent processing later. */
5893 bmask = phba->vpi_bmask;
5894 ids = phba->vpi_ids;
5895 ext_blk_list = &phba->lpfc_vpi_blk_list;
5896 break;
5897 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07005898 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005899 sizeof(unsigned long),
5900 GFP_KERNEL);
5901 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5902 rc = -ENOMEM;
5903 goto err_exit;
5904 }
James Smart8a9d2e82012-05-09 21:16:12 -04005905 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07005906 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005907 sizeof(uint16_t),
5908 GFP_KERNEL);
5909 if (unlikely(!phba->sli4_hba.xri_ids)) {
5910 kfree(phba->sli4_hba.xri_bmask);
5911 rc = -ENOMEM;
5912 goto err_exit;
5913 }
5914
5915 /* Initialize local ptrs for common extent processing later. */
5916 bmask = phba->sli4_hba.xri_bmask;
5917 ids = phba->sli4_hba.xri_ids;
5918 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5919 break;
5920 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07005921 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005922 sizeof(unsigned long),
5923 GFP_KERNEL);
5924 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5925 rc = -ENOMEM;
5926 goto err_exit;
5927 }
Kees Cook6396bb22018-06-12 14:03:40 -07005928 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005929 sizeof(uint16_t),
5930 GFP_KERNEL);
5931 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5932 kfree(phba->sli4_hba.vfi_bmask);
5933 rc = -ENOMEM;
5934 goto err_exit;
5935 }
5936
5937 /* Initialize local ptrs for common extent processing later. */
5938 bmask = phba->sli4_hba.vfi_bmask;
5939 ids = phba->sli4_hba.vfi_ids;
5940 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5941 break;
5942 default:
5943 /* Unsupported Opcode. Fail call. */
5944 id_array = NULL;
5945 bmask = NULL;
5946 ids = NULL;
5947 ext_blk_list = NULL;
5948 goto err_exit;
5949 }
5950
5951 /*
5952 * Complete initializing the extent configuration with the
5953 * allocated ids assigned to this function. The bitmask serves
5954 * as an index into the array and manages the available ids. The
5955 * array just stores the ids communicated to the port via the wqes.
5956 */
5957 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5958 if ((i % 2) == 0)
5959 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5960 &id_array[k]);
5961 else
5962 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5963 &id_array[k]);
5964
5965 rsrc_blks = kzalloc(length, GFP_KERNEL);
5966 if (unlikely(!rsrc_blks)) {
5967 rc = -ENOMEM;
5968 kfree(bmask);
5969 kfree(ids);
5970 goto err_exit;
5971 }
5972 rsrc_blks->rsrc_start = rsrc_id;
5973 rsrc_blks->rsrc_size = rsrc_size;
5974 list_add_tail(&rsrc_blks->list, ext_blk_list);
5975 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005976 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005977 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005978 lpfc_sli4_get_iocb_cnt(phba);
5979 phba->sli4_hba.nvme_xri_start =
5980 phba->sli4_hba.scsi_xri_start +
5981 phba->sli4_hba.scsi_xri_max;
5982 }
James Smart6d368e52011-05-24 11:44:12 -04005983
5984 while (rsrc_id < (rsrc_start + rsrc_size)) {
5985 ids[j] = rsrc_id;
5986 rsrc_id++;
5987 j++;
5988 }
5989 /* Entire word processed. Get next word.*/
5990 if ((i % 2) == 1)
5991 k++;
5992 }
5993 err_exit:
5994 lpfc_sli4_mbox_cmd_free(phba, mbox);
5995 return rc;
5996}
5997
James Smart895427b2017-02-12 13:52:30 -08005998
5999
James Smart6d368e52011-05-24 11:44:12 -04006000/**
6001 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6002 * @phba: Pointer to HBA context object.
6003 * @type: the extent's type.
6004 *
6005 * This function deallocates all extents of a particular resource type.
6006 * SLI4 does not allow for deallocating a particular extent range. It
6007 * is the caller's responsibility to release all kernel memory resources.
6008 **/
6009static int
6010lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6011{
6012 int rc;
6013 uint32_t length, mbox_tmo = 0;
6014 LPFC_MBOXQ_t *mbox;
6015 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6016 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6017
6018 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6019 if (!mbox)
6020 return -ENOMEM;
6021
6022 /*
6023 * This function sends an embedded mailbox because it only sends the
6024 * the resource type. All extents of this type are released by the
6025 * port.
6026 */
6027 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6028 sizeof(struct lpfc_sli4_cfg_mhdr));
6029 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6030 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6031 length, LPFC_SLI4_MBX_EMBED);
6032
6033 /* Send an extents count of 0 - the dealloc doesn't use it. */
6034 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6035 LPFC_SLI4_MBX_EMBED);
6036 if (unlikely(rc)) {
6037 rc = -EIO;
6038 goto out_free_mbox;
6039 }
6040 if (!phba->sli4_hba.intr_enable)
6041 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6042 else {
James Smarta183a152011-10-10 21:32:43 -04006043 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006044 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6045 }
6046 if (unlikely(rc)) {
6047 rc = -EIO;
6048 goto out_free_mbox;
6049 }
6050
6051 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6052 if (bf_get(lpfc_mbox_hdr_status,
6053 &dealloc_rsrc->header.cfg_shdr.response)) {
6054 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6055 "2919 Failed to release resource extents "
6056 "for type %d - Status 0x%x Add'l Status 0x%x. "
6057 "Resource memory not released.\n",
6058 type,
6059 bf_get(lpfc_mbox_hdr_status,
6060 &dealloc_rsrc->header.cfg_shdr.response),
6061 bf_get(lpfc_mbox_hdr_add_status,
6062 &dealloc_rsrc->header.cfg_shdr.response));
6063 rc = -EIO;
6064 goto out_free_mbox;
6065 }
6066
6067 /* Release kernel memory resources for the specific type. */
6068 switch (type) {
6069 case LPFC_RSC_TYPE_FCOE_VPI:
6070 kfree(phba->vpi_bmask);
6071 kfree(phba->vpi_ids);
6072 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6073 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6074 &phba->lpfc_vpi_blk_list, list) {
6075 list_del_init(&rsrc_blk->list);
6076 kfree(rsrc_blk);
6077 }
James Smart16a3a202013-04-17 20:14:38 -04006078 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006079 break;
6080 case LPFC_RSC_TYPE_FCOE_XRI:
6081 kfree(phba->sli4_hba.xri_bmask);
6082 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006083 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6084 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6085 list_del_init(&rsrc_blk->list);
6086 kfree(rsrc_blk);
6087 }
6088 break;
6089 case LPFC_RSC_TYPE_FCOE_VFI:
6090 kfree(phba->sli4_hba.vfi_bmask);
6091 kfree(phba->sli4_hba.vfi_ids);
6092 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6093 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6094 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6095 list_del_init(&rsrc_blk->list);
6096 kfree(rsrc_blk);
6097 }
6098 break;
6099 case LPFC_RSC_TYPE_FCOE_RPI:
6100 /* RPI bitmask and physical id array are cleaned up earlier. */
6101 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6102 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6103 list_del_init(&rsrc_blk->list);
6104 kfree(rsrc_blk);
6105 }
6106 break;
6107 default:
6108 break;
6109 }
6110
6111 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6112
6113 out_free_mbox:
6114 mempool_free(mbox, phba->mbox_mem_pool);
6115 return rc;
6116}
6117
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006118static void
James Smart7bdedb32016-07-06 12:36:00 -07006119lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6120 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006121{
James Smart65791f12016-07-06 12:35:56 -07006122 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006123
James Smart65791f12016-07-06 12:35:56 -07006124 len = sizeof(struct lpfc_mbx_set_feature) -
6125 sizeof(struct lpfc_sli4_cfg_mhdr);
6126 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6127 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6128 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006129
James Smart7bdedb32016-07-06 12:36:00 -07006130 switch (feature) {
6131 case LPFC_SET_UE_RECOVERY:
6132 bf_set(lpfc_mbx_set_feature_UER,
6133 &mbox->u.mqe.un.set_feature, 1);
6134 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6135 mbox->u.mqe.un.set_feature.param_len = 8;
6136 break;
6137 case LPFC_SET_MDS_DIAGS:
6138 bf_set(lpfc_mbx_set_feature_mds,
6139 &mbox->u.mqe.un.set_feature, 1);
6140 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006141 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006142 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6143 mbox->u.mqe.un.set_feature.param_len = 8;
6144 break;
James Smart65791f12016-07-06 12:35:56 -07006145 }
James Smart7bdedb32016-07-06 12:36:00 -07006146
6147 return;
James Smart65791f12016-07-06 12:35:56 -07006148}
6149
James Smart6d368e52011-05-24 11:44:12 -04006150/**
James Smartd2cc9bc2018-09-10 10:30:50 -07006151 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6152 * @phba: Pointer to HBA context object.
6153 *
6154 * This function is called to free memory allocated for RAS FW logging
6155 * support in the driver.
6156 **/
6157void
6158lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6159{
6160 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6161 struct lpfc_dmabuf *dmabuf, *next;
6162
6163 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6164 list_for_each_entry_safe(dmabuf, next,
6165 &ras_fwlog->fwlog_buff_list,
6166 list) {
6167 list_del(&dmabuf->list);
6168 dma_free_coherent(&phba->pcidev->dev,
6169 LPFC_RAS_MAX_ENTRY_SIZE,
6170 dmabuf->virt, dmabuf->phys);
6171 kfree(dmabuf);
6172 }
6173 }
6174
6175 if (ras_fwlog->lwpd.virt) {
6176 dma_free_coherent(&phba->pcidev->dev,
6177 sizeof(uint32_t) * 2,
6178 ras_fwlog->lwpd.virt,
6179 ras_fwlog->lwpd.phys);
6180 ras_fwlog->lwpd.virt = NULL;
6181 }
6182
6183 ras_fwlog->ras_active = false;
6184}
6185
6186/**
6187 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6188 * @phba: Pointer to HBA context object.
6189 * @fwlog_buff_count: Count of buffers to be created.
6190 *
6191 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6192 * to update FW log is posted to the adapter.
6193 * Buffer count is calculated based on module param ras_fwlog_buffsize
6194 * Size of each buffer posted to FW is 64K.
6195 **/
6196
6197static int
6198lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6199 uint32_t fwlog_buff_count)
6200{
6201 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6202 struct lpfc_dmabuf *dmabuf;
6203 int rc = 0, i = 0;
6204
6205 /* Initialize List */
6206 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6207
6208 /* Allocate memory for the LWPD */
6209 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6210 sizeof(uint32_t) * 2,
6211 &ras_fwlog->lwpd.phys,
6212 GFP_KERNEL);
6213 if (!ras_fwlog->lwpd.virt) {
6214 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6215 "6185 LWPD Memory Alloc Failed\n");
6216
6217 return -ENOMEM;
6218 }
6219
6220 ras_fwlog->fw_buffcount = fwlog_buff_count;
6221 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6222 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6223 GFP_KERNEL);
6224 if (!dmabuf) {
6225 rc = -ENOMEM;
6226 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6227 "6186 Memory Alloc failed FW logging");
6228 goto free_mem;
6229 }
6230
6231 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6232 LPFC_RAS_MAX_ENTRY_SIZE,
6233 &dmabuf->phys,
6234 GFP_KERNEL);
6235 if (!dmabuf->virt) {
6236 kfree(dmabuf);
6237 rc = -ENOMEM;
6238 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6239 "6187 DMA Alloc Failed FW logging");
6240 goto free_mem;
6241 }
6242 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6243 dmabuf->buffer_tag = i;
6244 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6245 }
6246
6247free_mem:
6248 if (rc)
6249 lpfc_sli4_ras_dma_free(phba);
6250
6251 return rc;
6252}
6253
6254/**
6255 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6256 * @phba: pointer to lpfc hba data structure.
6257 * @pmboxq: pointer to the driver internal queue element for mailbox command.
6258 *
6259 * Completion handler for driver's RAS MBX command to the device.
6260 **/
6261static void
6262lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6263{
6264 MAILBOX_t *mb;
6265 union lpfc_sli4_cfg_shdr *shdr;
6266 uint32_t shdr_status, shdr_add_status;
6267 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6268
6269 mb = &pmb->u.mb;
6270
6271 shdr = (union lpfc_sli4_cfg_shdr *)
6272 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6273 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6274 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6275
6276 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
6277 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6278 "6188 FW LOG mailbox "
6279 "completed with status x%x add_status x%x,"
6280 " mbx status x%x\n",
6281 shdr_status, shdr_add_status, mb->mbxStatus);
6282 goto disable_ras;
6283 }
6284
6285 ras_fwlog->ras_active = true;
6286 mempool_free(pmb, phba->mbox_mem_pool);
6287
6288 return;
6289
6290disable_ras:
6291 /* Free RAS DMA memory */
6292 lpfc_sli4_ras_dma_free(phba);
6293 mempool_free(pmb, phba->mbox_mem_pool);
6294}
6295
6296/**
6297 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6298 * @phba: pointer to lpfc hba data structure.
6299 * @fwlog_level: Logging verbosity level.
6300 * @fwlog_enable: Enable/Disable logging.
6301 *
6302 * Initialize memory and post mailbox command to enable FW logging in host
6303 * memory.
6304 **/
6305int
6306lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6307 uint32_t fwlog_level,
6308 uint32_t fwlog_enable)
6309{
6310 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6311 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6312 struct lpfc_dmabuf *dmabuf;
6313 LPFC_MBOXQ_t *mbox;
6314 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6315 int rc = 0;
6316
6317 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6318 phba->cfg_ras_fwlog_buffsize);
6319 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6320
6321 /*
6322 * If re-enabling FW logging support use earlier allocated
6323 * DMA buffers while posting MBX command.
6324 **/
6325 if (!ras_fwlog->lwpd.virt) {
6326 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6327 if (rc) {
6328 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6329 "6189 RAS FW Log Support Not Enabled");
6330 return rc;
6331 }
6332 }
6333
6334 /* Setup Mailbox command */
6335 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6336 if (!mbox) {
6337 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6338 "6190 RAS MBX Alloc Failed");
6339 rc = -ENOMEM;
6340 goto mem_free;
6341 }
6342
6343 ras_fwlog->fw_loglevel = fwlog_level;
6344 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6345 sizeof(struct lpfc_sli4_cfg_mhdr));
6346
6347 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6348 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6349 len, LPFC_SLI4_MBX_EMBED);
6350
6351 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6352 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6353 fwlog_enable);
6354 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6355 ras_fwlog->fw_loglevel);
6356 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6357 ras_fwlog->fw_buffcount);
6358 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6359 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6360
6361 /* Update DMA buffer address */
6362 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6363 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6364
6365 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6366 putPaddrLow(dmabuf->phys);
6367
6368 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6369 putPaddrHigh(dmabuf->phys);
6370 }
6371
6372 /* Update LPWD address */
6373 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6374 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6375
6376 mbox->vport = phba->pport;
6377 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6378
6379 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6380
6381 if (rc == MBX_NOT_FINISHED) {
6382 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6383 "6191 RAS Mailbox failed. "
6384 "status %d mbxStatus : x%x", rc,
6385 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6386 mempool_free(mbox, phba->mbox_mem_pool);
6387 rc = -EIO;
6388 goto mem_free;
6389 } else
6390 rc = 0;
6391mem_free:
6392 if (rc)
6393 lpfc_sli4_ras_dma_free(phba);
6394
6395 return rc;
6396}
6397
6398/**
6399 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6400 * @phba: Pointer to HBA context object.
6401 *
6402 * Check if RAS is supported on the adapter and initialize it.
6403 **/
6404void
6405lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6406{
6407 /* Check RAS FW Log needs to be enabled or not */
6408 if (lpfc_check_fwlog_support(phba))
6409 return;
6410
6411 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6412 LPFC_RAS_ENABLE_LOGGING);
6413}
6414
6415/**
James Smart6d368e52011-05-24 11:44:12 -04006416 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6417 * @phba: Pointer to HBA context object.
6418 *
6419 * This function allocates all SLI4 resource identifiers.
6420 **/
6421int
6422lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6423{
6424 int i, rc, error = 0;
6425 uint16_t count, base;
6426 unsigned long longs;
6427
James Smartff78d8f2011-12-13 13:21:35 -05006428 if (!phba->sli4_hba.rpi_hdrs_in_use)
6429 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006430 if (phba->sli4_hba.extents_in_use) {
6431 /*
6432 * The port supports resource extents. The XRI, VPI, VFI, RPI
6433 * resource extent count must be read and allocated before
6434 * provisioning the resource id arrays.
6435 */
6436 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6437 LPFC_IDX_RSRC_RDY) {
6438 /*
6439 * Extent-based resources are set - the driver could
6440 * be in a port reset. Figure out if any corrective
6441 * actions need to be taken.
6442 */
6443 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6444 LPFC_RSC_TYPE_FCOE_VFI);
6445 if (rc != 0)
6446 error++;
6447 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6448 LPFC_RSC_TYPE_FCOE_VPI);
6449 if (rc != 0)
6450 error++;
6451 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6452 LPFC_RSC_TYPE_FCOE_XRI);
6453 if (rc != 0)
6454 error++;
6455 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6456 LPFC_RSC_TYPE_FCOE_RPI);
6457 if (rc != 0)
6458 error++;
6459
6460 /*
6461 * It's possible that the number of resources
6462 * provided to this port instance changed between
6463 * resets. Detect this condition and reallocate
6464 * resources. Otherwise, there is no action.
6465 */
6466 if (error) {
6467 lpfc_printf_log(phba, KERN_INFO,
6468 LOG_MBOX | LOG_INIT,
6469 "2931 Detected extent resource "
6470 "change. Reallocating all "
6471 "extents.\n");
6472 rc = lpfc_sli4_dealloc_extent(phba,
6473 LPFC_RSC_TYPE_FCOE_VFI);
6474 rc = lpfc_sli4_dealloc_extent(phba,
6475 LPFC_RSC_TYPE_FCOE_VPI);
6476 rc = lpfc_sli4_dealloc_extent(phba,
6477 LPFC_RSC_TYPE_FCOE_XRI);
6478 rc = lpfc_sli4_dealloc_extent(phba,
6479 LPFC_RSC_TYPE_FCOE_RPI);
6480 } else
6481 return 0;
6482 }
6483
6484 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6485 if (unlikely(rc))
6486 goto err_exit;
6487
6488 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6489 if (unlikely(rc))
6490 goto err_exit;
6491
6492 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6493 if (unlikely(rc))
6494 goto err_exit;
6495
6496 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6497 if (unlikely(rc))
6498 goto err_exit;
6499 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6500 LPFC_IDX_RSRC_RDY);
6501 return rc;
6502 } else {
6503 /*
6504 * The port does not support resource extents. The XRI, VPI,
6505 * VFI, RPI resource ids were determined from READ_CONFIG.
6506 * Just allocate the bitmasks and provision the resource id
6507 * arrays. If a port reset is active, the resources don't
6508 * need any action - just exit.
6509 */
6510 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006511 LPFC_IDX_RSRC_RDY) {
6512 lpfc_sli4_dealloc_resource_identifiers(phba);
6513 lpfc_sli4_remove_rpis(phba);
6514 }
James Smart6d368e52011-05-24 11:44:12 -04006515 /* RPIs. */
6516 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006517 if (count <= 0) {
6518 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6519 "3279 Invalid provisioning of "
6520 "rpi:%d\n", count);
6521 rc = -EINVAL;
6522 goto err_exit;
6523 }
James Smart6d368e52011-05-24 11:44:12 -04006524 base = phba->sli4_hba.max_cfg_param.rpi_base;
6525 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006526 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006527 sizeof(unsigned long),
6528 GFP_KERNEL);
6529 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6530 rc = -ENOMEM;
6531 goto err_exit;
6532 }
Kees Cook6396bb22018-06-12 14:03:40 -07006533 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006534 GFP_KERNEL);
6535 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6536 rc = -ENOMEM;
6537 goto free_rpi_bmask;
6538 }
6539
6540 for (i = 0; i < count; i++)
6541 phba->sli4_hba.rpi_ids[i] = base + i;
6542
6543 /* VPIs. */
6544 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006545 if (count <= 0) {
6546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6547 "3280 Invalid provisioning of "
6548 "vpi:%d\n", count);
6549 rc = -EINVAL;
6550 goto free_rpi_ids;
6551 }
James Smart6d368e52011-05-24 11:44:12 -04006552 base = phba->sli4_hba.max_cfg_param.vpi_base;
6553 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006554 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006555 GFP_KERNEL);
6556 if (unlikely(!phba->vpi_bmask)) {
6557 rc = -ENOMEM;
6558 goto free_rpi_ids;
6559 }
Kees Cook6396bb22018-06-12 14:03:40 -07006560 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006561 GFP_KERNEL);
6562 if (unlikely(!phba->vpi_ids)) {
6563 rc = -ENOMEM;
6564 goto free_vpi_bmask;
6565 }
6566
6567 for (i = 0; i < count; i++)
6568 phba->vpi_ids[i] = base + i;
6569
6570 /* XRIs. */
6571 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006572 if (count <= 0) {
6573 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6574 "3281 Invalid provisioning of "
6575 "xri:%d\n", count);
6576 rc = -EINVAL;
6577 goto free_vpi_ids;
6578 }
James Smart6d368e52011-05-24 11:44:12 -04006579 base = phba->sli4_hba.max_cfg_param.xri_base;
6580 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006581 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006582 sizeof(unsigned long),
6583 GFP_KERNEL);
6584 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6585 rc = -ENOMEM;
6586 goto free_vpi_ids;
6587 }
James Smart41899be2012-03-01 22:34:19 -05006588 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006589 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006590 GFP_KERNEL);
6591 if (unlikely(!phba->sli4_hba.xri_ids)) {
6592 rc = -ENOMEM;
6593 goto free_xri_bmask;
6594 }
6595
6596 for (i = 0; i < count; i++)
6597 phba->sli4_hba.xri_ids[i] = base + i;
6598
6599 /* VFIs. */
6600 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006601 if (count <= 0) {
6602 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6603 "3282 Invalid provisioning of "
6604 "vfi:%d\n", count);
6605 rc = -EINVAL;
6606 goto free_xri_ids;
6607 }
James Smart6d368e52011-05-24 11:44:12 -04006608 base = phba->sli4_hba.max_cfg_param.vfi_base;
6609 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006610 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006611 sizeof(unsigned long),
6612 GFP_KERNEL);
6613 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6614 rc = -ENOMEM;
6615 goto free_xri_ids;
6616 }
Kees Cook6396bb22018-06-12 14:03:40 -07006617 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006618 GFP_KERNEL);
6619 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6620 rc = -ENOMEM;
6621 goto free_vfi_bmask;
6622 }
6623
6624 for (i = 0; i < count; i++)
6625 phba->sli4_hba.vfi_ids[i] = base + i;
6626
6627 /*
6628 * Mark all resources ready. An HBA reset doesn't need
6629 * to reset the initialization.
6630 */
6631 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6632 LPFC_IDX_RSRC_RDY);
6633 return 0;
6634 }
6635
6636 free_vfi_bmask:
6637 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006638 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006639 free_xri_ids:
6640 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006641 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006642 free_xri_bmask:
6643 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006644 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006645 free_vpi_ids:
6646 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006647 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006648 free_vpi_bmask:
6649 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006650 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006651 free_rpi_ids:
6652 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006653 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006654 free_rpi_bmask:
6655 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006656 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006657 err_exit:
6658 return rc;
6659}
6660
6661/**
6662 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6663 * @phba: Pointer to HBA context object.
6664 *
6665 * This function allocates the number of elements for the specified
6666 * resource type.
6667 **/
6668int
6669lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6670{
6671 if (phba->sli4_hba.extents_in_use) {
6672 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6673 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6674 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6675 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6676 } else {
6677 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006678 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006679 kfree(phba->vpi_ids);
6680 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6681 kfree(phba->sli4_hba.xri_bmask);
6682 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006683 kfree(phba->sli4_hba.vfi_bmask);
6684 kfree(phba->sli4_hba.vfi_ids);
6685 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6686 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6687 }
6688
6689 return 0;
6690}
6691
6692/**
James Smartb76f2dc2011-07-22 18:37:42 -04006693 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6694 * @phba: Pointer to HBA context object.
6695 * @type: The resource extent type.
6696 * @extnt_count: buffer to hold port extent count response
6697 * @extnt_size: buffer to hold port extent size response.
6698 *
6699 * This function calls the port to read the host allocated extents
6700 * for a particular type.
6701 **/
6702int
6703lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6704 uint16_t *extnt_cnt, uint16_t *extnt_size)
6705{
6706 bool emb;
6707 int rc = 0;
6708 uint16_t curr_blks = 0;
6709 uint32_t req_len, emb_len;
6710 uint32_t alloc_len, mbox_tmo;
6711 struct list_head *blk_list_head;
6712 struct lpfc_rsrc_blks *rsrc_blk;
6713 LPFC_MBOXQ_t *mbox;
6714 void *virtaddr = NULL;
6715 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6716 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6717 union lpfc_sli4_cfg_shdr *shdr;
6718
6719 switch (type) {
6720 case LPFC_RSC_TYPE_FCOE_VPI:
6721 blk_list_head = &phba->lpfc_vpi_blk_list;
6722 break;
6723 case LPFC_RSC_TYPE_FCOE_XRI:
6724 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6725 break;
6726 case LPFC_RSC_TYPE_FCOE_VFI:
6727 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6728 break;
6729 case LPFC_RSC_TYPE_FCOE_RPI:
6730 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6731 break;
6732 default:
6733 return -EIO;
6734 }
6735
6736 /* Count the number of extents currently allocatd for this type. */
6737 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6738 if (curr_blks == 0) {
6739 /*
6740 * The GET_ALLOCATED mailbox does not return the size,
6741 * just the count. The size should be just the size
6742 * stored in the current allocated block and all sizes
6743 * for an extent type are the same so set the return
6744 * value now.
6745 */
6746 *extnt_size = rsrc_blk->rsrc_size;
6747 }
6748 curr_blks++;
6749 }
6750
James Smartb76f2dc2011-07-22 18:37:42 -04006751 /*
6752 * Calculate the size of an embedded mailbox. The uint32_t
6753 * accounts for extents-specific word.
6754 */
6755 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6756 sizeof(uint32_t);
6757
6758 /*
6759 * Presume the allocation and response will fit into an embedded
6760 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6761 */
6762 emb = LPFC_SLI4_MBX_EMBED;
6763 req_len = emb_len;
6764 if (req_len > emb_len) {
6765 req_len = curr_blks * sizeof(uint16_t) +
6766 sizeof(union lpfc_sli4_cfg_shdr) +
6767 sizeof(uint32_t);
6768 emb = LPFC_SLI4_MBX_NEMBED;
6769 }
6770
6771 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6772 if (!mbox)
6773 return -ENOMEM;
6774 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6775
6776 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6777 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6778 req_len, emb);
6779 if (alloc_len < req_len) {
6780 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6781 "2983 Allocated DMA memory size (x%x) is "
6782 "less than the requested DMA memory "
6783 "size (x%x)\n", alloc_len, req_len);
6784 rc = -ENOMEM;
6785 goto err_exit;
6786 }
6787 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6788 if (unlikely(rc)) {
6789 rc = -EIO;
6790 goto err_exit;
6791 }
6792
6793 if (!phba->sli4_hba.intr_enable)
6794 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6795 else {
James Smarta183a152011-10-10 21:32:43 -04006796 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006797 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6798 }
6799
6800 if (unlikely(rc)) {
6801 rc = -EIO;
6802 goto err_exit;
6803 }
6804
6805 /*
6806 * Figure out where the response is located. Then get local pointers
6807 * to the response data. The port does not guarantee to respond to
6808 * all extents counts request so update the local variable with the
6809 * allocated count from the port.
6810 */
6811 if (emb == LPFC_SLI4_MBX_EMBED) {
6812 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6813 shdr = &rsrc_ext->header.cfg_shdr;
6814 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6815 } else {
6816 virtaddr = mbox->sge_array->addr[0];
6817 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6818 shdr = &n_rsrc->cfg_shdr;
6819 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6820 }
6821
6822 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6823 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6824 "2984 Failed to read allocated resources "
6825 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6826 type,
6827 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6828 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6829 rc = -EIO;
6830 goto err_exit;
6831 }
6832 err_exit:
6833 lpfc_sli4_mbox_cmd_free(phba, mbox);
6834 return rc;
6835}
6836
6837/**
James Smart0ef69962017-04-21 16:04:50 -07006838 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006839 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006840 * @pring: Pointer to driver SLI ring object.
6841 * @sgl_list: linked link of sgl buffers to post
6842 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006843 *
James Smart895427b2017-02-12 13:52:30 -08006844 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006845 * repost them to the port by using SGL block post. This is needed after a
6846 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006847 * of buffer sgls which contains contiguous xris and uses the non-embedded
6848 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006849 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6850 * mailbox command for posting.
6851 *
6852 * Returns: 0 = success, non-zero failure.
6853 **/
6854static int
James Smart895427b2017-02-12 13:52:30 -08006855lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6856 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006857{
6858 struct lpfc_sglq *sglq_entry = NULL;
6859 struct lpfc_sglq *sglq_entry_next = NULL;
6860 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006861 int status, total_cnt;
6862 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006863 int last_xritag = NO_XRI;
6864 LIST_HEAD(prep_sgl_list);
6865 LIST_HEAD(blck_sgl_list);
6866 LIST_HEAD(allc_sgl_list);
6867 LIST_HEAD(post_sgl_list);
6868 LIST_HEAD(free_sgl_list);
6869
James Smart38c20672013-03-01 16:37:44 -05006870 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006871 spin_lock(&phba->sli4_hba.sgl_list_lock);
6872 list_splice_init(sgl_list, &allc_sgl_list);
6873 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006874 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006875
James Smart895427b2017-02-12 13:52:30 -08006876 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006877 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6878 &allc_sgl_list, list) {
6879 list_del_init(&sglq_entry->list);
6880 block_cnt++;
6881 if ((last_xritag != NO_XRI) &&
6882 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6883 /* a hole in xri block, form a sgl posting block */
6884 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6885 post_cnt = block_cnt - 1;
6886 /* prepare list for next posting block */
6887 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6888 block_cnt = 1;
6889 } else {
6890 /* prepare list for next posting block */
6891 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6892 /* enough sgls for non-embed sgl mbox command */
6893 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6894 list_splice_init(&prep_sgl_list,
6895 &blck_sgl_list);
6896 post_cnt = block_cnt;
6897 block_cnt = 0;
6898 }
6899 }
6900 num_posted++;
6901
6902 /* keep track of last sgl's xritag */
6903 last_xritag = sglq_entry->sli4_xritag;
6904
James Smart895427b2017-02-12 13:52:30 -08006905 /* end of repost sgl list condition for buffers */
6906 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006907 if (post_cnt == 0) {
6908 list_splice_init(&prep_sgl_list,
6909 &blck_sgl_list);
6910 post_cnt = block_cnt;
6911 } else if (block_cnt == 1) {
6912 status = lpfc_sli4_post_sgl(phba,
6913 sglq_entry->phys, 0,
6914 sglq_entry->sli4_xritag);
6915 if (!status) {
6916 /* successful, put sgl to posted list */
6917 list_add_tail(&sglq_entry->list,
6918 &post_sgl_list);
6919 } else {
6920 /* Failure, put sgl to free list */
6921 lpfc_printf_log(phba, KERN_WARNING,
6922 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006923 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006924 "sgl, xritag:x%x\n",
6925 sglq_entry->sli4_xritag);
6926 list_add_tail(&sglq_entry->list,
6927 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006928 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006929 }
6930 }
6931 }
6932
6933 /* continue until a nembed page worth of sgls */
6934 if (post_cnt == 0)
6935 continue;
6936
James Smart895427b2017-02-12 13:52:30 -08006937 /* post the buffer list sgls as a block */
6938 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6939 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006940
6941 if (!status) {
6942 /* success, put sgl list to posted sgl list */
6943 list_splice_init(&blck_sgl_list, &post_sgl_list);
6944 } else {
6945 /* Failure, put sgl list to free sgl list */
6946 sglq_entry_first = list_first_entry(&blck_sgl_list,
6947 struct lpfc_sglq,
6948 list);
6949 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006950 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006951 "xritag:x%x-x%x\n",
6952 sglq_entry_first->sli4_xritag,
6953 (sglq_entry_first->sli4_xritag +
6954 post_cnt - 1));
6955 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006956 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006957 }
6958
6959 /* don't reset xirtag due to hole in xri block */
6960 if (block_cnt == 0)
6961 last_xritag = NO_XRI;
6962
James Smart895427b2017-02-12 13:52:30 -08006963 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006964 post_cnt = 0;
6965 }
6966
James Smart895427b2017-02-12 13:52:30 -08006967 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006968 lpfc_free_sgl_list(phba, &free_sgl_list);
6969
James Smart895427b2017-02-12 13:52:30 -08006970 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006971 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006972 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006973 spin_lock(&phba->sli4_hba.sgl_list_lock);
6974 list_splice_init(&post_sgl_list, sgl_list);
6975 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006976 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006977 } else {
6978 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006979 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006980 return -EIO;
6981 }
James Smart895427b2017-02-12 13:52:30 -08006982
6983 /* return the number of XRIs actually posted */
6984 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006985}
6986
James Smart61bda8f2016-10-13 15:06:05 -07006987void
6988lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6989{
6990 uint32_t len;
6991
6992 len = sizeof(struct lpfc_mbx_set_host_data) -
6993 sizeof(struct lpfc_sli4_cfg_mhdr);
6994 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6995 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6996 LPFC_SLI4_MBX_EMBED);
6997
6998 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006999 mbox->u.mqe.un.set_host_data.param_len =
7000 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07007001 snprintf(mbox->u.mqe.un.set_host_data.data,
7002 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7003 "Linux %s v"LPFC_DRIVER_VERSION,
7004 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7005}
7006
James Smarta8cf5df2017-05-15 15:20:46 -07007007int
James Smart6c621a22017-05-15 15:20:45 -07007008lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07007009 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07007010{
7011 int rc, i;
7012 struct lpfc_rqe hrqe;
7013 struct lpfc_rqe drqe;
7014 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08007015 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07007016 struct rqb_dmabuf *rqb_buffer;
7017 LIST_HEAD(rqb_buf_list);
7018
James Smart411de512018-01-30 15:58:52 -08007019 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007020 rqbp = hrq->rqbp;
7021 for (i = 0; i < count; i++) {
7022 /* IF RQ is already full, don't bother */
7023 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
7024 break;
7025 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7026 if (!rqb_buffer)
7027 break;
7028 rqb_buffer->hrq = hrq;
7029 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07007030 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07007031 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7032 }
7033 while (!list_empty(&rqb_buf_list)) {
7034 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7035 hbuf.list);
7036
7037 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7038 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7039 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7040 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7041 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7042 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08007043 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7044 "6421 Cannot post to HRQ %d: %x %x %x "
7045 "DRQ %x %x\n",
7046 hrq->queue_id,
7047 hrq->host_index,
7048 hrq->hba_index,
7049 hrq->entry_count,
7050 drq->host_index,
7051 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07007052 rqbp->rqb_free_buffer(phba, rqb_buffer);
7053 } else {
7054 list_add_tail(&rqb_buffer->hbuf.list,
7055 &rqbp->rqb_buffer_list);
7056 rqbp->buffer_count++;
7057 }
7058 }
James Smart411de512018-01-30 15:58:52 -08007059 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007060 return 1;
7061}
7062
James Smart8a9d2e82012-05-09 21:16:12 -04007063/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08007064 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04007065 * @phba: Pointer to HBA context object.
7066 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08007067 * This function is the main SLI4 device initialization PCI function. This
7068 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04007069 * HBA error attention handler code. Caller is not required to hold any
7070 * locks.
7071 **/
7072int
7073lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7074{
James Smart6c621a22017-05-15 15:20:45 -07007075 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04007076 LPFC_MBOXQ_t *mboxq;
7077 struct lpfc_mqe *mqe;
7078 uint8_t *vpd;
7079 uint32_t vpd_size;
7080 uint32_t ftr_rsp = 0;
7081 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7082 struct lpfc_vport *vport = phba->pport;
7083 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08007084 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04007085
7086 /* Perform a PCI function reset to start from clean */
7087 rc = lpfc_pci_function_reset(phba);
7088 if (unlikely(rc))
7089 return -ENODEV;
7090
7091 /* Check the HBA Host Status Register for readyness */
7092 rc = lpfc_sli4_post_status_check(phba);
7093 if (unlikely(rc))
7094 return -ENODEV;
7095 else {
7096 spin_lock_irq(&phba->hbalock);
7097 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7098 spin_unlock_irq(&phba->hbalock);
7099 }
7100
7101 /*
7102 * Allocate a single mailbox container for initializing the
7103 * port.
7104 */
7105 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7106 if (!mboxq)
7107 return -ENOMEM;
7108
James Smartda0436e2009-05-22 14:51:39 -04007109 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04007110 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04007111 vpd = kzalloc(vpd_size, GFP_KERNEL);
7112 if (!vpd) {
7113 rc = -ENOMEM;
7114 goto out_free_mbox;
7115 }
7116
7117 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05007118 if (unlikely(rc)) {
7119 kfree(vpd);
7120 goto out_free_mbox;
7121 }
James Smart572709e2013-07-15 18:32:43 -04007122
James Smartda0436e2009-05-22 14:51:39 -04007123 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04007124 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07007125 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05007126 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007127 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7128 } else {
James Smart76a95d72010-11-20 23:11:48 -05007129 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007130 }
James Smart45ed1192009-10-02 15:17:02 -04007131
7132 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7133 LPFC_DCBX_CEE_MODE)
7134 phba->hba_flag |= HBA_FIP_SUPPORT;
7135 else
7136 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7137
James Smart4f2e66c2012-05-09 21:17:07 -04007138 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
7139
James Smartc31098c2011-04-16 11:03:33 -04007140 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007141 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7142 "0376 READ_REV Error. SLI Level %d "
7143 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05007144 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04007145 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007146 kfree(vpd);
7147 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007148 }
James Smartcd1c8302011-10-10 21:33:25 -04007149
7150 /*
James Smartff78d8f2011-12-13 13:21:35 -05007151 * Continue initialization with default values even if driver failed
7152 * to read FCoE param config regions, only read parameters if the
7153 * board is FCoE
7154 */
7155 if (phba->hba_flag & HBA_FCOE_MODE &&
7156 lpfc_sli4_read_fcoe_params(phba))
7157 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7158 "2570 Failed to read FCoE parameters\n");
7159
7160 /*
James Smartcd1c8302011-10-10 21:33:25 -04007161 * Retrieve sli4 device physical port name, failure of doing it
7162 * is considered as non-fatal.
7163 */
7164 rc = lpfc_sli4_retrieve_pport_name(phba);
7165 if (!rc)
7166 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7167 "3080 Successful retrieving SLI4 device "
7168 "physical port name: %s.\n", phba->Port);
7169
James Smartda0436e2009-05-22 14:51:39 -04007170 /*
7171 * Evaluate the read rev and vpd data. Populate the driver
7172 * state with the results. If this routine fails, the failure
7173 * is not fatal as the driver will use generic values.
7174 */
7175 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7176 if (unlikely(!rc)) {
7177 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7178 "0377 Error %d parsing vpd. "
7179 "Using defaults.\n", rc);
7180 rc = 0;
7181 }
James Smart76a95d72010-11-20 23:11:48 -05007182 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04007183
James Smartf1126682009-06-10 17:22:44 -04007184 /* Save information as VPD data */
7185 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7186 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08007187
7188 /*
7189 * This is because first G7 ASIC doesn't support the standard
7190 * 0x5a NVME cmd descriptor type/subtype
7191 */
7192 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7193 LPFC_SLI_INTF_IF_TYPE_6) &&
7194 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7195 (phba->vpd.rev.smRev == 0) &&
7196 (phba->cfg_nvme_embed_cmd == 1))
7197 phba->cfg_nvme_embed_cmd = 0;
7198
James Smartf1126682009-06-10 17:22:44 -04007199 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7200 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7201 &mqe->un.read_rev);
7202 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7203 &mqe->un.read_rev);
7204 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7205 &mqe->un.read_rev);
7206 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7207 &mqe->un.read_rev);
7208 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7209 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7210 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7211 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7212 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7213 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7214 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7215 "(%d):0380 READ_REV Status x%x "
7216 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7217 mboxq->vport ? mboxq->vport->vpi : 0,
7218 bf_get(lpfc_mqe_status, mqe),
7219 phba->vpd.rev.opFwName,
7220 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7221 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04007222
James Smart572709e2013-07-15 18:32:43 -04007223 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
7224 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
7225 if (phba->pport->cfg_lun_queue_depth > rc) {
7226 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7227 "3362 LUN queue depth changed from %d to %d\n",
7228 phba->pport->cfg_lun_queue_depth, rc);
7229 phba->pport->cfg_lun_queue_depth = rc;
7230 }
7231
James Smart65791f12016-07-06 12:35:56 -07007232 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07007233 LPFC_SLI_INTF_IF_TYPE_0) {
7234 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7235 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7236 if (rc == MBX_SUCCESS) {
7237 phba->hba_flag |= HBA_RECOVERABLE_UE;
7238 /* Set 1Sec interval to detect UE */
7239 phba->eratt_poll_interval = 1;
7240 phba->sli4_hba.ue_to_sr = bf_get(
7241 lpfc_mbx_set_feature_UESR,
7242 &mboxq->u.mqe.un.set_feature);
7243 phba->sli4_hba.ue_to_rp = bf_get(
7244 lpfc_mbx_set_feature_UERP,
7245 &mboxq->u.mqe.un.set_feature);
7246 }
7247 }
7248
7249 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7250 /* Enable MDS Diagnostics only if the SLI Port supports it */
7251 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7252 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7253 if (rc != MBX_SUCCESS)
7254 phba->mds_diags_support = 0;
7255 }
James Smart572709e2013-07-15 18:32:43 -04007256
James Smartda0436e2009-05-22 14:51:39 -04007257 /*
7258 * Discover the port's supported feature set and match it against the
7259 * hosts requests.
7260 */
7261 lpfc_request_features(phba, mboxq);
7262 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7263 if (unlikely(rc)) {
7264 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007265 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007266 }
7267
7268 /*
7269 * The port must support FCP initiator mode as this is the
7270 * only mode running in the host.
7271 */
7272 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7273 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7274 "0378 No support for fcpi mode.\n");
7275 ftr_rsp++;
7276 }
James Smart0bc2b7c2018-02-22 08:18:48 -08007277
7278 /* Performance Hints are ONLY for FCoE */
7279 if (phba->hba_flag & HBA_FCOE_MODE) {
7280 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7281 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7282 else
7283 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7284 }
7285
James Smartda0436e2009-05-22 14:51:39 -04007286 /*
7287 * If the port cannot support the host's requested features
7288 * then turn off the global config parameters to disable the
7289 * feature in the driver. This is not a fatal error.
7290 */
James Smartf44ac122018-03-05 12:04:08 -08007291 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7292 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7293 phba->cfg_enable_bg = 0;
7294 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04007295 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08007296 }
James Smartbf086112011-08-21 21:48:13 -04007297 }
James Smartda0436e2009-05-22 14:51:39 -04007298
7299 if (phba->max_vpi && phba->cfg_enable_npiv &&
7300 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7301 ftr_rsp++;
7302
7303 if (ftr_rsp) {
7304 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7305 "0379 Feature Mismatch Data: x%08x %08x "
7306 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7307 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7308 phba->cfg_enable_npiv, phba->max_vpi);
7309 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7310 phba->cfg_enable_bg = 0;
7311 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7312 phba->cfg_enable_npiv = 0;
7313 }
7314
7315 /* These SLI3 features are assumed in SLI4 */
7316 spin_lock_irq(&phba->hbalock);
7317 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7318 spin_unlock_irq(&phba->hbalock);
7319
James Smart6d368e52011-05-24 11:44:12 -04007320 /*
7321 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7322 * calls depends on these resources to complete port setup.
7323 */
7324 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7325 if (rc) {
7326 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7327 "2920 Failed to alloc Resource IDs "
7328 "rc = x%x\n", rc);
7329 goto out_free_mbox;
7330 }
7331
James Smart61bda8f2016-10-13 15:06:05 -07007332 lpfc_set_host_data(phba, mboxq);
7333
7334 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7335 if (rc) {
7336 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7337 "2134 Failed to set host os driver version %x",
7338 rc);
7339 }
7340
James Smartda0436e2009-05-22 14:51:39 -04007341 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007342 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7343 if (rc) {
7344 phba->link_state = LPFC_HBA_ERROR;
7345 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007346 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007347 }
7348
James Smartda0436e2009-05-22 14:51:39 -04007349 mboxq->vport = vport;
7350 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7351 mp = (struct lpfc_dmabuf *) mboxq->context1;
7352 if (rc == MBX_SUCCESS) {
7353 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7354 rc = 0;
7355 }
7356
7357 /*
7358 * This memory was allocated by the lpfc_read_sparam routine. Release
7359 * it to the mbuf pool.
7360 */
7361 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7362 kfree(mp);
7363 mboxq->context1 = NULL;
7364 if (unlikely(rc)) {
7365 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7366 "0382 READ_SPARAM command failed "
7367 "status %d, mbxStatus x%x\n",
7368 rc, bf_get(lpfc_mqe_status, mqe));
7369 phba->link_state = LPFC_HBA_ERROR;
7370 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007371 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007372 }
7373
James Smart05580562011-05-24 11:40:48 -04007374 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007375
7376 /* Update the fc_host data structures with new wwn. */
7377 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7378 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7379
James Smart895427b2017-02-12 13:52:30 -08007380 /* Create all the SLI4 queues */
7381 rc = lpfc_sli4_queue_create(phba);
7382 if (rc) {
7383 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7384 "3089 Failed to allocate queues\n");
7385 rc = -ENODEV;
7386 goto out_free_mbox;
7387 }
7388 /* Set up all the queues to the device */
7389 rc = lpfc_sli4_queue_setup(phba);
7390 if (unlikely(rc)) {
7391 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7392 "0381 Error %d during queue setup.\n ", rc);
7393 goto out_stop_timers;
7394 }
7395 /* Initialize the driver internal SLI layer lists. */
7396 lpfc_sli4_setup(phba);
7397 lpfc_sli4_queue_init(phba);
7398
7399 /* update host els xri-sgl sizes and mappings */
7400 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007401 if (unlikely(rc)) {
7402 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7403 "1400 Failed to update xri-sgl size and "
7404 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007405 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007406 }
7407
James Smart8a9d2e82012-05-09 21:16:12 -04007408 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007409 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7410 phba->sli4_hba.els_xri_cnt);
7411 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007412 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7413 "0582 Error %d during els sgl post "
7414 "operation\n", rc);
7415 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007416 goto out_destroy_queue;
7417 }
7418 phba->sli4_hba.els_xri_cnt = rc;
7419
James Smartf358dd02017-02-12 13:52:34 -08007420 if (phba->nvmet_support) {
7421 /* update host nvmet xri-sgl sizes and mappings */
7422 rc = lpfc_sli4_nvmet_sgl_update(phba);
7423 if (unlikely(rc)) {
7424 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7425 "6308 Failed to update nvmet-sgl size "
7426 "and mapping: %d\n", rc);
7427 goto out_destroy_queue;
7428 }
7429
7430 /* register the nvmet sgl pool to the port */
7431 rc = lpfc_sli4_repost_sgl_list(
7432 phba,
7433 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7434 phba->sli4_hba.nvmet_xri_cnt);
7435 if (unlikely(rc < 0)) {
7436 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7437 "3117 Error %d during nvmet "
7438 "sgl post\n", rc);
7439 rc = -ENODEV;
7440 goto out_destroy_queue;
7441 }
7442 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007443
7444 cnt = phba->cfg_iocb_cnt * 1024;
7445 /* We need 1 iocbq for every SGL, for IO processing */
7446 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08007447 } else {
James Smart895427b2017-02-12 13:52:30 -08007448 /* update host scsi xri-sgl sizes and mappings */
7449 rc = lpfc_sli4_scsi_sgl_update(phba);
7450 if (unlikely(rc)) {
7451 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7452 "6309 Failed to update scsi-sgl size "
7453 "and mapping: %d\n", rc);
7454 goto out_destroy_queue;
7455 }
7456
7457 /* update host nvme xri-sgl sizes and mappings */
7458 rc = lpfc_sli4_nvme_sgl_update(phba);
7459 if (unlikely(rc)) {
7460 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7461 "6082 Failed to update nvme-sgl size "
7462 "and mapping: %d\n", rc);
7463 goto out_destroy_queue;
7464 }
James Smart6c621a22017-05-15 15:20:45 -07007465
7466 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07007467 }
7468
7469 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007470 /* Initialize and populate the iocb list per host */
7471 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007472 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07007473 phba->cfg_iocb_cnt, cnt);
7474 rc = lpfc_init_iocb_list(phba, cnt);
7475 if (rc) {
7476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007477 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007478 goto out_destroy_queue;
7479 }
James Smart8a9d2e82012-05-09 21:16:12 -04007480 }
7481
James Smart11e644e2017-06-15 22:56:48 -07007482 if (phba->nvmet_support)
7483 lpfc_nvmet_create_targetport(phba);
7484
James Smart2d7dbc42017-02-12 13:52:35 -08007485 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007486 /* Post initial buffers to all RQs created */
7487 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7488 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7489 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7490 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7491 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007492 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007493 rqbp->buffer_count = 0;
7494
James Smart2d7dbc42017-02-12 13:52:35 -08007495 lpfc_post_rq_buffer(
7496 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7497 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007498 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007499 }
7500 }
7501
James Smart895427b2017-02-12 13:52:30 -08007502 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7503 /* register the allocated scsi sgl pool to the port */
7504 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7505 if (unlikely(rc)) {
7506 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7507 "0383 Error %d during scsi sgl post "
7508 "operation\n", rc);
7509 /* Some Scsi buffers were moved to abort scsi list */
7510 /* A pci function reset will repost them */
7511 rc = -ENODEV;
7512 goto out_destroy_queue;
7513 }
James Smartda0436e2009-05-22 14:51:39 -04007514 }
7515
James Smart01649562017-02-12 13:52:32 -08007516 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7517 (phba->nvmet_support == 0)) {
7518
7519 /* register the allocated nvme sgl pool to the port */
7520 rc = lpfc_repost_nvme_sgl_list(phba);
7521 if (unlikely(rc)) {
7522 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7523 "6116 Error %d during nvme sgl post "
7524 "operation\n", rc);
7525 /* Some NVME buffers were moved to abort nvme list */
7526 /* A pci function reset will repost them */
7527 rc = -ENODEV;
7528 goto out_destroy_queue;
7529 }
James Smartda0436e2009-05-22 14:51:39 -04007530 }
7531
7532 /* Post the rpi header region to the device. */
7533 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7534 if (unlikely(rc)) {
7535 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7536 "0393 Error %d during rpi post operation\n",
7537 rc);
7538 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007539 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007540 }
James Smart97f2ecf2012-03-01 22:35:23 -05007541 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007542
James Smart895427b2017-02-12 13:52:30 -08007543 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007544 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007545 /*
7546 * The FC Port needs to register FCFI (index 0)
7547 */
7548 lpfc_reg_fcfi(phba, mboxq);
7549 mboxq->vport = phba->pport;
7550 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7551 if (rc != MBX_SUCCESS)
7552 goto out_unset_queue;
7553 rc = 0;
7554 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7555 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007556 } else {
7557 /* We are a NVME Target mode with MRQ > 1 */
7558
7559 /* First register the FCFI */
7560 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7561 mboxq->vport = phba->pport;
7562 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7563 if (rc != MBX_SUCCESS)
7564 goto out_unset_queue;
7565 rc = 0;
7566 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7567 &mboxq->u.mqe.un.reg_fcfi_mrq);
7568
7569 /* Next register the MRQs */
7570 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7571 mboxq->vport = phba->pport;
7572 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7573 if (rc != MBX_SUCCESS)
7574 goto out_unset_queue;
7575 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007576 }
7577 /* Check if the port is configured to be disabled */
7578 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007579 }
7580
7581 /* Arm the CQs and then EQs on device */
7582 lpfc_sli4_arm_cqeq_intr(phba);
7583
7584 /* Indicate device interrupt mode */
7585 phba->sli4_hba.intr_enable = 1;
7586
7587 /* Allow asynchronous mailbox command to go through */
7588 spin_lock_irq(&phba->hbalock);
7589 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7590 spin_unlock_irq(&phba->hbalock);
7591
7592 /* Post receive buffers to the device */
7593 lpfc_sli4_rb_setup(phba);
7594
James Smartfc2b9892010-02-26 14:15:29 -05007595 /* Reset HBA FCF states after HBA reset */
7596 phba->fcf.fcf_flag = 0;
7597 phba->fcf.current_rec.flag = 0;
7598
James Smartda0436e2009-05-22 14:51:39 -04007599 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007600 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007601 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007602
7603 /* Start heart beat timer */
7604 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007605 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007606 phba->hb_outstanding = 0;
7607 phba->last_completion_time = jiffies;
7608
7609 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007610 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007611 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007612
James Smart75baf692010-06-08 18:31:21 -04007613 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7614 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7615 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7616 if (!rc) {
7617 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7618 "2829 This device supports "
7619 "Advanced Error Reporting (AER)\n");
7620 spin_lock_irq(&phba->hbalock);
7621 phba->hba_flag |= HBA_AER_ENABLED;
7622 spin_unlock_irq(&phba->hbalock);
7623 } else {
7624 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7625 "2830 This device does not support "
7626 "Advanced Error Reporting (AER)\n");
7627 phba->cfg_aer_support = 0;
7628 }
James Smart0a96e972011-07-22 18:37:28 -04007629 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007630 }
7631
James Smartda0436e2009-05-22 14:51:39 -04007632 /*
7633 * The port is ready, set the host's link state to LINK_DOWN
7634 * in preparation for link interrupts.
7635 */
James Smartda0436e2009-05-22 14:51:39 -04007636 spin_lock_irq(&phba->hbalock);
7637 phba->link_state = LPFC_LINK_DOWN;
7638 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007639 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7640 (phba->hba_flag & LINK_DISABLED)) {
7641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7642 "3103 Adapter Link is disabled.\n");
7643 lpfc_down_link(phba, mboxq);
7644 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7645 if (rc != MBX_SUCCESS) {
7646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7647 "3104 Adapter failed to issue "
7648 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7649 goto out_unset_queue;
7650 }
7651 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007652 /* don't perform init_link on SLI4 FC port loopback test */
7653 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7654 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7655 if (rc)
7656 goto out_unset_queue;
7657 }
James Smart5350d872011-10-10 21:33:49 -04007658 }
7659 mempool_free(mboxq, phba->mbox_mem_pool);
7660 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007661out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007662 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007663 lpfc_sli4_queue_unset(phba);
7664out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007665 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007666 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007667out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007668 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007669out_free_mbox:
7670 mempool_free(mboxq, phba->mbox_mem_pool);
7671 return rc;
7672}
James Smarte59058c2008-08-24 21:49:00 -04007673
7674/**
James Smart3621a712009-04-06 18:47:14 -04007675 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007676 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007677 *
James Smarte59058c2008-08-24 21:49:00 -04007678 * This is the callback function for mailbox timer. The mailbox
7679 * timer is armed when a new mailbox command is issued and the timer
7680 * is deleted when the mailbox complete. The function is called by
7681 * the kernel timer code when a mailbox does not complete within
7682 * expected time. This function wakes up the worker thread to
7683 * process the mailbox timeout and returns. All the processing is
7684 * done by the worker thread function lpfc_mbox_timeout_handler.
7685 **/
dea31012005-04-17 16:05:31 -05007686void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007687lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007688{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007689 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007690 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007691 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007692
James Smart2e0fef82007-06-17 19:56:36 -05007693 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007694 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007695 if (!tmo_posted)
7696 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7697 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7698
James Smart5e9d9b82008-06-14 22:52:53 -04007699 if (!tmo_posted)
7700 lpfc_worker_wake_up(phba);
7701 return;
dea31012005-04-17 16:05:31 -05007702}
7703
James Smarte8d3c3b2013-10-10 12:21:30 -04007704/**
7705 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7706 * are pending
7707 * @phba: Pointer to HBA context object.
7708 *
7709 * This function checks if any mailbox completions are present on the mailbox
7710 * completion queue.
7711 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007712static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007713lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7714{
7715
7716 uint32_t idx;
7717 struct lpfc_queue *mcq;
7718 struct lpfc_mcqe *mcqe;
7719 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08007720 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04007721
7722 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7723 return false;
7724
7725 /* Check for completions on mailbox completion queue */
7726
7727 mcq = phba->sli4_hba.mbx_cq;
7728 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08007729 qe_valid = mcq->qe_valid;
7730 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
James Smarte8d3c3b2013-10-10 12:21:30 -04007731 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7732 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7733 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7734 pending_completions = true;
7735 break;
7736 }
7737 idx = (idx + 1) % mcq->entry_count;
7738 if (mcq->hba_index == idx)
7739 break;
James Smart7365f6f2018-02-22 08:18:46 -08007740
7741 /* if the index wrapped around, toggle the valid bit */
7742 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7743 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04007744 }
7745 return pending_completions;
7746
7747}
7748
7749/**
7750 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7751 * that were missed.
7752 * @phba: Pointer to HBA context object.
7753 *
7754 * For sli4, it is possible to miss an interrupt. As such mbox completions
7755 * maybe missed causing erroneous mailbox timeouts to occur. This function
7756 * checks to see if mbox completions are on the mailbox completion queue
7757 * and will process all the completions associated with the eq for the
7758 * mailbox completion queue.
7759 **/
7760bool
7761lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7762{
James Smartb71413d2018-02-22 08:18:40 -08007763 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04007764 uint32_t eqidx;
7765 struct lpfc_queue *fpeq = NULL;
7766 struct lpfc_eqe *eqe;
7767 bool mbox_pending;
7768
7769 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7770 return false;
7771
7772 /* Find the eq associated with the mcq */
7773
James Smartb71413d2018-02-22 08:18:40 -08007774 if (sli4_hba->hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007775 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smartb71413d2018-02-22 08:18:40 -08007776 if (sli4_hba->hba_eq[eqidx]->queue_id ==
7777 sli4_hba->mbx_cq->assoc_qid) {
7778 fpeq = sli4_hba->hba_eq[eqidx];
James Smarte8d3c3b2013-10-10 12:21:30 -04007779 break;
7780 }
7781 if (!fpeq)
7782 return false;
7783
7784 /* Turn off interrupts from this EQ */
7785
James Smartb71413d2018-02-22 08:18:40 -08007786 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04007787
7788 /* Check to see if a mbox completion is pending */
7789
7790 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7791
7792 /*
7793 * If a mbox completion is pending, process all the events on EQ
7794 * associated with the mbox completion queue (this could include
7795 * mailbox commands, async events, els commands, receive queue data
7796 * and fcp commands)
7797 */
7798
7799 if (mbox_pending)
7800 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7801 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7802 fpeq->EQ_processed++;
7803 }
7804
7805 /* Always clear and re-arm the EQ */
7806
James Smartb71413d2018-02-22 08:18:40 -08007807 sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04007808
7809 return mbox_pending;
7810
7811}
James Smarte59058c2008-08-24 21:49:00 -04007812
7813/**
James Smart3621a712009-04-06 18:47:14 -04007814 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007815 * @phba: Pointer to HBA context object.
7816 *
7817 * This function is called from worker thread when a mailbox command times out.
7818 * The caller is not required to hold any locks. This function will reset the
7819 * HBA and recover all the pending commands.
7820 **/
dea31012005-04-17 16:05:31 -05007821void
7822lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7823{
James Smart2e0fef82007-06-17 19:56:36 -05007824 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007825 MAILBOX_t *mb = NULL;
7826
James Smart1dcb58e2007-04-25 09:51:30 -04007827 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007828
James Smarte8d3c3b2013-10-10 12:21:30 -04007829 /* If the mailbox completed, process the completion and return */
7830 if (lpfc_sli4_process_missed_mbox_completions(phba))
7831 return;
7832
James Smarteb016562014-09-03 12:58:06 -04007833 if (pmbox != NULL)
7834 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007835 /* Check the pmbox pointer first. There is a race condition
7836 * between the mbox timeout handler getting executed in the
7837 * worklist and the mailbox actually completing. When this
7838 * race condition occurs, the mbox_active will be NULL.
7839 */
7840 spin_lock_irq(&phba->hbalock);
7841 if (pmbox == NULL) {
7842 lpfc_printf_log(phba, KERN_WARNING,
7843 LOG_MBOX | LOG_SLI,
7844 "0353 Active Mailbox cleared - mailbox timeout "
7845 "exiting\n");
7846 spin_unlock_irq(&phba->hbalock);
7847 return;
7848 }
7849
dea31012005-04-17 16:05:31 -05007850 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007851 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007852 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007853 mb->mbxCommand,
7854 phba->pport->port_state,
7855 phba->sli.sli_flag,
7856 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007857 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007858
James Smart1dcb58e2007-04-25 09:51:30 -04007859 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7860 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007861 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007862 */
James Smart2e0fef82007-06-17 19:56:36 -05007863 spin_lock_irq(&phba->pport->work_port_lock);
7864 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7865 spin_unlock_irq(&phba->pport->work_port_lock);
7866 spin_lock_irq(&phba->hbalock);
7867 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007868 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007869 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007870
James Smartdb55fba2014-04-04 13:52:02 -04007871 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007872
7873 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007874 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007875
7876 /* Reset the HBA device */
7877 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007878}
7879
James Smarte59058c2008-08-24 21:49:00 -04007880/**
James Smart3772a992009-05-22 14:50:54 -04007881 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007882 * @phba: Pointer to HBA context object.
7883 * @pmbox: Pointer to mailbox object.
7884 * @flag: Flag indicating how the mailbox need to be processed.
7885 *
7886 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007887 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7888 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007889 * The mailbox command can be submitted in polling mode, in which case
7890 * this function will wait in a polling loop for the completion of the
7891 * mailbox.
7892 * If the mailbox is submitted in no_wait mode (not polling) the
7893 * function will submit the command and returns immediately without waiting
7894 * for the mailbox completion. The no_wait is supported only when HBA
7895 * is in SLI2/SLI3 mode - interrupts are enabled.
7896 * The SLI interface allows only one mailbox pending at a time. If the
7897 * mailbox is issued in polling mode and there is already a mailbox
7898 * pending, then the function will return an error. If the mailbox is issued
7899 * in NO_WAIT mode and there is a mailbox pending already, the function
7900 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7901 * The sli layer owns the mailbox object until the completion of mailbox
7902 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7903 * return codes the caller owns the mailbox command after the return of
7904 * the function.
7905 **/
James Smart3772a992009-05-22 14:50:54 -04007906static int
7907lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7908 uint32_t flag)
dea31012005-04-17 16:05:31 -05007909{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007910 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007911 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007912 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007913 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007914 int i;
James Smart09372822008-01-11 01:52:54 -05007915 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007916 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007917 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007918 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007919 int processing_queue = 0;
7920
7921 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7922 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007923 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007924 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007925 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7926 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7927 return MBX_SUCCESS;
7928 }
James Smart58da1ff2008-04-07 10:15:56 -04007929 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007930 pmbox = lpfc_mbox_get(phba);
7931 if (!pmbox) {
7932 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7933 return MBX_SUCCESS;
7934 }
7935 }
dea31012005-04-17 16:05:31 -05007936
James Smarted957682007-06-17 19:56:37 -05007937 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007938 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007939 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007940 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007941 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007942 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007943 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007944 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007945 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007946 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007947 }
7948 }
7949
Linas Vepstas8d63f372007-02-14 14:28:36 -06007950 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007951 if (unlikely(pci_channel_offline(phba->pcidev))) {
7952 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7953 goto out_not_finished;
7954 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007955
James Smarta257bf92009-04-06 18:48:10 -04007956 /* If HBA has a deferred error attention, fail the iocb. */
7957 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7958 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7959 goto out_not_finished;
7960 }
7961
dea31012005-04-17 16:05:31 -05007962 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007963
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007964 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007965 status = MBX_SUCCESS;
7966
James Smart2e0fef82007-06-17 19:56:36 -05007967 if (phba->link_state == LPFC_HBA_ERROR) {
7968 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007969
7970 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007971 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7972 "(%d):0311 Mailbox command x%x cannot "
7973 "issue Data: x%x x%x\n",
7974 pmbox->vport ? pmbox->vport->vpi : 0,
7975 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007976 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007977 }
7978
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007979 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007980 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7981 !(hc_copy & HC_MBINT_ENA)) {
7982 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7983 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007984 "(%d):2528 Mailbox command x%x cannot "
7985 "issue Data: x%x x%x\n",
7986 pmbox->vport ? pmbox->vport->vpi : 0,
7987 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007988 goto out_not_finished;
7989 }
James Smart92908312006-03-07 15:04:13 -05007990 }
7991
dea31012005-04-17 16:05:31 -05007992 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7993 /* Polling for a mbox command when another one is already active
7994 * is not allowed in SLI. Also, the driver must have established
7995 * SLI2 mode to queue and process multiple mbox commands.
7996 */
7997
7998 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007999 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008000
8001 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008002 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8003 "(%d):2529 Mailbox command x%x "
8004 "cannot issue Data: x%x x%x\n",
8005 pmbox->vport ? pmbox->vport->vpi : 0,
8006 pmbox->u.mb.mbxCommand,
8007 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008008 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008009 }
8010
James Smart3772a992009-05-22 14:50:54 -04008011 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05008012 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008013 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008014 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8015 "(%d):2530 Mailbox command x%x "
8016 "cannot issue Data: x%x x%x\n",
8017 pmbox->vport ? pmbox->vport->vpi : 0,
8018 pmbox->u.mb.mbxCommand,
8019 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008020 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008021 }
8022
dea31012005-04-17 16:05:31 -05008023 /* Another mailbox command is still being processed, queue this
8024 * command to be processed later.
8025 */
8026 lpfc_mbox_put(phba, pmbox);
8027
8028 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05008029 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008030 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008031 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008032 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07008033 mbx->mbxCommand,
8034 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008035 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008036
8037 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05008038 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008039
James Smart858c9f62007-06-17 19:56:39 -05008040 if (pmbox->vport) {
8041 lpfc_debugfs_disc_trc(pmbox->vport,
8042 LPFC_DISC_TRC_MBOX_VPORT,
8043 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008044 (uint32_t)mbx->mbxCommand,
8045 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008046 }
8047 else {
8048 lpfc_debugfs_disc_trc(phba->pport,
8049 LPFC_DISC_TRC_MBOX,
8050 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008051 (uint32_t)mbx->mbxCommand,
8052 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008053 }
8054
James Smart2e0fef82007-06-17 19:56:36 -05008055 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05008056 }
8057
dea31012005-04-17 16:05:31 -05008058 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8059
8060 /* If we are not polling, we MUST be in SLI2 mode */
8061 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04008062 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008063 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05008064 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008065 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008066 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008067 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8068 "(%d):2531 Mailbox command x%x "
8069 "cannot issue Data: x%x x%x\n",
8070 pmbox->vport ? pmbox->vport->vpi : 0,
8071 pmbox->u.mb.mbxCommand,
8072 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008073 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008074 }
8075 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04008076 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8077 1000);
8078 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05008079 }
8080
8081 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05008082 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008083 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05008084 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008085 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07008086 mbx->mbxCommand,
8087 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008088 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008089
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008090 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05008091 if (pmbox->vport) {
8092 lpfc_debugfs_disc_trc(pmbox->vport,
8093 LPFC_DISC_TRC_MBOX_VPORT,
8094 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008095 (uint32_t)mbx->mbxCommand,
8096 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008097 }
8098 else {
8099 lpfc_debugfs_disc_trc(phba->pport,
8100 LPFC_DISC_TRC_MBOX,
8101 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008102 (uint32_t)mbx->mbxCommand,
8103 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008104 }
8105 }
8106
dea31012005-04-17 16:05:31 -05008107 psli->slistat.mbox_cmd++;
8108 evtctr = psli->slistat.mbox_event;
8109
8110 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008111 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05008112
James Smart3772a992009-05-22 14:50:54 -04008113 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04008114 /* Populate mbox extension offset word. */
8115 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008116 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008117 = (uint8_t *)phba->mbox_ext
8118 - (uint8_t *)phba->mbox;
8119 }
8120
8121 /* Copy the mailbox extension data */
8122 if (pmbox->in_ext_byte_len && pmbox->context2) {
8123 lpfc_sli_pcimem_bcopy(pmbox->context2,
8124 (uint8_t *)phba->mbox_ext,
8125 pmbox->in_ext_byte_len);
8126 }
8127 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008128 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008129 } else {
James Smart7a470272010-03-15 11:25:20 -04008130 /* Populate mbox extension offset word. */
8131 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008132 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008133 = MAILBOX_HBA_EXT_OFFSET;
8134
8135 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08008136 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04008137 lpfc_memcpy_to_slim(phba->MBslimaddr +
8138 MAILBOX_HBA_EXT_OFFSET,
8139 pmbox->context2, pmbox->in_ext_byte_len);
8140
James Smart895427b2017-02-12 13:52:30 -08008141 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008142 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08008143 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8144 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008145
8146 /* First copy mbox command data to HBA SLIM, skip past first
8147 word */
8148 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008149 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05008150 MAILBOX_CMD_SIZE - sizeof (uint32_t));
8151
8152 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008153 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05008154 to_slim = phba->MBslimaddr;
8155 writel(ldata, to_slim);
8156 readl(to_slim); /* flush */
8157
James Smart895427b2017-02-12 13:52:30 -08008158 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008159 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04008160 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008161 }
8162
8163 wmb();
dea31012005-04-17 16:05:31 -05008164
8165 switch (flag) {
8166 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05008167 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05008168 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05008169 /* Interrupt board to do it */
8170 writel(CA_MBATT, phba->CAregaddr);
8171 readl(phba->CAregaddr); /* flush */
8172 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05008173 break;
8174
8175 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05008176 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05008177 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05008178 /* Interrupt board to do it */
8179 writel(CA_MBATT, phba->CAregaddr);
8180 readl(phba->CAregaddr); /* flush */
8181
James Smart3772a992009-05-22 14:50:54 -04008182 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008183 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04008184 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008185 word0 = le32_to_cpu(word0);
8186 } else {
8187 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05008188 if (lpfc_readl(phba->MBslimaddr, &word0)) {
8189 spin_unlock_irqrestore(&phba->hbalock,
8190 drvr_flag);
8191 goto out_not_finished;
8192 }
dea31012005-04-17 16:05:31 -05008193 }
8194
8195 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008196 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8197 spin_unlock_irqrestore(&phba->hbalock,
8198 drvr_flag);
8199 goto out_not_finished;
8200 }
James Smarta183a152011-10-10 21:32:43 -04008201 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8202 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05008203 i = 0;
dea31012005-04-17 16:05:31 -05008204 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05008205 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8206 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05008207 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05008208 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05008209 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008210 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05008211 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04008212 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008213 }
8214
8215 /* Check if we took a mbox interrupt while we were
8216 polling */
8217 if (((word0 & OWN_CHIP) != OWN_CHIP)
8218 && (evtctr != psli->slistat.mbox_event))
8219 break;
8220
James Smart09372822008-01-11 01:52:54 -05008221 if (i++ > 10) {
8222 spin_unlock_irqrestore(&phba->hbalock,
8223 drvr_flag);
8224 msleep(1);
8225 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8226 }
dea31012005-04-17 16:05:31 -05008227
James Smart3772a992009-05-22 14:50:54 -04008228 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008229 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04008230 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008231 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008232 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05008233 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04008234 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05008235 /* Check real SLIM for any errors */
8236 slimword0 = readl(phba->MBslimaddr);
8237 slimmb = (MAILBOX_t *) & slimword0;
8238 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8239 && slimmb->mbxStatus) {
8240 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04008241 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008242 word0 = slimword0;
8243 }
8244 }
8245 } else {
8246 /* First copy command data */
8247 word0 = readl(phba->MBslimaddr);
8248 }
8249 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008250 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8251 spin_unlock_irqrestore(&phba->hbalock,
8252 drvr_flag);
8253 goto out_not_finished;
8254 }
dea31012005-04-17 16:05:31 -05008255 }
8256
James Smart3772a992009-05-22 14:50:54 -04008257 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008258 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08008259 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8260 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008261 /* Copy the mailbox extension data */
8262 if (pmbox->out_ext_byte_len && pmbox->context2) {
8263 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
8264 pmbox->context2,
8265 pmbox->out_ext_byte_len);
8266 }
dea31012005-04-17 16:05:31 -05008267 } else {
8268 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008269 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08008270 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008271 /* Copy the mailbox extension data */
8272 if (pmbox->out_ext_byte_len && pmbox->context2) {
8273 lpfc_memcpy_from_slim(pmbox->context2,
8274 phba->MBslimaddr +
8275 MAILBOX_HBA_EXT_OFFSET,
8276 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05008277 }
8278 }
8279
8280 writel(HA_MBATT, phba->HAregaddr);
8281 readl(phba->HAregaddr); /* flush */
8282
8283 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008284 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05008285 }
8286
James Smart2e0fef82007-06-17 19:56:36 -05008287 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8288 return status;
James Smart58da1ff2008-04-07 10:15:56 -04008289
8290out_not_finished:
8291 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04008292 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04008293 lpfc_mbox_cmpl_put(phba, pmbox);
8294 }
8295 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008296}
8297
James Smarte59058c2008-08-24 21:49:00 -04008298/**
James Smartf1126682009-06-10 17:22:44 -04008299 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8300 * @phba: Pointer to HBA context object.
8301 *
8302 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8303 * the driver internal pending mailbox queue. It will then try to wait out the
8304 * possible outstanding mailbox command before return.
8305 *
8306 * Returns:
8307 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8308 * the outstanding mailbox command timed out.
8309 **/
8310static int
8311lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8312{
8313 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008314 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008315 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008316
8317 /* Mark the asynchronous mailbox command posting as blocked */
8318 spin_lock_irq(&phba->hbalock);
8319 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008320 /* Determine how long we might wait for the active mailbox
8321 * command to be gracefully completed by firmware.
8322 */
James Smarta183a152011-10-10 21:32:43 -04008323 if (phba->sli.mbox_active)
8324 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8325 phba->sli.mbox_active) *
8326 1000) + jiffies;
8327 spin_unlock_irq(&phba->hbalock);
8328
James Smarte8d3c3b2013-10-10 12:21:30 -04008329 /* Make sure the mailbox is really active */
8330 if (timeout)
8331 lpfc_sli4_process_missed_mbox_completions(phba);
8332
James Smartf1126682009-06-10 17:22:44 -04008333 /* Wait for the outstnading mailbox command to complete */
8334 while (phba->sli.mbox_active) {
8335 /* Check active mailbox complete status every 2ms */
8336 msleep(2);
8337 if (time_after(jiffies, timeout)) {
8338 /* Timeout, marked the outstanding cmd not complete */
8339 rc = 1;
8340 break;
8341 }
8342 }
8343
8344 /* Can not cleanly block async mailbox command, fails it */
8345 if (rc) {
8346 spin_lock_irq(&phba->hbalock);
8347 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8348 spin_unlock_irq(&phba->hbalock);
8349 }
8350 return rc;
8351}
8352
8353/**
8354 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8355 * @phba: Pointer to HBA context object.
8356 *
8357 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8358 * commands from the driver internal pending mailbox queue. It makes sure
8359 * that there is no outstanding mailbox command before resuming posting
8360 * asynchronous mailbox commands. If, for any reason, there is outstanding
8361 * mailbox command, it will try to wait it out before resuming asynchronous
8362 * mailbox command posting.
8363 **/
8364static void
8365lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8366{
8367 struct lpfc_sli *psli = &phba->sli;
8368
8369 spin_lock_irq(&phba->hbalock);
8370 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8371 /* Asynchronous mailbox posting is not blocked, do nothing */
8372 spin_unlock_irq(&phba->hbalock);
8373 return;
8374 }
8375
8376 /* Outstanding synchronous mailbox command is guaranteed to be done,
8377 * successful or timeout, after timing-out the outstanding mailbox
8378 * command shall always be removed, so just unblock posting async
8379 * mailbox command and resume
8380 */
8381 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8382 spin_unlock_irq(&phba->hbalock);
8383
8384 /* wake up worker thread to post asynchronlous mailbox command */
8385 lpfc_worker_wake_up(phba);
8386}
8387
8388/**
James Smart2d843ed2012-09-29 11:29:06 -04008389 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8390 * @phba: Pointer to HBA context object.
8391 * @mboxq: Pointer to mailbox object.
8392 *
8393 * The function waits for the bootstrap mailbox register ready bit from
8394 * port for twice the regular mailbox command timeout value.
8395 *
8396 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8397 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8398 **/
8399static int
8400lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8401{
8402 uint32_t db_ready;
8403 unsigned long timeout;
8404 struct lpfc_register bmbx_reg;
8405
8406 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8407 * 1000) + jiffies;
8408
8409 do {
8410 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8411 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8412 if (!db_ready)
8413 msleep(2);
8414
8415 if (time_after(jiffies, timeout))
8416 return MBXERR_ERROR;
8417 } while (!db_ready);
8418
8419 return 0;
8420}
8421
8422/**
James Smartda0436e2009-05-22 14:51:39 -04008423 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8424 * @phba: Pointer to HBA context object.
8425 * @mboxq: Pointer to mailbox object.
8426 *
8427 * The function posts a mailbox to the port. The mailbox is expected
8428 * to be comletely filled in and ready for the port to operate on it.
8429 * This routine executes a synchronous completion operation on the
8430 * mailbox by polling for its completion.
8431 *
8432 * The caller must not be holding any locks when calling this routine.
8433 *
8434 * Returns:
8435 * MBX_SUCCESS - mailbox posted successfully
8436 * Any of the MBX error values.
8437 **/
8438static int
8439lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8440{
8441 int rc = MBX_SUCCESS;
8442 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008443 uint32_t mcqe_status;
8444 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008445 struct lpfc_sli *psli = &phba->sli;
8446 struct lpfc_mqe *mb = &mboxq->u.mqe;
8447 struct lpfc_bmbx_create *mbox_rgn;
8448 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008449
8450 /*
8451 * Only one mailbox can be active to the bootstrap mailbox region
8452 * at a time and there is no queueing provided.
8453 */
8454 spin_lock_irqsave(&phba->hbalock, iflag);
8455 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8456 spin_unlock_irqrestore(&phba->hbalock, iflag);
8457 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008458 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008459 "cannot issue Data: x%x x%x\n",
8460 mboxq->vport ? mboxq->vport->vpi : 0,
8461 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008462 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8463 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008464 psli->sli_flag, MBX_POLL);
8465 return MBXERR_ERROR;
8466 }
8467 /* The server grabs the token and owns it until release */
8468 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8469 phba->sli.mbox_active = mboxq;
8470 spin_unlock_irqrestore(&phba->hbalock, iflag);
8471
James Smart2d843ed2012-09-29 11:29:06 -04008472 /* wait for bootstrap mbox register for readyness */
8473 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8474 if (rc)
8475 goto exit;
8476
James Smartda0436e2009-05-22 14:51:39 -04008477 /*
8478 * Initialize the bootstrap memory region to avoid stale data areas
8479 * in the mailbox post. Then copy the caller's mailbox contents to
8480 * the bmbx mailbox region.
8481 */
8482 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8483 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07008484 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8485 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008486
8487 /* Post the high mailbox dma address to the port and wait for ready. */
8488 dma_address = &phba->sli4_hba.bmbx.dma_address;
8489 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8490
James Smart2d843ed2012-09-29 11:29:06 -04008491 /* wait for bootstrap mbox register for hi-address write done */
8492 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8493 if (rc)
8494 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008495
8496 /* Post the low mailbox dma address to the port. */
8497 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008498
James Smart2d843ed2012-09-29 11:29:06 -04008499 /* wait for bootstrap mbox register for low address write done */
8500 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8501 if (rc)
8502 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008503
8504 /*
8505 * Read the CQ to ensure the mailbox has completed.
8506 * If so, update the mailbox status so that the upper layers
8507 * can complete the request normally.
8508 */
James Smart48f8fdb2018-05-04 20:37:51 -07008509 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8510 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008511 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07008512 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8513 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04008514 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008515 /*
8516 * When the CQE status indicates a failure and the mailbox status
8517 * indicates success then copy the CQE status into the mailbox status
8518 * (and prefix it with x4000).
8519 */
James Smartda0436e2009-05-22 14:51:39 -04008520 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008521 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8522 bf_set(lpfc_mqe_status, mb,
8523 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008524 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008525 } else
8526 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008527
8528 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008529 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008530 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8531 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008532 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8533 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8534 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008535 bf_get(lpfc_mqe_status, mb),
8536 mb->un.mb_words[0], mb->un.mb_words[1],
8537 mb->un.mb_words[2], mb->un.mb_words[3],
8538 mb->un.mb_words[4], mb->un.mb_words[5],
8539 mb->un.mb_words[6], mb->un.mb_words[7],
8540 mb->un.mb_words[8], mb->un.mb_words[9],
8541 mb->un.mb_words[10], mb->un.mb_words[11],
8542 mb->un.mb_words[12], mboxq->mcqe.word0,
8543 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8544 mboxq->mcqe.trailer);
8545exit:
8546 /* We are holding the token, no needed for lock when release */
8547 spin_lock_irqsave(&phba->hbalock, iflag);
8548 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8549 phba->sli.mbox_active = NULL;
8550 spin_unlock_irqrestore(&phba->hbalock, iflag);
8551 return rc;
8552}
8553
8554/**
8555 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8556 * @phba: Pointer to HBA context object.
8557 * @pmbox: Pointer to mailbox object.
8558 * @flag: Flag indicating how the mailbox need to be processed.
8559 *
8560 * This function is called by discovery code and HBA management code to submit
8561 * a mailbox command to firmware with SLI-4 interface spec.
8562 *
8563 * Return codes the caller owns the mailbox command after the return of the
8564 * function.
8565 **/
8566static int
8567lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8568 uint32_t flag)
8569{
8570 struct lpfc_sli *psli = &phba->sli;
8571 unsigned long iflags;
8572 int rc;
8573
James Smartb76f2dc2011-07-22 18:37:42 -04008574 /* dump from issue mailbox command if setup */
8575 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8576
James Smart8fa38512009-07-19 10:01:03 -04008577 rc = lpfc_mbox_dev_check(phba);
8578 if (unlikely(rc)) {
8579 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008580 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008581 "cannot issue Data: x%x x%x\n",
8582 mboxq->vport ? mboxq->vport->vpi : 0,
8583 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008584 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8585 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008586 psli->sli_flag, flag);
8587 goto out_not_finished;
8588 }
8589
James Smartda0436e2009-05-22 14:51:39 -04008590 /* Detect polling mode and jump to a handler */
8591 if (!phba->sli4_hba.intr_enable) {
8592 if (flag == MBX_POLL)
8593 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8594 else
8595 rc = -EIO;
8596 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008597 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008598 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008599 "(x%x/x%x) failure: "
8600 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8601 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008602 mboxq->vport ? mboxq->vport->vpi : 0,
8603 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008604 lpfc_sli_config_mbox_subsys_get(phba,
8605 mboxq),
8606 lpfc_sli_config_mbox_opcode_get(phba,
8607 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008608 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8609 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8610 bf_get(lpfc_mcqe_ext_status,
8611 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008612 psli->sli_flag, flag);
8613 return rc;
8614 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008615 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8616 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008617 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008618 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008619 mboxq->vport ? mboxq->vport->vpi : 0,
8620 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008621 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8622 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008623 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008624 /* Try to block the asynchronous mailbox posting */
8625 rc = lpfc_sli4_async_mbox_block(phba);
8626 if (!rc) {
8627 /* Successfully blocked, now issue sync mbox cmd */
8628 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8629 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008630 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008631 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008632 "(%d):2597 Sync Mailbox command "
8633 "x%x (x%x/x%x) failure: "
8634 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8635 "Data: x%x x%x\n,",
8636 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008637 mboxq->u.mb.mbxCommand,
8638 lpfc_sli_config_mbox_subsys_get(phba,
8639 mboxq),
8640 lpfc_sli_config_mbox_opcode_get(phba,
8641 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008642 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8643 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8644 bf_get(lpfc_mcqe_ext_status,
8645 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008646 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008647 /* Unblock the async mailbox posting afterward */
8648 lpfc_sli4_async_mbox_unblock(phba);
8649 }
8650 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008651 }
8652
8653 /* Now, interrupt mode asynchrous mailbox command */
8654 rc = lpfc_mbox_cmd_check(phba, mboxq);
8655 if (rc) {
8656 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008657 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008658 "cannot issue Data: x%x x%x\n",
8659 mboxq->vport ? mboxq->vport->vpi : 0,
8660 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008661 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8662 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008663 psli->sli_flag, flag);
8664 goto out_not_finished;
8665 }
James Smartda0436e2009-05-22 14:51:39 -04008666
8667 /* Put the mailbox command to the driver internal FIFO */
8668 psli->slistat.mbox_busy++;
8669 spin_lock_irqsave(&phba->hbalock, iflags);
8670 lpfc_mbox_put(phba, mboxq);
8671 spin_unlock_irqrestore(&phba->hbalock, iflags);
8672 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8673 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008674 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008675 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8676 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008677 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8678 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008679 phba->pport->port_state,
8680 psli->sli_flag, MBX_NOWAIT);
8681 /* Wake up worker thread to transport mailbox command from head */
8682 lpfc_worker_wake_up(phba);
8683
8684 return MBX_BUSY;
8685
8686out_not_finished:
8687 return MBX_NOT_FINISHED;
8688}
8689
8690/**
8691 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8692 * @phba: Pointer to HBA context object.
8693 *
8694 * This function is called by worker thread to send a mailbox command to
8695 * SLI4 HBA firmware.
8696 *
8697 **/
8698int
8699lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8700{
8701 struct lpfc_sli *psli = &phba->sli;
8702 LPFC_MBOXQ_t *mboxq;
8703 int rc = MBX_SUCCESS;
8704 unsigned long iflags;
8705 struct lpfc_mqe *mqe;
8706 uint32_t mbx_cmnd;
8707
8708 /* Check interrupt mode before post async mailbox command */
8709 if (unlikely(!phba->sli4_hba.intr_enable))
8710 return MBX_NOT_FINISHED;
8711
8712 /* Check for mailbox command service token */
8713 spin_lock_irqsave(&phba->hbalock, iflags);
8714 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8715 spin_unlock_irqrestore(&phba->hbalock, iflags);
8716 return MBX_NOT_FINISHED;
8717 }
8718 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8719 spin_unlock_irqrestore(&phba->hbalock, iflags);
8720 return MBX_NOT_FINISHED;
8721 }
8722 if (unlikely(phba->sli.mbox_active)) {
8723 spin_unlock_irqrestore(&phba->hbalock, iflags);
8724 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8725 "0384 There is pending active mailbox cmd\n");
8726 return MBX_NOT_FINISHED;
8727 }
8728 /* Take the mailbox command service token */
8729 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8730
8731 /* Get the next mailbox command from head of queue */
8732 mboxq = lpfc_mbox_get(phba);
8733
8734 /* If no more mailbox command waiting for post, we're done */
8735 if (!mboxq) {
8736 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8737 spin_unlock_irqrestore(&phba->hbalock, iflags);
8738 return MBX_SUCCESS;
8739 }
8740 phba->sli.mbox_active = mboxq;
8741 spin_unlock_irqrestore(&phba->hbalock, iflags);
8742
8743 /* Check device readiness for posting mailbox command */
8744 rc = lpfc_mbox_dev_check(phba);
8745 if (unlikely(rc))
8746 /* Driver clean routine will clean up pending mailbox */
8747 goto out_not_finished;
8748
8749 /* Prepare the mbox command to be posted */
8750 mqe = &mboxq->u.mqe;
8751 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8752
8753 /* Start timer for the mbox_tmo and log some mailbox post messages */
8754 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008755 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008756
8757 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008758 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008759 "x%x x%x\n",
8760 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008761 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8762 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008763 phba->pport->port_state, psli->sli_flag);
8764
8765 if (mbx_cmnd != MBX_HEARTBEAT) {
8766 if (mboxq->vport) {
8767 lpfc_debugfs_disc_trc(mboxq->vport,
8768 LPFC_DISC_TRC_MBOX_VPORT,
8769 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8770 mbx_cmnd, mqe->un.mb_words[0],
8771 mqe->un.mb_words[1]);
8772 } else {
8773 lpfc_debugfs_disc_trc(phba->pport,
8774 LPFC_DISC_TRC_MBOX,
8775 "MBOX Send: cmd:x%x mb:x%x x%x",
8776 mbx_cmnd, mqe->un.mb_words[0],
8777 mqe->un.mb_words[1]);
8778 }
8779 }
8780 psli->slistat.mbox_cmd++;
8781
8782 /* Post the mailbox command to the port */
8783 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8784 if (rc != MBX_SUCCESS) {
8785 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008786 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008787 "cannot issue Data: x%x x%x\n",
8788 mboxq->vport ? mboxq->vport->vpi : 0,
8789 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008790 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8791 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008792 psli->sli_flag, MBX_NOWAIT);
8793 goto out_not_finished;
8794 }
8795
8796 return rc;
8797
8798out_not_finished:
8799 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008800 if (phba->sli.mbox_active) {
8801 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8802 __lpfc_mbox_cmpl_put(phba, mboxq);
8803 /* Release the token */
8804 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8805 phba->sli.mbox_active = NULL;
8806 }
James Smartda0436e2009-05-22 14:51:39 -04008807 spin_unlock_irqrestore(&phba->hbalock, iflags);
8808
8809 return MBX_NOT_FINISHED;
8810}
8811
8812/**
8813 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8814 * @phba: Pointer to HBA context object.
8815 * @pmbox: Pointer to mailbox object.
8816 * @flag: Flag indicating how the mailbox need to be processed.
8817 *
8818 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8819 * the API jump table function pointer from the lpfc_hba struct.
8820 *
8821 * Return codes the caller owns the mailbox command after the return of the
8822 * function.
8823 **/
8824int
8825lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8826{
8827 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8828}
8829
8830/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008831 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008832 * @phba: The hba struct for which this call is being executed.
8833 * @dev_grp: The HBA PCI-Device group number.
8834 *
8835 * This routine sets up the mbox interface API function jump table in @phba
8836 * struct.
8837 * Returns: 0 - success, -ENODEV - failure.
8838 **/
8839int
8840lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8841{
8842
8843 switch (dev_grp) {
8844 case LPFC_PCI_DEV_LP:
8845 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8846 phba->lpfc_sli_handle_slow_ring_event =
8847 lpfc_sli_handle_slow_ring_event_s3;
8848 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8849 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8850 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8851 break;
8852 case LPFC_PCI_DEV_OC:
8853 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8854 phba->lpfc_sli_handle_slow_ring_event =
8855 lpfc_sli_handle_slow_ring_event_s4;
8856 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8857 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8858 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8859 break;
8860 default:
8861 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8862 "1420 Invalid HBA PCI-device group: 0x%x\n",
8863 dev_grp);
8864 return -ENODEV;
8865 break;
8866 }
8867 return 0;
8868}
8869
8870/**
James Smart3621a712009-04-06 18:47:14 -04008871 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008872 * @phba: Pointer to HBA context object.
8873 * @pring: Pointer to driver SLI ring object.
8874 * @piocb: Pointer to address of newly added command iocb.
8875 *
8876 * This function is called with hbalock held to add a command
8877 * iocb to the txq when SLI layer cannot submit the command iocb
8878 * to the ring.
8879 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008880void
James Smart92d7f7b2007-06-17 19:56:38 -05008881__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008882 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008883{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008884 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008885 /* Insert the caller's iocb in the txq tail for later processing. */
8886 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008887}
8888
James Smarte59058c2008-08-24 21:49:00 -04008889/**
James Smart3621a712009-04-06 18:47:14 -04008890 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008891 * @phba: Pointer to HBA context object.
8892 * @pring: Pointer to driver SLI ring object.
8893 * @piocb: Pointer to address of newly added command iocb.
8894 *
8895 * This function is called with hbalock held before a new
8896 * iocb is submitted to the firmware. This function checks
8897 * txq to flush the iocbs in txq to Firmware before
8898 * submitting new iocbs to the Firmware.
8899 * If there are iocbs in the txq which need to be submitted
8900 * to firmware, lpfc_sli_next_iocb returns the first element
8901 * of the txq after dequeuing it from txq.
8902 * If there is no iocb in the txq then the function will return
8903 * *piocb and *piocb is set to NULL. Caller needs to check
8904 * *piocb to find if there are more commands in the txq.
8905 **/
dea31012005-04-17 16:05:31 -05008906static struct lpfc_iocbq *
8907lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008908 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008909{
8910 struct lpfc_iocbq * nextiocb;
8911
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008912 lockdep_assert_held(&phba->hbalock);
8913
dea31012005-04-17 16:05:31 -05008914 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8915 if (!nextiocb) {
8916 nextiocb = *piocb;
8917 *piocb = NULL;
8918 }
8919
8920 return nextiocb;
8921}
8922
James Smarte59058c2008-08-24 21:49:00 -04008923/**
James Smart3772a992009-05-22 14:50:54 -04008924 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008925 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008926 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008927 * @piocb: Pointer to command iocb.
8928 * @flag: Flag indicating if this command can be put into txq.
8929 *
James Smart3772a992009-05-22 14:50:54 -04008930 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8931 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8932 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8933 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8934 * this function allows only iocbs for posting buffers. This function finds
8935 * next available slot in the command ring and posts the command to the
8936 * available slot and writes the port attention register to request HBA start
8937 * processing new iocb. If there is no slot available in the ring and
8938 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8939 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008940 *
James Smart3772a992009-05-22 14:50:54 -04008941 * This function is called with hbalock held. The function will return success
8942 * after it successfully submit the iocb to firmware or after adding to the
8943 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008944 **/
James Smart98c9ea52007-10-27 13:37:33 -04008945static int
James Smart3772a992009-05-22 14:50:54 -04008946__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008947 struct lpfc_iocbq *piocb, uint32_t flag)
8948{
8949 struct lpfc_iocbq *nextiocb;
8950 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008951 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008952
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008953 lockdep_assert_held(&phba->hbalock);
8954
James Smart92d7f7b2007-06-17 19:56:38 -05008955 if (piocb->iocb_cmpl && (!piocb->vport) &&
8956 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8957 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8958 lpfc_printf_log(phba, KERN_ERR,
8959 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008960 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008961 piocb->iocb.ulpCommand);
8962 dump_stack();
8963 return IOCB_ERROR;
8964 }
8965
8966
Linas Vepstas8d63f372007-02-14 14:28:36 -06008967 /* If the PCI channel is in offline state, do not post iocbs. */
8968 if (unlikely(pci_channel_offline(phba->pcidev)))
8969 return IOCB_ERROR;
8970
James Smarta257bf92009-04-06 18:48:10 -04008971 /* If HBA has a deferred error attention, fail the iocb. */
8972 if (unlikely(phba->hba_flag & DEFER_ERATT))
8973 return IOCB_ERROR;
8974
dea31012005-04-17 16:05:31 -05008975 /*
8976 * We should never get an IOCB if we are in a < LINK_DOWN state
8977 */
James Smart2e0fef82007-06-17 19:56:36 -05008978 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008979 return IOCB_ERROR;
8980
8981 /*
8982 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008983 * outstanding event.
dea31012005-04-17 16:05:31 -05008984 */
James Smart0b727fe2007-10-27 13:37:25 -04008985 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008986 goto iocb_busy;
8987
James Smart2e0fef82007-06-17 19:56:36 -05008988 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008989 /*
James Smart2680eea2007-04-25 09:52:55 -04008990 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008991 * can be issued if the link is not up.
8992 */
8993 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008994 case CMD_GEN_REQUEST64_CR:
8995 case CMD_GEN_REQUEST64_CX:
8996 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8997 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008998 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008999 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9000 MENLO_TRANSPORT_TYPE))
9001
9002 goto iocb_busy;
9003 break;
dea31012005-04-17 16:05:31 -05009004 case CMD_QUE_RING_BUF_CN:
9005 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05009006 /*
9007 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9008 * completion, iocb_cmpl MUST be 0.
9009 */
9010 if (piocb->iocb_cmpl)
9011 piocb->iocb_cmpl = NULL;
9012 /*FALLTHROUGH*/
9013 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04009014 case CMD_CLOSE_XRI_CN:
9015 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05009016 break;
9017 default:
9018 goto iocb_busy;
9019 }
9020
9021 /*
9022 * For FCP commands, we must be in a state where we can process link
9023 * attention events.
9024 */
James Smart895427b2017-02-12 13:52:30 -08009025 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05009026 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05009027 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05009028 }
dea31012005-04-17 16:05:31 -05009029
dea31012005-04-17 16:05:31 -05009030 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9031 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9032 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9033
9034 if (iocb)
9035 lpfc_sli_update_ring(phba, pring);
9036 else
9037 lpfc_sli_update_full_ring(phba, pring);
9038
9039 if (!piocb)
9040 return IOCB_SUCCESS;
9041
9042 goto out_busy;
9043
9044 iocb_busy:
9045 pring->stats.iocb_cmd_delay++;
9046
9047 out_busy:
9048
9049 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009050 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05009051 return IOCB_SUCCESS;
9052 }
9053
9054 return IOCB_BUSY;
9055}
9056
James Smart3772a992009-05-22 14:50:54 -04009057/**
James Smart4f774512009-05-22 14:52:35 -04009058 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9059 * @phba: Pointer to HBA context object.
9060 * @piocb: Pointer to command iocb.
9061 * @sglq: Pointer to the scatter gather queue object.
9062 *
9063 * This routine converts the bpl or bde that is in the IOCB
9064 * to a sgl list for the sli4 hardware. The physical address
9065 * of the bpl/bde is converted back to a virtual address.
9066 * If the IOCB contains a BPL then the list of BDE's is
9067 * converted to sli4_sge's. If the IOCB contains a single
9068 * BDE then it is converted to a single sli_sge.
9069 * The IOCB is still in cpu endianess so the contents of
9070 * the bpl can be used without byte swapping.
9071 *
9072 * Returns valid XRI = Success, NO_XRI = Failure.
9073**/
9074static uint16_t
9075lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9076 struct lpfc_sglq *sglq)
9077{
9078 uint16_t xritag = NO_XRI;
9079 struct ulp_bde64 *bpl = NULL;
9080 struct ulp_bde64 bde;
9081 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05009082 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04009083 IOCB_t *icmd;
9084 int numBdes = 0;
9085 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05009086 uint32_t offset = 0; /* accumulated offset in the sg request list */
9087 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04009088
9089 if (!piocbq || !sglq)
9090 return xritag;
9091
9092 sgl = (struct sli4_sge *)sglq->sgl;
9093 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05009094 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9095 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009096 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9097 numBdes = icmd->un.genreq64.bdl.bdeSize /
9098 sizeof(struct ulp_bde64);
9099 /* The addrHigh and addrLow fields within the IOCB
9100 * have not been byteswapped yet so there is no
9101 * need to swap them back.
9102 */
James Smart1b511972011-12-13 13:23:09 -05009103 if (piocbq->context3)
9104 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9105 else
9106 return xritag;
James Smart4f774512009-05-22 14:52:35 -04009107
James Smart1b511972011-12-13 13:23:09 -05009108 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04009109 if (!bpl)
9110 return xritag;
9111
9112 for (i = 0; i < numBdes; i++) {
9113 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05009114 sgl->addr_hi = bpl->addrHigh;
9115 sgl->addr_lo = bpl->addrLow;
9116
James Smart05580562011-05-24 11:40:48 -04009117 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009118 if ((i+1) == numBdes)
9119 bf_set(lpfc_sli4_sge_last, sgl, 1);
9120 else
9121 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05009122 /* swap the size field back to the cpu so we
9123 * can assign it to the sgl.
9124 */
9125 bde.tus.w = le32_to_cpu(bpl->tus.w);
9126 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05009127 /* The offsets in the sgl need to be accumulated
9128 * separately for the request and reply lists.
9129 * The request is always first, the reply follows.
9130 */
9131 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9132 /* add up the reply sg entries */
9133 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9134 inbound++;
9135 /* first inbound? reset the offset */
9136 if (inbound == 1)
9137 offset = 0;
9138 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04009139 bf_set(lpfc_sli4_sge_type, sgl,
9140 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05009141 offset += bde.tus.f.bdeSize;
9142 }
James Smart546fc852011-03-11 16:06:29 -05009143 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009144 bpl++;
9145 sgl++;
9146 }
9147 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9148 /* The addrHigh and addrLow fields of the BDE have not
9149 * been byteswapped yet so they need to be swapped
9150 * before putting them in the sgl.
9151 */
9152 sgl->addr_hi =
9153 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9154 sgl->addr_lo =
9155 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04009156 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009157 bf_set(lpfc_sli4_sge_last, sgl, 1);
9158 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05009159 sgl->sge_len =
9160 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04009161 }
9162 return sglq->sli4_xritag;
9163}
9164
9165/**
James Smart4f774512009-05-22 14:52:35 -04009166 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9167 * @phba: Pointer to HBA context object.
9168 * @piocb: Pointer to command iocb.
9169 * @wqe: Pointer to the work queue entry.
9170 *
9171 * This routine converts the iocb command to its Work Queue Entry
9172 * equivalent. The wqe pointer should not have any fields set when
9173 * this routine is called because it will memcpy over them.
9174 * This routine does not set the CQ_ID or the WQEC bits in the
9175 * wqe.
9176 *
9177 * Returns: 0 = Success, IOCB_ERROR = Failure.
9178 **/
9179static int
9180lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08009181 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04009182{
James Smart5ffc2662009-11-18 15:39:44 -05009183 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04009184 uint8_t ct = 0;
9185 uint32_t fip;
9186 uint32_t abort_tag;
9187 uint8_t command_type = ELS_COMMAND_NON_FIP;
9188 uint8_t cmnd;
9189 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04009190 uint16_t abrt_iotag;
9191 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04009192 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04009193 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05009194 int numBdes, i;
9195 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04009196 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05009197 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05009198 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04009199
James Smart45ed1192009-10-02 15:17:02 -04009200 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04009201 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04009202 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04009203 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05009204 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04009205 command_type = ELS_COMMAND_FIP;
9206 else
9207 command_type = ELS_COMMAND_NON_FIP;
9208
James Smartb5c53952016-03-31 14:12:30 -07009209 if (phba->fcp_embed_io)
9210 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04009211 /* Some of the fields are in the right position already */
9212 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07009213 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
9214 /* The ct field has moved so reset */
9215 wqe->generic.wqe_com.word7 = 0;
9216 wqe->generic.wqe_com.word10 = 0;
9217 }
James Smartb5c53952016-03-31 14:12:30 -07009218
9219 abort_tag = (uint32_t) iocbq->iotag;
9220 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009221 /* words0-2 bpl convert bde */
9222 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05009223 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9224 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04009225 bpl = (struct ulp_bde64 *)
9226 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9227 if (!bpl)
9228 return IOCB_ERROR;
9229
9230 /* Should already be byte swapped. */
9231 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
9232 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
9233 /* swap the size field back to the cpu so we
9234 * can assign it to the sgl.
9235 */
9236 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05009237 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9238 total_len = 0;
9239 for (i = 0; i < numBdes; i++) {
9240 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9241 total_len += bde.tus.f.bdeSize;
9242 }
James Smart4f774512009-05-22 14:52:35 -04009243 } else
James Smart5ffc2662009-11-18 15:39:44 -05009244 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04009245
9246 iocbq->iocb.ulpIoTag = iocbq->iotag;
9247 cmnd = iocbq->iocb.ulpCommand;
9248
9249 switch (iocbq->iocb.ulpCommand) {
9250 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04009251 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9252 ndlp = iocbq->context_un.ndlp;
9253 else
9254 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009255 if (!iocbq->iocb.ulpLe) {
9256 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9257 "2007 Only Limited Edition cmd Format"
9258 " supported 0x%x\n",
9259 iocbq->iocb.ulpCommand);
9260 return IOCB_ERROR;
9261 }
James Smartff78d8f2011-12-13 13:21:35 -05009262
James Smart5ffc2662009-11-18 15:39:44 -05009263 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009264 /* Els_reguest64 has a TMO */
9265 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9266 iocbq->iocb.ulpTimeout);
9267 /* Need a VF for word 4 set the vf bit*/
9268 bf_set(els_req64_vf, &wqe->els_req, 0);
9269 /* And a VFID for word 12 */
9270 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04009271 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04009272 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9273 iocbq->iocb.ulpContext);
9274 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9275 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04009276 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05009277 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05009278 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9279 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05009280 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9281 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05009282 if_type = bf_get(lpfc_sli_intf_if_type,
9283 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009284 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05009285 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05009286 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05009287 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05009288 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05009289 *pcmd == ELS_CMD_PLOGI)) {
9290 bf_set(els_req64_sp, &wqe->els_req, 1);
9291 bf_set(els_req64_sid, &wqe->els_req,
9292 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009293 if ((*pcmd == ELS_CMD_FLOGI) &&
9294 !(phba->fc_topology ==
9295 LPFC_TOPOLOGY_LOOP))
9296 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009297 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9298 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009299 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009300 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009301 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9302 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9303 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9304 }
James Smartc8685952009-11-18 15:39:16 -05009305 }
James Smart6d368e52011-05-24 11:44:12 -04009306 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9307 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009308 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9309 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9310 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9311 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9312 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9313 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009314 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009315 break;
James Smart5ffc2662009-11-18 15:39:44 -05009316 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009317 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9318 iocbq->iocb.un.ulpWord[3]);
9319 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009320 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009321 /* The entire sequence is transmitted for this IOCB */
9322 xmit_len = total_len;
9323 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009324 if (phba->link_flag & LS_LOOPBACK_MODE)
9325 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04009326 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009327 /* word3 iocb=io_tag32 wqe=reserved */
9328 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009329 /* word4 relative_offset memcpy */
9330 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009331 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9332 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9333 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9334 LPFC_WQE_IOD_WRITE);
9335 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9336 LPFC_WQE_LENLOC_WORD12);
9337 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009338 wqe->xmit_sequence.xmit_len = xmit_len;
9339 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009340 break;
James Smart4f774512009-05-22 14:52:35 -04009341 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009342 /* word3 iocb=iotag32 wqe=seq_payload_len */
9343 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009344 /* word4 iocb=rsvd wqe=rsvd */
9345 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9346 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009347 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009348 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009349 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9350 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9351 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9352 LPFC_WQE_LENLOC_WORD3);
9353 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009354 break;
James Smart4f774512009-05-22 14:52:35 -04009355 case CMD_FCP_IWRITE64_CR:
9356 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009357 /* word3 iocb=iotag wqe=payload_offset_len */
9358 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009359 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9360 xmit_len + sizeof(struct fcp_rsp));
9361 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9362 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009363 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9364 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9365 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9366 iocbq->iocb.ulpFCP2Rcvy);
9367 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9368 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009369 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9370 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9371 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009372 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009373 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009374 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9375 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009376 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9377 if (iocbq->priority) {
9378 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9379 (iocbq->priority << 1));
9380 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009381 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9382 (phba->cfg_XLanePriority << 1));
9383 }
9384 }
James Smartb5c53952016-03-31 14:12:30 -07009385 /* Note, word 10 is already initialized to 0 */
9386
James Smart414abe02018-06-26 08:24:26 -07009387 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9388 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009389 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9390 else
9391 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9392
James Smartb5c53952016-03-31 14:12:30 -07009393 if (phba->fcp_embed_io) {
9394 struct lpfc_scsi_buf *lpfc_cmd;
9395 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009396 struct fcp_cmnd *fcp_cmnd;
9397 uint32_t *ptr;
9398
9399 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009400
9401 lpfc_cmd = iocbq->context1;
9402 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9403 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9404
9405 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009406 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009407 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009408 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9409 wqe->generic.bde.addrHigh = 0;
9410 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009411
James Smart205e8242018-03-05 12:04:03 -08009412 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9413 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009414
9415 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009416 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009417 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9418 }
James Smart7851fe22011-07-22 18:36:52 -04009419 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009420 case CMD_FCP_IREAD64_CR:
9421 /* word3 iocb=iotag wqe=payload_offset_len */
9422 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009423 bf_set(payload_offset_len, &wqe->fcp_iread,
9424 xmit_len + sizeof(struct fcp_rsp));
9425 bf_set(cmd_buff_len, &wqe->fcp_iread,
9426 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009427 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9428 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9429 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9430 iocbq->iocb.ulpFCP2Rcvy);
9431 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009432 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009433 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9434 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9435 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009436 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009437 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009438 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9439 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009440 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9441 if (iocbq->priority) {
9442 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9443 (iocbq->priority << 1));
9444 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009445 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9446 (phba->cfg_XLanePriority << 1));
9447 }
9448 }
James Smartb5c53952016-03-31 14:12:30 -07009449 /* Note, word 10 is already initialized to 0 */
9450
James Smart414abe02018-06-26 08:24:26 -07009451 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9452 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009453 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9454 else
9455 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9456
James Smartb5c53952016-03-31 14:12:30 -07009457 if (phba->fcp_embed_io) {
9458 struct lpfc_scsi_buf *lpfc_cmd;
9459 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009460 struct fcp_cmnd *fcp_cmnd;
9461 uint32_t *ptr;
9462
9463 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009464
9465 lpfc_cmd = iocbq->context1;
9466 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9467 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9468
9469 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009470 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009471 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009472 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9473 wqe->generic.bde.addrHigh = 0;
9474 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009475
James Smart205e8242018-03-05 12:04:03 -08009476 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9477 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009478
9479 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009480 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009481 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9482 }
James Smart7851fe22011-07-22 18:36:52 -04009483 break;
James Smartf1126682009-06-10 17:22:44 -04009484 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009485 /* word3 iocb=iotag wqe=payload_offset_len */
9486 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9487 bf_set(payload_offset_len, &wqe->fcp_icmd,
9488 xmit_len + sizeof(struct fcp_rsp));
9489 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9490 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009491 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009492 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009493 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009494 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9495 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9496 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9497 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9498 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009499 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9500 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009501 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9502 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009503 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9504 if (iocbq->priority) {
9505 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9506 (iocbq->priority << 1));
9507 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009508 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9509 (phba->cfg_XLanePriority << 1));
9510 }
9511 }
James Smartb5c53952016-03-31 14:12:30 -07009512 /* Note, word 10 is already initialized to 0 */
9513
9514 if (phba->fcp_embed_io) {
9515 struct lpfc_scsi_buf *lpfc_cmd;
9516 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009517 struct fcp_cmnd *fcp_cmnd;
9518 uint32_t *ptr;
9519
9520 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009521
9522 lpfc_cmd = iocbq->context1;
9523 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9524 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9525
9526 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009527 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009528 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009529 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9530 wqe->generic.bde.addrHigh = 0;
9531 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009532
James Smart205e8242018-03-05 12:04:03 -08009533 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9534 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009535
9536 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009537 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009538 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9539 }
James Smart7851fe22011-07-22 18:36:52 -04009540 break;
James Smart4f774512009-05-22 14:52:35 -04009541 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009542 /* For this command calculate the xmit length of the
9543 * request bde.
9544 */
9545 xmit_len = 0;
9546 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9547 sizeof(struct ulp_bde64);
9548 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009549 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009550 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9551 break;
James Smart63e801c2010-11-20 23:14:19 -05009552 xmit_len += bde.tus.f.bdeSize;
9553 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009554 /* word3 iocb=IO_TAG wqe=request_payload_len */
9555 wqe->gen_req.request_payload_len = xmit_len;
9556 /* word4 iocb=parameter wqe=relative_offset memcpy */
9557 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009558 /* word6 context tag copied in memcpy */
9559 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9560 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9561 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9562 "2015 Invalid CT %x command 0x%x\n",
9563 ct, iocbq->iocb.ulpCommand);
9564 return IOCB_ERROR;
9565 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009566 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9567 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9568 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9569 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9570 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9571 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9572 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9573 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009574 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009575 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009576 break;
James Smart4f774512009-05-22 14:52:35 -04009577 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009578 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009579 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009580 /* word3 iocb=iotag32 wqe=response_payload_len */
9581 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009582 /* word4 */
9583 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009584 /* word5 iocb=rsvd wge=did */
9585 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009586 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9587
9588 if_type = bf_get(lpfc_sli_intf_if_type,
9589 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009590 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009591 if (iocbq->vport->fc_flag & FC_PT2PT) {
9592 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9593 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9594 iocbq->vport->fc_myDID);
9595 if (iocbq->vport->fc_myDID == Fabric_DID) {
9596 bf_set(wqe_els_did,
9597 &wqe->xmit_els_rsp.wqe_dest, 0);
9598 }
9599 }
9600 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009601 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9602 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9603 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9604 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009605 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009606 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009607 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009608 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009609 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9610 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9611 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9612 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9613 LPFC_WQE_LENLOC_WORD3);
9614 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009615 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9616 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009617 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9618 iocbq->context2)->virt);
9619 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009620 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9621 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009622 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009623 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9624 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009625 phba->vpi_ids[phba->pport->vpi]);
9626 }
James Smart4f774512009-05-22 14:52:35 -04009627 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009628 break;
James Smart4f774512009-05-22 14:52:35 -04009629 case CMD_CLOSE_XRI_CN:
9630 case CMD_ABORT_XRI_CN:
9631 case CMD_ABORT_XRI_CX:
9632 /* words 0-2 memcpy should be 0 rserved */
9633 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009634 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9635 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9636 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9637 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9638 } else
9639 fip = 0;
9640
9641 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009642 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009643 * The link is down, or the command was ELS_FIP
9644 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009645 * on the wire.
9646 */
9647 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9648 else
9649 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9650 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009651 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9652 wqe->abort_cmd.rsrvd5 = 0;
9653 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009654 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9655 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009656 /*
9657 * The abort handler will send us CMD_ABORT_XRI_CN or
9658 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9659 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009660 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9661 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9662 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9663 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009664 cmnd = CMD_ABORT_XRI_CX;
9665 command_type = OTHER_COMMAND;
9666 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009667 break;
James Smart6669f9b2009-10-02 15:16:45 -04009668 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009669 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009670 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009671 * we re-construct this WQE here based on information in
9672 * iocbq from scratch.
9673 */
9674 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009675 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009676 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009677 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9678 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009679 LPFC_ABTS_UNSOL_INT) {
9680 /* ABTS sent by initiator to CT exchange, the
9681 * RX_ID field will be filled with the newly
9682 * allocated responder XRI.
9683 */
9684 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9685 iocbq->sli4_xritag);
9686 } else {
9687 /* ABTS sent by responder to CT exchange, the
9688 * RX_ID field will be filled with the responder
9689 * RX_ID from ABTS.
9690 */
9691 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009692 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009693 }
James Smart6669f9b2009-10-02 15:16:45 -04009694 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9695 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009696
9697 /* Use CT=VPI */
9698 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9699 ndlp->nlp_DID);
9700 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9701 iocbq->iocb.ulpContext);
9702 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009703 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009704 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009705 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9706 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9707 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009708 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9709 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009710 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9711 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9712 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9713 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9714 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9715 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9716 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9717 }
9718
James Smart7851fe22011-07-22 18:36:52 -04009719 break;
James Smartae9e28f2017-05-15 15:20:51 -07009720 case CMD_SEND_FRAME:
9721 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9722 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9723 return 0;
James Smart4f774512009-05-22 14:52:35 -04009724 case CMD_XRI_ABORTED_CX:
9725 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009726 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9727 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9728 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9729 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9730 default:
9731 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9732 "2014 Invalid command 0x%x\n",
9733 iocbq->iocb.ulpCommand);
9734 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009735 break;
James Smart4f774512009-05-22 14:52:35 -04009736 }
James Smart6d368e52011-05-24 11:44:12 -04009737
James Smart8012cc32012-10-31 14:44:49 -04009738 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9739 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9740 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9741 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9742 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9743 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9744 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9745 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009746 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9747 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9748 wqe->generic.wqe_com.abort_tag = abort_tag;
9749 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9750 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9751 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9752 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009753 return 0;
9754}
9755
9756/**
9757 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9758 * @phba: Pointer to HBA context object.
9759 * @ring_number: SLI ring number to issue iocb on.
9760 * @piocb: Pointer to command iocb.
9761 * @flag: Flag indicating if this command can be put into txq.
9762 *
9763 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9764 * an iocb command to an HBA with SLI-4 interface spec.
9765 *
9766 * This function is called with hbalock held. The function will return success
9767 * after it successfully submit the iocb to firmware or after adding to the
9768 * txq.
9769 **/
9770static int
9771__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9772 struct lpfc_iocbq *piocb, uint32_t flag)
9773{
9774 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -08009775 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -05009776 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009777 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009778
James Smart895427b2017-02-12 13:52:30 -08009779 /* Get the WQ */
9780 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9781 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9782 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9783 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9784 else
9785 wq = phba->sli4_hba.oas_wq;
9786 } else {
9787 wq = phba->sli4_hba.els_wq;
9788 }
9789
9790 /* Get corresponding ring */
9791 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009792
James Smartb5c53952016-03-31 14:12:30 -07009793 /*
9794 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -07009795 */
James Smartb5c53952016-03-31 14:12:30 -07009796
James Smart895427b2017-02-12 13:52:30 -08009797 lockdep_assert_held(&phba->hbalock);
9798
James Smart4f774512009-05-22 14:52:35 -04009799 if (piocb->sli4_xritag == NO_XRI) {
9800 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009801 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009802 sglq = NULL;
9803 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009804 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009805 if (!(flag & SLI_IOCB_RET_IOCB)) {
9806 __lpfc_sli_ringtx_put(phba,
9807 pring, piocb);
9808 return IOCB_SUCCESS;
9809 } else {
9810 return IOCB_BUSY;
9811 }
9812 } else {
James Smart895427b2017-02-12 13:52:30 -08009813 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009814 if (!sglq) {
9815 if (!(flag & SLI_IOCB_RET_IOCB)) {
9816 __lpfc_sli_ringtx_put(phba,
9817 pring,
9818 piocb);
9819 return IOCB_SUCCESS;
9820 } else
9821 return IOCB_BUSY;
9822 }
9823 }
James Smart4f774512009-05-22 14:52:35 -04009824 }
James Smart2ea259e2017-02-12 13:52:27 -08009825 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009826 /* These IO's already have an XRI and a mapped sgl. */
9827 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009828 else {
James Smart6d368e52011-05-24 11:44:12 -04009829 /*
9830 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009831 * sglq is on the active list
9832 */
James Smartedccdc12013-01-03 15:43:45 -05009833 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009834 if (!sglq)
9835 return IOCB_ERROR;
9836 }
9837
9838 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009839 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009840 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009841 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009842 return IOCB_ERROR;
9843 }
9844
James Smart205e8242018-03-05 12:04:03 -08009845 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -04009846 return IOCB_ERROR;
9847
James Smart205e8242018-03-05 12:04:03 -08009848 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -08009849 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009850 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9851
9852 return 0;
9853}
9854
9855/**
James Smart3772a992009-05-22 14:50:54 -04009856 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9857 *
9858 * This routine wraps the actual lockless version for issusing IOCB function
9859 * pointer from the lpfc_hba struct.
9860 *
9861 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009862 * IOCB_ERROR - Error
9863 * IOCB_SUCCESS - Success
9864 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009865 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009866int
James Smart3772a992009-05-22 14:50:54 -04009867__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9868 struct lpfc_iocbq *piocb, uint32_t flag)
9869{
9870 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9871}
9872
9873/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009874 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009875 * @phba: The hba struct for which this call is being executed.
9876 * @dev_grp: The HBA PCI-Device group number.
9877 *
9878 * This routine sets up the SLI interface API function jump table in @phba
9879 * struct.
9880 * Returns: 0 - success, -ENODEV - failure.
9881 **/
9882int
9883lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9884{
9885
9886 switch (dev_grp) {
9887 case LPFC_PCI_DEV_LP:
9888 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9889 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9890 break;
James Smart4f774512009-05-22 14:52:35 -04009891 case LPFC_PCI_DEV_OC:
9892 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9893 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9894 break;
James Smart3772a992009-05-22 14:50:54 -04009895 default:
9896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9897 "1419 Invalid HBA PCI-device group: 0x%x\n",
9898 dev_grp);
9899 return -ENODEV;
9900 break;
9901 }
9902 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9903 return 0;
9904}
James Smart92d7f7b2007-06-17 19:56:38 -05009905
James Smarta1efe162015-05-21 13:55:20 -04009906/**
James Smart895427b2017-02-12 13:52:30 -08009907 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009908 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009909 * @piocb: Pointer to command iocb.
9910 *
James Smart895427b2017-02-12 13:52:30 -08009911 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9912 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009913 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009914 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009915 */
James Smart895427b2017-02-12 13:52:30 -08009916struct lpfc_sli_ring *
9917lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009918{
James Smart895427b2017-02-12 13:52:30 -08009919 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009920 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009921 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009922 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009923 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009924 /*
James Smart895427b2017-02-12 13:52:30 -08009925 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009926 * be setup based on what work queue we used.
9927 */
Dick Kennedy8e036a92017-09-29 17:34:35 -07009928 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart895427b2017-02-12 13:52:30 -08009929 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009930 lpfc_sli4_scmd_to_wqidx_distr(phba,
9931 piocb->context1);
Dick Kennedy8e036a92017-09-29 17:34:35 -07009932 piocb->hba_wqidx = piocb->hba_wqidx %
9933 phba->cfg_fcp_io_channel;
9934 }
James Smart895427b2017-02-12 13:52:30 -08009935 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009936 } else {
9937 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009938 return NULL;
9939 piocb->hba_wqidx = 0;
9940 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009941 }
James Smart895427b2017-02-12 13:52:30 -08009942 } else {
9943 if (unlikely(!phba->sli4_hba.els_wq))
9944 return NULL;
9945 piocb->hba_wqidx = 0;
9946 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009947 }
James Smart9bd2bff52014-09-03 12:57:30 -04009948}
9949
James Smarte59058c2008-08-24 21:49:00 -04009950/**
James Smart3621a712009-04-06 18:47:14 -04009951 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009952 * @phba: Pointer to HBA context object.
9953 * @pring: Pointer to driver SLI ring object.
9954 * @piocb: Pointer to command iocb.
9955 * @flag: Flag indicating if this command can be put into txq.
9956 *
9957 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9958 * function. This function gets the hbalock and calls
9959 * __lpfc_sli_issue_iocb function and will return the error returned
9960 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9961 * functions which do not hold hbalock.
9962 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009963int
James Smart3772a992009-05-22 14:50:54 -04009964lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009965 struct lpfc_iocbq *piocb, uint32_t flag)
9966{
James Smart895427b2017-02-12 13:52:30 -08009967 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009968 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009969 struct lpfc_queue *fpeq;
9970 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009971 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009972 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009973
James Smart7e56aa22012-08-03 12:35:34 -04009974 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009975 pring = lpfc_sli4_calc_ring(phba, piocb);
9976 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009977 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009978
James Smart9bd2bff52014-09-03 12:57:30 -04009979 spin_lock_irqsave(&pring->ring_lock, iflags);
9980 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9981 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009982
James Smart9bd2bff52014-09-03 12:57:30 -04009983 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009984 idx = piocb->hba_wqidx;
9985 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009986
James Smart895427b2017-02-12 13:52:30 -08009987 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009988
James Smart9bd2bff52014-09-03 12:57:30 -04009989 /* Get associated EQ with this index */
9990 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009991
James Smart9bd2bff52014-09-03 12:57:30 -04009992 /* Turn off interrupts from this EQ */
James Smartb71413d2018-02-22 08:18:40 -08009993 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009994
James Smart9bd2bff52014-09-03 12:57:30 -04009995 /*
9996 * Process all the events on FCP EQ
9997 */
9998 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9999 lpfc_sli4_hba_handle_eqe(phba,
10000 eqe, idx);
10001 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -040010002 }
James Smartba20c852012-08-03 12:36:52 -040010003
James Smart9bd2bff52014-09-03 12:57:30 -040010004 /* Always clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -080010005 phba->sli4_hba.sli4_eq_release(fpeq,
James Smart9bd2bff52014-09-03 12:57:30 -040010006 LPFC_QUEUE_REARM);
10007 }
James Smart895427b2017-02-12 13:52:30 -080010008 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -040010009 }
James Smart7e56aa22012-08-03 12:35:34 -040010010 } else {
10011 /* For now, SLI2/3 will still use hbalock */
10012 spin_lock_irqsave(&phba->hbalock, iflags);
10013 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10014 spin_unlock_irqrestore(&phba->hbalock, iflags);
10015 }
James Smart92d7f7b2007-06-17 19:56:38 -050010016 return rc;
10017}
10018
James Smarte59058c2008-08-24 21:49:00 -040010019/**
James Smart3621a712009-04-06 18:47:14 -040010020 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010021 * @phba: Pointer to HBA context object.
10022 *
10023 * This function is called while driver attaches with the
10024 * HBA to setup the extra ring. The extra ring is used
10025 * only when driver needs to support target mode functionality
10026 * or IP over FC functionalities.
10027 *
James Smart895427b2017-02-12 13:52:30 -080010028 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -040010029 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010030static int
10031lpfc_extra_ring_setup( struct lpfc_hba *phba)
10032{
10033 struct lpfc_sli *psli;
10034 struct lpfc_sli_ring *pring;
10035
10036 psli = &phba->sli;
10037
10038 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -050010039
10040 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -080010041 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -040010042 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10043 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10044 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10045 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010046
James Smarta4bc3372006-12-02 13:34:16 -050010047 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -080010048 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -050010049
James Smart7e56aa22012-08-03 12:35:34 -040010050 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10051 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10052 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10053 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010054
10055 /* Setup default profile for this ring */
10056 pring->iotag_max = 4096;
10057 pring->num_mask = 1;
10058 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -050010059 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10060 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010061 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10062 return 0;
10063}
10064
James Smartcb69f7d2011-12-13 13:21:57 -050010065/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10066 * @phba: Pointer to HBA context object.
10067 * @iocbq: Pointer to iocb object.
10068 *
10069 * The async_event handler calls this routine when it receives
10070 * an ASYNC_STATUS_CN event from the port. The port generates
10071 * this event when an Abort Sequence request to an rport fails
10072 * twice in succession. The abort could be originated by the
10073 * driver or by the port. The ABTS could have been for an ELS
10074 * or FCP IO. The port only generates this event when an ABTS
10075 * fails to complete after one retry.
10076 */
10077static void
10078lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10079 struct lpfc_iocbq *iocbq)
10080{
10081 struct lpfc_nodelist *ndlp = NULL;
10082 uint16_t rpi = 0, vpi = 0;
10083 struct lpfc_vport *vport = NULL;
10084
10085 /* The rpi in the ulpContext is vport-sensitive. */
10086 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10087 rpi = iocbq->iocb.ulpContext;
10088
10089 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10090 "3092 Port generated ABTS async event "
10091 "on vpi %d rpi %d status 0x%x\n",
10092 vpi, rpi, iocbq->iocb.ulpStatus);
10093
10094 vport = lpfc_find_vport_by_vpid(phba, vpi);
10095 if (!vport)
10096 goto err_exit;
10097 ndlp = lpfc_findnode_rpi(vport, rpi);
10098 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
10099 goto err_exit;
10100
10101 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10102 lpfc_sli_abts_recover_port(vport, ndlp);
10103 return;
10104
10105 err_exit:
10106 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10107 "3095 Event Context not found, no "
10108 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10109 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10110 vpi, rpi);
10111}
10112
10113/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10114 * @phba: pointer to HBA context object.
10115 * @ndlp: nodelist pointer for the impacted rport.
10116 * @axri: pointer to the wcqe containing the failed exchange.
10117 *
10118 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10119 * port. The port generates this event when an abort exchange request to an
10120 * rport fails twice in succession with no reply. The abort could be originated
10121 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
10122 */
10123void
10124lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10125 struct lpfc_nodelist *ndlp,
10126 struct sli4_wcqe_xri_aborted *axri)
10127{
10128 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -050010129 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -050010130
James Smart6b5151f2012-01-18 16:24:06 -050010131 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -050010132 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10133 "3115 Node Context not found, driver "
10134 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -050010135 return;
10136 }
10137
James Smartcb69f7d2011-12-13 13:21:57 -050010138 vport = ndlp->vport;
10139 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10140 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -050010141 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -040010142 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -050010143 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -050010144 bf_get(lpfc_wcqe_xa_status, axri),
10145 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -050010146
James Smart5c1db2a2012-03-01 22:34:36 -050010147 /*
10148 * Catch the ABTS protocol failure case. Older OCe FW releases returned
10149 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10150 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10151 */
James Smarte3d2b802012-08-14 14:25:43 -040010152 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -050010153 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10154 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -050010155 lpfc_sli_abts_recover_port(vport, ndlp);
10156}
10157
James Smarte59058c2008-08-24 21:49:00 -040010158/**
James Smart3621a712009-04-06 18:47:14 -040010159 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -040010160 * @phba: Pointer to HBA context object.
10161 * @pring: Pointer to driver SLI ring object.
10162 * @iocbq: Pointer to iocb object.
10163 *
10164 * This function is called by the slow ring event handler
10165 * function when there is an ASYNC event iocb in the ring.
10166 * This function is called with no lock held.
10167 * Currently this function handles only temperature related
10168 * ASYNC events. The function decodes the temperature sensor
10169 * event message and posts events for the management applications.
10170 **/
James Smart98c9ea52007-10-27 13:37:33 -040010171static void
James Smart57127f12007-10-27 13:37:05 -040010172lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10173 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10174{
10175 IOCB_t *icmd;
10176 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -040010177 struct temp_event temp_event_data;
10178 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -040010179 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -040010180
10181 icmd = &iocbq->iocb;
10182 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -040010183
James Smartcb69f7d2011-12-13 13:21:57 -050010184 switch (evt_code) {
10185 case ASYNC_TEMP_WARN:
10186 case ASYNC_TEMP_SAFE:
10187 temp_event_data.data = (uint32_t) icmd->ulpContext;
10188 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10189 if (evt_code == ASYNC_TEMP_WARN) {
10190 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10191 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10192 "0347 Adapter is very hot, please take "
10193 "corrective action. temperature : %d Celsius\n",
10194 (uint32_t) icmd->ulpContext);
10195 } else {
10196 temp_event_data.event_code = LPFC_NORMAL_TEMP;
10197 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10198 "0340 Adapter temperature is OK now. "
10199 "temperature : %d Celsius\n",
10200 (uint32_t) icmd->ulpContext);
10201 }
10202
10203 /* Send temperature change event to applications */
10204 shost = lpfc_shost_from_vport(phba->pport);
10205 fc_host_post_vendor_event(shost, fc_get_event_number(),
10206 sizeof(temp_event_data), (char *) &temp_event_data,
10207 LPFC_NL_VENDOR_ID);
10208 break;
10209 case ASYNC_STATUS_CN:
10210 lpfc_sli_abts_err_handler(phba, iocbq);
10211 break;
10212 default:
James Smarta257bf92009-04-06 18:48:10 -040010213 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -050010214 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -040010215 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -040010216 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -040010217 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
10218 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
10219 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
10220 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -050010221 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -040010222 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10223 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10224 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10225 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10226
James Smartcb69f7d2011-12-13 13:21:57 -050010227 break;
James Smart57127f12007-10-27 13:37:05 -040010228 }
James Smart57127f12007-10-27 13:37:05 -040010229}
10230
10231
James Smarte59058c2008-08-24 21:49:00 -040010232/**
James Smart895427b2017-02-12 13:52:30 -080010233 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010234 * @phba: Pointer to HBA context object.
10235 *
10236 * lpfc_sli_setup sets up rings of the SLI interface with
10237 * number of iocbs per ring and iotags. This function is
10238 * called while driver attach to the HBA and before the
10239 * interrupts are enabled. So there is no need for locking.
10240 *
10241 * This function always returns 0.
10242 **/
dea31012005-04-17 16:05:31 -050010243int
James Smart895427b2017-02-12 13:52:30 -080010244lpfc_sli4_setup(struct lpfc_hba *phba)
10245{
10246 struct lpfc_sli_ring *pring;
10247
10248 pring = phba->sli4_hba.els_wq->pring;
10249 pring->num_mask = LPFC_MAX_RING_MASK;
10250 pring->prt[0].profile = 0; /* Mask 0 */
10251 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10252 pring->prt[0].type = FC_TYPE_ELS;
10253 pring->prt[0].lpfc_sli_rcv_unsol_event =
10254 lpfc_els_unsol_event;
10255 pring->prt[1].profile = 0; /* Mask 1 */
10256 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10257 pring->prt[1].type = FC_TYPE_ELS;
10258 pring->prt[1].lpfc_sli_rcv_unsol_event =
10259 lpfc_els_unsol_event;
10260 pring->prt[2].profile = 0; /* Mask 2 */
10261 /* NameServer Inquiry */
10262 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10263 /* NameServer */
10264 pring->prt[2].type = FC_TYPE_CT;
10265 pring->prt[2].lpfc_sli_rcv_unsol_event =
10266 lpfc_ct_unsol_event;
10267 pring->prt[3].profile = 0; /* Mask 3 */
10268 /* NameServer response */
10269 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10270 /* NameServer */
10271 pring->prt[3].type = FC_TYPE_CT;
10272 pring->prt[3].lpfc_sli_rcv_unsol_event =
10273 lpfc_ct_unsol_event;
10274 return 0;
10275}
10276
10277/**
10278 * lpfc_sli_setup - SLI ring setup function
10279 * @phba: Pointer to HBA context object.
10280 *
10281 * lpfc_sli_setup sets up rings of the SLI interface with
10282 * number of iocbs per ring and iotags. This function is
10283 * called while driver attach to the HBA and before the
10284 * interrupts are enabled. So there is no need for locking.
10285 *
10286 * This function always returns 0. SLI3 only.
10287 **/
10288int
dea31012005-04-17 16:05:31 -050010289lpfc_sli_setup(struct lpfc_hba *phba)
10290{
James Smarted957682007-06-17 19:56:37 -050010291 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050010292 struct lpfc_sli *psli = &phba->sli;
10293 struct lpfc_sli_ring *pring;
10294
James Smart2a76a282012-08-03 12:35:54 -040010295 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010296 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010297
James Bottomley604a3e32005-10-29 10:28:33 -050010298 psli->iocbq_lookup = NULL;
10299 psli->iocbq_lookup_len = 0;
10300 psli->last_iotag = 0;
10301
dea31012005-04-17 16:05:31 -050010302 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010303 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010304 switch (i) {
10305 case LPFC_FCP_RING: /* ring 0 - FCP */
10306 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010307 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10308 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10309 pring->sli.sli3.numCiocb +=
10310 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10311 pring->sli.sli3.numRiocb +=
10312 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10313 pring->sli.sli3.numCiocb +=
10314 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10315 pring->sli.sli3.numRiocb +=
10316 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10317 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010318 SLI3_IOCB_CMD_SIZE :
10319 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010320 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010321 SLI3_IOCB_RSP_SIZE :
10322 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010323 pring->iotag_ctr = 0;
10324 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010325 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010326 pring->fast_iotag = pring->iotag_max;
10327 pring->num_mask = 0;
10328 break;
James Smarta4bc3372006-12-02 13:34:16 -050010329 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010330 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010331 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10332 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10333 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010334 SLI3_IOCB_CMD_SIZE :
10335 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010336 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010337 SLI3_IOCB_RSP_SIZE :
10338 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010339 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010340 pring->num_mask = 0;
10341 break;
10342 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10343 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010344 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10345 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10346 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010347 SLI3_IOCB_CMD_SIZE :
10348 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010349 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010350 SLI3_IOCB_RSP_SIZE :
10351 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010352 pring->fast_iotag = 0;
10353 pring->iotag_ctr = 0;
10354 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010355 pring->lpfc_sli_rcv_async_status =
10356 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010357 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010358 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010359 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10360 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010361 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010362 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010363 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010364 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10365 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010366 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010367 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010368 pring->prt[2].profile = 0; /* Mask 2 */
10369 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010370 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010371 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010372 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010373 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010374 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010375 pring->prt[3].profile = 0; /* Mask 3 */
10376 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010377 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010378 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010379 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010380 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010381 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010382 break;
10383 }
James Smart7e56aa22012-08-03 12:35:34 -040010384 totiocbsize += (pring->sli.sli3.numCiocb *
10385 pring->sli.sli3.sizeCiocb) +
10386 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010387 }
James Smarted957682007-06-17 19:56:37 -050010388 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010389 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010390 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10391 "SLI2 SLIM Data: x%x x%lx\n",
10392 phba->brd_no, totiocbsize,
10393 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010394 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010395 if (phba->cfg_multi_ring_support == 2)
10396 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010397
10398 return 0;
10399}
10400
James Smarte59058c2008-08-24 21:49:00 -040010401/**
James Smart895427b2017-02-12 13:52:30 -080010402 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010403 * @phba: Pointer to HBA context object.
10404 *
James Smart895427b2017-02-12 13:52:30 -080010405 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010406 * ring. This function also initializes ring indices of each ring.
10407 * This function is called during the initialization of the SLI
10408 * interface of an HBA.
10409 * This function is called with no lock held and always returns
10410 * 1.
10411 **/
James Smart895427b2017-02-12 13:52:30 -080010412void
10413lpfc_sli4_queue_init(struct lpfc_hba *phba)
10414{
10415 struct lpfc_sli *psli;
10416 struct lpfc_sli_ring *pring;
10417 int i;
10418
10419 psli = &phba->sli;
10420 spin_lock_irq(&phba->hbalock);
10421 INIT_LIST_HEAD(&psli->mboxq);
10422 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10423 /* Initialize list headers for txq and txcmplq as double linked lists */
10424 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10425 pring = phba->sli4_hba.fcp_wq[i]->pring;
10426 pring->flag = 0;
10427 pring->ringno = LPFC_FCP_RING;
10428 INIT_LIST_HEAD(&pring->txq);
10429 INIT_LIST_HEAD(&pring->txcmplq);
10430 INIT_LIST_HEAD(&pring->iocb_continueq);
10431 spin_lock_init(&pring->ring_lock);
10432 }
10433 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10434 pring = phba->sli4_hba.nvme_wq[i]->pring;
10435 pring->flag = 0;
10436 pring->ringno = LPFC_FCP_RING;
10437 INIT_LIST_HEAD(&pring->txq);
10438 INIT_LIST_HEAD(&pring->txcmplq);
10439 INIT_LIST_HEAD(&pring->iocb_continueq);
10440 spin_lock_init(&pring->ring_lock);
10441 }
10442 pring = phba->sli4_hba.els_wq->pring;
10443 pring->flag = 0;
10444 pring->ringno = LPFC_ELS_RING;
10445 INIT_LIST_HEAD(&pring->txq);
10446 INIT_LIST_HEAD(&pring->txcmplq);
10447 INIT_LIST_HEAD(&pring->iocb_continueq);
10448 spin_lock_init(&pring->ring_lock);
10449
10450 if (phba->cfg_nvme_io_channel) {
10451 pring = phba->sli4_hba.nvmels_wq->pring;
10452 pring->flag = 0;
10453 pring->ringno = LPFC_ELS_RING;
10454 INIT_LIST_HEAD(&pring->txq);
10455 INIT_LIST_HEAD(&pring->txcmplq);
10456 INIT_LIST_HEAD(&pring->iocb_continueq);
10457 spin_lock_init(&pring->ring_lock);
10458 }
10459
10460 if (phba->cfg_fof) {
10461 pring = phba->sli4_hba.oas_wq->pring;
10462 pring->flag = 0;
10463 pring->ringno = LPFC_FCP_RING;
10464 INIT_LIST_HEAD(&pring->txq);
10465 INIT_LIST_HEAD(&pring->txcmplq);
10466 INIT_LIST_HEAD(&pring->iocb_continueq);
10467 spin_lock_init(&pring->ring_lock);
10468 }
10469
10470 spin_unlock_irq(&phba->hbalock);
10471}
10472
10473/**
10474 * lpfc_sli_queue_init - Queue initialization function
10475 * @phba: Pointer to HBA context object.
10476 *
10477 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10478 * ring. This function also initializes ring indices of each ring.
10479 * This function is called during the initialization of the SLI
10480 * interface of an HBA.
10481 * This function is called with no lock held and always returns
10482 * 1.
10483 **/
10484void
10485lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010486{
10487 struct lpfc_sli *psli;
10488 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010489 int i;
dea31012005-04-17 16:05:31 -050010490
10491 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010492 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010493 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010494 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010495 /* Initialize list headers for txq and txcmplq as double linked lists */
10496 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010497 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010498 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010499 pring->sli.sli3.next_cmdidx = 0;
10500 pring->sli.sli3.local_getidx = 0;
10501 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010502 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010503 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010504 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010505 pring->flag = 0;
10506 INIT_LIST_HEAD(&pring->txq);
10507 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010508 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010509 }
James Smart2e0fef82007-06-17 19:56:36 -050010510 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010511}
10512
James Smarte59058c2008-08-24 21:49:00 -040010513/**
James Smart04c68492009-05-22 14:52:52 -040010514 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10515 * @phba: Pointer to HBA context object.
10516 *
10517 * This routine flushes the mailbox command subsystem. It will unconditionally
10518 * flush all the mailbox commands in the three possible stages in the mailbox
10519 * command sub-system: pending mailbox command queue; the outstanding mailbox
10520 * command; and completed mailbox command queue. It is caller's responsibility
10521 * to make sure that the driver is in the proper state to flush the mailbox
10522 * command sub-system. Namely, the posting of mailbox commands into the
10523 * pending mailbox command queue from the various clients must be stopped;
10524 * either the HBA is in a state that it will never works on the outstanding
10525 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10526 * mailbox command has been completed.
10527 **/
10528static void
10529lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10530{
10531 LIST_HEAD(completions);
10532 struct lpfc_sli *psli = &phba->sli;
10533 LPFC_MBOXQ_t *pmb;
10534 unsigned long iflag;
10535
James Smart523128e2018-09-10 10:30:46 -070010536 /* Disable softirqs, including timers from obtaining phba->hbalock */
10537 local_bh_disable();
10538
James Smart04c68492009-05-22 14:52:52 -040010539 /* Flush all the mailbox commands in the mbox system */
10540 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart523128e2018-09-10 10:30:46 -070010541
James Smart04c68492009-05-22 14:52:52 -040010542 /* The pending mailbox command queue */
10543 list_splice_init(&phba->sli.mboxq, &completions);
10544 /* The outstanding active mailbox command */
10545 if (psli->mbox_active) {
10546 list_add_tail(&psli->mbox_active->list, &completions);
10547 psli->mbox_active = NULL;
10548 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10549 }
10550 /* The completed mailbox command queue */
10551 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10552 spin_unlock_irqrestore(&phba->hbalock, iflag);
10553
James Smart523128e2018-09-10 10:30:46 -070010554 /* Enable softirqs again, done with phba->hbalock */
10555 local_bh_enable();
10556
James Smart04c68492009-05-22 14:52:52 -040010557 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10558 while (!list_empty(&completions)) {
10559 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10560 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10561 if (pmb->mbox_cmpl)
10562 pmb->mbox_cmpl(phba, pmb);
10563 }
10564}
10565
10566/**
James Smart3621a712009-04-06 18:47:14 -040010567 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010568 * @vport: Pointer to virtual port object.
10569 *
10570 * lpfc_sli_host_down is called to clean up the resources
10571 * associated with a vport before destroying virtual
10572 * port data structures.
10573 * This function does following operations:
10574 * - Free discovery resources associated with this virtual
10575 * port.
10576 * - Free iocbs associated with this virtual port in
10577 * the txq.
10578 * - Send abort for all iocb commands associated with this
10579 * vport in txcmplq.
10580 *
10581 * This function is called with no lock held and always returns 1.
10582 **/
dea31012005-04-17 16:05:31 -050010583int
James Smart92d7f7b2007-06-17 19:56:38 -050010584lpfc_sli_host_down(struct lpfc_vport *vport)
10585{
James Smart858c9f62007-06-17 19:56:39 -050010586 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010587 struct lpfc_hba *phba = vport->phba;
10588 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010589 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010590 struct lpfc_sli_ring *pring;
10591 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010592 int i;
10593 unsigned long flags = 0;
10594 uint16_t prev_pring_flag;
10595
10596 lpfc_cleanup_discovery_resources(vport);
10597
10598 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010599
James Smart895427b2017-02-12 13:52:30 -080010600 /*
10601 * Error everything on the txq since these iocbs
10602 * have not been given to the FW yet.
10603 * Also issue ABTS for everything on the txcmplq
10604 */
10605 if (phba->sli_rev != LPFC_SLI_REV4) {
10606 for (i = 0; i < psli->num_rings; i++) {
10607 pring = &psli->sli3_ring[i];
10608 prev_pring_flag = pring->flag;
10609 /* Only slow rings */
10610 if (pring->ringno == LPFC_ELS_RING) {
10611 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10612 /* Set the lpfc data pending flag */
10613 set_bit(LPFC_DATA_READY, &phba->data_flags);
10614 }
10615 list_for_each_entry_safe(iocb, next_iocb,
10616 &pring->txq, list) {
10617 if (iocb->vport != vport)
10618 continue;
10619 list_move_tail(&iocb->list, &completions);
10620 }
10621 list_for_each_entry_safe(iocb, next_iocb,
10622 &pring->txcmplq, list) {
10623 if (iocb->vport != vport)
10624 continue;
10625 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10626 }
10627 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010628 }
James Smart895427b2017-02-12 13:52:30 -080010629 } else {
10630 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10631 pring = qp->pring;
10632 if (!pring)
10633 continue;
10634 if (pring == phba->sli4_hba.els_wq->pring) {
10635 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10636 /* Set the lpfc data pending flag */
10637 set_bit(LPFC_DATA_READY, &phba->data_flags);
10638 }
10639 prev_pring_flag = pring->flag;
10640 spin_lock_irq(&pring->ring_lock);
10641 list_for_each_entry_safe(iocb, next_iocb,
10642 &pring->txq, list) {
10643 if (iocb->vport != vport)
10644 continue;
10645 list_move_tail(&iocb->list, &completions);
10646 }
10647 spin_unlock_irq(&pring->ring_lock);
10648 list_for_each_entry_safe(iocb, next_iocb,
10649 &pring->txcmplq, list) {
10650 if (iocb->vport != vport)
10651 continue;
10652 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10653 }
10654 pring->flag = prev_pring_flag;
10655 }
James Smart92d7f7b2007-06-17 19:56:38 -050010656 }
James Smart92d7f7b2007-06-17 19:56:38 -050010657 spin_unlock_irqrestore(&phba->hbalock, flags);
10658
James Smarta257bf92009-04-06 18:48:10 -040010659 /* Cancel all the IOCBs from the completions list */
10660 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10661 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010662 return 1;
10663}
10664
James Smarte59058c2008-08-24 21:49:00 -040010665/**
James Smart3621a712009-04-06 18:47:14 -040010666 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010667 * @phba: Pointer to HBA context object.
10668 *
10669 * This function cleans up all iocb, buffers, mailbox commands
10670 * while shutting down the HBA. This function is called with no
10671 * lock held and always returns 1.
10672 * This function does the following to cleanup driver resources:
10673 * - Free discovery resources for each virtual port
10674 * - Cleanup any pending fabric iocbs
10675 * - Iterate through the iocb txq and free each entry
10676 * in the list.
10677 * - Free up any buffer posted to the HBA
10678 * - Free mailbox commands in the mailbox queue.
10679 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010680int
James Smart2e0fef82007-06-17 19:56:36 -050010681lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010682{
James Smart2534ba72007-04-25 09:52:20 -040010683 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010684 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010685 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010686 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010687 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010688 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010689 int i;
10690
10691 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010692 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010693
dea31012005-04-17 16:05:31 -050010694 lpfc_hba_down_prep(phba);
10695
James Smart523128e2018-09-10 10:30:46 -070010696 /* Disable softirqs, including timers from obtaining phba->hbalock */
10697 local_bh_disable();
10698
James Smart92d7f7b2007-06-17 19:56:38 -050010699 lpfc_fabric_abort_hba(phba);
10700
James Smart2e0fef82007-06-17 19:56:36 -050010701 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010702
James Smart895427b2017-02-12 13:52:30 -080010703 /*
10704 * Error everything on the txq since these iocbs
10705 * have not been given to the FW yet.
10706 */
10707 if (phba->sli_rev != LPFC_SLI_REV4) {
10708 for (i = 0; i < psli->num_rings; i++) {
10709 pring = &psli->sli3_ring[i];
10710 /* Only slow rings */
10711 if (pring->ringno == LPFC_ELS_RING) {
10712 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10713 /* Set the lpfc data pending flag */
10714 set_bit(LPFC_DATA_READY, &phba->data_flags);
10715 }
10716 list_splice_init(&pring->txq, &completions);
10717 }
10718 } else {
10719 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10720 pring = qp->pring;
10721 if (!pring)
10722 continue;
10723 spin_lock_irq(&pring->ring_lock);
10724 list_splice_init(&pring->txq, &completions);
10725 spin_unlock_irq(&pring->ring_lock);
10726 if (pring == phba->sli4_hba.els_wq->pring) {
10727 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10728 /* Set the lpfc data pending flag */
10729 set_bit(LPFC_DATA_READY, &phba->data_flags);
10730 }
10731 }
dea31012005-04-17 16:05:31 -050010732 }
James Smart2e0fef82007-06-17 19:56:36 -050010733 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010734
James Smarta257bf92009-04-06 18:48:10 -040010735 /* Cancel all the IOCBs from the completions list */
10736 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10737 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010738
James Smart0ff10d42008-01-11 01:52:36 -050010739 spin_lock_irqsave(&phba->hbalock, flags);
10740 list_splice_init(&phba->elsbuf, &completions);
10741 phba->elsbuf_cnt = 0;
10742 phba->elsbuf_prev_cnt = 0;
10743 spin_unlock_irqrestore(&phba->hbalock, flags);
10744
10745 while (!list_empty(&completions)) {
10746 list_remove_head(&completions, buf_ptr,
10747 struct lpfc_dmabuf, list);
10748 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10749 kfree(buf_ptr);
10750 }
10751
James Smart523128e2018-09-10 10:30:46 -070010752 /* Enable softirqs again, done with phba->hbalock */
10753 local_bh_enable();
10754
dea31012005-04-17 16:05:31 -050010755 /* Return any active mbox cmds */
10756 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010757
James Smartda0436e2009-05-22 14:51:39 -040010758 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010759 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010760 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010761
James Smartda0436e2009-05-22 14:51:39 -040010762 return 1;
10763}
James Smart92d7f7b2007-06-17 19:56:38 -050010764
James Smartda0436e2009-05-22 14:51:39 -040010765/**
James Smart3621a712009-04-06 18:47:14 -040010766 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010767 * @srcp: Source memory pointer.
10768 * @destp: Destination memory pointer.
10769 * @cnt: Number of words required to be copied.
10770 *
10771 * This function is used for copying data between driver memory
10772 * and the SLI memory. This function also changes the endianness
10773 * of each word if native endianness is different from SLI
10774 * endianness. This function can be called with or without
10775 * lock.
10776 **/
dea31012005-04-17 16:05:31 -050010777void
10778lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10779{
10780 uint32_t *src = srcp;
10781 uint32_t *dest = destp;
10782 uint32_t ldata;
10783 int i;
10784
10785 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10786 ldata = *src;
10787 ldata = le32_to_cpu(ldata);
10788 *dest = ldata;
10789 src++;
10790 dest++;
10791 }
10792}
10793
James Smarte59058c2008-08-24 21:49:00 -040010794
10795/**
James Smarta0c87cb2009-07-19 10:01:10 -040010796 * lpfc_sli_bemem_bcopy - SLI memory copy function
10797 * @srcp: Source memory pointer.
10798 * @destp: Destination memory pointer.
10799 * @cnt: Number of words required to be copied.
10800 *
10801 * This function is used for copying data between a data structure
10802 * with big endian representation to local endianness.
10803 * This function can be called with or without lock.
10804 **/
10805void
10806lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10807{
10808 uint32_t *src = srcp;
10809 uint32_t *dest = destp;
10810 uint32_t ldata;
10811 int i;
10812
10813 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10814 ldata = *src;
10815 ldata = be32_to_cpu(ldata);
10816 *dest = ldata;
10817 src++;
10818 dest++;
10819 }
10820}
10821
10822/**
James Smart3621a712009-04-06 18:47:14 -040010823 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010824 * @phba: Pointer to HBA context object.
10825 * @pring: Pointer to driver SLI ring object.
10826 * @mp: Pointer to driver buffer object.
10827 *
10828 * This function is called with no lock held.
10829 * It always return zero after adding the buffer to the postbufq
10830 * buffer list.
10831 **/
dea31012005-04-17 16:05:31 -050010832int
James Smart2e0fef82007-06-17 19:56:36 -050010833lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10834 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010835{
10836 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10837 later */
James Smart2e0fef82007-06-17 19:56:36 -050010838 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010839 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010840 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010841 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010842 return 0;
10843}
10844
James Smarte59058c2008-08-24 21:49:00 -040010845/**
James Smart3621a712009-04-06 18:47:14 -040010846 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010847 * @phba: Pointer to HBA context object.
10848 *
10849 * When HBQ is enabled, buffers are searched based on tags. This function
10850 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10851 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10852 * does not conflict with tags of buffer posted for unsolicited events.
10853 * The function returns the allocated tag. The function is called with
10854 * no locks held.
10855 **/
James Smart76bb24e2007-10-27 13:38:00 -040010856uint32_t
10857lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10858{
10859 spin_lock_irq(&phba->hbalock);
10860 phba->buffer_tag_count++;
10861 /*
10862 * Always set the QUE_BUFTAG_BIT to distiguish between
10863 * a tag assigned by HBQ.
10864 */
10865 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10866 spin_unlock_irq(&phba->hbalock);
10867 return phba->buffer_tag_count;
10868}
10869
James Smarte59058c2008-08-24 21:49:00 -040010870/**
James Smart3621a712009-04-06 18:47:14 -040010871 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010872 * @phba: Pointer to HBA context object.
10873 * @pring: Pointer to driver SLI ring object.
10874 * @tag: Buffer tag.
10875 *
10876 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10877 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10878 * iocb is posted to the response ring with the tag of the buffer.
10879 * This function searches the pring->postbufq list using the tag
10880 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10881 * iocb. If the buffer is found then lpfc_dmabuf object of the
10882 * buffer is returned to the caller else NULL is returned.
10883 * This function is called with no lock held.
10884 **/
James Smart76bb24e2007-10-27 13:38:00 -040010885struct lpfc_dmabuf *
10886lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10887 uint32_t tag)
10888{
10889 struct lpfc_dmabuf *mp, *next_mp;
10890 struct list_head *slp = &pring->postbufq;
10891
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010892 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010893 spin_lock_irq(&phba->hbalock);
10894 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10895 if (mp->buffer_tag == tag) {
10896 list_del_init(&mp->list);
10897 pring->postbufq_cnt--;
10898 spin_unlock_irq(&phba->hbalock);
10899 return mp;
10900 }
10901 }
10902
10903 spin_unlock_irq(&phba->hbalock);
10904 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010905 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010906 "ring %d Data x%lx x%p x%p x%x\n",
10907 pring->ringno, (unsigned long) tag,
10908 slp->next, slp->prev, pring->postbufq_cnt);
10909
10910 return NULL;
10911}
dea31012005-04-17 16:05:31 -050010912
James Smarte59058c2008-08-24 21:49:00 -040010913/**
James Smart3621a712009-04-06 18:47:14 -040010914 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010915 * @phba: Pointer to HBA context object.
10916 * @pring: Pointer to driver SLI ring object.
10917 * @phys: DMA address of the buffer.
10918 *
10919 * This function searches the buffer list using the dma_address
10920 * of unsolicited event to find the driver's lpfc_dmabuf object
10921 * corresponding to the dma_address. The function returns the
10922 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10923 * This function is called by the ct and els unsolicited event
10924 * handlers to get the buffer associated with the unsolicited
10925 * event.
10926 *
10927 * This function is called with no lock held.
10928 **/
dea31012005-04-17 16:05:31 -050010929struct lpfc_dmabuf *
10930lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10931 dma_addr_t phys)
10932{
10933 struct lpfc_dmabuf *mp, *next_mp;
10934 struct list_head *slp = &pring->postbufq;
10935
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010936 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010937 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010938 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10939 if (mp->phys == phys) {
10940 list_del_init(&mp->list);
10941 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010942 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010943 return mp;
10944 }
10945 }
10946
James Smart2e0fef82007-06-17 19:56:36 -050010947 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010949 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010950 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010951 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010952 slp->next, slp->prev, pring->postbufq_cnt);
10953 return NULL;
10954}
10955
James Smarte59058c2008-08-24 21:49:00 -040010956/**
James Smart3621a712009-04-06 18:47:14 -040010957 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010958 * @phba: Pointer to HBA context object.
10959 * @cmdiocb: Pointer to driver command iocb object.
10960 * @rspiocb: Pointer to driver response iocb object.
10961 *
10962 * This function is the completion handler for the abort iocbs for
10963 * ELS commands. This function is called from the ELS ring event
10964 * handler with no lock held. This function frees memory resources
10965 * associated with the abort iocb.
10966 **/
dea31012005-04-17 16:05:31 -050010967static void
James Smart2e0fef82007-06-17 19:56:36 -050010968lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10969 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010970{
James Smart2e0fef82007-06-17 19:56:36 -050010971 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010972 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010973 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010974
10975 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010976
10977 /*
10978 * Assume that the port already completed and returned, or
10979 * will return the iocb. Just Log the message.
10980 */
James Smart2680eea2007-04-25 09:52:55 -040010981 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10982 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10983
James Smart2e0fef82007-06-17 19:56:36 -050010984 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010985 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smartfaa832e2018-07-31 17:23:18 -070010986 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
10987 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
10988 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
10989 spin_unlock_irq(&phba->hbalock);
10990 goto release_iocb;
10991 }
James Smart45ed1192009-10-02 15:17:02 -040010992 if (abort_iotag != 0 &&
10993 abort_iotag <= phba->sli.last_iotag)
10994 abort_iocb =
10995 phba->sli.iocbq_lookup[abort_iotag];
10996 } else
10997 /* For sli4 the abort_tag is the XRI,
10998 * so the abort routine puts the iotag of the iocb
10999 * being aborted in the context field of the abort
11000 * IOCB.
11001 */
11002 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040011003
James Smart2a9bf3d2010-06-07 15:24:45 -040011004 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
11005 "0327 Cannot abort els iocb %p "
11006 "with tag %x context %x, abort status %x, "
11007 "abort code %x\n",
11008 abort_iocb, abort_iotag, abort_context,
11009 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040011010
James Smartff78d8f2011-12-13 13:21:35 -050011011 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040011012 }
James Smartfaa832e2018-07-31 17:23:18 -070011013release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050011014 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050011015 return;
11016}
11017
James Smarte59058c2008-08-24 21:49:00 -040011018/**
James Smart3621a712009-04-06 18:47:14 -040011019 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040011020 * @phba: Pointer to HBA context object.
11021 * @cmdiocb: Pointer to driver command iocb object.
11022 * @rspiocb: Pointer to driver response iocb object.
11023 *
11024 * The function is called from SLI ring event handler with no
11025 * lock held. This function is the completion handler for ELS commands
11026 * which are aborted. The function frees memory resources used for
11027 * the aborted ELS commands.
11028 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011029static void
11030lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11031 struct lpfc_iocbq *rspiocb)
11032{
11033 IOCB_t *irsp = &rspiocb->iocb;
11034
11035 /* ELS cmd tag <ulpIoTag> completes */
11036 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040011037 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050011038 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011039 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050011040 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050011041 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11042 lpfc_ct_free_iocb(phba, cmdiocb);
11043 else
11044 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050011045 return;
11046}
11047
James Smarte59058c2008-08-24 21:49:00 -040011048/**
James Smart5af5eee2010-10-22 11:06:38 -040011049 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040011050 * @phba: Pointer to HBA context object.
11051 * @pring: Pointer to driver SLI ring object.
11052 * @cmdiocb: Pointer to driver command iocb object.
11053 *
James Smart5af5eee2010-10-22 11:06:38 -040011054 * This function issues an abort iocb for the provided command iocb down to
11055 * the port. Other than the case the outstanding command iocb is an abort
11056 * request, this function issues abort out unconditionally. This function is
11057 * called with hbalock held. The function returns 0 when it fails due to
11058 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040011059 **/
James Smart5af5eee2010-10-22 11:06:38 -040011060static int
11061lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050011062 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050011063{
James Smart2e0fef82007-06-17 19:56:36 -050011064 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011065 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050011066 IOCB_t *icmd = NULL;
11067 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011068 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040011069 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070011070 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040011071
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011072 lockdep_assert_held(&phba->hbalock);
11073
James Smart92d7f7b2007-06-17 19:56:38 -050011074 /*
11075 * There are certain command types we don't want to abort. And we
11076 * don't want to abort commands that are already in the process of
11077 * being aborted.
James Smart07951072007-04-25 09:51:38 -040011078 */
11079 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050011080 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050011081 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11082 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040011083 return 0;
11084
dea31012005-04-17 16:05:31 -050011085 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050011086 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011087 if (abtsiocbp == NULL)
11088 return 0;
dea31012005-04-17 16:05:31 -050011089
James Smart07951072007-04-25 09:51:38 -040011090 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050011091 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040011092 */
11093 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11094
dea31012005-04-17 16:05:31 -050011095 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040011096 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11097 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040011098 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040011099 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040011100 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070011101 } else {
James Smartda0436e2009-05-22 14:51:39 -040011102 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070011103 if (pring->ringno == LPFC_ELS_RING) {
11104 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11105 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11106 }
11107 }
dea31012005-04-17 16:05:31 -050011108 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040011109 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050011110
James Smart5ffc2662009-11-18 15:39:44 -050011111 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011112 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011113 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
11114 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011115 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11116 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011117
James Smart2e0fef82007-06-17 19:56:36 -050011118 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040011119 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11120 else
11121 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11122
11123 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080011124 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040011125
James Smarte8b62012007-08-02 11:10:09 -040011126 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11127 "0339 Abort xri x%x, original iotag x%x, "
11128 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040011129 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040011130 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040011131 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040011132
11133 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011134 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11135 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040011136 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040011137 /* Note: both hbalock and ring_lock need to be set here */
11138 spin_lock_irqsave(&pring->ring_lock, iflags);
11139 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11140 abtsiocbp, 0);
11141 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11142 } else {
11143 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11144 abtsiocbp, 0);
11145 }
James Smart07951072007-04-25 09:51:38 -040011146
James Smartd7c255b2008-08-24 21:50:00 -040011147 if (retval)
11148 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040011149
11150 /*
11151 * Caller to this routine should check for IOCB_ERROR
11152 * and handle it properly. This routine no longer removes
11153 * iocb off txcmplq and call compl in case of IOCB_ERROR.
11154 */
11155 return retval;
11156}
11157
11158/**
11159 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11160 * @phba: Pointer to HBA context object.
11161 * @pring: Pointer to driver SLI ring object.
11162 * @cmdiocb: Pointer to driver command iocb object.
11163 *
11164 * This function issues an abort iocb for the provided command iocb. In case
11165 * of unloading, the abort iocb will not be issued to commands on the ELS
11166 * ring. Instead, the callback function shall be changed to those commands
11167 * so that nothing happens when them finishes. This function is called with
11168 * hbalock held. The function returns 0 when the command iocb is an abort
11169 * request.
11170 **/
11171int
11172lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11173 struct lpfc_iocbq *cmdiocb)
11174{
11175 struct lpfc_vport *vport = cmdiocb->vport;
11176 int retval = IOCB_ERROR;
11177 IOCB_t *icmd = NULL;
11178
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011179 lockdep_assert_held(&phba->hbalock);
11180
James Smart5af5eee2010-10-22 11:06:38 -040011181 /*
11182 * There are certain command types we don't want to abort. And we
11183 * don't want to abort commands that are already in the process of
11184 * being aborted.
11185 */
11186 icmd = &cmdiocb->iocb;
11187 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11188 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11189 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11190 return 0;
11191
Dick Kennedy1234a6d2017-09-29 17:34:29 -070011192 if (!pring) {
11193 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11194 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11195 else
11196 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11197 goto abort_iotag_exit;
11198 }
11199
James Smart5af5eee2010-10-22 11:06:38 -040011200 /*
11201 * If we're unloading, don't abort iocb on the ELS ring, but change
11202 * the callback so that nothing happens when it finishes.
11203 */
11204 if ((vport->load_flag & FC_UNLOADING) &&
11205 (pring->ringno == LPFC_ELS_RING)) {
11206 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11207 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11208 else
11209 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11210 goto abort_iotag_exit;
11211 }
11212
11213 /* Now, we try to issue the abort to the cmdiocb out */
11214 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
11215
James Smart07951072007-04-25 09:51:38 -040011216abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050011217 /*
11218 * Caller to this routine should check for IOCB_ERROR
11219 * and handle it properly. This routine no longer removes
11220 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040011221 */
James Smart2e0fef82007-06-17 19:56:36 -050011222 return retval;
dea31012005-04-17 16:05:31 -050011223}
11224
James Smarte59058c2008-08-24 21:49:00 -040011225/**
James Smart895427b2017-02-12 13:52:30 -080011226 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
11227 * @phba: Pointer to HBA context object.
11228 * @pring: Pointer to driver SLI ring object.
11229 * @cmdiocb: Pointer to driver command iocb object.
11230 *
11231 * This function issues an abort iocb for the provided command iocb down to
11232 * the port. Other than the case the outstanding command iocb is an abort
11233 * request, this function issues abort out unconditionally. This function is
11234 * called with hbalock held. The function returns 0 when it fails due to
11235 * memory allocation failure or when the command iocb is an abort request.
11236 **/
11237static int
11238lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11239 struct lpfc_iocbq *cmdiocb)
11240{
11241 struct lpfc_vport *vport = cmdiocb->vport;
11242 struct lpfc_iocbq *abtsiocbp;
James Smart205e8242018-03-05 12:04:03 -080011243 union lpfc_wqe128 *abts_wqe;
James Smart895427b2017-02-12 13:52:30 -080011244 int retval;
11245
11246 /*
11247 * There are certain command types we don't want to abort. And we
11248 * don't want to abort commands that are already in the process of
11249 * being aborted.
11250 */
11251 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
11252 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
11253 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11254 return 0;
11255
11256 /* issue ABTS for this io based on iotag */
11257 abtsiocbp = __lpfc_sli_get_iocbq(phba);
11258 if (abtsiocbp == NULL)
11259 return 0;
11260
11261 /* This signals the response to set the correct status
11262 * before calling the completion handler
11263 */
11264 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11265
11266 /* Complete prepping the abort wqe and issue to the FW. */
11267 abts_wqe = &abtsiocbp->wqe;
11268 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
11269 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
11270
11271 /* Explicitly set reserved fields to zero.*/
11272 abts_wqe->abort_cmd.rsrvd4 = 0;
11273 abts_wqe->abort_cmd.rsrvd5 = 0;
11274
11275 /* WQE Common - word 6. Context is XRI tag. Set 0. */
11276 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
11277 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
11278
11279 /* word 7 */
11280 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
11281 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
11282 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
11283 cmdiocb->iocb.ulpClass);
11284
11285 /* word 8 - tell the FW to abort the IO associated with this
11286 * outstanding exchange ID.
11287 */
11288 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
11289
11290 /* word 9 - this is the iotag for the abts_wqe completion. */
11291 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
11292 abtsiocbp->iotag);
11293
11294 /* word 10 */
11295 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
11296 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
11297 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
11298
11299 /* word 11 */
11300 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11301 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
11302 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
11303
11304 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11305 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
11306 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080011307 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080011308 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070011309 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080011310 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11311 "6147 Failed abts issue_wqe with status x%x "
11312 "for oxid x%x\n",
11313 retval, cmdiocb->sli4_xritag);
11314 lpfc_sli_release_iocbq(phba, abtsiocbp);
11315 return retval;
11316 }
11317
11318 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11319 "6148 Drv Abort NVME Request Issued for "
11320 "ox_id x%x on reqtag x%x\n",
11321 cmdiocb->sli4_xritag,
11322 abtsiocbp->iotag);
11323
11324 return retval;
11325}
11326
11327/**
James Smart5af5eee2010-10-22 11:06:38 -040011328 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11329 * @phba: pointer to lpfc HBA data structure.
11330 *
11331 * This routine will abort all pending and outstanding iocbs to an HBA.
11332 **/
11333void
11334lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11335{
11336 struct lpfc_sli *psli = &phba->sli;
11337 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011338 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011339 int i;
11340
James Smart895427b2017-02-12 13:52:30 -080011341 if (phba->sli_rev != LPFC_SLI_REV4) {
11342 for (i = 0; i < psli->num_rings; i++) {
11343 pring = &psli->sli3_ring[i];
11344 lpfc_sli_abort_iocb_ring(phba, pring);
11345 }
11346 return;
11347 }
11348 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11349 pring = qp->pring;
11350 if (!pring)
11351 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011352 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011353 }
11354}
11355
11356/**
James Smart3621a712009-04-06 18:47:14 -040011357 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011358 * @iocbq: Pointer to driver iocb object.
11359 * @vport: Pointer to driver virtual port object.
11360 * @tgt_id: SCSI ID of the target.
11361 * @lun_id: LUN ID of the scsi device.
11362 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11363 *
James Smart3621a712009-04-06 18:47:14 -040011364 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011365 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11366 * 0 if the filtering criteria is met for the given iocb and will return
11367 * 1 if the filtering criteria is not met.
11368 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11369 * given iocb is for the SCSI device specified by vport, tgt_id and
11370 * lun_id parameter.
11371 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11372 * given iocb is for the SCSI target specified by vport and tgt_id
11373 * parameters.
11374 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11375 * given iocb is for the SCSI host associated with the given vport.
11376 * This function is called with no locks held.
11377 **/
dea31012005-04-17 16:05:31 -050011378static int
James Smart51ef4c22007-08-02 11:10:31 -040011379lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11380 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011381 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011382{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011383 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011384 int rc = 1;
11385
James Smartb0e83012018-06-26 08:24:29 -070011386 if (iocbq->vport != vport)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011387 return rc;
11388
James Smartb0e83012018-06-26 08:24:29 -070011389 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
11390 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
James Smart51ef4c22007-08-02 11:10:31 -040011391 return rc;
11392
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011393 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011394
James Smart495a7142008-06-14 22:52:59 -040011395 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011396 return rc;
11397
11398 switch (ctx_cmd) {
11399 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070011400 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011401 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11402 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011403 rc = 0;
11404 break;
11405 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070011406 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011407 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011408 rc = 0;
11409 break;
dea31012005-04-17 16:05:31 -050011410 case LPFC_CTX_HOST:
11411 rc = 0;
11412 break;
11413 default:
11414 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011415 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011416 break;
11417 }
11418
11419 return rc;
11420}
11421
James Smarte59058c2008-08-24 21:49:00 -040011422/**
James Smart3621a712009-04-06 18:47:14 -040011423 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011424 * @vport: Pointer to virtual port.
11425 * @tgt_id: SCSI ID of the target.
11426 * @lun_id: LUN ID of the scsi device.
11427 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11428 *
11429 * This function returns number of FCP commands pending for the vport.
11430 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11431 * commands pending on the vport associated with SCSI device specified
11432 * by tgt_id and lun_id parameters.
11433 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11434 * commands pending on the vport associated with SCSI target specified
11435 * by tgt_id parameter.
11436 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11437 * commands pending on the vport.
11438 * This function returns the number of iocbs which satisfy the filter.
11439 * This function is called without any lock held.
11440 **/
dea31012005-04-17 16:05:31 -050011441int
James Smart51ef4c22007-08-02 11:10:31 -040011442lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11443 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011444{
James Smart51ef4c22007-08-02 11:10:31 -040011445 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011446 struct lpfc_iocbq *iocbq;
11447 int sum, i;
dea31012005-04-17 16:05:31 -050011448
Johannes Thumshirn31979002016-07-18 16:06:03 +020011449 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011450 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11451 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011452
James Smart51ef4c22007-08-02 11:10:31 -040011453 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11454 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011455 sum++;
dea31012005-04-17 16:05:31 -050011456 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011457 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011458
dea31012005-04-17 16:05:31 -050011459 return sum;
11460}
11461
James Smarte59058c2008-08-24 21:49:00 -040011462/**
James Smart3621a712009-04-06 18:47:14 -040011463 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011464 * @phba: Pointer to HBA context object
11465 * @cmdiocb: Pointer to command iocb object.
11466 * @rspiocb: Pointer to response iocb object.
11467 *
11468 * This function is called when an aborted FCP iocb completes. This
11469 * function is called by the ring event handler with no lock held.
11470 * This function frees the iocb.
11471 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011472void
James Smart2e0fef82007-06-17 19:56:36 -050011473lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11474 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011475{
James Smartcb69f7d2011-12-13 13:21:57 -050011476 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011477 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011478 "original iotag x%x, abort cmd iotag x%x "
11479 "status 0x%x, reason 0x%x\n",
11480 cmdiocb->iocb.un.acxri.abortContextTag,
11481 cmdiocb->iocb.un.acxri.abortIoTag,
11482 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11483 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011484 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011485 return;
11486}
11487
James Smarte59058c2008-08-24 21:49:00 -040011488/**
James Smart3621a712009-04-06 18:47:14 -040011489 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011490 * @vport: Pointer to virtual port.
11491 * @pring: Pointer to driver SLI ring object.
11492 * @tgt_id: SCSI ID of the target.
11493 * @lun_id: LUN ID of the scsi device.
11494 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11495 *
11496 * This function sends an abort command for every SCSI command
11497 * associated with the given virtual port pending on the ring
11498 * filtered by lpfc_sli_validate_fcp_iocb function.
11499 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11500 * FCP iocbs associated with lun specified by tgt_id and lun_id
11501 * parameters
11502 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11503 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11504 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11505 * FCP iocbs associated with virtual port.
11506 * This function returns number of iocbs it failed to abort.
11507 * This function is called with no locks held.
11508 **/
dea31012005-04-17 16:05:31 -050011509int
James Smart51ef4c22007-08-02 11:10:31 -040011510lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11511 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011512{
James Smart51ef4c22007-08-02 11:10:31 -040011513 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011514 struct lpfc_iocbq *iocbq;
11515 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011516 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011517 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011518 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011519 int i;
dea31012005-04-17 16:05:31 -050011520
James Smartb0e83012018-06-26 08:24:29 -070011521 /* all I/Os are in process of being flushed */
11522 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH)
11523 return errcnt;
11524
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011525 for (i = 1; i <= phba->sli.last_iotag; i++) {
11526 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011527
James Smart51ef4c22007-08-02 11:10:31 -040011528 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011529 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011530 continue;
11531
James Smartafbd8d82013-09-06 12:22:13 -040011532 /*
11533 * If the iocbq is already being aborted, don't take a second
11534 * action, but do count it.
11535 */
11536 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11537 continue;
11538
dea31012005-04-17 16:05:31 -050011539 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011540 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011541 if (abtsiocb == NULL) {
11542 errcnt++;
11543 continue;
11544 }
dea31012005-04-17 16:05:31 -050011545
James Smartafbd8d82013-09-06 12:22:13 -040011546 /* indicate the IO is being aborted by the driver. */
11547 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11548
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011549 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011550 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11551 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011552 if (phba->sli_rev == LPFC_SLI_REV4)
11553 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11554 else
11555 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011556 abtsiocb->iocb.ulpLe = 1;
11557 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011558 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011559
James Smart5ffc2662009-11-18 15:39:44 -050011560 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011561 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011562 if (iocbq->iocb_flag & LPFC_IO_FCP)
11563 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011564 if (iocbq->iocb_flag & LPFC_IO_FOF)
11565 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011566
James Smart2e0fef82007-06-17 19:56:36 -050011567 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011568 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11569 else
11570 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11571
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011572 /* Setup callback routine and issue the command. */
11573 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011574 if (phba->sli_rev == LPFC_SLI_REV4) {
11575 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11576 if (!pring_s4)
11577 continue;
11578 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11579 abtsiocb, 0);
11580 } else
11581 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11582 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011583 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011584 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011585 errcnt++;
11586 continue;
11587 }
11588 }
11589
11590 return errcnt;
11591}
11592
James Smarte59058c2008-08-24 21:49:00 -040011593/**
James Smart98912dda2014-04-04 13:52:31 -040011594 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11595 * @vport: Pointer to virtual port.
11596 * @pring: Pointer to driver SLI ring object.
11597 * @tgt_id: SCSI ID of the target.
11598 * @lun_id: LUN ID of the scsi device.
11599 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11600 *
11601 * This function sends an abort command for every SCSI command
11602 * associated with the given virtual port pending on the ring
11603 * filtered by lpfc_sli_validate_fcp_iocb function.
11604 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11605 * FCP iocbs associated with lun specified by tgt_id and lun_id
11606 * parameters
11607 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11608 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11609 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11610 * FCP iocbs associated with virtual port.
11611 * This function returns number of iocbs it aborted .
11612 * This function is called with no locks held right after a taskmgmt
11613 * command is sent.
11614 **/
11615int
11616lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11617 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11618{
11619 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011620 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011621 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011622 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011623 struct lpfc_iocbq *iocbq;
11624 IOCB_t *icmd;
11625 int sum, i, ret_val;
11626 unsigned long iflags;
11627 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011628
James Smart59c68ea2018-04-09 14:24:25 -070011629 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011630
11631 /* all I/Os are in process of being flushed */
11632 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070011633 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011634 return 0;
11635 }
11636 sum = 0;
11637
11638 for (i = 1; i <= phba->sli.last_iotag; i++) {
11639 iocbq = phba->sli.iocbq_lookup[i];
11640
11641 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11642 cmd) != 0)
11643 continue;
11644
11645 /*
11646 * If the iocbq is already being aborted, don't take a second
11647 * action, but do count it.
11648 */
11649 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11650 continue;
11651
11652 /* issue ABTS for this IOCB based on iotag */
11653 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11654 if (abtsiocbq == NULL)
11655 continue;
11656
11657 icmd = &iocbq->iocb;
11658 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11659 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11660 if (phba->sli_rev == LPFC_SLI_REV4)
11661 abtsiocbq->iocb.un.acxri.abortIoTag =
11662 iocbq->sli4_xritag;
11663 else
11664 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11665 abtsiocbq->iocb.ulpLe = 1;
11666 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11667 abtsiocbq->vport = vport;
11668
11669 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011670 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011671 if (iocbq->iocb_flag & LPFC_IO_FCP)
11672 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011673 if (iocbq->iocb_flag & LPFC_IO_FOF)
11674 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011675
James Smart8c50d252014-09-03 12:58:16 -040011676 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11677 ndlp = lpfc_cmd->rdata->pnode;
11678
11679 if (lpfc_is_link_up(phba) &&
11680 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011681 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11682 else
11683 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11684
11685 /* Setup callback routine and issue the command. */
11686 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11687
11688 /*
11689 * Indicate the IO is being aborted by the driver and set
11690 * the caller's flag into the aborted IO.
11691 */
11692 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11693
11694 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart59c68ea2018-04-09 14:24:25 -070011695 pring_s4 = lpfc_sli4_calc_ring(phba, abtsiocbq);
11696 if (!pring_s4)
James Smart895427b2017-02-12 13:52:30 -080011697 continue;
James Smart98912dda2014-04-04 13:52:31 -040011698 /* Note: both hbalock and ring_lock must be set here */
James Smart59c68ea2018-04-09 14:24:25 -070011699 spin_lock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011700 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11701 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070011702 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011703 } else {
11704 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11705 abtsiocbq, 0);
11706 }
11707
11708
11709 if (ret_val == IOCB_ERROR)
11710 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11711 else
11712 sum++;
11713 }
James Smart59c68ea2018-04-09 14:24:25 -070011714 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011715 return sum;
11716}
11717
11718/**
James Smart3621a712009-04-06 18:47:14 -040011719 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011720 * @phba: Pointer to HBA context object.
11721 * @cmdiocbq: Pointer to command iocb.
11722 * @rspiocbq: Pointer to response iocb.
11723 *
11724 * This function is the completion handler for iocbs issued using
11725 * lpfc_sli_issue_iocb_wait function. This function is called by the
11726 * ring event handler function without any lock held. This function
11727 * can be called from both worker thread context and interrupt
11728 * context. This function also can be called from other thread which
11729 * cleans up the SLI layer objects.
11730 * This function copy the contents of the response iocb to the
11731 * response iocb memory object provided by the caller of
11732 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11733 * sleeps for the iocb completion.
11734 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011735static void
11736lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11737 struct lpfc_iocbq *cmdiocbq,
11738 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011739{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011740 wait_queue_head_t *pdone_q;
11741 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011742 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011743
James Smart2e0fef82007-06-17 19:56:36 -050011744 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011745 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11746
11747 /*
11748 * A time out has occurred for the iocb. If a time out
11749 * completion handler has been supplied, call it. Otherwise,
11750 * just free the iocbq.
11751 */
11752
11753 spin_unlock_irqrestore(&phba->hbalock, iflags);
11754 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11755 cmdiocbq->wait_iocb_cmpl = NULL;
11756 if (cmdiocbq->iocb_cmpl)
11757 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11758 else
11759 lpfc_sli_release_iocbq(phba, cmdiocbq);
11760 return;
11761 }
11762
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011763 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11764 if (cmdiocbq->context2 && rspiocbq)
11765 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11766 &rspiocbq->iocb, sizeof(IOCB_t));
11767
James Smart0f65ff62010-02-26 14:14:23 -050011768 /* Set the exchange busy flag for task management commands */
11769 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11770 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11771 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11772 cur_iocbq);
11773 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11774 }
11775
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011776 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011777 if (pdone_q)
11778 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011779 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011780 return;
11781}
11782
James Smarte59058c2008-08-24 21:49:00 -040011783/**
James Smartd11e31d2009-06-10 17:23:06 -040011784 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11785 * @phba: Pointer to HBA context object..
11786 * @piocbq: Pointer to command iocb.
11787 * @flag: Flag to test.
11788 *
11789 * This routine grabs the hbalock and then test the iocb_flag to
11790 * see if the passed in flag is set.
11791 * Returns:
11792 * 1 if flag is set.
11793 * 0 if flag is not set.
11794 **/
11795static int
11796lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11797 struct lpfc_iocbq *piocbq, uint32_t flag)
11798{
11799 unsigned long iflags;
11800 int ret;
11801
11802 spin_lock_irqsave(&phba->hbalock, iflags);
11803 ret = piocbq->iocb_flag & flag;
11804 spin_unlock_irqrestore(&phba->hbalock, iflags);
11805 return ret;
11806
11807}
11808
11809/**
James Smart3621a712009-04-06 18:47:14 -040011810 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011811 * @phba: Pointer to HBA context object..
11812 * @pring: Pointer to sli ring.
11813 * @piocb: Pointer to command iocb.
11814 * @prspiocbq: Pointer to response iocb.
11815 * @timeout: Timeout in number of seconds.
11816 *
11817 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011818 * iocb to complete. The iocb_cmpl field of the shall be used
11819 * to handle iocbs which time out. If the field is NULL, the
11820 * function shall free the iocbq structure. If more clean up is
11821 * needed, the caller is expected to provide a completion function
11822 * that will provide the needed clean up. If the iocb command is
11823 * not completed within timeout seconds, the function will either
11824 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11825 * completion function set in the iocb_cmpl field and then return
11826 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11827 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011828 * The function waits for the iocb completion using an
11829 * non-interruptible wait.
11830 * This function will sleep while waiting for iocb completion.
11831 * So, this function should not be called from any context which
11832 * does not allow sleeping. Due to the same reason, this function
11833 * cannot be called with interrupt disabled.
11834 * This function assumes that the iocb completions occur while
11835 * this function sleep. So, this function cannot be called from
11836 * the thread which process iocb completion for this ring.
11837 * This function clears the iocb_flag of the iocb object before
11838 * issuing the iocb and the iocb completion handler sets this
11839 * flag and wakes this thread when the iocb completes.
11840 * The contents of the response iocb will be copied to prspiocbq
11841 * by the completion handler when the command completes.
11842 * This function returns IOCB_SUCCESS when success.
11843 * This function is called with no lock held.
11844 **/
dea31012005-04-17 16:05:31 -050011845int
James Smart2e0fef82007-06-17 19:56:36 -050011846lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011847 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011848 struct lpfc_iocbq *piocb,
11849 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011850 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011851{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011852 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011853 long timeleft, timeout_req = 0;
11854 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011855 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011856 struct lpfc_iocbq *iocb;
11857 int txq_cnt = 0;
11858 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011859 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011860 unsigned long iflags;
11861 bool iocb_completed = true;
11862
James Smart895427b2017-02-12 13:52:30 -080011863 if (phba->sli_rev >= LPFC_SLI_REV4)
11864 pring = lpfc_sli4_calc_ring(phba, piocb);
11865 else
11866 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011867 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011868 * If the caller has provided a response iocbq buffer, then context2
11869 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011870 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011871 if (prspiocbq) {
11872 if (piocb->context2)
11873 return IOCB_ERROR;
11874 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011875 }
11876
James Smart5a0916b2013-07-15 18:31:42 -040011877 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011878 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11879 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011880 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011881
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011882 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011883 if (lpfc_readl(phba->HCregaddr, &creg_val))
11884 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011885 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11886 writel(creg_val, phba->HCregaddr);
11887 readl(phba->HCregaddr); /* flush */
11888 }
11889
James Smart2a9bf3d2010-06-07 15:24:45 -040011890 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11891 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011892 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011893 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011894 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011895 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011896 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011897 spin_lock_irqsave(&phba->hbalock, iflags);
11898 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011899
James Smart5a0916b2013-07-15 18:31:42 -040011900 /*
11901 * IOCB timed out. Inform the wake iocb wait
11902 * completion function and set local status
11903 */
11904
11905 iocb_completed = false;
11906 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11907 }
11908 spin_unlock_irqrestore(&phba->hbalock, iflags);
11909 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011910 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011911 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011912 /* Note: we are not indicating if the IOCB has a success
11913 * status or not - that's for the caller to check.
11914 * IOCB_SUCCESS means just that the command was sent and
11915 * completed. Not that it completed successfully.
11916 * */
James Smart7054a602007-04-25 09:52:34 -040011917 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011918 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011919 "0338 IOCB wait timeout error - no "
11920 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011921 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011922 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011923 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011924 "0330 IOCB wake NOT set, "
11925 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011926 timeout, (timeleft / jiffies));
11927 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011928 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011929 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011930 if (phba->cfg_log_verbose & LOG_SLI) {
11931 list_for_each_entry(iocb, &pring->txq, list) {
11932 txq_cnt++;
11933 }
11934 list_for_each_entry(iocb, &pring->txcmplq, list) {
11935 txcmplq_cnt++;
11936 }
11937 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11938 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11939 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11940 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011941 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011942 } else {
11943 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011944 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011945 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011946 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011947 }
11948
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011949 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011950 if (lpfc_readl(phba->HCregaddr, &creg_val))
11951 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011952 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11953 writel(creg_val, phba->HCregaddr);
11954 readl(phba->HCregaddr); /* flush */
11955 }
11956
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011957 if (prspiocbq)
11958 piocb->context2 = NULL;
11959
11960 piocb->context_un.wait_queue = NULL;
11961 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011962 return retval;
11963}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011964
James Smarte59058c2008-08-24 21:49:00 -040011965/**
James Smart3621a712009-04-06 18:47:14 -040011966 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011967 * @phba: Pointer to HBA context object.
11968 * @pmboxq: Pointer to driver mailbox object.
11969 * @timeout: Timeout in number of seconds.
11970 *
11971 * This function issues the mailbox to firmware and waits for the
11972 * mailbox command to complete. If the mailbox command is not
11973 * completed within timeout seconds, it returns MBX_TIMEOUT.
11974 * The function waits for the mailbox completion using an
11975 * interruptible wait. If the thread is woken up due to a
11976 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11977 * should not free the mailbox resources, if this function returns
11978 * MBX_TIMEOUT.
11979 * This function will sleep while waiting for mailbox completion.
11980 * So, this function should not be called from any context which
11981 * does not allow sleeping. Due to the same reason, this function
11982 * cannot be called with interrupt disabled.
11983 * This function assumes that the mailbox completion occurs while
11984 * this function sleep. So, this function cannot be called from
11985 * the worker thread which processes mailbox completion.
11986 * This function is called in the context of HBA management
11987 * applications.
11988 * This function returns MBX_SUCCESS when successful.
11989 * This function is called with no lock held.
11990 **/
dea31012005-04-17 16:05:31 -050011991int
James Smart2e0fef82007-06-17 19:56:36 -050011992lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011993 uint32_t timeout)
11994{
James Smarte29d74f2018-03-05 12:04:07 -080011995 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050011996 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011997 unsigned long flag;
dea31012005-04-17 16:05:31 -050011998
James Smart495a7142008-06-14 22:52:59 -040011999 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050012000 /* setup wake call as IOCB callback */
12001 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050012002
James Smarte29d74f2018-03-05 12:04:07 -080012003 /* setup context3 field to pass wait_queue pointer to wake function */
12004 init_completion(&mbox_done);
12005 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050012006 /* now issue the command */
12007 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050012008 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080012009 wait_for_completion_timeout(&mbox_done,
12010 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040012011
James Smart858c9f62007-06-17 19:56:39 -050012012 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080012013 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040012014 /*
12015 * if LPFC_MBX_WAKE flag is set the mailbox is completed
12016 * else do not free the resources.
12017 */
James Smartd7c47992010-06-08 18:31:54 -040012018 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050012019 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040012020 } else {
James Smart7054a602007-04-25 09:52:34 -040012021 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050012022 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12023 }
12024 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050012025 }
dea31012005-04-17 16:05:31 -050012026 return retval;
12027}
12028
James Smarte59058c2008-08-24 21:49:00 -040012029/**
James Smart3772a992009-05-22 14:50:54 -040012030 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040012031 * @phba: Pointer to HBA context.
12032 *
James Smart3772a992009-05-22 14:50:54 -040012033 * This function is called to shutdown the driver's mailbox sub-system.
12034 * It first marks the mailbox sub-system is in a block state to prevent
12035 * the asynchronous mailbox command from issued off the pending mailbox
12036 * command queue. If the mailbox command sub-system shutdown is due to
12037 * HBA error conditions such as EEH or ERATT, this routine shall invoke
12038 * the mailbox sub-system flush routine to forcefully bring down the
12039 * mailbox sub-system. Otherwise, if it is due to normal condition (such
12040 * as with offline or HBA function reset), this routine will wait for the
12041 * outstanding mailbox command to complete before invoking the mailbox
12042 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040012043 **/
James Smart3772a992009-05-22 14:50:54 -040012044void
James Smart618a5232012-06-12 13:54:36 -040012045lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040012046{
James Smart3772a992009-05-22 14:50:54 -040012047 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040012048 unsigned long timeout;
12049
James Smart618a5232012-06-12 13:54:36 -040012050 if (mbx_action == LPFC_MBX_NO_WAIT) {
12051 /* delay 100ms for port state */
12052 msleep(100);
12053 lpfc_sli_mbox_sys_flush(phba);
12054 return;
12055 }
James Smarta183a152011-10-10 21:32:43 -040012056 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050012057
James Smart523128e2018-09-10 10:30:46 -070012058 /* Disable softirqs, including timers from obtaining phba->hbalock */
12059 local_bh_disable();
12060
James Smart3772a992009-05-22 14:50:54 -040012061 spin_lock_irq(&phba->hbalock);
12062 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040012063
12064 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040012065 /* Determine how long we might wait for the active mailbox
12066 * command to be gracefully completed by firmware.
12067 */
James Smarta183a152011-10-10 21:32:43 -040012068 if (phba->sli.mbox_active)
12069 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12070 phba->sli.mbox_active) *
12071 1000) + jiffies;
12072 spin_unlock_irq(&phba->hbalock);
12073
James Smart523128e2018-09-10 10:30:46 -070012074 /* Enable softirqs again, done with phba->hbalock */
12075 local_bh_enable();
12076
James Smart3772a992009-05-22 14:50:54 -040012077 while (phba->sli.mbox_active) {
12078 /* Check active mailbox complete status every 2ms */
12079 msleep(2);
12080 if (time_after(jiffies, timeout))
12081 /* Timeout, let the mailbox flush routine to
12082 * forcefully release active mailbox command
12083 */
12084 break;
12085 }
James Smart523128e2018-09-10 10:30:46 -070012086 } else {
James Smartd7069f02012-03-01 22:36:29 -050012087 spin_unlock_irq(&phba->hbalock);
12088
James Smart523128e2018-09-10 10:30:46 -070012089 /* Enable softirqs again, done with phba->hbalock */
12090 local_bh_enable();
12091 }
12092
James Smart3772a992009-05-22 14:50:54 -040012093 lpfc_sli_mbox_sys_flush(phba);
12094}
12095
12096/**
12097 * lpfc_sli_eratt_read - read sli-3 error attention events
12098 * @phba: Pointer to HBA context.
12099 *
12100 * This function is called to read the SLI3 device error attention registers
12101 * for possible error attention events. The caller must hold the hostlock
12102 * with spin_lock_irq().
12103 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012104 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040012105 * Register and returns 0 otherwise.
12106 **/
12107static int
12108lpfc_sli_eratt_read(struct lpfc_hba *phba)
12109{
James Smarted957682007-06-17 19:56:37 -050012110 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040012111
James Smart3772a992009-05-22 14:50:54 -040012112 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050012113 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12114 goto unplug_err;
12115
James Smart3772a992009-05-22 14:50:54 -040012116 if (ha_copy & HA_ERATT) {
12117 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050012118 if (lpfc_sli_read_hs(phba))
12119 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040012120
James Smart3772a992009-05-22 14:50:54 -040012121 /* Check if there is a deferred error condition is active */
12122 if ((HS_FFER1 & phba->work_hs) &&
12123 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012124 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040012125 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040012126 /* Clear all interrupt enable conditions */
12127 writel(0, phba->HCregaddr);
12128 readl(phba->HCregaddr);
12129 }
12130
12131 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040012132 phba->work_ha |= HA_ERATT;
12133 /* Indicate polling handles this ERATT */
12134 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012135 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012136 }
James Smart3772a992009-05-22 14:50:54 -040012137 return 0;
James Smart9940b972011-03-11 16:06:12 -050012138
12139unplug_err:
12140 /* Set the driver HS work bitmap */
12141 phba->work_hs |= UNPLUG_ERR;
12142 /* Set the driver HA work bitmap */
12143 phba->work_ha |= HA_ERATT;
12144 /* Indicate polling handles this ERATT */
12145 phba->hba_flag |= HBA_ERATT_HANDLED;
12146 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012147}
12148
James Smarte59058c2008-08-24 21:49:00 -040012149/**
James Smartda0436e2009-05-22 14:51:39 -040012150 * lpfc_sli4_eratt_read - read sli-4 error attention events
12151 * @phba: Pointer to HBA context.
12152 *
12153 * This function is called to read the SLI4 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 Smartda0436e2009-05-22 14:51:39 -040012158 * Register and returns 0 otherwise.
12159 **/
12160static int
12161lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12162{
12163 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050012164 uint32_t if_type, portsmphr;
12165 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040012166
James Smart2fcee4b2010-12-15 17:57:46 -050012167 /*
12168 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040012169 * registers for error attention. This can be changed later.
12170 */
James Smart2fcee4b2010-12-15 17:57:46 -050012171 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12172 switch (if_type) {
12173 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050012174 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12175 &uerr_sta_lo) ||
12176 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12177 &uerr_sta_hi)) {
12178 phba->work_hs |= UNPLUG_ERR;
12179 phba->work_ha |= HA_ERATT;
12180 phba->hba_flag |= HBA_ERATT_HANDLED;
12181 return 1;
12182 }
James Smart2fcee4b2010-12-15 17:57:46 -050012183 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12184 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12186 "1423 HBA Unrecoverable error: "
12187 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12188 "ue_mask_lo_reg=0x%x, "
12189 "ue_mask_hi_reg=0x%x\n",
12190 uerr_sta_lo, uerr_sta_hi,
12191 phba->sli4_hba.ue_mask_lo,
12192 phba->sli4_hba.ue_mask_hi);
12193 phba->work_status[0] = uerr_sta_lo;
12194 phba->work_status[1] = uerr_sta_hi;
12195 phba->work_ha |= HA_ERATT;
12196 phba->hba_flag |= HBA_ERATT_HANDLED;
12197 return 1;
12198 }
12199 break;
12200 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080012201 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050012202 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12203 &portstat_reg.word0) ||
12204 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12205 &portsmphr)){
12206 phba->work_hs |= UNPLUG_ERR;
12207 phba->work_ha |= HA_ERATT;
12208 phba->hba_flag |= HBA_ERATT_HANDLED;
12209 return 1;
12210 }
James Smart2fcee4b2010-12-15 17:57:46 -050012211 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12212 phba->work_status[0] =
12213 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12214 phba->work_status[1] =
12215 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12216 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050012217 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050012218 "port status reg 0x%x, "
12219 "port smphr reg 0x%x, "
12220 "error 1=0x%x, error 2=0x%x\n",
12221 portstat_reg.word0,
12222 portsmphr,
12223 phba->work_status[0],
12224 phba->work_status[1]);
12225 phba->work_ha |= HA_ERATT;
12226 phba->hba_flag |= HBA_ERATT_HANDLED;
12227 return 1;
12228 }
12229 break;
12230 case LPFC_SLI_INTF_IF_TYPE_1:
12231 default:
James Smarta747c9c2009-11-18 15:41:10 -050012232 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050012233 "2886 HBA Error Attention on unsupported "
12234 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050012235 return 1;
James Smartda0436e2009-05-22 14:51:39 -040012236 }
James Smart2fcee4b2010-12-15 17:57:46 -050012237
James Smartda0436e2009-05-22 14:51:39 -040012238 return 0;
12239}
12240
12241/**
James Smart3621a712009-04-06 18:47:14 -040012242 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040012243 * @phba: Pointer to HBA context.
12244 *
James Smart3772a992009-05-22 14:50:54 -040012245 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040012246 * error attention register bit for error attention events.
12247 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012248 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040012249 * Register and returns 0 otherwise.
12250 **/
12251int
12252lpfc_sli_check_eratt(struct lpfc_hba *phba)
12253{
12254 uint32_t ha_copy;
12255
12256 /* If somebody is waiting to handle an eratt, don't process it
12257 * here. The brdkill function will do this.
12258 */
12259 if (phba->link_flag & LS_IGNORE_ERATT)
12260 return 0;
12261
12262 /* Check if interrupt handler handles this ERATT */
12263 spin_lock_irq(&phba->hbalock);
12264 if (phba->hba_flag & HBA_ERATT_HANDLED) {
12265 /* Interrupt handler has handled ERATT */
12266 spin_unlock_irq(&phba->hbalock);
12267 return 0;
12268 }
12269
James Smarta257bf92009-04-06 18:48:10 -040012270 /*
12271 * If there is deferred error attention, do not check for error
12272 * attention
12273 */
12274 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12275 spin_unlock_irq(&phba->hbalock);
12276 return 0;
12277 }
12278
James Smart3772a992009-05-22 14:50:54 -040012279 /* If PCI channel is offline, don't process it */
12280 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040012281 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012282 return 0;
12283 }
12284
12285 switch (phba->sli_rev) {
12286 case LPFC_SLI_REV2:
12287 case LPFC_SLI_REV3:
12288 /* Read chip Host Attention (HA) register */
12289 ha_copy = lpfc_sli_eratt_read(phba);
12290 break;
James Smartda0436e2009-05-22 14:51:39 -040012291 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050012292 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040012293 ha_copy = lpfc_sli4_eratt_read(phba);
12294 break;
James Smart3772a992009-05-22 14:50:54 -040012295 default:
12296 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12297 "0299 Invalid SLI revision (%d)\n",
12298 phba->sli_rev);
12299 ha_copy = 0;
12300 break;
James Smart93996272008-08-24 21:50:30 -040012301 }
12302 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012303
12304 return ha_copy;
12305}
12306
12307/**
12308 * lpfc_intr_state_check - Check device state for interrupt handling
12309 * @phba: Pointer to HBA context.
12310 *
12311 * This inline routine checks whether a device or its PCI slot is in a state
12312 * that the interrupt should be handled.
12313 *
12314 * This function returns 0 if the device or the PCI slot is in a state that
12315 * interrupt should be handled, otherwise -EIO.
12316 */
12317static inline int
12318lpfc_intr_state_check(struct lpfc_hba *phba)
12319{
12320 /* If the pci channel is offline, ignore all the interrupts */
12321 if (unlikely(pci_channel_offline(phba->pcidev)))
12322 return -EIO;
12323
12324 /* Update device level interrupt statistics */
12325 phba->sli.slistat.sli_intr++;
12326
12327 /* Ignore all interrupts during initialization. */
12328 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12329 return -EIO;
12330
James Smart93996272008-08-24 21:50:30 -040012331 return 0;
12332}
12333
12334/**
James Smart3772a992009-05-22 14:50:54 -040012335 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012336 * @irq: Interrupt number.
12337 * @dev_id: The device context pointer.
12338 *
James Smart93996272008-08-24 21:50:30 -040012339 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012340 * service routine when device with SLI-3 interface spec is enabled with
12341 * MSI-X multi-message interrupt mode and there are slow-path events in
12342 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12343 * interrupt mode, this function is called as part of the device-level
12344 * interrupt handler. When the PCI slot is in error recovery or the HBA
12345 * is undergoing initialization, the interrupt handler will not process
12346 * the interrupt. The link attention and ELS ring attention events are
12347 * handled by the worker thread. The interrupt handler signals the worker
12348 * thread and returns for these events. This function is called without
12349 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012350 * structures.
12351 *
12352 * This function returns IRQ_HANDLED when interrupt is handled else it
12353 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012354 **/
dea31012005-04-17 16:05:31 -050012355irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012356lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012357{
James Smart2e0fef82007-06-17 19:56:36 -050012358 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012359 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012360 uint32_t work_ha_copy;
12361 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012362 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012363 uint32_t control;
12364
James Smart92d7f7b2007-06-17 19:56:38 -050012365 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012366 struct lpfc_vport *vport;
12367 struct lpfc_nodelist *ndlp;
12368 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012369 LPFC_MBOXQ_t *pmb;
12370 int rc;
12371
dea31012005-04-17 16:05:31 -050012372 /*
12373 * Get the driver's phba structure from the dev_id and
12374 * assume the HBA is not interrupting.
12375 */
James Smart93996272008-08-24 21:50:30 -040012376 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012377
12378 if (unlikely(!phba))
12379 return IRQ_NONE;
12380
dea31012005-04-17 16:05:31 -050012381 /*
James Smart93996272008-08-24 21:50:30 -040012382 * Stuff needs to be attented to when this function is invoked as an
12383 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012384 */
James Smart93996272008-08-24 21:50:30 -040012385 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012386 /* Check device state for handling interrupt */
12387 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012388 return IRQ_NONE;
12389 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012390 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012391 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12392 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012393 /* If somebody is waiting to handle an eratt don't process it
12394 * here. The brdkill function will do this.
12395 */
12396 if (phba->link_flag & LS_IGNORE_ERATT)
12397 ha_copy &= ~HA_ERATT;
12398 /* Check the need for handling ERATT in interrupt handler */
12399 if (ha_copy & HA_ERATT) {
12400 if (phba->hba_flag & HBA_ERATT_HANDLED)
12401 /* ERATT polling has handled ERATT */
12402 ha_copy &= ~HA_ERATT;
12403 else
12404 /* Indicate interrupt handler handles ERATT */
12405 phba->hba_flag |= HBA_ERATT_HANDLED;
12406 }
James Smarta257bf92009-04-06 18:48:10 -040012407
12408 /*
12409 * If there is deferred error attention, do not check for any
12410 * interrupt.
12411 */
12412 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012413 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012414 return IRQ_NONE;
12415 }
12416
James Smart93996272008-08-24 21:50:30 -040012417 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012418 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12419 goto unplug_error;
12420
James Smarta747c9c2009-11-18 15:41:10 -050012421 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12422 HC_LAINT_ENA | HC_ERINT_ENA),
12423 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012424 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12425 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012426 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012427 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012428 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012429 } else
12430 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012431
dea31012005-04-17 16:05:31 -050012432 work_ha_copy = ha_copy & phba->work_ha_mask;
12433
James Smart93996272008-08-24 21:50:30 -040012434 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012435 if (work_ha_copy & HA_LATT) {
12436 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12437 /*
12438 * Turn off Link Attention interrupts
12439 * until CLEAR_LA done
12440 */
James Smart5b75da22008-12-04 22:39:35 -050012441 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012442 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012443 if (lpfc_readl(phba->HCregaddr, &control))
12444 goto unplug_error;
dea31012005-04-17 16:05:31 -050012445 control &= ~HC_LAINT_ENA;
12446 writel(control, phba->HCregaddr);
12447 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012448 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012449 }
12450 else
12451 work_ha_copy &= ~HA_LATT;
12452 }
12453
James Smart93996272008-08-24 21:50:30 -040012454 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012455 /*
12456 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12457 * the only slow ring.
12458 */
12459 status = (work_ha_copy &
12460 (HA_RXMASK << (4*LPFC_ELS_RING)));
12461 status >>= (4*LPFC_ELS_RING);
12462 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012463 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012464 if (lpfc_readl(phba->HCregaddr, &control))
12465 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012466
12467 lpfc_debugfs_slow_ring_trc(phba,
12468 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12469 control, status,
12470 (uint32_t)phba->sli.slistat.sli_intr);
12471
James Smart858c9f62007-06-17 19:56:39 -050012472 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012473 lpfc_debugfs_slow_ring_trc(phba,
12474 "ISR Disable ring:"
12475 "pwork:x%x hawork:x%x wait:x%x",
12476 phba->work_ha, work_ha_copy,
12477 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012478 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012479
James Smart858c9f62007-06-17 19:56:39 -050012480 control &=
12481 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012482 writel(control, phba->HCregaddr);
12483 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012484 }
James Smarta58cbd52007-08-02 11:09:43 -040012485 else {
12486 lpfc_debugfs_slow_ring_trc(phba,
12487 "ISR slow ring: pwork:"
12488 "x%x hawork:x%x wait:x%x",
12489 phba->work_ha, work_ha_copy,
12490 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012491 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012492 }
James Smart5b75da22008-12-04 22:39:35 -050012493 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012494 }
12495 }
James Smart5b75da22008-12-04 22:39:35 -050012496 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012497 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012498 if (lpfc_sli_read_hs(phba))
12499 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012500 /*
12501 * Check if there is a deferred error condition
12502 * is active
12503 */
12504 if ((HS_FFER1 & phba->work_hs) &&
12505 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012506 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12507 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012508 phba->hba_flag |= DEFER_ERATT;
12509 /* Clear all interrupt enable conditions */
12510 writel(0, phba->HCregaddr);
12511 readl(phba->HCregaddr);
12512 }
12513 }
12514
James Smart93996272008-08-24 21:50:30 -040012515 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012516 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012517 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012518 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012519 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012520
12521 /* First check out the status word */
12522 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12523 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012524 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012525 /*
12526 * Stray Mailbox Interrupt, mbxCommand <cmd>
12527 * mbxStatus <status>
12528 */
James Smart09372822008-01-11 01:52:54 -050012529 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012530 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012531 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012532 "Interrupt mbxCommand x%x "
12533 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012534 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012535 pmbox->mbxCommand,
12536 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012537 /* clear mailbox attention bit */
12538 work_ha_copy &= ~HA_MBATT;
12539 } else {
James Smart97eab632008-04-07 10:16:05 -040012540 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012541 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012542 phba->last_completion_time = jiffies;
12543 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012544 if (pmb->mbox_cmpl) {
12545 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12546 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012547 if (pmb->out_ext_byte_len &&
12548 pmb->context2)
12549 lpfc_sli_pcimem_bcopy(
12550 phba->mbox_ext,
12551 pmb->context2,
12552 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012553 }
James Smart09372822008-01-11 01:52:54 -050012554 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12555 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12556
12557 lpfc_debugfs_disc_trc(vport,
12558 LPFC_DISC_TRC_MBOX_VPORT,
12559 "MBOX dflt rpi: : "
12560 "status:x%x rpi:x%x",
12561 (uint32_t)pmbox->mbxStatus,
12562 pmbox->un.varWords[0], 0);
12563
12564 if (!pmbox->mbxStatus) {
12565 mp = (struct lpfc_dmabuf *)
12566 (pmb->context1);
12567 ndlp = (struct lpfc_nodelist *)
12568 pmb->context2;
12569
12570 /* Reg_LOGIN of dflt RPI was
12571 * successful. new lets get
12572 * rid of the RPI using the
12573 * same mbox buffer.
12574 */
12575 lpfc_unreg_login(phba,
12576 vport->vpi,
12577 pmbox->un.varWords[0],
12578 pmb);
12579 pmb->mbox_cmpl =
12580 lpfc_mbx_cmpl_dflt_rpi;
12581 pmb->context1 = mp;
12582 pmb->context2 = ndlp;
12583 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012584 rc = lpfc_sli_issue_mbox(phba,
12585 pmb,
12586 MBX_NOWAIT);
12587 if (rc != MBX_BUSY)
12588 lpfc_printf_log(phba,
12589 KERN_ERR,
12590 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012591 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012592 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012593 if (rc != MBX_NOT_FINISHED)
12594 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012595 }
12596 }
James Smart5b75da22008-12-04 22:39:35 -050012597 spin_lock_irqsave(
12598 &phba->pport->work_port_lock,
12599 iflag);
James Smart09372822008-01-11 01:52:54 -050012600 phba->pport->work_port_events &=
12601 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012602 spin_unlock_irqrestore(
12603 &phba->pport->work_port_lock,
12604 iflag);
James Smart09372822008-01-11 01:52:54 -050012605 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012606 }
James Smart97eab632008-04-07 10:16:05 -040012607 } else
James Smart5b75da22008-12-04 22:39:35 -050012608 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012609
James Smart92d7f7b2007-06-17 19:56:38 -050012610 if ((work_ha_copy & HA_MBATT) &&
12611 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012612send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012613 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012614 do {
12615 rc = lpfc_sli_issue_mbox(phba, NULL,
12616 MBX_NOWAIT);
12617 } while (rc == MBX_NOT_FINISHED);
12618 if (rc != MBX_SUCCESS)
12619 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12620 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012621 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012622 }
12623
James Smart5b75da22008-12-04 22:39:35 -050012624 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012625 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012626 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012627 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012628 }
James Smart93996272008-08-24 21:50:30 -040012629 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012630unplug_error:
12631 spin_unlock_irqrestore(&phba->hbalock, iflag);
12632 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012633
James Smart3772a992009-05-22 14:50:54 -040012634} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012635
12636/**
James Smart3772a992009-05-22 14:50:54 -040012637 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012638 * @irq: Interrupt number.
12639 * @dev_id: The device context pointer.
12640 *
12641 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012642 * service routine when device with SLI-3 interface spec is enabled with
12643 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12644 * ring event in the HBA. However, when the device is enabled with either
12645 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12646 * device-level interrupt handler. When the PCI slot is in error recovery
12647 * or the HBA is undergoing initialization, the interrupt handler will not
12648 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12649 * the intrrupt context. This function is called without any lock held.
12650 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012651 *
12652 * This function returns IRQ_HANDLED when interrupt is handled else it
12653 * returns IRQ_NONE.
12654 **/
12655irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012656lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012657{
12658 struct lpfc_hba *phba;
12659 uint32_t ha_copy;
12660 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012661 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012662 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012663
12664 /* Get the driver's phba structure from the dev_id and
12665 * assume the HBA is not interrupting.
12666 */
12667 phba = (struct lpfc_hba *) dev_id;
12668
12669 if (unlikely(!phba))
12670 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012671
12672 /*
James Smart93996272008-08-24 21:50:30 -040012673 * Stuff needs to be attented to when this function is invoked as an
12674 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012675 */
James Smart93996272008-08-24 21:50:30 -040012676 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012677 /* Check device state for handling interrupt */
12678 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012679 return IRQ_NONE;
12680 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012681 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12682 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012683 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012684 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012685 /*
12686 * If there is deferred error attention, do not check for
12687 * any interrupt.
12688 */
12689 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012690 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012691 return IRQ_NONE;
12692 }
James Smart93996272008-08-24 21:50:30 -040012693 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12694 phba->HAregaddr);
12695 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012696 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012697 } else
12698 ha_copy = phba->ha_copy;
12699
12700 /*
12701 * Process all events on FCP ring. Take the optimized path for FCP IO.
12702 */
12703 ha_copy &= ~(phba->work_ha_mask);
12704
12705 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012706 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012707 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012708 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012709 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012710
12711 if (phba->cfg_multi_ring_support == 2) {
12712 /*
James Smart93996272008-08-24 21:50:30 -040012713 * Process all events on extra ring. Take the optimized path
12714 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012715 */
James Smart93996272008-08-24 21:50:30 -040012716 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012717 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012718 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012719 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012720 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012721 status);
12722 }
12723 }
dea31012005-04-17 16:05:31 -050012724 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012725} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012726
James Smart93996272008-08-24 21:50:30 -040012727/**
James Smart3772a992009-05-22 14:50:54 -040012728 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012729 * @irq: Interrupt number.
12730 * @dev_id: The device context pointer.
12731 *
James Smart3772a992009-05-22 14:50:54 -040012732 * This function is the HBA device-level interrupt handler to device with
12733 * SLI-3 interface spec, called from the PCI layer when either MSI or
12734 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12735 * requires driver attention. This function invokes the slow-path interrupt
12736 * attention handling function and fast-path interrupt attention handling
12737 * function in turn to process the relevant HBA attention events. This
12738 * function is called without any lock held. It gets the hbalock to access
12739 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012740 *
12741 * This function returns IRQ_HANDLED when interrupt is handled, else it
12742 * returns IRQ_NONE.
12743 **/
12744irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012745lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012746{
12747 struct lpfc_hba *phba;
12748 irqreturn_t sp_irq_rc, fp_irq_rc;
12749 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012750 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012751
12752 /*
12753 * Get the driver's phba structure from the dev_id and
12754 * assume the HBA is not interrupting.
12755 */
12756 phba = (struct lpfc_hba *) dev_id;
12757
12758 if (unlikely(!phba))
12759 return IRQ_NONE;
12760
James Smart3772a992009-05-22 14:50:54 -040012761 /* Check device state for handling interrupt */
12762 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012763 return IRQ_NONE;
12764
12765 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012766 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12767 spin_unlock(&phba->hbalock);
12768 return IRQ_HANDLED;
12769 }
12770
James Smart93996272008-08-24 21:50:30 -040012771 if (unlikely(!phba->ha_copy)) {
12772 spin_unlock(&phba->hbalock);
12773 return IRQ_NONE;
12774 } else if (phba->ha_copy & HA_ERATT) {
12775 if (phba->hba_flag & HBA_ERATT_HANDLED)
12776 /* ERATT polling has handled ERATT */
12777 phba->ha_copy &= ~HA_ERATT;
12778 else
12779 /* Indicate interrupt handler handles ERATT */
12780 phba->hba_flag |= HBA_ERATT_HANDLED;
12781 }
12782
James Smarta257bf92009-04-06 18:48:10 -040012783 /*
12784 * If there is deferred error attention, do not check for any interrupt.
12785 */
12786 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012787 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012788 return IRQ_NONE;
12789 }
12790
James Smart93996272008-08-24 21:50:30 -040012791 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012792 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12793 spin_unlock(&phba->hbalock);
12794 return IRQ_HANDLED;
12795 }
James Smarta747c9c2009-11-18 15:41:10 -050012796 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12797 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12798 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012799 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012800 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012801 readl(phba->HAregaddr); /* flush */
12802 spin_unlock(&phba->hbalock);
12803
12804 /*
12805 * Invokes slow-path host attention interrupt handling as appropriate.
12806 */
12807
12808 /* status of events with mailbox and link attention */
12809 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12810
12811 /* status of events with ELS ring */
12812 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12813 status2 >>= (4*LPFC_ELS_RING);
12814
12815 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012816 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012817 else
12818 sp_irq_rc = IRQ_NONE;
12819
12820 /*
12821 * Invoke fast-path host attention interrupt handling as appropriate.
12822 */
12823
12824 /* status of events with FCP ring */
12825 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12826 status1 >>= (4*LPFC_FCP_RING);
12827
12828 /* status of events with extra ring */
12829 if (phba->cfg_multi_ring_support == 2) {
12830 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12831 status2 >>= (4*LPFC_EXTRA_RING);
12832 } else
12833 status2 = 0;
12834
12835 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012836 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012837 else
12838 fp_irq_rc = IRQ_NONE;
12839
12840 /* Return device-level interrupt handling status */
12841 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012842} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012843
12844/**
12845 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12846 * @phba: pointer to lpfc hba data structure.
12847 *
12848 * This routine is invoked by the worker thread to process all the pending
12849 * SLI4 FCP abort XRI events.
12850 **/
12851void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12852{
12853 struct lpfc_cq_event *cq_event;
12854
12855 /* First, declare the fcp xri abort event has been handled */
12856 spin_lock_irq(&phba->hbalock);
12857 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12858 spin_unlock_irq(&phba->hbalock);
12859 /* Now, handle all the fcp xri abort events */
12860 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12861 /* Get the first event from the head of the event queue */
12862 spin_lock_irq(&phba->hbalock);
12863 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12864 cq_event, struct lpfc_cq_event, list);
12865 spin_unlock_irq(&phba->hbalock);
12866 /* Notify aborted XRI for FCP work queue */
12867 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12868 /* Free the event processed back to the free pool */
12869 lpfc_sli4_cq_event_release(phba, cq_event);
12870 }
12871}
12872
12873/**
12874 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12875 * @phba: pointer to lpfc hba data structure.
12876 *
12877 * This routine is invoked by the worker thread to process all the pending
12878 * SLI4 els abort xri events.
12879 **/
12880void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12881{
12882 struct lpfc_cq_event *cq_event;
12883
12884 /* First, declare the els xri abort event has been handled */
12885 spin_lock_irq(&phba->hbalock);
12886 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12887 spin_unlock_irq(&phba->hbalock);
12888 /* Now, handle all the els xri abort events */
12889 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12890 /* Get the first event from the head of the event queue */
12891 spin_lock_irq(&phba->hbalock);
12892 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12893 cq_event, struct lpfc_cq_event, list);
12894 spin_unlock_irq(&phba->hbalock);
12895 /* Notify aborted XRI for ELS work queue */
12896 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12897 /* Free the event processed back to the free pool */
12898 lpfc_sli4_cq_event_release(phba, cq_event);
12899 }
12900}
12901
James Smart341af102010-01-26 23:07:37 -050012902/**
12903 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12904 * @phba: pointer to lpfc hba data structure
12905 * @pIocbIn: pointer to the rspiocbq
12906 * @pIocbOut: pointer to the cmdiocbq
12907 * @wcqe: pointer to the complete wcqe
12908 *
12909 * This routine transfers the fields of a command iocbq to a response iocbq
12910 * by copying all the IOCB fields from command iocbq and transferring the
12911 * completion status information from the complete wcqe.
12912 **/
James Smart4f774512009-05-22 14:52:35 -040012913static void
James Smart341af102010-01-26 23:07:37 -050012914lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12915 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012916 struct lpfc_iocbq *pIocbOut,
12917 struct lpfc_wcqe_complete *wcqe)
12918{
James Smartaf227412013-10-10 12:23:10 -040012919 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012920 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012921 uint32_t status, max_response;
12922 struct lpfc_dmabuf *dmabuf;
12923 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012924 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12925
12926 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12927 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012928 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012929 status = bf_get(lpfc_wcqe_c_status, wcqe);
12930 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012931 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12932 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12933 pIocbIn->iocb.un.fcpi.fcpi_parm =
12934 pIocbOut->iocb.un.fcpi.fcpi_parm -
12935 wcqe->total_data_placed;
12936 else
12937 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012938 else {
James Smart4f774512009-05-22 14:52:35 -040012939 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012940 switch (pIocbOut->iocb.ulpCommand) {
12941 case CMD_ELS_REQUEST64_CR:
12942 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12943 bpl = (struct ulp_bde64 *)dmabuf->virt;
12944 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12945 max_response = bde.tus.f.bdeSize;
12946 break;
12947 case CMD_GEN_REQUEST64_CR:
12948 max_response = 0;
12949 if (!pIocbOut->context3)
12950 break;
12951 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12952 sizeof(struct ulp_bde64);
12953 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12954 bpl = (struct ulp_bde64 *)dmabuf->virt;
12955 for (i = 0; i < numBdes; i++) {
12956 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12957 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12958 max_response += bde.tus.f.bdeSize;
12959 }
12960 break;
12961 default:
12962 max_response = wcqe->total_data_placed;
12963 break;
12964 }
12965 if (max_response < wcqe->total_data_placed)
12966 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12967 else
12968 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12969 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012970 }
James Smart341af102010-01-26 23:07:37 -050012971
James Smartacd68592012-01-18 16:25:09 -050012972 /* Convert BG errors for completion status */
12973 if (status == CQE_STATUS_DI_ERROR) {
12974 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12975
12976 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12977 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12978 else
12979 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12980
12981 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12982 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12983 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12984 BGS_GUARD_ERR_MASK;
12985 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12986 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12987 BGS_APPTAG_ERR_MASK;
12988 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12989 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12990 BGS_REFTAG_ERR_MASK;
12991
12992 /* Check to see if there was any good data before the error */
12993 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12994 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12995 BGS_HI_WATER_MARK_PRESENT_MASK;
12996 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12997 wcqe->total_data_placed;
12998 }
12999
13000 /*
13001 * Set ALL the error bits to indicate we don't know what
13002 * type of error it is.
13003 */
13004 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
13005 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13006 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
13007 BGS_GUARD_ERR_MASK);
13008 }
13009
James Smart341af102010-01-26 23:07:37 -050013010 /* Pick up HBA exchange busy condition */
13011 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
13012 spin_lock_irqsave(&phba->hbalock, iflags);
13013 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
13014 spin_unlock_irqrestore(&phba->hbalock, iflags);
13015 }
James Smart4f774512009-05-22 14:52:35 -040013016}
13017
13018/**
James Smart45ed1192009-10-02 15:17:02 -040013019 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
13020 * @phba: Pointer to HBA context object.
13021 * @wcqe: Pointer to work-queue completion queue entry.
13022 *
13023 * This routine handles an ELS work-queue completion event and construct
13024 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
13025 * discovery engine to handle.
13026 *
13027 * Return: Pointer to the receive IOCBQ, NULL otherwise.
13028 **/
13029static struct lpfc_iocbq *
13030lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
13031 struct lpfc_iocbq *irspiocbq)
13032{
James Smart895427b2017-02-12 13:52:30 -080013033 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040013034 struct lpfc_iocbq *cmdiocbq;
13035 struct lpfc_wcqe_complete *wcqe;
13036 unsigned long iflags;
13037
James Smart895427b2017-02-12 13:52:30 -080013038 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070013039 if (unlikely(!pring))
13040 return NULL;
James Smart895427b2017-02-12 13:52:30 -080013041
James Smart45ed1192009-10-02 15:17:02 -040013042 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040013043 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040013044 pring->stats.iocb_event++;
13045 /* Look up the ELS command IOCB and create pseudo response IOCB */
13046 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13047 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040013048 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070013049 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040013050 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13051 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070013052 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
13053 wcqe->word0, wcqe->total_data_placed,
13054 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040013055 lpfc_sli_release_iocbq(phba, irspiocbq);
13056 return NULL;
13057 }
13058
Dick Kennedy401bb412017-09-29 17:34:28 -070013059 /* Put the iocb back on the txcmplq */
13060 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13061 spin_unlock_irqrestore(&pring->ring_lock, iflags);
13062
James Smart45ed1192009-10-02 15:17:02 -040013063 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013064 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040013065
13066 return irspiocbq;
13067}
13068
James Smart8a5ca102017-11-20 16:00:30 -080013069inline struct lpfc_cq_event *
13070lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13071{
13072 struct lpfc_cq_event *cq_event;
13073
13074 /* Allocate a new internal CQ_EVENT entry */
13075 cq_event = lpfc_sli4_cq_event_alloc(phba);
13076 if (!cq_event) {
13077 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13078 "0602 Failed to alloc CQ_EVENT entry\n");
13079 return NULL;
13080 }
13081
13082 /* Move the CQE into the event */
13083 memcpy(&cq_event->cqe, entry, size);
13084 return cq_event;
13085}
13086
James Smart45ed1192009-10-02 15:17:02 -040013087/**
James Smart04c68492009-05-22 14:52:52 -040013088 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
13089 * @phba: Pointer to HBA context object.
13090 * @cqe: Pointer to mailbox completion queue entry.
13091 *
13092 * This routine process a mailbox completion queue entry with asynchrous
13093 * event.
13094 *
13095 * Return: true if work posted to worker thread, otherwise false.
13096 **/
13097static bool
13098lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13099{
13100 struct lpfc_cq_event *cq_event;
13101 unsigned long iflags;
13102
13103 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13104 "0392 Async Event: word0:x%x, word1:x%x, "
13105 "word2:x%x, word3:x%x\n", mcqe->word0,
13106 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13107
James Smart8a5ca102017-11-20 16:00:30 -080013108 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13109 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040013110 return false;
James Smart04c68492009-05-22 14:52:52 -040013111 spin_lock_irqsave(&phba->hbalock, iflags);
13112 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13113 /* Set the async event flag */
13114 phba->hba_flag |= ASYNC_EVENT;
13115 spin_unlock_irqrestore(&phba->hbalock, iflags);
13116
13117 return true;
13118}
13119
13120/**
13121 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13122 * @phba: Pointer to HBA context object.
13123 * @cqe: Pointer to mailbox completion queue entry.
13124 *
13125 * This routine process a mailbox completion queue entry with mailbox
13126 * completion event.
13127 *
13128 * Return: true if work posted to worker thread, otherwise false.
13129 **/
13130static bool
13131lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13132{
13133 uint32_t mcqe_status;
13134 MAILBOX_t *mbox, *pmbox;
13135 struct lpfc_mqe *mqe;
13136 struct lpfc_vport *vport;
13137 struct lpfc_nodelist *ndlp;
13138 struct lpfc_dmabuf *mp;
13139 unsigned long iflags;
13140 LPFC_MBOXQ_t *pmb;
13141 bool workposted = false;
13142 int rc;
13143
13144 /* If not a mailbox complete MCQE, out by checking mailbox consume */
13145 if (!bf_get(lpfc_trailer_completed, mcqe))
13146 goto out_no_mqe_complete;
13147
13148 /* Get the reference to the active mbox command */
13149 spin_lock_irqsave(&phba->hbalock, iflags);
13150 pmb = phba->sli.mbox_active;
13151 if (unlikely(!pmb)) {
13152 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13153 "1832 No pending MBOX command to handle\n");
13154 spin_unlock_irqrestore(&phba->hbalock, iflags);
13155 goto out_no_mqe_complete;
13156 }
13157 spin_unlock_irqrestore(&phba->hbalock, iflags);
13158 mqe = &pmb->u.mqe;
13159 pmbox = (MAILBOX_t *)&pmb->u.mqe;
13160 mbox = phba->mbox;
13161 vport = pmb->vport;
13162
13163 /* Reset heartbeat timer */
13164 phba->last_completion_time = jiffies;
13165 del_timer(&phba->sli.mbox_tmo);
13166
13167 /* Move mbox data to caller's mailbox region, do endian swapping */
13168 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070013169 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040013170
James Smart73d91e52011-10-10 21:32:10 -040013171 /*
13172 * For mcqe errors, conditionally move a modified error code to
13173 * the mbox so that the error will not be missed.
13174 */
13175 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13176 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13177 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13178 bf_set(lpfc_mqe_status, mqe,
13179 (LPFC_MBX_ERROR_RANGE | mcqe_status));
13180 }
James Smart04c68492009-05-22 14:52:52 -040013181 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13182 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13183 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13184 "MBOX dflt rpi: status:x%x rpi:x%x",
13185 mcqe_status,
13186 pmbox->un.varWords[0], 0);
13187 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
13188 mp = (struct lpfc_dmabuf *)(pmb->context1);
13189 ndlp = (struct lpfc_nodelist *)pmb->context2;
13190 /* Reg_LOGIN of dflt RPI was successful. Now lets get
13191 * RID of the PPI using the same mbox buffer.
13192 */
13193 lpfc_unreg_login(phba, vport->vpi,
13194 pmbox->un.varWords[0], pmb);
13195 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
13196 pmb->context1 = mp;
13197 pmb->context2 = ndlp;
13198 pmb->vport = vport;
13199 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13200 if (rc != MBX_BUSY)
13201 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
13202 LOG_SLI, "0385 rc should "
13203 "have been MBX_BUSY\n");
13204 if (rc != MBX_NOT_FINISHED)
13205 goto send_current_mbox;
13206 }
13207 }
13208 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13209 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13210 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13211
13212 /* There is mailbox completion work to do */
13213 spin_lock_irqsave(&phba->hbalock, iflags);
13214 __lpfc_mbox_cmpl_put(phba, pmb);
13215 phba->work_ha |= HA_MBATT;
13216 spin_unlock_irqrestore(&phba->hbalock, iflags);
13217 workposted = true;
13218
13219send_current_mbox:
13220 spin_lock_irqsave(&phba->hbalock, iflags);
13221 /* Release the mailbox command posting token */
13222 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13223 /* Setting active mailbox pointer need to be in sync to flag clear */
13224 phba->sli.mbox_active = NULL;
13225 spin_unlock_irqrestore(&phba->hbalock, iflags);
13226 /* Wake up worker thread to post the next pending mailbox command */
13227 lpfc_worker_wake_up(phba);
13228out_no_mqe_complete:
13229 if (bf_get(lpfc_trailer_consumed, mcqe))
13230 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13231 return workposted;
13232}
13233
13234/**
13235 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13236 * @phba: Pointer to HBA context object.
13237 * @cqe: Pointer to mailbox completion queue entry.
13238 *
13239 * This routine process a mailbox completion queue entry, it invokes the
13240 * proper mailbox complete handling or asynchrous event handling routine
13241 * according to the MCQE's async bit.
13242 *
13243 * Return: true if work posted to worker thread, otherwise false.
13244 **/
13245static bool
13246lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
13247{
13248 struct lpfc_mcqe mcqe;
13249 bool workposted;
13250
13251 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013252 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040013253
13254 /* Invoke the proper event handling routine */
13255 if (!bf_get(lpfc_trailer_async, &mcqe))
13256 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13257 else
13258 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13259 return workposted;
13260}
13261
13262/**
James Smart4f774512009-05-22 14:52:35 -040013263 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13264 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040013265 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040013266 * @wcqe: Pointer to work-queue completion queue entry.
13267 *
13268 * This routine handles an ELS work-queue completion event.
13269 *
13270 * Return: true if work posted to worker thread, otherwise false.
13271 **/
13272static bool
James Smart2a76a282012-08-03 12:35:54 -040013273lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013274 struct lpfc_wcqe_complete *wcqe)
13275{
James Smart4f774512009-05-22 14:52:35 -040013276 struct lpfc_iocbq *irspiocbq;
13277 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040013278 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050013279 int txq_cnt = 0;
13280 int txcmplq_cnt = 0;
13281 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040013282
James Smart11f0e342018-05-04 20:37:57 -070013283 /* Check for response status */
13284 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13285 /* Log the error status */
13286 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13287 "0357 ELS CQE error: status=x%x: "
13288 "CQE: %08x %08x %08x %08x\n",
13289 bf_get(lpfc_wcqe_c_status, wcqe),
13290 wcqe->word0, wcqe->total_data_placed,
13291 wcqe->parameter, wcqe->word3);
13292 }
13293
James Smart45ed1192009-10-02 15:17:02 -040013294 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040013295 irspiocbq = lpfc_sli_get_iocbq(phba);
13296 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013297 if (!list_empty(&pring->txq))
13298 txq_cnt++;
13299 if (!list_empty(&pring->txcmplq))
13300 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040013301 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040013302 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13303 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050013304 txq_cnt, phba->iocb_cnt,
13305 fcp_txcmplq_cnt,
13306 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040013307 return false;
James Smart4f774512009-05-22 14:52:35 -040013308 }
James Smart4f774512009-05-22 14:52:35 -040013309
James Smart45ed1192009-10-02 15:17:02 -040013310 /* Save off the slow-path queue event for work thread to process */
13311 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040013312 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040013313 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013314 &phba->sli4_hba.sp_queue_event);
13315 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013316 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013317
James Smart45ed1192009-10-02 15:17:02 -040013318 return true;
James Smart4f774512009-05-22 14:52:35 -040013319}
13320
13321/**
13322 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13323 * @phba: Pointer to HBA context object.
13324 * @wcqe: Pointer to work-queue completion queue entry.
13325 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013326 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013327 * proper WQ release routine to the slow-path WQ.
13328 **/
13329static void
13330lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13331 struct lpfc_wcqe_release *wcqe)
13332{
James Smart2e90f4b2011-12-13 13:22:37 -050013333 /* sanity check on queue memory */
13334 if (unlikely(!phba->sli4_hba.els_wq))
13335 return;
James Smart4f774512009-05-22 14:52:35 -040013336 /* Check for the slow-path ELS work queue */
13337 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13338 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13339 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13340 else
13341 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13342 "2579 Slow-path wqe consume event carries "
13343 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13344 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13345 phba->sli4_hba.els_wq->queue_id);
13346}
13347
13348/**
13349 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13350 * @phba: Pointer to HBA context object.
13351 * @cq: Pointer to a WQ completion queue.
13352 * @wcqe: Pointer to work-queue completion queue entry.
13353 *
13354 * This routine handles an XRI abort event.
13355 *
13356 * Return: true if work posted to worker thread, otherwise false.
13357 **/
13358static bool
13359lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13360 struct lpfc_queue *cq,
13361 struct sli4_wcqe_xri_aborted *wcqe)
13362{
13363 bool workposted = false;
13364 struct lpfc_cq_event *cq_event;
13365 unsigned long iflags;
13366
James Smart4f774512009-05-22 14:52:35 -040013367 switch (cq->subtype) {
13368 case LPFC_FCP:
James Smart8a5ca102017-11-20 16:00:30 -080013369 cq_event = lpfc_cq_event_setup(
13370 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13371 if (!cq_event)
13372 return false;
James Smart4f774512009-05-22 14:52:35 -040013373 spin_lock_irqsave(&phba->hbalock, iflags);
13374 list_add_tail(&cq_event->list,
13375 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13376 /* Set the fcp xri abort event flag */
13377 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13378 spin_unlock_irqrestore(&phba->hbalock, iflags);
13379 workposted = true;
13380 break;
James Smart422c4cb2017-11-20 16:00:32 -080013381 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013382 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013383 cq_event = lpfc_cq_event_setup(
13384 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13385 if (!cq_event)
13386 return false;
James Smart4f774512009-05-22 14:52:35 -040013387 spin_lock_irqsave(&phba->hbalock, iflags);
13388 list_add_tail(&cq_event->list,
13389 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13390 /* Set the els xri abort event flag */
13391 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13392 spin_unlock_irqrestore(&phba->hbalock, iflags);
13393 workposted = true;
13394 break;
James Smart318083a2017-03-04 09:30:30 -080013395 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080013396 /* Notify aborted XRI for NVME work queue */
13397 if (phba->nvmet_support)
13398 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13399 else
13400 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13401
13402 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080013403 break;
James Smart4f774512009-05-22 14:52:35 -040013404 default:
13405 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013406 "0603 Invalid CQ subtype %d: "
13407 "%08x %08x %08x %08x\n",
13408 cq->subtype, wcqe->word0, wcqe->parameter,
13409 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013410 workposted = false;
13411 break;
13412 }
13413 return workposted;
13414}
13415
13416/**
James Smart4d9ab992009-10-02 15:16:39 -040013417 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013418 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013419 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013420 *
James Smart4d9ab992009-10-02 15:16:39 -040013421 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013422 *
13423 * Return: true if work posted to worker thread, otherwise false.
13424 **/
13425static bool
James Smart4d9ab992009-10-02 15:16:39 -040013426lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13427{
13428 bool workposted = false;
13429 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13430 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013431 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013432 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013433 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013434 unsigned long iflags;
13435
James Smart2e90f4b2011-12-13 13:22:37 -050013436 /* sanity check on queue memory */
13437 if (unlikely(!hrq) || unlikely(!drq))
13438 return workposted;
13439
James Smart7851fe22011-07-22 18:36:52 -040013440 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13441 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13442 else
13443 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13444 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013445 goto out;
13446
13447 status = bf_get(lpfc_rcqe_status, rcqe);
13448 switch (status) {
13449 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13450 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13451 "2537 Receive Frame Truncated!!\n");
13452 case FC_STATUS_RQ_SUCCESS:
13453 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013454 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013455 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13456 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013457 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013458 spin_unlock_irqrestore(&phba->hbalock, iflags);
13459 goto out;
13460 }
James Smartb84daac2012-08-03 12:35:13 -040013461 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013462 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013463 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013464
James Smart4d9ab992009-10-02 15:16:39 -040013465 /* save off the frame for the word thread to process */
13466 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013467 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013468 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013469 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013470 spin_unlock_irqrestore(&phba->hbalock, iflags);
13471 workposted = true;
13472 break;
James Smart4d9ab992009-10-02 15:16:39 -040013473 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013474 if (phba->nvmet_support) {
13475 tgtp = phba->targetport->private;
13476 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13477 "6402 RQE Error x%x, posted %d err_cnt "
13478 "%d: %x %x %x\n",
13479 status, hrq->RQ_buf_posted,
13480 hrq->RQ_no_posted_buf,
13481 atomic_read(&tgtp->rcv_fcp_cmd_in),
13482 atomic_read(&tgtp->rcv_fcp_cmd_out),
13483 atomic_read(&tgtp->xmt_fcp_release));
13484 }
13485 /* fallthrough */
13486
13487 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013488 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013489 /* Post more buffers if possible */
13490 spin_lock_irqsave(&phba->hbalock, iflags);
13491 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13492 spin_unlock_irqrestore(&phba->hbalock, iflags);
13493 workposted = true;
13494 break;
13495 }
13496out:
13497 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013498}
13499
13500/**
13501 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13502 * @phba: Pointer to HBA context object.
13503 * @cq: Pointer to the completion queue.
13504 * @wcqe: Pointer to a completion queue entry.
13505 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013506 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013507 * entry.
13508 *
13509 * Return: true if work posted to worker thread, otherwise false.
13510 **/
13511static bool
13512lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013513 struct lpfc_cqe *cqe)
13514{
James Smart45ed1192009-10-02 15:17:02 -040013515 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013516 bool workposted = false;
13517
13518 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013519 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013520
13521 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013522 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013523 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013524 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013525 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013526 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013527 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013528 break;
13529 case CQE_CODE_RELEASE_WQE:
13530 /* Process the WQ release event */
13531 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013532 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013533 break;
13534 case CQE_CODE_XRI_ABORTED:
13535 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013536 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013537 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013538 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013539 break;
James Smart4d9ab992009-10-02 15:16:39 -040013540 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013541 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013542 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013543 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013544 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013545 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013546 break;
James Smart4f774512009-05-22 14:52:35 -040013547 default:
13548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13549 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013550 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013551 break;
13552 }
13553 return workposted;
13554}
13555
13556/**
James Smart4f774512009-05-22 14:52:35 -040013557 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13558 * @phba: Pointer to HBA context object.
13559 * @eqe: Pointer to fast-path event queue entry.
13560 *
13561 * This routine process a event queue entry from the slow-path event queue.
13562 * It will check the MajorCode and MinorCode to determine this is for a
13563 * completion event on a completion queue, if not, an error shall be logged
13564 * and just return. Otherwise, it will get to the corresponding completion
13565 * queue and process all the entries on that completion queue, rearm the
13566 * completion queue, and then return.
13567 *
13568 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013569static void
James Smart67d12732012-08-03 12:36:13 -040013570lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13571 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013572{
James Smart67d12732012-08-03 12:36:13 -040013573 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013574 uint16_t cqid;
13575
James Smart4f774512009-05-22 14:52:35 -040013576 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013577 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013578
James Smart4f774512009-05-22 14:52:35 -040013579 list_for_each_entry(childq, &speq->child_list, list) {
13580 if (childq->queue_id == cqid) {
13581 cq = childq;
13582 break;
13583 }
13584 }
13585 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013586 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13587 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13588 "0365 Slow-path CQ identifier "
13589 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013590 return;
James Smart4f774512009-05-22 14:52:35 -040013591 }
13592
James Smart895427b2017-02-12 13:52:30 -080013593 /* Save EQ associated with this CQ */
13594 cq->assoc_qp = speq;
13595
Dick Kennedyf485c182017-09-29 17:34:34 -070013596 if (!queue_work(phba->wq, &cq->spwork))
13597 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13598 "0390 Cannot schedule soft IRQ "
13599 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13600 cqid, cq->queue_id, smp_processor_id());
13601}
13602
13603/**
13604 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13605 * @phba: Pointer to HBA context object.
13606 *
13607 * This routine process a event queue entry from the slow-path event queue.
13608 * It will check the MajorCode and MinorCode to determine this is for a
13609 * completion event on a completion queue, if not, an error shall be logged
13610 * and just return. Otherwise, it will get to the corresponding completion
13611 * queue and process all the entries on that completion queue, rearm the
13612 * completion queue, and then return.
13613 *
13614 **/
13615static void
13616lpfc_sli4_sp_process_cq(struct work_struct *work)
13617{
13618 struct lpfc_queue *cq =
13619 container_of(work, struct lpfc_queue, spwork);
13620 struct lpfc_hba *phba = cq->phba;
13621 struct lpfc_cqe *cqe;
13622 bool workposted = false;
13623 int ccount = 0;
13624
James Smart4f774512009-05-22 14:52:35 -040013625 /* Process all the entries to the CQ */
13626 switch (cq->type) {
13627 case LPFC_MCQ:
13628 while ((cqe = lpfc_sli4_cq_get(cq))) {
13629 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013630 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013631 break;
James Smartb84daac2012-08-03 12:35:13 -040013632 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013633 }
13634 break;
13635 case LPFC_WCQ:
13636 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013637 if (cq->subtype == LPFC_FCP ||
13638 cq->subtype == LPFC_NVME) {
13639#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13640 if (phba->ktime_on)
13641 cq->isr_timestamp = ktime_get_ns();
13642 else
13643 cq->isr_timestamp = 0;
13644#endif
James Smart895427b2017-02-12 13:52:30 -080013645 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013646 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013647 } else {
James Smart05580562011-05-24 11:40:48 -040013648 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13649 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013650 }
Dick Kennedyf485c182017-09-29 17:34:34 -070013651 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013652 break;
James Smart4f774512009-05-22 14:52:35 -040013653 }
James Smartb84daac2012-08-03 12:35:13 -040013654
13655 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013656 if (ccount > cq->CQ_max_cqe)
13657 cq->CQ_max_cqe = ccount;
James Smart4f774512009-05-22 14:52:35 -040013658 break;
13659 default:
13660 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13661 "0370 Invalid completion queue type (%d)\n",
13662 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013663 return;
James Smart4f774512009-05-22 14:52:35 -040013664 }
13665
13666 /* Catch the no cq entry condition, log an error */
Dick Kennedyf485c182017-09-29 17:34:34 -070013667 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013668 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13669 "0371 No entry from the CQ: identifier "
13670 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13671
13672 /* In any case, flash and re-arm the RCQ */
James Smartb71413d2018-02-22 08:18:40 -080013673 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013674
13675 /* wake up worker thread if there are works to be done */
13676 if (workposted)
13677 lpfc_worker_wake_up(phba);
13678}
13679
13680/**
13681 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013682 * @phba: Pointer to HBA context object.
13683 * @cq: Pointer to associated CQ
13684 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013685 *
13686 * This routine process a fast-path work queue completion entry from fast-path
13687 * event queue for FCP command response completion.
13688 **/
13689static void
James Smart2a76a282012-08-03 12:35:54 -040013690lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013691 struct lpfc_wcqe_complete *wcqe)
13692{
James Smart2a76a282012-08-03 12:35:54 -040013693 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013694 struct lpfc_iocbq *cmdiocbq;
13695 struct lpfc_iocbq irspiocbq;
13696 unsigned long iflags;
13697
James Smart4f774512009-05-22 14:52:35 -040013698 /* Check for response status */
13699 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13700 /* If resource errors reported from HBA, reduce queue
13701 * depth of the SCSI device.
13702 */
James Smarte3d2b802012-08-14 14:25:43 -040013703 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13704 IOSTAT_LOCAL_REJECT)) &&
13705 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13706 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013707 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013708
James Smart4f774512009-05-22 14:52:35 -040013709 /* Log the error status */
James Smart11f0e342018-05-04 20:37:57 -070013710 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13711 "0373 FCP CQE error: status=x%x: "
13712 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013713 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070013714 wcqe->word0, wcqe->total_data_placed,
13715 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013716 }
13717
13718 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013719 spin_lock_irqsave(&pring->ring_lock, iflags);
13720 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013721 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13722 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013723 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013724 if (unlikely(!cmdiocbq)) {
13725 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13726 "0374 FCP complete with no corresponding "
13727 "cmdiocb: iotag (%d)\n",
13728 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13729 return;
13730 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013731#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13732 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13733#endif
James Smart895427b2017-02-12 13:52:30 -080013734 if (cmdiocbq->iocb_cmpl == NULL) {
13735 if (cmdiocbq->wqe_cmpl) {
13736 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13737 spin_lock_irqsave(&phba->hbalock, iflags);
13738 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13739 spin_unlock_irqrestore(&phba->hbalock, iflags);
13740 }
13741
13742 /* Pass the cmd_iocb and the wcqe to the upper layer */
13743 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13744 return;
13745 }
James Smart4f774512009-05-22 14:52:35 -040013746 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13747 "0375 FCP cmdiocb not callback function "
13748 "iotag: (%d)\n",
13749 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13750 return;
13751 }
13752
13753 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013754 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013755
James Smart0f65ff62010-02-26 14:14:23 -050013756 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13757 spin_lock_irqsave(&phba->hbalock, iflags);
13758 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13759 spin_unlock_irqrestore(&phba->hbalock, iflags);
13760 }
13761
James Smart4f774512009-05-22 14:52:35 -040013762 /* Pass the cmd_iocb and the rsp state to the upper layer */
13763 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13764}
13765
13766/**
13767 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13768 * @phba: Pointer to HBA context object.
13769 * @cq: Pointer to completion queue.
13770 * @wcqe: Pointer to work-queue completion queue entry.
13771 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013772 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013773 * proper WQ release routine to the slow-path WQ.
13774 **/
13775static void
13776lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13777 struct lpfc_wcqe_release *wcqe)
13778{
13779 struct lpfc_queue *childwq;
13780 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013781 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013782
13783 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013784 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013785 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013786 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013787 lpfc_sli4_wq_release(childwq,
13788 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013789 if (childwq->q_flag & HBA_NVMET_WQFULL)
13790 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013791 wqid_matched = true;
13792 break;
13793 }
13794 }
13795 /* Report warning log message if no match found */
13796 if (wqid_matched != true)
13797 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13798 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013799 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013800}
13801
13802/**
James Smart2d7dbc42017-02-12 13:52:35 -080013803 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13804 * @phba: Pointer to HBA context object.
13805 * @rcqe: Pointer to receive-queue completion queue entry.
13806 *
13807 * This routine process a receive-queue completion queue entry.
13808 *
13809 * Return: true if work posted to worker thread, otherwise false.
13810 **/
13811static bool
13812lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13813 struct lpfc_rcqe *rcqe)
13814{
13815 bool workposted = false;
13816 struct lpfc_queue *hrq;
13817 struct lpfc_queue *drq;
13818 struct rqb_dmabuf *dma_buf;
13819 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013820 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013821 uint32_t status, rq_id;
13822 unsigned long iflags;
13823 uint32_t fctl, idx;
13824
13825 if ((phba->nvmet_support == 0) ||
13826 (phba->sli4_hba.nvmet_cqset == NULL))
13827 return workposted;
13828
13829 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13830 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13831 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13832
13833 /* sanity check on queue memory */
13834 if (unlikely(!hrq) || unlikely(!drq))
13835 return workposted;
13836
13837 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13838 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13839 else
13840 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13841
13842 if ((phba->nvmet_support == 0) ||
13843 (rq_id != hrq->queue_id))
13844 return workposted;
13845
13846 status = bf_get(lpfc_rcqe_status, rcqe);
13847 switch (status) {
13848 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13849 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13850 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013851 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013852 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013853 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013854 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013855 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13856 if (!dma_buf) {
13857 hrq->RQ_no_buf_found++;
13858 spin_unlock_irqrestore(&phba->hbalock, iflags);
13859 goto out;
13860 }
13861 spin_unlock_irqrestore(&phba->hbalock, iflags);
13862 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013863 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013864 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13865
13866 /* Just some basic sanity checks on FCP Command frame */
13867 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13868 fc_hdr->fh_f_ctl[1] << 8 |
13869 fc_hdr->fh_f_ctl[2]);
13870 if (((fctl &
13871 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13872 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13873 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13874 goto drop;
13875
13876 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13877 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013878 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070013879 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013880 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013881 return false;
13882 }
13883drop:
13884 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13885 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013886 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013887 if (phba->nvmet_support) {
13888 tgtp = phba->targetport->private;
13889 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13890 "6401 RQE Error x%x, posted %d err_cnt "
13891 "%d: %x %x %x\n",
13892 status, hrq->RQ_buf_posted,
13893 hrq->RQ_no_posted_buf,
13894 atomic_read(&tgtp->rcv_fcp_cmd_in),
13895 atomic_read(&tgtp->rcv_fcp_cmd_out),
13896 atomic_read(&tgtp->xmt_fcp_release));
13897 }
13898 /* fallthrough */
13899
13900 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013901 hrq->RQ_no_posted_buf++;
13902 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013903 break;
13904 }
13905out:
13906 return workposted;
13907}
13908
13909/**
James Smart895427b2017-02-12 13:52:30 -080013910 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013911 * @cq: Pointer to the completion queue.
13912 * @eqe: Pointer to fast-path completion queue entry.
13913 *
13914 * This routine process a fast-path work queue completion entry from fast-path
13915 * event queue for FCP command response completion.
13916 **/
13917static int
James Smart895427b2017-02-12 13:52:30 -080013918lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013919 struct lpfc_cqe *cqe)
13920{
13921 struct lpfc_wcqe_release wcqe;
13922 bool workposted = false;
13923
13924 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013925 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013926
13927 /* Check and process for different type of WCQE and dispatch */
13928 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13929 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013930 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013931 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013932 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013933 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013934 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13935 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13936 (struct lpfc_wcqe_complete *)&wcqe);
13937 if (cq->subtype == LPFC_NVME_LS)
13938 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013939 (struct lpfc_wcqe_complete *)&wcqe);
13940 break;
13941 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013942 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013943 /* Process the WQ release event */
13944 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13945 (struct lpfc_wcqe_release *)&wcqe);
13946 break;
13947 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013948 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013949 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013950 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013951 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13952 (struct sli4_wcqe_xri_aborted *)&wcqe);
13953 break;
James Smart895427b2017-02-12 13:52:30 -080013954 case CQE_CODE_RECEIVE_V1:
13955 case CQE_CODE_RECEIVE:
13956 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013957 if (cq->subtype == LPFC_NVMET) {
13958 workposted = lpfc_sli4_nvmet_handle_rcqe(
13959 phba, cq, (struct lpfc_rcqe *)&wcqe);
13960 }
James Smart895427b2017-02-12 13:52:30 -080013961 break;
James Smart4f774512009-05-22 14:52:35 -040013962 default:
13963 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013964 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013965 bf_get(lpfc_wcqe_c_code, &wcqe));
13966 break;
13967 }
13968 return workposted;
13969}
13970
13971/**
James Smart67d12732012-08-03 12:36:13 -040013972 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013973 * @phba: Pointer to HBA context object.
13974 * @eqe: Pointer to fast-path event queue entry.
13975 *
13976 * This routine process a event queue entry from the fast-path event queue.
13977 * It will check the MajorCode and MinorCode to determine this is for a
13978 * completion event on a completion queue, if not, an error shall be logged
13979 * and just return. Otherwise, it will get to the corresponding completion
13980 * queue and process all the entries on the completion queue, rearm the
13981 * completion queue, and then return.
13982 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013983static void
James Smart67d12732012-08-03 12:36:13 -040013984lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13985 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013986{
James Smart895427b2017-02-12 13:52:30 -080013987 struct lpfc_queue *cq = NULL;
James Smart2d7dbc42017-02-12 13:52:35 -080013988 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013989
James Smartcb5172e2010-03-15 11:25:07 -040013990 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013991 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013992 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013993 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013994 bf_get_le32(lpfc_eqe_major_code, eqe),
13995 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070013996 return;
James Smart4f774512009-05-22 14:52:35 -040013997 }
13998
James Smart67d12732012-08-03 12:36:13 -040013999 /* Get the reference to the corresponding CQ */
14000 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14001
James Smart2d7dbc42017-02-12 13:52:35 -080014002 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14003 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14004 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14005 /* Process NVMET unsol rcv */
14006 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14007 goto process_cq;
14008 }
14009 }
14010
James Smart895427b2017-02-12 13:52:30 -080014011 if (phba->sli4_hba.nvme_cq_map &&
14012 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080014013 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080014014 cq = phba->sli4_hba.nvme_cq[qidx];
14015 goto process_cq;
14016 }
14017
14018 if (phba->sli4_hba.fcp_cq_map &&
14019 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
14020 /* Process FCP command completion */
14021 cq = phba->sli4_hba.fcp_cq[qidx];
14022 goto process_cq;
14023 }
14024
14025 if (phba->sli4_hba.nvmels_cq &&
14026 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14027 /* Process NVME unsol rcv */
14028 cq = phba->sli4_hba.nvmels_cq;
14029 }
14030
14031 /* Otherwise this is a Slow path event */
14032 if (cq == NULL) {
Dick Kennedyf485c182017-09-29 17:34:34 -070014033 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
14034 return;
James Smart67d12732012-08-03 12:36:13 -040014035 }
14036
James Smart895427b2017-02-12 13:52:30 -080014037process_cq:
James Smart4f774512009-05-22 14:52:35 -040014038 if (unlikely(cqid != cq->queue_id)) {
14039 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14040 "0368 Miss-matched fast-path completion "
14041 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14042 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070014043 return;
James Smart4f774512009-05-22 14:52:35 -040014044 }
14045
James Smart895427b2017-02-12 13:52:30 -080014046 /* Save EQ associated with this CQ */
14047 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
14048
Dick Kennedyf485c182017-09-29 17:34:34 -070014049 if (!queue_work(phba->wq, &cq->irqwork))
14050 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14051 "0363 Cannot schedule soft IRQ "
14052 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14053 cqid, cq->queue_id, smp_processor_id());
14054}
14055
14056/**
14057 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14058 * @phba: Pointer to HBA context object.
14059 * @eqe: Pointer to fast-path event queue entry.
14060 *
14061 * This routine process a event queue entry from the fast-path event queue.
14062 * It will check the MajorCode and MinorCode to determine this is for a
14063 * completion event on a completion queue, if not, an error shall be logged
14064 * and just return. Otherwise, it will get to the corresponding completion
14065 * queue and process all the entries on the completion queue, rearm the
14066 * completion queue, and then return.
14067 **/
14068static void
14069lpfc_sli4_hba_process_cq(struct work_struct *work)
14070{
14071 struct lpfc_queue *cq =
14072 container_of(work, struct lpfc_queue, irqwork);
14073 struct lpfc_hba *phba = cq->phba;
14074 struct lpfc_cqe *cqe;
14075 bool workposted = false;
14076 int ccount = 0;
14077
James Smart4f774512009-05-22 14:52:35 -040014078 /* Process all the entries to the CQ */
14079 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070014080#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
14081 if (phba->ktime_on)
14082 cq->isr_timestamp = ktime_get_ns();
14083 else
14084 cq->isr_timestamp = 0;
14085#endif
James Smart895427b2017-02-12 13:52:30 -080014086 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070014087 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070014088 break;
James Smart4f774512009-05-22 14:52:35 -040014089 }
14090
James Smartb84daac2012-08-03 12:35:13 -040014091 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070014092 if (ccount > cq->CQ_max_cqe)
14093 cq->CQ_max_cqe = ccount;
14094 cq->assoc_qp->EQ_cqe_cnt += ccount;
James Smartb84daac2012-08-03 12:35:13 -040014095
James Smart4f774512009-05-22 14:52:35 -040014096 /* Catch the no cq entry condition */
Dick Kennedyf485c182017-09-29 17:34:34 -070014097 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040014098 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14099 "0369 No entry from fast-path completion "
14100 "queue fcpcqid=%d\n", cq->queue_id);
14101
14102 /* In any case, flash and re-arm the CQ */
James Smartb71413d2018-02-22 08:18:40 -080014103 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014104
14105 /* wake up worker thread if there are works to be done */
14106 if (workposted)
14107 lpfc_worker_wake_up(phba);
14108}
14109
14110static void
14111lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
14112{
14113 struct lpfc_eqe *eqe;
14114
14115 /* walk all the EQ entries and drop on the floor */
14116 while ((eqe = lpfc_sli4_eq_get(eq)))
14117 ;
14118
14119 /* Clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -080014120 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014121}
14122
James Smart1ba981f2014-02-20 09:56:45 -050014123
14124/**
14125 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
14126 * entry
14127 * @phba: Pointer to HBA context object.
14128 * @eqe: Pointer to fast-path event queue entry.
14129 *
14130 * This routine process a event queue entry from the Flash Optimized Fabric
14131 * event queue. It will check the MajorCode and MinorCode to determine this
14132 * is for a completion event on a completion queue, if not, an error shall be
14133 * logged and just return. Otherwise, it will get to the corresponding
14134 * completion queue and process all the entries on the completion queue, rearm
14135 * the completion queue, and then return.
14136 **/
14137static void
14138lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
14139{
14140 struct lpfc_queue *cq;
James Smart1ba981f2014-02-20 09:56:45 -050014141 uint16_t cqid;
James Smart1ba981f2014-02-20 09:56:45 -050014142
14143 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
14144 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14145 "9147 Not a valid completion "
14146 "event: majorcode=x%x, minorcode=x%x\n",
14147 bf_get_le32(lpfc_eqe_major_code, eqe),
14148 bf_get_le32(lpfc_eqe_minor_code, eqe));
14149 return;
14150 }
14151
14152 /* Get the reference to the corresponding CQ */
14153 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14154
14155 /* Next check for OAS */
14156 cq = phba->sli4_hba.oas_cq;
14157 if (unlikely(!cq)) {
14158 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
14159 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14160 "9148 OAS completion queue "
14161 "does not exist\n");
14162 return;
14163 }
14164
14165 if (unlikely(cqid != cq->queue_id)) {
14166 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14167 "9149 Miss-matched fast-path compl "
14168 "queue id: eqcqid=%d, fcpcqid=%d\n",
14169 cqid, cq->queue_id);
14170 return;
14171 }
14172
James Smartd41b65b2017-06-15 22:56:41 -070014173 /* Save EQ associated with this CQ */
14174 cq->assoc_qp = phba->sli4_hba.fof_eq;
14175
Dick Kennedyf485c182017-09-29 17:34:34 -070014176 /* CQ work will be processed on CPU affinitized to this IRQ */
14177 if (!queue_work(phba->wq, &cq->irqwork))
James Smart1ba981f2014-02-20 09:56:45 -050014178 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf485c182017-09-29 17:34:34 -070014179 "0367 Cannot schedule soft IRQ "
14180 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14181 cqid, cq->queue_id, smp_processor_id());
James Smart1ba981f2014-02-20 09:56:45 -050014182}
14183
14184/**
14185 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
14186 * @irq: Interrupt number.
14187 * @dev_id: The device context pointer.
14188 *
14189 * This function is directly called from the PCI layer as an interrupt
14190 * service routine when device with SLI-4 interface spec is enabled with
14191 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
14192 * IOCB ring event in the HBA. However, when the device is enabled with either
14193 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14194 * device-level interrupt handler. When the PCI slot is in error recovery
14195 * or the HBA is undergoing initialization, the interrupt handler will not
14196 * process the interrupt. The Flash Optimized Fabric ring event are handled in
14197 * the intrrupt context. This function is called without any lock held.
14198 * It gets the hbalock to access and update SLI data structures. Note that,
14199 * the EQ to CQ are one-to-one map such that the EQ index is
14200 * equal to that of CQ index.
14201 *
14202 * This function returns IRQ_HANDLED when interrupt is handled else it
14203 * returns IRQ_NONE.
14204 **/
14205irqreturn_t
14206lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
14207{
14208 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080014209 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050014210 struct lpfc_queue *eq;
14211 struct lpfc_eqe *eqe;
14212 unsigned long iflag;
14213 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050014214
14215 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014216 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14217 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050014218
14219 if (unlikely(!phba))
14220 return IRQ_NONE;
14221
14222 /* Get to the EQ struct associated with this vector */
14223 eq = phba->sli4_hba.fof_eq;
14224 if (unlikely(!eq))
14225 return IRQ_NONE;
14226
14227 /* Check device state for handling interrupt */
14228 if (unlikely(lpfc_intr_state_check(phba))) {
James Smart1ba981f2014-02-20 09:56:45 -050014229 /* Check again for link_state with lock held */
14230 spin_lock_irqsave(&phba->hbalock, iflag);
14231 if (phba->link_state < LPFC_LINK_DOWN)
14232 /* Flush, clear interrupt, and rearm the EQ */
14233 lpfc_sli4_eq_flush(phba, eq);
14234 spin_unlock_irqrestore(&phba->hbalock, iflag);
14235 return IRQ_NONE;
14236 }
14237
14238 /*
14239 * Process all the event on FCP fast-path EQ
14240 */
14241 while ((eqe = lpfc_sli4_eq_get(eq))) {
14242 lpfc_sli4_fof_handle_eqe(phba, eqe);
14243 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070014244 break;
James Smart1ba981f2014-02-20 09:56:45 -050014245 eq->EQ_processed++;
14246 }
14247
14248 /* Track the max number of EQEs processed in 1 intr */
14249 if (ecount > eq->EQ_max_eqe)
14250 eq->EQ_max_eqe = ecount;
14251
14252
14253 if (unlikely(ecount == 0)) {
14254 eq->EQ_no_entry++;
14255
14256 if (phba->intr_type == MSIX)
14257 /* MSI-X treated interrupt served as no EQ share INT */
14258 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14259 "9145 MSI-X interrupt with no EQE\n");
14260 else {
14261 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14262 "9146 ISR interrupt with no EQE\n");
14263 /* Non MSI-X treated on interrupt as EQ share INT */
14264 return IRQ_NONE;
14265 }
14266 }
14267 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080014268 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -050014269 return IRQ_HANDLED;
14270}
14271
James Smart4f774512009-05-22 14:52:35 -040014272/**
James Smart67d12732012-08-03 12:36:13 -040014273 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040014274 * @irq: Interrupt number.
14275 * @dev_id: The device context pointer.
14276 *
14277 * This function is directly called from the PCI layer as an interrupt
14278 * service routine when device with SLI-4 interface spec is enabled with
14279 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14280 * ring event in the HBA. However, when the device is enabled with either
14281 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14282 * device-level interrupt handler. When the PCI slot is in error recovery
14283 * or the HBA is undergoing initialization, the interrupt handler will not
14284 * process the interrupt. The SCSI FCP fast-path ring event are handled in
14285 * the intrrupt context. This function is called without any lock held.
14286 * It gets the hbalock to access and update SLI data structures. Note that,
14287 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14288 * equal to that of FCP CQ index.
14289 *
James Smart67d12732012-08-03 12:36:13 -040014290 * The link attention and ELS ring attention events are handled
14291 * by the worker thread. The interrupt handler signals the worker thread
14292 * and returns for these events. This function is called without any lock
14293 * held. It gets the hbalock to access and update SLI data structures.
14294 *
James Smart4f774512009-05-22 14:52:35 -040014295 * This function returns IRQ_HANDLED when interrupt is handled else it
14296 * returns IRQ_NONE.
14297 **/
14298irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040014299lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040014300{
14301 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080014302 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040014303 struct lpfc_queue *fpeq;
14304 struct lpfc_eqe *eqe;
14305 unsigned long iflag;
14306 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080014307 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040014308
14309 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014310 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14311 phba = hba_eq_hdl->phba;
14312 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014313
14314 if (unlikely(!phba))
14315 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040014316 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040014317 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014318
14319 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080014320 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050014321 if (unlikely(!fpeq))
14322 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014323
James Smartba20c852012-08-03 12:36:52 -040014324 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080014325 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartb71413d2018-02-22 08:18:40 -080014326 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -040014327 else {
James Smart895427b2017-02-12 13:52:30 -080014328 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040014329 return IRQ_NONE;
14330 }
14331 }
14332
James Smart4f774512009-05-22 14:52:35 -040014333 /* Check device state for handling interrupt */
14334 if (unlikely(lpfc_intr_state_check(phba))) {
14335 /* Check again for link_state with lock held */
14336 spin_lock_irqsave(&phba->hbalock, iflag);
14337 if (phba->link_state < LPFC_LINK_DOWN)
14338 /* Flush, clear interrupt, and rearm the EQ */
14339 lpfc_sli4_eq_flush(phba, fpeq);
14340 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040014341 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014342 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040014343 return IRQ_NONE;
14344 }
14345
14346 /*
14347 * Process all the event on FCP fast-path EQ
14348 */
14349 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
Dick Kennedyf485c182017-09-29 17:34:34 -070014350 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14351 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070014352 break;
James Smartb84daac2012-08-03 12:35:13 -040014353 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040014354 }
14355
James Smartb84daac2012-08-03 12:35:13 -040014356 /* Track the max number of EQEs processed in 1 intr */
14357 if (ecount > fpeq->EQ_max_eqe)
14358 fpeq->EQ_max_eqe = ecount;
14359
James Smart4f774512009-05-22 14:52:35 -040014360 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080014361 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014362
14363 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014364 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040014365
14366 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080014367 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040014368 return IRQ_NONE;
14369 }
14370
James Smart4f774512009-05-22 14:52:35 -040014371 if (phba->intr_type == MSIX)
14372 /* MSI-X treated interrupt served as no EQ share INT */
14373 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14374 "0358 MSI-X interrupt with no EQE\n");
14375 else
14376 /* Non MSI-X treated on interrupt as EQ share INT */
14377 return IRQ_NONE;
14378 }
14379
James Smartba20c852012-08-03 12:36:52 -040014380 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014381 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14382
James Smart4f774512009-05-22 14:52:35 -040014383 return IRQ_HANDLED;
14384} /* lpfc_sli4_fp_intr_handler */
14385
14386/**
14387 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14388 * @irq: Interrupt number.
14389 * @dev_id: The device context pointer.
14390 *
14391 * This function is the device-level interrupt handler to device with SLI-4
14392 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14393 * interrupt mode is enabled and there is an event in the HBA which requires
14394 * driver attention. This function invokes the slow-path interrupt attention
14395 * handling function and fast-path interrupt attention handling function in
14396 * turn to process the relevant HBA attention events. This function is called
14397 * without any lock held. It gets the hbalock to access and update SLI data
14398 * structures.
14399 *
14400 * This function returns IRQ_HANDLED when interrupt is handled, else it
14401 * returns IRQ_NONE.
14402 **/
14403irqreturn_t
14404lpfc_sli4_intr_handler(int irq, void *dev_id)
14405{
14406 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014407 irqreturn_t hba_irq_rc;
14408 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014409 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014410
14411 /* Get the driver's phba structure from the dev_id */
14412 phba = (struct lpfc_hba *)dev_id;
14413
14414 if (unlikely(!phba))
14415 return IRQ_NONE;
14416
14417 /*
James Smart4f774512009-05-22 14:52:35 -040014418 * Invoke fast-path host attention interrupt handling as appropriate.
14419 */
James Smart895427b2017-02-12 13:52:30 -080014420 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014421 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014422 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014423 if (hba_irq_rc == IRQ_HANDLED)
14424 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014425 }
14426
James Smart1ba981f2014-02-20 09:56:45 -050014427 if (phba->cfg_fof) {
14428 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014429 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050014430 if (hba_irq_rc == IRQ_HANDLED)
14431 hba_handled |= true;
14432 }
14433
James Smart67d12732012-08-03 12:36:13 -040014434 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014435} /* lpfc_sli4_intr_handler */
14436
14437/**
14438 * lpfc_sli4_queue_free - free a queue structure and associated memory
14439 * @queue: The queue structure to free.
14440 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014441 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014442 * the host resident queue. This function must be called after destroying the
14443 * queue on the HBA.
14444 **/
14445void
14446lpfc_sli4_queue_free(struct lpfc_queue *queue)
14447{
14448 struct lpfc_dmabuf *dmabuf;
14449
14450 if (!queue)
14451 return;
14452
14453 while (!list_empty(&queue->page_list)) {
14454 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14455 list);
James Smart81b96ed2017-11-20 16:00:29 -080014456 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014457 dmabuf->virt, dmabuf->phys);
14458 kfree(dmabuf);
14459 }
James Smart895427b2017-02-12 13:52:30 -080014460 if (queue->rqbp) {
14461 lpfc_free_rq_buffer(queue->phba, queue);
14462 kfree(queue->rqbp);
14463 }
James Smartd1f525a2017-04-21 16:04:55 -070014464
14465 if (!list_empty(&queue->wq_list))
14466 list_del(&queue->wq_list);
14467
James Smart4f774512009-05-22 14:52:35 -040014468 kfree(queue);
14469 return;
14470}
14471
14472/**
14473 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14474 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014475 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014476 * @entry_size: The size of each queue entry for this queue.
14477 * @entry count: The number of entries that this queue will handle.
14478 *
14479 * This function allocates a queue structure and the DMAable memory used for
14480 * the host resident queue. This function must be called before creating the
14481 * queue on the HBA.
14482 **/
14483struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014484lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14485 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040014486{
14487 struct lpfc_queue *queue;
14488 struct lpfc_dmabuf *dmabuf;
14489 int x, total_qe_count;
14490 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040014491 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040014492
James Smartcb5172e2010-03-15 11:25:07 -040014493 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014494 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014495
James Smart4f774512009-05-22 14:52:35 -040014496 queue = kzalloc(sizeof(struct lpfc_queue) +
14497 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14498 if (!queue)
14499 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040014500 queue->page_count = (ALIGN(entry_size * entry_count,
14501 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014502
14503 /* If needed, Adjust page count to match the max the adapter supports */
14504 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14505 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14506
James Smart4f774512009-05-22 14:52:35 -040014507 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014508 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014509 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014510 INIT_LIST_HEAD(&queue->page_list);
14511 INIT_LIST_HEAD(&queue->child_list);
James Smart81b96ed2017-11-20 16:00:29 -080014512
14513 /* Set queue parameters now. If the system cannot provide memory
14514 * resources, the free routine needs to know what was allocated.
14515 */
14516 queue->entry_size = entry_size;
14517 queue->entry_count = entry_count;
14518 queue->page_size = hw_page_size;
14519 queue->phba = phba;
14520
James Smart4f774512009-05-22 14:52:35 -040014521 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14522 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14523 if (!dmabuf)
14524 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040014525 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14526 hw_page_size, &dmabuf->phys,
14527 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014528 if (!dmabuf->virt) {
14529 kfree(dmabuf);
14530 goto out_fail;
14531 }
14532 dmabuf->buffer_tag = x;
14533 list_add_tail(&dmabuf->list, &queue->page_list);
14534 /* initialize queue's entry array */
14535 dma_pointer = dmabuf->virt;
14536 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040014537 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040014538 total_qe_count++, dma_pointer += entry_size) {
14539 queue->qe[total_qe_count].address = dma_pointer;
14540 }
14541 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014542 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14543 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014544
James Smart64eb4dc2017-05-15 15:20:49 -070014545 /* entry_repost will be set during q creation */
14546
James Smart4f774512009-05-22 14:52:35 -040014547 return queue;
14548out_fail:
14549 lpfc_sli4_queue_free(queue);
14550 return NULL;
14551}
14552
14553/**
James Smart962bc512013-01-03 15:44:00 -050014554 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14555 * @phba: HBA structure that indicates port to create a queue on.
14556 * @pci_barset: PCI BAR set flag.
14557 *
14558 * This function shall perform iomap of the specified PCI BAR address to host
14559 * memory address if not already done so and return it. The returned host
14560 * memory address can be NULL.
14561 */
14562static void __iomem *
14563lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14564{
James Smart962bc512013-01-03 15:44:00 -050014565 if (!phba->pcidev)
14566 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014567
14568 switch (pci_barset) {
14569 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014570 return phba->pci_bar0_memmap_p;
14571 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014572 return phba->pci_bar2_memmap_p;
14573 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014574 return phba->pci_bar4_memmap_p;
14575 default:
14576 break;
14577 }
14578 return NULL;
14579}
14580
14581/**
James Smart895427b2017-02-12 13:52:30 -080014582 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040014583 * @phba: HBA structure that indicates port to create a queue on.
14584 * @startq: The starting FCP EQ to modify
14585 *
14586 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080014587 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14588 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040014589 *
14590 * The @phba struct is used to send mailbox command to HBA. The @startq
14591 * is used to get the starting FCP EQ to change.
14592 * This function is asynchronous and will wait for the mailbox
14593 * command to finish before continuing.
14594 *
14595 * On success this function will return a zero. If unable to allocate enough
14596 * memory this function will return -ENOMEM. If the queue create mailbox command
14597 * fails this function will return -ENXIO.
14598 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014599int
James Smart0cf07f842017-06-01 21:07:10 -070014600lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14601 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014602{
14603 struct lpfc_mbx_modify_eq_delay *eq_delay;
14604 LPFC_MBOXQ_t *mbox;
14605 struct lpfc_queue *eq;
14606 int cnt, rc, length, status = 0;
14607 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014608 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014609 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014610 union lpfc_sli4_cfg_shdr *shdr;
14611 uint16_t dmult;
14612
James Smart895427b2017-02-12 13:52:30 -080014613 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040014614 return 0;
14615
14616 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14617 if (!mbox)
14618 return -ENOMEM;
14619 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14620 sizeof(struct lpfc_sli4_cfg_mhdr));
14621 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14622 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14623 length, LPFC_SLI4_MBX_EMBED);
14624 eq_delay = &mbox->u.mqe.un.eq_delay;
14625
14626 /* Calculate delay multiper from maximum interrupt per second */
James Smart0cf07f842017-06-01 21:07:10 -070014627 result = imax / phba->io_channel_irqs;
James Smart895427b2017-02-12 13:52:30 -080014628 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014629 dmult = 0;
14630 else
14631 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014632 if (dmult > LPFC_DMULT_MAX)
14633 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014634
14635 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014636 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14637 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014638 if (!eq)
14639 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014640 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014641 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14642 eq_delay->u.request.eq[cnt].phase = 0;
14643 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14644 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014645
14646 /* q_mode is only used for auto_imax */
14647 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14648 /* Use EQ Delay Register method for q_mode */
14649
14650 /* Convert for EQ Delay register */
14651 val = phba->cfg_fcp_imax;
14652 if (val) {
14653 /* First, interrupts per sec per EQ */
14654 val = phba->cfg_fcp_imax /
14655 phba->io_channel_irqs;
14656
14657 /* us delay between each interrupt */
14658 val = LPFC_SEC_TO_USEC / val;
14659 }
14660 eq->q_mode = val;
14661 } else {
14662 eq->q_mode = imax;
14663 }
14664
14665 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014666 break;
14667 }
14668 eq_delay->u.request.num_eq = cnt;
14669
14670 mbox->vport = phba->pport;
14671 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14672 mbox->context1 = NULL;
14673 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14674 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14675 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14676 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14677 if (shdr_status || shdr_add_status || rc) {
14678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14679 "2512 MODIFY_EQ_DELAY mailbox failed with "
14680 "status x%x add_status x%x, mbx status x%x\n",
14681 shdr_status, shdr_add_status, rc);
14682 status = -ENXIO;
14683 }
14684 mempool_free(mbox, phba->mbox_mem_pool);
14685 return status;
14686}
14687
14688/**
James Smart4f774512009-05-22 14:52:35 -040014689 * lpfc_eq_create - Create an Event Queue on the HBA
14690 * @phba: HBA structure that indicates port to create a queue on.
14691 * @eq: The queue structure to use to create the event queue.
14692 * @imax: The maximum interrupt per second limit.
14693 *
14694 * This function creates an event queue, as detailed in @eq, on a port,
14695 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14696 *
14697 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14698 * is used to get the entry count and entry size that are necessary to
14699 * determine the number of pages to allocate and use for this queue. This
14700 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14701 * event queue. This function is asynchronous and will wait for the mailbox
14702 * command to finish before continuing.
14703 *
14704 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014705 * memory this function will return -ENOMEM. If the queue create mailbox command
14706 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014707 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014708int
James Smartee020062012-09-29 11:28:52 -040014709lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014710{
14711 struct lpfc_mbx_eq_create *eq_create;
14712 LPFC_MBOXQ_t *mbox;
14713 int rc, length, status = 0;
14714 struct lpfc_dmabuf *dmabuf;
14715 uint32_t shdr_status, shdr_add_status;
14716 union lpfc_sli4_cfg_shdr *shdr;
14717 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014718 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14719
James Smart2e90f4b2011-12-13 13:22:37 -050014720 /* sanity check on queue memory */
14721 if (!eq)
14722 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014723 if (!phba->sli4_hba.pc_sli4_params.supported)
14724 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014725
14726 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14727 if (!mbox)
14728 return -ENOMEM;
14729 length = (sizeof(struct lpfc_mbx_eq_create) -
14730 sizeof(struct lpfc_sli4_cfg_mhdr));
14731 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14732 LPFC_MBOX_OPCODE_EQ_CREATE,
14733 length, LPFC_SLI4_MBX_EMBED);
14734 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014735 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014736 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14737 eq->page_count);
14738 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14739 LPFC_EQE_SIZE);
14740 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014741
14742 /* Use version 2 of CREATE_EQ if eqav is set */
14743 if (phba->sli4_hba.pc_sli4_params.eqav) {
14744 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14745 LPFC_Q_CREATE_VERSION_2);
14746 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14747 phba->sli4_hba.pc_sli4_params.eqav);
14748 }
14749
James Smart2c9c5a02015-04-07 15:07:15 -040014750 /* don't setup delay multiplier using EQ_CREATE */
14751 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014752 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14753 dmult);
14754 switch (eq->entry_count) {
14755 default:
14756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14757 "0360 Unsupported EQ count. (%d)\n",
14758 eq->entry_count);
14759 if (eq->entry_count < 256)
14760 return -EINVAL;
14761 /* otherwise default to smallest count (drop through) */
14762 case 256:
14763 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14764 LPFC_EQ_CNT_256);
14765 break;
14766 case 512:
14767 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14768 LPFC_EQ_CNT_512);
14769 break;
14770 case 1024:
14771 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14772 LPFC_EQ_CNT_1024);
14773 break;
14774 case 2048:
14775 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14776 LPFC_EQ_CNT_2048);
14777 break;
14778 case 4096:
14779 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14780 LPFC_EQ_CNT_4096);
14781 break;
14782 }
14783 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014784 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014785 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14786 putPaddrLow(dmabuf->phys);
14787 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14788 putPaddrHigh(dmabuf->phys);
14789 }
14790 mbox->vport = phba->pport;
14791 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14792 mbox->context1 = NULL;
14793 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040014794 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14795 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14796 if (shdr_status || shdr_add_status || rc) {
14797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14798 "2500 EQ_CREATE mailbox failed with "
14799 "status x%x add_status x%x, mbx status x%x\n",
14800 shdr_status, shdr_add_status, rc);
14801 status = -ENXIO;
14802 }
14803 eq->type = LPFC_EQ;
14804 eq->subtype = LPFC_NONE;
14805 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14806 if (eq->queue_id == 0xFFFF)
14807 status = -ENXIO;
14808 eq->host_index = 0;
14809 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014810 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014811
James Smart8fa38512009-07-19 10:01:03 -040014812 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014813 return status;
14814}
14815
14816/**
14817 * lpfc_cq_create - Create a Completion Queue on the HBA
14818 * @phba: HBA structure that indicates port to create a queue on.
14819 * @cq: The queue structure to use to create the completion queue.
14820 * @eq: The event queue to bind this completion queue to.
14821 *
14822 * This function creates a completion queue, as detailed in @wq, on a port,
14823 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14824 *
14825 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14826 * is used to get the entry count and entry size that are necessary to
14827 * determine the number of pages to allocate and use for this queue. The @eq
14828 * is used to indicate which event queue to bind this completion queue to. This
14829 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14830 * completion queue. This function is asynchronous and will wait for the mailbox
14831 * command to finish before continuing.
14832 *
14833 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014834 * memory this function will return -ENOMEM. If the queue create mailbox command
14835 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014836 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014837int
James Smart4f774512009-05-22 14:52:35 -040014838lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14839 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14840{
14841 struct lpfc_mbx_cq_create *cq_create;
14842 struct lpfc_dmabuf *dmabuf;
14843 LPFC_MBOXQ_t *mbox;
14844 int rc, length, status = 0;
14845 uint32_t shdr_status, shdr_add_status;
14846 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014847
James Smart2e90f4b2011-12-13 13:22:37 -050014848 /* sanity check on queue memory */
14849 if (!cq || !eq)
14850 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014851
James Smart4f774512009-05-22 14:52:35 -040014852 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14853 if (!mbox)
14854 return -ENOMEM;
14855 length = (sizeof(struct lpfc_mbx_cq_create) -
14856 sizeof(struct lpfc_sli4_cfg_mhdr));
14857 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14858 LPFC_MBOX_OPCODE_CQ_CREATE,
14859 length, LPFC_SLI4_MBX_EMBED);
14860 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014861 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014862 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14863 cq->page_count);
14864 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14865 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014866 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14867 phba->sli4_hba.pc_sli4_params.cqv);
14868 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014869 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14870 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014871 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14872 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080014873 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14874 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050014875 } else {
14876 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14877 eq->queue_id);
14878 }
James Smart4f774512009-05-22 14:52:35 -040014879 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014880 case 2048:
14881 case 4096:
14882 if (phba->sli4_hba.pc_sli4_params.cqv ==
14883 LPFC_Q_CREATE_VERSION_2) {
14884 cq_create->u.request.context.lpfc_cq_context_count =
14885 cq->entry_count;
14886 bf_set(lpfc_cq_context_count,
14887 &cq_create->u.request.context,
14888 LPFC_CQ_CNT_WORD7);
14889 break;
14890 }
14891 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014892 default:
14893 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014894 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014895 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014896 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014897 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014898 if (cq->entry_count < 256) {
14899 status = -EINVAL;
14900 goto out;
14901 }
James Smart4f774512009-05-22 14:52:35 -040014902 /* otherwise default to smallest count (drop through) */
14903 case 256:
14904 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14905 LPFC_CQ_CNT_256);
14906 break;
14907 case 512:
14908 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14909 LPFC_CQ_CNT_512);
14910 break;
14911 case 1024:
14912 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14913 LPFC_CQ_CNT_1024);
14914 break;
14915 }
14916 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014917 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014918 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14919 putPaddrLow(dmabuf->phys);
14920 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14921 putPaddrHigh(dmabuf->phys);
14922 }
14923 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14924
14925 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014926 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14927 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14928 if (shdr_status || shdr_add_status || rc) {
14929 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14930 "2501 CQ_CREATE mailbox failed with "
14931 "status x%x add_status x%x, mbx status x%x\n",
14932 shdr_status, shdr_add_status, rc);
14933 status = -ENXIO;
14934 goto out;
14935 }
14936 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14937 if (cq->queue_id == 0xFFFF) {
14938 status = -ENXIO;
14939 goto out;
14940 }
14941 /* link the cq onto the parent eq child list */
14942 list_add_tail(&cq->list, &eq->child_list);
14943 /* Set up completion queue's type and subtype */
14944 cq->type = type;
14945 cq->subtype = subtype;
14946 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014947 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014948 cq->host_index = 0;
14949 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014950 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014951
James Smart8fa38512009-07-19 10:01:03 -040014952out:
14953 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014954 return status;
14955}
14956
14957/**
James Smart2d7dbc42017-02-12 13:52:35 -080014958 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14959 * @phba: HBA structure that indicates port to create a queue on.
14960 * @cqp: The queue structure array to use to create the completion queues.
14961 * @eqp: The event queue array to bind these completion queues to.
14962 *
14963 * This function creates a set of completion queue, s to support MRQ
14964 * as detailed in @cqp, on a port,
14965 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14966 *
14967 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14968 * is used to get the entry count and entry size that are necessary to
14969 * determine the number of pages to allocate and use for this queue. The @eq
14970 * is used to indicate which event queue to bind this completion queue to. This
14971 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14972 * completion queue. This function is asynchronous and will wait for the mailbox
14973 * command to finish before continuing.
14974 *
14975 * On success this function will return a zero. If unable to allocate enough
14976 * memory this function will return -ENOMEM. If the queue create mailbox command
14977 * fails this function will return -ENXIO.
14978 **/
14979int
14980lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14981 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14982{
14983 struct lpfc_queue *cq;
14984 struct lpfc_queue *eq;
14985 struct lpfc_mbx_cq_create_set *cq_set;
14986 struct lpfc_dmabuf *dmabuf;
14987 LPFC_MBOXQ_t *mbox;
14988 int rc, length, alloclen, status = 0;
14989 int cnt, idx, numcq, page_idx = 0;
14990 uint32_t shdr_status, shdr_add_status;
14991 union lpfc_sli4_cfg_shdr *shdr;
14992 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14993
14994 /* sanity check on queue memory */
14995 numcq = phba->cfg_nvmet_mrq;
14996 if (!cqp || !eqp || !numcq)
14997 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014998
14999 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15000 if (!mbox)
15001 return -ENOMEM;
15002
15003 length = sizeof(struct lpfc_mbx_cq_create_set);
15004 length += ((numcq * cqp[0]->page_count) *
15005 sizeof(struct dma_address));
15006 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15007 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
15008 LPFC_SLI4_MBX_NEMBED);
15009 if (alloclen < length) {
15010 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15011 "3098 Allocated DMA memory size (%d) is "
15012 "less than the requested DMA memory size "
15013 "(%d)\n", alloclen, length);
15014 status = -ENOMEM;
15015 goto out;
15016 }
15017 cq_set = mbox->sge_array->addr[0];
15018 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
15019 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
15020
15021 for (idx = 0; idx < numcq; idx++) {
15022 cq = cqp[idx];
15023 eq = eqp[idx];
15024 if (!cq || !eq) {
15025 status = -ENOMEM;
15026 goto out;
15027 }
James Smart81b96ed2017-11-20 16:00:29 -080015028 if (!phba->sli4_hba.pc_sli4_params.supported)
15029 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080015030
15031 switch (idx) {
15032 case 0:
15033 bf_set(lpfc_mbx_cq_create_set_page_size,
15034 &cq_set->u.request,
15035 (hw_page_size / SLI4_PAGE_SIZE));
15036 bf_set(lpfc_mbx_cq_create_set_num_pages,
15037 &cq_set->u.request, cq->page_count);
15038 bf_set(lpfc_mbx_cq_create_set_evt,
15039 &cq_set->u.request, 1);
15040 bf_set(lpfc_mbx_cq_create_set_valid,
15041 &cq_set->u.request, 1);
15042 bf_set(lpfc_mbx_cq_create_set_cqe_size,
15043 &cq_set->u.request, 0);
15044 bf_set(lpfc_mbx_cq_create_set_num_cq,
15045 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080015046 bf_set(lpfc_mbx_cq_create_set_autovalid,
15047 &cq_set->u.request,
15048 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080015049 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080015050 case 2048:
15051 case 4096:
15052 if (phba->sli4_hba.pc_sli4_params.cqv ==
15053 LPFC_Q_CREATE_VERSION_2) {
15054 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15055 &cq_set->u.request,
15056 cq->entry_count);
15057 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15058 &cq_set->u.request,
15059 LPFC_CQ_CNT_WORD7);
15060 break;
15061 }
15062 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080015063 default:
15064 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15065 "3118 Bad CQ count. (%d)\n",
15066 cq->entry_count);
15067 if (cq->entry_count < 256) {
15068 status = -EINVAL;
15069 goto out;
15070 }
15071 /* otherwise default to smallest (drop thru) */
15072 case 256:
15073 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15074 &cq_set->u.request, LPFC_CQ_CNT_256);
15075 break;
15076 case 512:
15077 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15078 &cq_set->u.request, LPFC_CQ_CNT_512);
15079 break;
15080 case 1024:
15081 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15082 &cq_set->u.request, LPFC_CQ_CNT_1024);
15083 break;
15084 }
15085 bf_set(lpfc_mbx_cq_create_set_eq_id0,
15086 &cq_set->u.request, eq->queue_id);
15087 break;
15088 case 1:
15089 bf_set(lpfc_mbx_cq_create_set_eq_id1,
15090 &cq_set->u.request, eq->queue_id);
15091 break;
15092 case 2:
15093 bf_set(lpfc_mbx_cq_create_set_eq_id2,
15094 &cq_set->u.request, eq->queue_id);
15095 break;
15096 case 3:
15097 bf_set(lpfc_mbx_cq_create_set_eq_id3,
15098 &cq_set->u.request, eq->queue_id);
15099 break;
15100 case 4:
15101 bf_set(lpfc_mbx_cq_create_set_eq_id4,
15102 &cq_set->u.request, eq->queue_id);
15103 break;
15104 case 5:
15105 bf_set(lpfc_mbx_cq_create_set_eq_id5,
15106 &cq_set->u.request, eq->queue_id);
15107 break;
15108 case 6:
15109 bf_set(lpfc_mbx_cq_create_set_eq_id6,
15110 &cq_set->u.request, eq->queue_id);
15111 break;
15112 case 7:
15113 bf_set(lpfc_mbx_cq_create_set_eq_id7,
15114 &cq_set->u.request, eq->queue_id);
15115 break;
15116 case 8:
15117 bf_set(lpfc_mbx_cq_create_set_eq_id8,
15118 &cq_set->u.request, eq->queue_id);
15119 break;
15120 case 9:
15121 bf_set(lpfc_mbx_cq_create_set_eq_id9,
15122 &cq_set->u.request, eq->queue_id);
15123 break;
15124 case 10:
15125 bf_set(lpfc_mbx_cq_create_set_eq_id10,
15126 &cq_set->u.request, eq->queue_id);
15127 break;
15128 case 11:
15129 bf_set(lpfc_mbx_cq_create_set_eq_id11,
15130 &cq_set->u.request, eq->queue_id);
15131 break;
15132 case 12:
15133 bf_set(lpfc_mbx_cq_create_set_eq_id12,
15134 &cq_set->u.request, eq->queue_id);
15135 break;
15136 case 13:
15137 bf_set(lpfc_mbx_cq_create_set_eq_id13,
15138 &cq_set->u.request, eq->queue_id);
15139 break;
15140 case 14:
15141 bf_set(lpfc_mbx_cq_create_set_eq_id14,
15142 &cq_set->u.request, eq->queue_id);
15143 break;
15144 case 15:
15145 bf_set(lpfc_mbx_cq_create_set_eq_id15,
15146 &cq_set->u.request, eq->queue_id);
15147 break;
15148 }
15149
15150 /* link the cq onto the parent eq child list */
15151 list_add_tail(&cq->list, &eq->child_list);
15152 /* Set up completion queue's type and subtype */
15153 cq->type = type;
15154 cq->subtype = subtype;
15155 cq->assoc_qid = eq->queue_id;
15156 cq->host_index = 0;
15157 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070015158 cq->entry_repost = LPFC_CQ_REPOST;
James Smart81b96ed2017-11-20 16:00:29 -080015159 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080015160
15161 rc = 0;
15162 list_for_each_entry(dmabuf, &cq->page_list, list) {
15163 memset(dmabuf->virt, 0, hw_page_size);
15164 cnt = page_idx + dmabuf->buffer_tag;
15165 cq_set->u.request.page[cnt].addr_lo =
15166 putPaddrLow(dmabuf->phys);
15167 cq_set->u.request.page[cnt].addr_hi =
15168 putPaddrHigh(dmabuf->phys);
15169 rc++;
15170 }
15171 page_idx += rc;
15172 }
15173
15174 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15175
15176 /* The IOCTL status is embedded in the mailbox subheader. */
15177 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15178 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15179 if (shdr_status || shdr_add_status || rc) {
15180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15181 "3119 CQ_CREATE_SET mailbox failed with "
15182 "status x%x add_status x%x, mbx status x%x\n",
15183 shdr_status, shdr_add_status, rc);
15184 status = -ENXIO;
15185 goto out;
15186 }
15187 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15188 if (rc == 0xFFFF) {
15189 status = -ENXIO;
15190 goto out;
15191 }
15192
15193 for (idx = 0; idx < numcq; idx++) {
15194 cq = cqp[idx];
15195 cq->queue_id = rc + idx;
15196 }
15197
15198out:
15199 lpfc_sli4_mbox_cmd_free(phba, mbox);
15200 return status;
15201}
15202
15203/**
James Smartb19a0612010-04-06 14:48:51 -040015204 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040015205 * @phba: HBA structure that indicates port to create a queue on.
15206 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040015207 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15208 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040015209 *
James Smartb19a0612010-04-06 14:48:51 -040015210 * This function provides failback (fb) functionality when the
15211 * mq_create_ext fails on older FW generations. It's purpose is identical
15212 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040015213 *
James Smartb19a0612010-04-06 14:48:51 -040015214 * This routine cannot fail as all attributes were previously accessed and
15215 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040015216 **/
James Smartb19a0612010-04-06 14:48:51 -040015217static void
15218lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15219 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040015220{
15221 struct lpfc_mbx_mq_create *mq_create;
15222 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040015223 int length;
James Smart04c68492009-05-22 14:52:52 -040015224
James Smart04c68492009-05-22 14:52:52 -040015225 length = (sizeof(struct lpfc_mbx_mq_create) -
15226 sizeof(struct lpfc_sli4_cfg_mhdr));
15227 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15228 LPFC_MBOX_OPCODE_MQ_CREATE,
15229 length, LPFC_SLI4_MBX_EMBED);
15230 mq_create = &mbox->u.mqe.un.mq_create;
15231 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040015232 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040015233 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040015234 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040015235 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15236 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040015237 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015238 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15239 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040015240 break;
15241 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015242 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15243 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040015244 break;
15245 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015246 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15247 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040015248 break;
15249 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015250 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15251 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040015252 break;
15253 }
15254 list_for_each_entry(dmabuf, &mq->page_list, list) {
15255 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040015256 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040015257 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040015258 putPaddrHigh(dmabuf->phys);
15259 }
15260}
15261
15262/**
15263 * lpfc_mq_create - Create a mailbox Queue on the HBA
15264 * @phba: HBA structure that indicates port to create a queue on.
15265 * @mq: The queue structure to use to create the mailbox queue.
15266 * @cq: The completion queue to associate with this cq.
15267 * @subtype: The queue's subtype.
15268 *
15269 * This function creates a mailbox queue, as detailed in @mq, on a port,
15270 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15271 *
15272 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15273 * is used to get the entry count and entry size that are necessary to
15274 * determine the number of pages to allocate and use for this queue. This
15275 * function will send the MQ_CREATE mailbox command to the HBA to setup the
15276 * mailbox queue. This function is asynchronous and will wait for the mailbox
15277 * command to finish before continuing.
15278 *
15279 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015280 * memory this function will return -ENOMEM. If the queue create mailbox command
15281 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040015282 **/
15283int32_t
15284lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15285 struct lpfc_queue *cq, uint32_t subtype)
15286{
15287 struct lpfc_mbx_mq_create *mq_create;
15288 struct lpfc_mbx_mq_create_ext *mq_create_ext;
15289 struct lpfc_dmabuf *dmabuf;
15290 LPFC_MBOXQ_t *mbox;
15291 int rc, length, status = 0;
15292 uint32_t shdr_status, shdr_add_status;
15293 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015294 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040015295
James Smart2e90f4b2011-12-13 13:22:37 -050015296 /* sanity check on queue memory */
15297 if (!mq || !cq)
15298 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015299 if (!phba->sli4_hba.pc_sli4_params.supported)
15300 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040015301
15302 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15303 if (!mbox)
15304 return -ENOMEM;
15305 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15306 sizeof(struct lpfc_sli4_cfg_mhdr));
15307 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15308 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15309 length, LPFC_SLI4_MBX_EMBED);
15310
15311 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015312 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015313 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15314 &mq_create_ext->u.request, mq->page_count);
15315 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15316 &mq_create_ext->u.request, 1);
15317 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015318 &mq_create_ext->u.request, 1);
15319 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15320 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015321 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15322 &mq_create_ext->u.request, 1);
15323 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15324 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015325 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015326 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15327 phba->sli4_hba.pc_sli4_params.mqv);
15328 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15329 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15330 cq->queue_id);
15331 else
15332 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15333 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015334 switch (mq->entry_count) {
15335 default:
15336 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15337 "0362 Unsupported MQ count. (%d)\n",
15338 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015339 if (mq->entry_count < 16) {
15340 status = -EINVAL;
15341 goto out;
15342 }
James Smartb19a0612010-04-06 14:48:51 -040015343 /* otherwise default to smallest count (drop through) */
15344 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015345 bf_set(lpfc_mq_context_ring_size,
15346 &mq_create_ext->u.request.context,
15347 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015348 break;
15349 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015350 bf_set(lpfc_mq_context_ring_size,
15351 &mq_create_ext->u.request.context,
15352 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015353 break;
15354 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015355 bf_set(lpfc_mq_context_ring_size,
15356 &mq_create_ext->u.request.context,
15357 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015358 break;
15359 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015360 bf_set(lpfc_mq_context_ring_size,
15361 &mq_create_ext->u.request.context,
15362 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015363 break;
15364 }
15365 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015366 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015367 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15368 putPaddrLow(dmabuf->phys);
15369 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015370 putPaddrHigh(dmabuf->phys);
15371 }
15372 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015373 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15374 &mq_create_ext->u.response);
15375 if (rc != MBX_SUCCESS) {
15376 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15377 "2795 MQ_CREATE_EXT failed with "
15378 "status x%x. Failback to MQ_CREATE.\n",
15379 rc);
15380 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15381 mq_create = &mbox->u.mqe.un.mq_create;
15382 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15383 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15384 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15385 &mq_create->u.response);
15386 }
15387
James Smart04c68492009-05-22 14:52:52 -040015388 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015389 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15390 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15391 if (shdr_status || shdr_add_status || rc) {
15392 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15393 "2502 MQ_CREATE mailbox failed with "
15394 "status x%x add_status x%x, mbx status x%x\n",
15395 shdr_status, shdr_add_status, rc);
15396 status = -ENXIO;
15397 goto out;
15398 }
James Smart04c68492009-05-22 14:52:52 -040015399 if (mq->queue_id == 0xFFFF) {
15400 status = -ENXIO;
15401 goto out;
15402 }
15403 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015404 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015405 mq->subtype = subtype;
15406 mq->host_index = 0;
15407 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070015408 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040015409
15410 /* link the mq onto the parent cq child list */
15411 list_add_tail(&mq->list, &cq->child_list);
15412out:
James Smart8fa38512009-07-19 10:01:03 -040015413 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015414 return status;
15415}
15416
15417/**
James Smart4f774512009-05-22 14:52:35 -040015418 * lpfc_wq_create - Create a Work Queue on the HBA
15419 * @phba: HBA structure that indicates port to create a queue on.
15420 * @wq: The queue structure to use to create the work queue.
15421 * @cq: The completion queue to bind this work queue to.
15422 * @subtype: The subtype of the work queue indicating its functionality.
15423 *
15424 * This function creates a work queue, as detailed in @wq, on a port, described
15425 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15426 *
15427 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15428 * is used to get the entry count and entry size that are necessary to
15429 * determine the number of pages to allocate and use for this queue. The @cq
15430 * is used to indicate which completion queue to bind this work queue to. This
15431 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15432 * work queue. This function is asynchronous and will wait for the mailbox
15433 * command to finish before continuing.
15434 *
15435 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015436 * memory this function will return -ENOMEM. If the queue create mailbox command
15437 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015438 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015439int
James Smart4f774512009-05-22 14:52:35 -040015440lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15441 struct lpfc_queue *cq, uint32_t subtype)
15442{
15443 struct lpfc_mbx_wq_create *wq_create;
15444 struct lpfc_dmabuf *dmabuf;
15445 LPFC_MBOXQ_t *mbox;
15446 int rc, length, status = 0;
15447 uint32_t shdr_status, shdr_add_status;
15448 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015449 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015450 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015451 void __iomem *bar_memmap_p;
15452 uint32_t db_offset;
15453 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015454 uint8_t dpp_barset;
15455 uint32_t dpp_offset;
15456 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015457 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015458
James Smart2e90f4b2011-12-13 13:22:37 -050015459 /* sanity check on queue memory */
15460 if (!wq || !cq)
15461 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015462 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015463 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015464
15465 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15466 if (!mbox)
15467 return -ENOMEM;
15468 length = (sizeof(struct lpfc_mbx_wq_create) -
15469 sizeof(struct lpfc_sli4_cfg_mhdr));
15470 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15471 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15472 length, LPFC_SLI4_MBX_EMBED);
15473 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015474 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015475 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15476 wq->page_count);
15477 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15478 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015479
15480 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015481 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15482 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015483
James Smartc176ffa2018-01-30 15:58:46 -080015484 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15485 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015486 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15487 else
15488 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15489
James Smart0c651872013-07-15 18:33:23 -040015490
James Smart1351e692018-02-22 08:18:43 -080015491 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15492 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15493 else
15494 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15495
15496 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015497 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015498 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15499 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015500 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15501 LPFC_Q_CREATE_VERSION_1);
15502
James Smart5a6f1332011-03-11 16:05:35 -050015503 switch (wq->entry_size) {
15504 default:
15505 case 64:
15506 bf_set(lpfc_mbx_wq_create_wqe_size,
15507 &wq_create->u.request_1,
15508 LPFC_WQ_WQE_SIZE_64);
15509 break;
15510 case 128:
15511 bf_set(lpfc_mbx_wq_create_wqe_size,
15512 &wq_create->u.request_1,
15513 LPFC_WQ_WQE_SIZE_128);
15514 break;
15515 }
James Smart1351e692018-02-22 08:18:43 -080015516 /* Request DPP by default */
15517 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015518 bf_set(lpfc_mbx_wq_create_page_size,
15519 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015520 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015521 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015522 break;
15523 default:
James Smart1351e692018-02-22 08:18:43 -080015524 page = wq_create->u.request.page;
15525 break;
James Smart5a6f1332011-03-11 16:05:35 -050015526 }
James Smart0c651872013-07-15 18:33:23 -040015527
James Smart4f774512009-05-22 14:52:35 -040015528 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015529 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015530 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15531 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015532 }
James Smart962bc512013-01-03 15:44:00 -050015533
15534 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15535 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15536
James Smart4f774512009-05-22 14:52:35 -040015537 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15538 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015539 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15540 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15541 if (shdr_status || shdr_add_status || rc) {
15542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15543 "2503 WQ_CREATE mailbox failed with "
15544 "status x%x add_status x%x, mbx status x%x\n",
15545 shdr_status, shdr_add_status, rc);
15546 status = -ENXIO;
15547 goto out;
15548 }
James Smart1351e692018-02-22 08:18:43 -080015549
15550 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15551 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15552 &wq_create->u.response);
15553 else
15554 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15555 &wq_create->u.response_1);
15556
James Smart4f774512009-05-22 14:52:35 -040015557 if (wq->queue_id == 0xFFFF) {
15558 status = -ENXIO;
15559 goto out;
15560 }
James Smart1351e692018-02-22 08:18:43 -080015561
15562 wq->db_format = LPFC_DB_LIST_FORMAT;
15563 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15564 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15565 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15566 &wq_create->u.response);
15567 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15568 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15570 "3265 WQ[%d] doorbell format "
15571 "not supported: x%x\n",
15572 wq->queue_id, wq->db_format);
15573 status = -EINVAL;
15574 goto out;
15575 }
15576 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15577 &wq_create->u.response);
15578 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15579 pci_barset);
15580 if (!bar_memmap_p) {
15581 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15582 "3263 WQ[%d] failed to memmap "
15583 "pci barset:x%x\n",
15584 wq->queue_id, pci_barset);
15585 status = -ENOMEM;
15586 goto out;
15587 }
15588 db_offset = wq_create->u.response.doorbell_offset;
15589 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15590 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15592 "3252 WQ[%d] doorbell offset "
15593 "not supported: x%x\n",
15594 wq->queue_id, db_offset);
15595 status = -EINVAL;
15596 goto out;
15597 }
15598 wq->db_regaddr = bar_memmap_p + db_offset;
15599 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15600 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15601 "format:x%x\n", wq->queue_id,
15602 pci_barset, db_offset, wq->db_format);
15603 } else
15604 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015605 } else {
James Smart1351e692018-02-22 08:18:43 -080015606 /* Check if DPP was honored by the firmware */
15607 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15608 &wq_create->u.response_1);
15609 if (wq->dpp_enable) {
15610 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15611 &wq_create->u.response_1);
15612 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15613 pci_barset);
15614 if (!bar_memmap_p) {
15615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15616 "3267 WQ[%d] failed to memmap "
15617 "pci barset:x%x\n",
15618 wq->queue_id, pci_barset);
15619 status = -ENOMEM;
15620 goto out;
15621 }
15622 db_offset = wq_create->u.response_1.doorbell_offset;
15623 wq->db_regaddr = bar_memmap_p + db_offset;
15624 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15625 &wq_create->u.response_1);
15626 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15627 &wq_create->u.response_1);
15628 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15629 dpp_barset);
15630 if (!bar_memmap_p) {
15631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15632 "3268 WQ[%d] failed to memmap "
15633 "pci barset:x%x\n",
15634 wq->queue_id, dpp_barset);
15635 status = -ENOMEM;
15636 goto out;
15637 }
15638 dpp_offset = wq_create->u.response_1.dpp_offset;
15639 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15640 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15641 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15642 "dpp_id:x%x dpp_barset:x%x "
15643 "dpp_offset:x%x\n",
15644 wq->queue_id, pci_barset, db_offset,
15645 wq->dpp_id, dpp_barset, dpp_offset);
15646
15647 /* Enable combined writes for DPP aperture */
15648 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15649#ifdef CONFIG_X86
15650 rc = set_memory_wc(pg_addr, 1);
15651 if (rc) {
15652 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15653 "3272 Cannot setup Combined "
15654 "Write on WQ[%d] - disable DPP\n",
15655 wq->queue_id);
15656 phba->cfg_enable_dpp = 0;
15657 }
15658#else
15659 phba->cfg_enable_dpp = 0;
15660#endif
15661 } else
15662 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015663 }
James Smart895427b2017-02-12 13:52:30 -080015664 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15665 if (wq->pring == NULL) {
15666 status = -ENOMEM;
15667 goto out;
15668 }
James Smart4f774512009-05-22 14:52:35 -040015669 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015670 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015671 wq->subtype = subtype;
15672 wq->host_index = 0;
15673 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050015674 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040015675
15676 /* link the wq onto the parent cq child list */
15677 list_add_tail(&wq->list, &cq->child_list);
15678out:
James Smart8fa38512009-07-19 10:01:03 -040015679 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015680 return status;
15681}
15682
15683/**
15684 * lpfc_rq_create - Create a Receive Queue on the HBA
15685 * @phba: HBA structure that indicates port to create a queue on.
15686 * @hrq: The queue structure to use to create the header receive queue.
15687 * @drq: The queue structure to use to create the data receive queue.
15688 * @cq: The completion queue to bind this work queue to.
15689 *
15690 * This function creates a receive buffer queue pair , as detailed in @hrq and
15691 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15692 * to the HBA.
15693 *
15694 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15695 * struct is used to get the entry count that is necessary to determine the
15696 * number of pages to use for this queue. The @cq is used to indicate which
15697 * completion queue to bind received buffers that are posted to these queues to.
15698 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15699 * receive queue pair. This function is asynchronous and will wait for the
15700 * mailbox command to finish before continuing.
15701 *
15702 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015703 * memory this function will return -ENOMEM. If the queue create mailbox command
15704 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015705 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015706int
James Smart4f774512009-05-22 14:52:35 -040015707lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15708 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15709{
15710 struct lpfc_mbx_rq_create *rq_create;
15711 struct lpfc_dmabuf *dmabuf;
15712 LPFC_MBOXQ_t *mbox;
15713 int rc, length, status = 0;
15714 uint32_t shdr_status, shdr_add_status;
15715 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015716 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015717 void __iomem *bar_memmap_p;
15718 uint32_t db_offset;
15719 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015720
James Smart2e90f4b2011-12-13 13:22:37 -050015721 /* sanity check on queue memory */
15722 if (!hrq || !drq || !cq)
15723 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015724 if (!phba->sli4_hba.pc_sli4_params.supported)
15725 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015726
15727 if (hrq->entry_count != drq->entry_count)
15728 return -EINVAL;
15729 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15730 if (!mbox)
15731 return -ENOMEM;
15732 length = (sizeof(struct lpfc_mbx_rq_create) -
15733 sizeof(struct lpfc_sli4_cfg_mhdr));
15734 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15735 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15736 length, LPFC_SLI4_MBX_EMBED);
15737 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015738 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15739 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15740 phba->sli4_hba.pc_sli4_params.rqv);
15741 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15742 bf_set(lpfc_rq_context_rqe_count_1,
15743 &rq_create->u.request.context,
15744 hrq->entry_count);
15745 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015746 bf_set(lpfc_rq_context_rqe_size,
15747 &rq_create->u.request.context,
15748 LPFC_RQE_SIZE_8);
15749 bf_set(lpfc_rq_context_page_size,
15750 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015751 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015752 } else {
15753 switch (hrq->entry_count) {
15754 default:
15755 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15756 "2535 Unsupported RQ count. (%d)\n",
15757 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015758 if (hrq->entry_count < 512) {
15759 status = -EINVAL;
15760 goto out;
15761 }
James Smart5a6f1332011-03-11 16:05:35 -050015762 /* otherwise default to smallest count (drop through) */
15763 case 512:
15764 bf_set(lpfc_rq_context_rqe_count,
15765 &rq_create->u.request.context,
15766 LPFC_RQ_RING_SIZE_512);
15767 break;
15768 case 1024:
15769 bf_set(lpfc_rq_context_rqe_count,
15770 &rq_create->u.request.context,
15771 LPFC_RQ_RING_SIZE_1024);
15772 break;
15773 case 2048:
15774 bf_set(lpfc_rq_context_rqe_count,
15775 &rq_create->u.request.context,
15776 LPFC_RQ_RING_SIZE_2048);
15777 break;
15778 case 4096:
15779 bf_set(lpfc_rq_context_rqe_count,
15780 &rq_create->u.request.context,
15781 LPFC_RQ_RING_SIZE_4096);
15782 break;
15783 }
15784 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15785 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015786 }
15787 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15788 cq->queue_id);
15789 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15790 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015791 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015792 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015793 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15794 putPaddrLow(dmabuf->phys);
15795 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15796 putPaddrHigh(dmabuf->phys);
15797 }
James Smart962bc512013-01-03 15:44:00 -050015798 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15799 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15800
James Smart4f774512009-05-22 14:52:35 -040015801 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15802 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015803 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15804 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15805 if (shdr_status || shdr_add_status || rc) {
15806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15807 "2504 RQ_CREATE mailbox failed with "
15808 "status x%x add_status x%x, mbx status x%x\n",
15809 shdr_status, shdr_add_status, rc);
15810 status = -ENXIO;
15811 goto out;
15812 }
15813 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15814 if (hrq->queue_id == 0xFFFF) {
15815 status = -ENXIO;
15816 goto out;
15817 }
James Smart962bc512013-01-03 15:44:00 -050015818
15819 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15820 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15821 &rq_create->u.response);
15822 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15823 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15825 "3262 RQ [%d] doorbell format not "
15826 "supported: x%x\n", hrq->queue_id,
15827 hrq->db_format);
15828 status = -EINVAL;
15829 goto out;
15830 }
15831
15832 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15833 &rq_create->u.response);
15834 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15835 if (!bar_memmap_p) {
15836 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15837 "3269 RQ[%d] failed to memmap pci "
15838 "barset:x%x\n", hrq->queue_id,
15839 pci_barset);
15840 status = -ENOMEM;
15841 goto out;
15842 }
15843
15844 db_offset = rq_create->u.response.doorbell_offset;
15845 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15846 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15847 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15848 "3270 RQ[%d] doorbell offset not "
15849 "supported: x%x\n", hrq->queue_id,
15850 db_offset);
15851 status = -EINVAL;
15852 goto out;
15853 }
15854 hrq->db_regaddr = bar_memmap_p + db_offset;
15855 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015856 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15857 "format:x%x\n", hrq->queue_id, pci_barset,
15858 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015859 } else {
15860 hrq->db_format = LPFC_DB_RING_FORMAT;
15861 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15862 }
James Smart4f774512009-05-22 14:52:35 -040015863 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015864 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015865 hrq->subtype = subtype;
15866 hrq->host_index = 0;
15867 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015868 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015869
15870 /* now create the data queue */
15871 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15872 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15873 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015874 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15875 phba->sli4_hba.pc_sli4_params.rqv);
15876 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15877 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015878 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015879 if (subtype == LPFC_NVMET)
15880 rq_create->u.request.context.buffer_size =
15881 LPFC_NVMET_DATA_BUF_SIZE;
15882 else
15883 rq_create->u.request.context.buffer_size =
15884 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015885 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15886 LPFC_RQE_SIZE_8);
15887 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15888 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015889 } else {
15890 switch (drq->entry_count) {
15891 default:
15892 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15893 "2536 Unsupported RQ count. (%d)\n",
15894 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015895 if (drq->entry_count < 512) {
15896 status = -EINVAL;
15897 goto out;
15898 }
James Smart5a6f1332011-03-11 16:05:35 -050015899 /* otherwise default to smallest count (drop through) */
15900 case 512:
15901 bf_set(lpfc_rq_context_rqe_count,
15902 &rq_create->u.request.context,
15903 LPFC_RQ_RING_SIZE_512);
15904 break;
15905 case 1024:
15906 bf_set(lpfc_rq_context_rqe_count,
15907 &rq_create->u.request.context,
15908 LPFC_RQ_RING_SIZE_1024);
15909 break;
15910 case 2048:
15911 bf_set(lpfc_rq_context_rqe_count,
15912 &rq_create->u.request.context,
15913 LPFC_RQ_RING_SIZE_2048);
15914 break;
15915 case 4096:
15916 bf_set(lpfc_rq_context_rqe_count,
15917 &rq_create->u.request.context,
15918 LPFC_RQ_RING_SIZE_4096);
15919 break;
15920 }
James Smart3c603be2017-05-15 15:20:44 -070015921 if (subtype == LPFC_NVMET)
15922 bf_set(lpfc_rq_context_buf_size,
15923 &rq_create->u.request.context,
15924 LPFC_NVMET_DATA_BUF_SIZE);
15925 else
15926 bf_set(lpfc_rq_context_buf_size,
15927 &rq_create->u.request.context,
15928 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015929 }
15930 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15931 cq->queue_id);
15932 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15933 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015934 list_for_each_entry(dmabuf, &drq->page_list, list) {
15935 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15936 putPaddrLow(dmabuf->phys);
15937 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15938 putPaddrHigh(dmabuf->phys);
15939 }
James Smart962bc512013-01-03 15:44:00 -050015940 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15941 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015942 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15943 /* The IOCTL status is embedded in the mailbox subheader. */
15944 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15945 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15946 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15947 if (shdr_status || shdr_add_status || rc) {
15948 status = -ENXIO;
15949 goto out;
15950 }
15951 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15952 if (drq->queue_id == 0xFFFF) {
15953 status = -ENXIO;
15954 goto out;
15955 }
15956 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015957 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015958 drq->subtype = subtype;
15959 drq->host_index = 0;
15960 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015961 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015962
15963 /* link the header and data RQs onto the parent cq child list */
15964 list_add_tail(&hrq->list, &cq->child_list);
15965 list_add_tail(&drq->list, &cq->child_list);
15966
15967out:
James Smart8fa38512009-07-19 10:01:03 -040015968 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015969 return status;
15970}
15971
15972/**
James Smart2d7dbc42017-02-12 13:52:35 -080015973 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15974 * @phba: HBA structure that indicates port to create a queue on.
15975 * @hrqp: The queue structure array to use to create the header receive queues.
15976 * @drqp: The queue structure array to use to create the data receive queues.
15977 * @cqp: The completion queue array to bind these receive queues to.
15978 *
15979 * This function creates a receive buffer queue pair , as detailed in @hrq and
15980 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15981 * to the HBA.
15982 *
15983 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15984 * struct is used to get the entry count that is necessary to determine the
15985 * number of pages to use for this queue. The @cq is used to indicate which
15986 * completion queue to bind received buffers that are posted to these queues to.
15987 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15988 * receive queue pair. This function is asynchronous and will wait for the
15989 * mailbox command to finish before continuing.
15990 *
15991 * On success this function will return a zero. If unable to allocate enough
15992 * memory this function will return -ENOMEM. If the queue create mailbox command
15993 * fails this function will return -ENXIO.
15994 **/
15995int
15996lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15997 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15998 uint32_t subtype)
15999{
16000 struct lpfc_queue *hrq, *drq, *cq;
16001 struct lpfc_mbx_rq_create_v2 *rq_create;
16002 struct lpfc_dmabuf *dmabuf;
16003 LPFC_MBOXQ_t *mbox;
16004 int rc, length, alloclen, status = 0;
16005 int cnt, idx, numrq, page_idx = 0;
16006 uint32_t shdr_status, shdr_add_status;
16007 union lpfc_sli4_cfg_shdr *shdr;
16008 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16009
16010 numrq = phba->cfg_nvmet_mrq;
16011 /* sanity check on array memory */
16012 if (!hrqp || !drqp || !cqp || !numrq)
16013 return -ENODEV;
16014 if (!phba->sli4_hba.pc_sli4_params.supported)
16015 hw_page_size = SLI4_PAGE_SIZE;
16016
16017 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16018 if (!mbox)
16019 return -ENOMEM;
16020
16021 length = sizeof(struct lpfc_mbx_rq_create_v2);
16022 length += ((2 * numrq * hrqp[0]->page_count) *
16023 sizeof(struct dma_address));
16024
16025 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16026 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
16027 LPFC_SLI4_MBX_NEMBED);
16028 if (alloclen < length) {
16029 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16030 "3099 Allocated DMA memory size (%d) is "
16031 "less than the requested DMA memory size "
16032 "(%d)\n", alloclen, length);
16033 status = -ENOMEM;
16034 goto out;
16035 }
16036
16037
16038
16039 rq_create = mbox->sge_array->addr[0];
16040 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
16041
16042 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
16043 cnt = 0;
16044
16045 for (idx = 0; idx < numrq; idx++) {
16046 hrq = hrqp[idx];
16047 drq = drqp[idx];
16048 cq = cqp[idx];
16049
James Smart2d7dbc42017-02-12 13:52:35 -080016050 /* sanity check on queue memory */
16051 if (!hrq || !drq || !cq) {
16052 status = -ENODEV;
16053 goto out;
16054 }
16055
James Smart7aabe842017-03-04 09:30:22 -080016056 if (hrq->entry_count != drq->entry_count) {
16057 status = -EINVAL;
16058 goto out;
16059 }
16060
James Smart2d7dbc42017-02-12 13:52:35 -080016061 if (idx == 0) {
16062 bf_set(lpfc_mbx_rq_create_num_pages,
16063 &rq_create->u.request,
16064 hrq->page_count);
16065 bf_set(lpfc_mbx_rq_create_rq_cnt,
16066 &rq_create->u.request, (numrq * 2));
16067 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
16068 1);
16069 bf_set(lpfc_rq_context_base_cq,
16070 &rq_create->u.request.context,
16071 cq->queue_id);
16072 bf_set(lpfc_rq_context_data_size,
16073 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070016074 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080016075 bf_set(lpfc_rq_context_hdr_size,
16076 &rq_create->u.request.context,
16077 LPFC_HDR_BUF_SIZE);
16078 bf_set(lpfc_rq_context_rqe_count_1,
16079 &rq_create->u.request.context,
16080 hrq->entry_count);
16081 bf_set(lpfc_rq_context_rqe_size,
16082 &rq_create->u.request.context,
16083 LPFC_RQE_SIZE_8);
16084 bf_set(lpfc_rq_context_page_size,
16085 &rq_create->u.request.context,
16086 (PAGE_SIZE/SLI4_PAGE_SIZE));
16087 }
16088 rc = 0;
16089 list_for_each_entry(dmabuf, &hrq->page_list, list) {
16090 memset(dmabuf->virt, 0, hw_page_size);
16091 cnt = page_idx + dmabuf->buffer_tag;
16092 rq_create->u.request.page[cnt].addr_lo =
16093 putPaddrLow(dmabuf->phys);
16094 rq_create->u.request.page[cnt].addr_hi =
16095 putPaddrHigh(dmabuf->phys);
16096 rc++;
16097 }
16098 page_idx += rc;
16099
16100 rc = 0;
16101 list_for_each_entry(dmabuf, &drq->page_list, list) {
16102 memset(dmabuf->virt, 0, hw_page_size);
16103 cnt = page_idx + dmabuf->buffer_tag;
16104 rq_create->u.request.page[cnt].addr_lo =
16105 putPaddrLow(dmabuf->phys);
16106 rq_create->u.request.page[cnt].addr_hi =
16107 putPaddrHigh(dmabuf->phys);
16108 rc++;
16109 }
16110 page_idx += rc;
16111
16112 hrq->db_format = LPFC_DB_RING_FORMAT;
16113 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16114 hrq->type = LPFC_HRQ;
16115 hrq->assoc_qid = cq->queue_id;
16116 hrq->subtype = subtype;
16117 hrq->host_index = 0;
16118 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070016119 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080016120
16121 drq->db_format = LPFC_DB_RING_FORMAT;
16122 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16123 drq->type = LPFC_DRQ;
16124 drq->assoc_qid = cq->queue_id;
16125 drq->subtype = subtype;
16126 drq->host_index = 0;
16127 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070016128 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080016129
16130 list_add_tail(&hrq->list, &cq->child_list);
16131 list_add_tail(&drq->list, &cq->child_list);
16132 }
16133
16134 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16135 /* The IOCTL status is embedded in the mailbox subheader. */
16136 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16137 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16138 if (shdr_status || shdr_add_status || rc) {
16139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16140 "3120 RQ_CREATE mailbox failed with "
16141 "status x%x add_status x%x, mbx status x%x\n",
16142 shdr_status, shdr_add_status, rc);
16143 status = -ENXIO;
16144 goto out;
16145 }
16146 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16147 if (rc == 0xFFFF) {
16148 status = -ENXIO;
16149 goto out;
16150 }
16151
16152 /* Initialize all RQs with associated queue id */
16153 for (idx = 0; idx < numrq; idx++) {
16154 hrq = hrqp[idx];
16155 hrq->queue_id = rc + (2 * idx);
16156 drq = drqp[idx];
16157 drq->queue_id = rc + (2 * idx) + 1;
16158 }
16159
16160out:
16161 lpfc_sli4_mbox_cmd_free(phba, mbox);
16162 return status;
16163}
16164
16165/**
James Smart4f774512009-05-22 14:52:35 -040016166 * lpfc_eq_destroy - Destroy an event Queue on the HBA
16167 * @eq: The queue structure associated with the queue to destroy.
16168 *
16169 * This function destroys a queue, as detailed in @eq by sending an mailbox
16170 * command, specific to the type of queue, to the HBA.
16171 *
16172 * The @eq struct is used to get the queue ID of the queue to destroy.
16173 *
16174 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016175 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016176 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016177int
James Smart4f774512009-05-22 14:52:35 -040016178lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16179{
16180 LPFC_MBOXQ_t *mbox;
16181 int rc, length, status = 0;
16182 uint32_t shdr_status, shdr_add_status;
16183 union lpfc_sli4_cfg_shdr *shdr;
16184
James Smart2e90f4b2011-12-13 13:22:37 -050016185 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016186 if (!eq)
16187 return -ENODEV;
16188 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16189 if (!mbox)
16190 return -ENOMEM;
16191 length = (sizeof(struct lpfc_mbx_eq_destroy) -
16192 sizeof(struct lpfc_sli4_cfg_mhdr));
16193 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16194 LPFC_MBOX_OPCODE_EQ_DESTROY,
16195 length, LPFC_SLI4_MBX_EMBED);
16196 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16197 eq->queue_id);
16198 mbox->vport = eq->phba->pport;
16199 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16200
16201 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16202 /* The IOCTL status is embedded in the mailbox subheader. */
16203 shdr = (union lpfc_sli4_cfg_shdr *)
16204 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16205 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16206 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16207 if (shdr_status || shdr_add_status || rc) {
16208 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16209 "2505 EQ_DESTROY mailbox failed with "
16210 "status x%x add_status x%x, mbx status x%x\n",
16211 shdr_status, shdr_add_status, rc);
16212 status = -ENXIO;
16213 }
16214
16215 /* Remove eq from any list */
16216 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040016217 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016218 return status;
16219}
16220
16221/**
16222 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16223 * @cq: The queue structure associated with the queue to destroy.
16224 *
16225 * This function destroys a queue, as detailed in @cq by sending an mailbox
16226 * command, specific to the type of queue, to the HBA.
16227 *
16228 * The @cq struct is used to get the queue ID of the queue to destroy.
16229 *
16230 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016231 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016232 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016233int
James Smart4f774512009-05-22 14:52:35 -040016234lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16235{
16236 LPFC_MBOXQ_t *mbox;
16237 int rc, length, status = 0;
16238 uint32_t shdr_status, shdr_add_status;
16239 union lpfc_sli4_cfg_shdr *shdr;
16240
James Smart2e90f4b2011-12-13 13:22:37 -050016241 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016242 if (!cq)
16243 return -ENODEV;
16244 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16245 if (!mbox)
16246 return -ENOMEM;
16247 length = (sizeof(struct lpfc_mbx_cq_destroy) -
16248 sizeof(struct lpfc_sli4_cfg_mhdr));
16249 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16250 LPFC_MBOX_OPCODE_CQ_DESTROY,
16251 length, LPFC_SLI4_MBX_EMBED);
16252 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16253 cq->queue_id);
16254 mbox->vport = cq->phba->pport;
16255 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16256 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16257 /* The IOCTL status is embedded in the mailbox subheader. */
16258 shdr = (union lpfc_sli4_cfg_shdr *)
16259 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16260 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16261 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16262 if (shdr_status || shdr_add_status || rc) {
16263 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16264 "2506 CQ_DESTROY mailbox failed with "
16265 "status x%x add_status x%x, mbx status x%x\n",
16266 shdr_status, shdr_add_status, rc);
16267 status = -ENXIO;
16268 }
16269 /* Remove cq from any list */
16270 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040016271 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016272 return status;
16273}
16274
16275/**
James Smart04c68492009-05-22 14:52:52 -040016276 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16277 * @qm: The queue structure associated with the queue to destroy.
16278 *
16279 * This function destroys a queue, as detailed in @mq by sending an mailbox
16280 * command, specific to the type of queue, to the HBA.
16281 *
16282 * The @mq struct is used to get the queue ID of the queue to destroy.
16283 *
16284 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016285 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040016286 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016287int
James Smart04c68492009-05-22 14:52:52 -040016288lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16289{
16290 LPFC_MBOXQ_t *mbox;
16291 int rc, length, status = 0;
16292 uint32_t shdr_status, shdr_add_status;
16293 union lpfc_sli4_cfg_shdr *shdr;
16294
James Smart2e90f4b2011-12-13 13:22:37 -050016295 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040016296 if (!mq)
16297 return -ENODEV;
16298 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16299 if (!mbox)
16300 return -ENOMEM;
16301 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16302 sizeof(struct lpfc_sli4_cfg_mhdr));
16303 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16304 LPFC_MBOX_OPCODE_MQ_DESTROY,
16305 length, LPFC_SLI4_MBX_EMBED);
16306 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16307 mq->queue_id);
16308 mbox->vport = mq->phba->pport;
16309 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16310 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16311 /* The IOCTL status is embedded in the mailbox subheader. */
16312 shdr = (union lpfc_sli4_cfg_shdr *)
16313 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16314 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16315 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16316 if (shdr_status || shdr_add_status || rc) {
16317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16318 "2507 MQ_DESTROY mailbox failed with "
16319 "status x%x add_status x%x, mbx status x%x\n",
16320 shdr_status, shdr_add_status, rc);
16321 status = -ENXIO;
16322 }
16323 /* Remove mq from any list */
16324 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016325 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016326 return status;
16327}
16328
16329/**
James Smart4f774512009-05-22 14:52:35 -040016330 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16331 * @wq: The queue structure associated with the queue to destroy.
16332 *
16333 * This function destroys a queue, as detailed in @wq by sending an mailbox
16334 * command, specific to the type of queue, to the HBA.
16335 *
16336 * The @wq struct is used to get the queue ID of the queue to destroy.
16337 *
16338 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016339 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016340 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016341int
James Smart4f774512009-05-22 14:52:35 -040016342lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16343{
16344 LPFC_MBOXQ_t *mbox;
16345 int rc, length, status = 0;
16346 uint32_t shdr_status, shdr_add_status;
16347 union lpfc_sli4_cfg_shdr *shdr;
16348
James Smart2e90f4b2011-12-13 13:22:37 -050016349 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016350 if (!wq)
16351 return -ENODEV;
16352 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16353 if (!mbox)
16354 return -ENOMEM;
16355 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16356 sizeof(struct lpfc_sli4_cfg_mhdr));
16357 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16358 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16359 length, LPFC_SLI4_MBX_EMBED);
16360 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16361 wq->queue_id);
16362 mbox->vport = wq->phba->pport;
16363 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16364 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16365 shdr = (union lpfc_sli4_cfg_shdr *)
16366 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16367 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16368 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16369 if (shdr_status || shdr_add_status || rc) {
16370 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16371 "2508 WQ_DESTROY mailbox failed with "
16372 "status x%x add_status x%x, mbx status x%x\n",
16373 shdr_status, shdr_add_status, rc);
16374 status = -ENXIO;
16375 }
16376 /* Remove wq from any list */
16377 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016378 kfree(wq->pring);
16379 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016380 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016381 return status;
16382}
16383
16384/**
16385 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16386 * @rq: The queue structure associated with the queue to destroy.
16387 *
16388 * This function destroys a queue, as detailed in @rq by sending an mailbox
16389 * command, specific to the type of queue, to the HBA.
16390 *
16391 * The @rq struct is used to get the queue ID of the queue to destroy.
16392 *
16393 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016394 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016395 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016396int
James Smart4f774512009-05-22 14:52:35 -040016397lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16398 struct lpfc_queue *drq)
16399{
16400 LPFC_MBOXQ_t *mbox;
16401 int rc, length, status = 0;
16402 uint32_t shdr_status, shdr_add_status;
16403 union lpfc_sli4_cfg_shdr *shdr;
16404
James Smart2e90f4b2011-12-13 13:22:37 -050016405 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016406 if (!hrq || !drq)
16407 return -ENODEV;
16408 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16409 if (!mbox)
16410 return -ENOMEM;
16411 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016412 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016413 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16414 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16415 length, LPFC_SLI4_MBX_EMBED);
16416 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16417 hrq->queue_id);
16418 mbox->vport = hrq->phba->pport;
16419 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16420 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16421 /* The IOCTL status is embedded in the mailbox subheader. */
16422 shdr = (union lpfc_sli4_cfg_shdr *)
16423 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16424 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16425 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16426 if (shdr_status || shdr_add_status || rc) {
16427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16428 "2509 RQ_DESTROY mailbox failed with "
16429 "status x%x add_status x%x, mbx status x%x\n",
16430 shdr_status, shdr_add_status, rc);
16431 if (rc != MBX_TIMEOUT)
16432 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16433 return -ENXIO;
16434 }
16435 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16436 drq->queue_id);
16437 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16438 shdr = (union lpfc_sli4_cfg_shdr *)
16439 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16440 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16441 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16442 if (shdr_status || shdr_add_status || rc) {
16443 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16444 "2510 RQ_DESTROY mailbox failed with "
16445 "status x%x add_status x%x, mbx status x%x\n",
16446 shdr_status, shdr_add_status, rc);
16447 status = -ENXIO;
16448 }
16449 list_del_init(&hrq->list);
16450 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016451 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016452 return status;
16453}
16454
16455/**
16456 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16457 * @phba: The virtual port for which this call being executed.
16458 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16459 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16460 * @xritag: the xritag that ties this io to the SGL pages.
16461 *
16462 * This routine will post the sgl pages for the IO that has the xritag
16463 * that is in the iocbq structure. The xritag is assigned during iocbq
16464 * creation and persists for as long as the driver is loaded.
16465 * if the caller has fewer than 256 scatter gather segments to map then
16466 * pdma_phys_addr1 should be 0.
16467 * If the caller needs to map more than 256 scatter gather segment then
16468 * pdma_phys_addr1 should be a valid physical address.
16469 * physical address for SGLs must be 64 byte aligned.
16470 * If you are going to map 2 SGL's then the first one must have 256 entries
16471 * the second sgl can have between 1 and 256 entries.
16472 *
16473 * Return codes:
16474 * 0 - Success
16475 * -ENXIO, -ENOMEM - Failure
16476 **/
16477int
16478lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16479 dma_addr_t pdma_phys_addr0,
16480 dma_addr_t pdma_phys_addr1,
16481 uint16_t xritag)
16482{
16483 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16484 LPFC_MBOXQ_t *mbox;
16485 int rc;
16486 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016487 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016488 union lpfc_sli4_cfg_shdr *shdr;
16489
16490 if (xritag == NO_XRI) {
16491 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16492 "0364 Invalid param:\n");
16493 return -EINVAL;
16494 }
16495
16496 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16497 if (!mbox)
16498 return -ENOMEM;
16499
16500 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16501 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16502 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016503 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016504
16505 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16506 &mbox->u.mqe.un.post_sgl_pages;
16507 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16508 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16509
16510 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16511 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16512 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16513 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16514
16515 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16516 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16517 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16518 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16519 if (!phba->sli4_hba.intr_enable)
16520 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016521 else {
James Smarta183a152011-10-10 21:32:43 -040016522 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016523 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16524 }
James Smart4f774512009-05-22 14:52:35 -040016525 /* The IOCTL status is embedded in the mailbox subheader. */
16526 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16527 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16528 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16529 if (rc != MBX_TIMEOUT)
16530 mempool_free(mbox, phba->mbox_mem_pool);
16531 if (shdr_status || shdr_add_status || rc) {
16532 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16533 "2511 POST_SGL mailbox failed with "
16534 "status x%x add_status x%x, mbx status x%x\n",
16535 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016536 }
16537 return 0;
16538}
James Smart4f774512009-05-22 14:52:35 -040016539
16540/**
James Smart88a2cfb2011-07-22 18:36:33 -040016541 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016542 * @phba: pointer to lpfc hba data structure.
16543 *
16544 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016545 * HBA consistent with the SLI-4 interface spec. This routine
16546 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16547 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016548 *
James Smart88a2cfb2011-07-22 18:36:33 -040016549 * Returns
16550 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16551 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16552 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016553static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016554lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16555{
16556 unsigned long xri;
16557
16558 /*
16559 * Fetch the next logical xri. Because this index is logical,
16560 * the driver starts at 0 each time.
16561 */
16562 spin_lock_irq(&phba->hbalock);
16563 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16564 phba->sli4_hba.max_cfg_param.max_xri, 0);
16565 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16566 spin_unlock_irq(&phba->hbalock);
16567 return NO_XRI;
16568 } else {
16569 set_bit(xri, phba->sli4_hba.xri_bmask);
16570 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016571 }
James Smart6d368e52011-05-24 11:44:12 -040016572 spin_unlock_irq(&phba->hbalock);
16573 return xri;
16574}
16575
16576/**
16577 * lpfc_sli4_free_xri - Release an xri for reuse.
16578 * @phba: pointer to lpfc hba data structure.
16579 *
16580 * This routine is invoked to release an xri to the pool of
16581 * available rpis maintained by the driver.
16582 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016583static void
James Smart6d368e52011-05-24 11:44:12 -040016584__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16585{
16586 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016587 phba->sli4_hba.max_cfg_param.xri_used--;
16588 }
16589}
16590
16591/**
16592 * lpfc_sli4_free_xri - Release an xri for reuse.
16593 * @phba: pointer to lpfc hba data structure.
16594 *
16595 * This routine is invoked to release an xri to the pool of
16596 * available rpis maintained by the driver.
16597 **/
16598void
16599lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16600{
16601 spin_lock_irq(&phba->hbalock);
16602 __lpfc_sli4_free_xri(phba, xri);
16603 spin_unlock_irq(&phba->hbalock);
16604}
16605
16606/**
James Smart4f774512009-05-22 14:52:35 -040016607 * lpfc_sli4_next_xritag - Get an xritag for the io
16608 * @phba: Pointer to HBA context object.
16609 *
16610 * This function gets an xritag for the iocb. If there is no unused xritag
16611 * it will return 0xffff.
16612 * The function returns the allocated xritag if successful, else returns zero.
16613 * Zero is not a valid xritag.
16614 * The caller is not required to hold any lock.
16615 **/
16616uint16_t
16617lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16618{
James Smart6d368e52011-05-24 11:44:12 -040016619 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016620
James Smart6d368e52011-05-24 11:44:12 -040016621 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016622 if (xri_index == NO_XRI)
16623 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16624 "2004 Failed to allocate XRI.last XRITAG is %d"
16625 " Max XRI is %d, Used XRI is %d\n",
16626 xri_index,
16627 phba->sli4_hba.max_cfg_param.max_xri,
16628 phba->sli4_hba.max_cfg_param.xri_used);
16629 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016630}
16631
16632/**
James Smart895427b2017-02-12 13:52:30 -080016633 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016634 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016635 * @post_sgl_list: pointer to els sgl entry list.
16636 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016637 *
16638 * This routine is invoked to post a block of driver's sgl pages to the
16639 * HBA using non-embedded mailbox command. No Lock is held. This routine
16640 * is only called when the driver is loading and after all IO has been
16641 * stopped.
16642 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016643static int
James Smart895427b2017-02-12 13:52:30 -080016644lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016645 struct list_head *post_sgl_list,
16646 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016647{
James Smart8a9d2e82012-05-09 21:16:12 -040016648 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016649 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16650 struct sgl_page_pairs *sgl_pg_pairs;
16651 void *viraddr;
16652 LPFC_MBOXQ_t *mbox;
16653 uint32_t reqlen, alloclen, pg_pairs;
16654 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016655 uint16_t xritag_start = 0;
16656 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016657 uint32_t shdr_status, shdr_add_status;
16658 union lpfc_sli4_cfg_shdr *shdr;
16659
James Smart895427b2017-02-12 13:52:30 -080016660 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016661 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016662 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016664 "2559 Block sgl registration required DMA "
16665 "size (%d) great than a page\n", reqlen);
16666 return -ENOMEM;
16667 }
James Smart895427b2017-02-12 13:52:30 -080016668
James Smart4f774512009-05-22 14:52:35 -040016669 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016670 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016671 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016672
16673 /* Allocate DMA memory and set up the non-embedded mailbox command */
16674 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16675 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16676 LPFC_SLI4_MBX_NEMBED);
16677
16678 if (alloclen < reqlen) {
16679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16680 "0285 Allocated DMA memory size (%d) is "
16681 "less than the requested DMA memory "
16682 "size (%d)\n", alloclen, reqlen);
16683 lpfc_sli4_mbox_cmd_free(phba, mbox);
16684 return -ENOMEM;
16685 }
James Smart4f774512009-05-22 14:52:35 -040016686 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016687 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016688 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16689 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16690
James Smart8a9d2e82012-05-09 21:16:12 -040016691 pg_pairs = 0;
16692 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016693 /* Set up the sge entry */
16694 sgl_pg_pairs->sgl_pg0_addr_lo =
16695 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16696 sgl_pg_pairs->sgl_pg0_addr_hi =
16697 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16698 sgl_pg_pairs->sgl_pg1_addr_lo =
16699 cpu_to_le32(putPaddrLow(0));
16700 sgl_pg_pairs->sgl_pg1_addr_hi =
16701 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016702
James Smart4f774512009-05-22 14:52:35 -040016703 /* Keep the first xritag on the list */
16704 if (pg_pairs == 0)
16705 xritag_start = sglq_entry->sli4_xritag;
16706 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016707 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016708 }
James Smart6d368e52011-05-24 11:44:12 -040016709
16710 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016711 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016712 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016713 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016714
James Smart4f774512009-05-22 14:52:35 -040016715 if (!phba->sli4_hba.intr_enable)
16716 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16717 else {
James Smarta183a152011-10-10 21:32:43 -040016718 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016719 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16720 }
16721 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16722 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16723 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16724 if (rc != MBX_TIMEOUT)
16725 lpfc_sli4_mbox_cmd_free(phba, mbox);
16726 if (shdr_status || shdr_add_status || rc) {
16727 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16728 "2513 POST_SGL_BLOCK mailbox command failed "
16729 "status x%x add_status x%x mbx status x%x\n",
16730 shdr_status, shdr_add_status, rc);
16731 rc = -ENXIO;
16732 }
16733 return rc;
16734}
16735
16736/**
16737 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16738 * @phba: pointer to lpfc hba data structure.
16739 * @sblist: pointer to scsi buffer list.
16740 * @count: number of scsi buffers on the list.
16741 *
16742 * This routine is invoked to post a block of @count scsi sgl pages from a
16743 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16744 * No Lock is held.
16745 *
16746 **/
16747int
James Smart8a9d2e82012-05-09 21:16:12 -040016748lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16749 struct list_head *sblist,
16750 int count)
James Smart4f774512009-05-22 14:52:35 -040016751{
16752 struct lpfc_scsi_buf *psb;
16753 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16754 struct sgl_page_pairs *sgl_pg_pairs;
16755 void *viraddr;
16756 LPFC_MBOXQ_t *mbox;
16757 uint32_t reqlen, alloclen, pg_pairs;
16758 uint32_t mbox_tmo;
16759 uint16_t xritag_start = 0;
16760 int rc = 0;
16761 uint32_t shdr_status, shdr_add_status;
16762 dma_addr_t pdma_phys_bpl1;
16763 union lpfc_sli4_cfg_shdr *shdr;
16764
16765 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016766 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016767 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016768 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016769 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16770 "0217 Block sgl registration required DMA "
16771 "size (%d) great than a page\n", reqlen);
16772 return -ENOMEM;
16773 }
16774 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16775 if (!mbox) {
16776 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16777 "0283 Failed to allocate mbox cmd memory\n");
16778 return -ENOMEM;
16779 }
16780
16781 /* Allocate DMA memory and set up the non-embedded mailbox command */
16782 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16783 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16784 LPFC_SLI4_MBX_NEMBED);
16785
16786 if (alloclen < reqlen) {
16787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16788 "2561 Allocated DMA memory size (%d) is "
16789 "less than the requested DMA memory "
16790 "size (%d)\n", alloclen, reqlen);
16791 lpfc_sli4_mbox_cmd_free(phba, mbox);
16792 return -ENOMEM;
16793 }
James Smart6d368e52011-05-24 11:44:12 -040016794
James Smart4f774512009-05-22 14:52:35 -040016795 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016796 viraddr = mbox->sge_array->addr[0];
16797
16798 /* Set up the SGL pages in the non-embedded DMA pages */
16799 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16800 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16801
16802 pg_pairs = 0;
16803 list_for_each_entry(psb, sblist, list) {
16804 /* Set up the sge entry */
16805 sgl_pg_pairs->sgl_pg0_addr_lo =
16806 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16807 sgl_pg_pairs->sgl_pg0_addr_hi =
16808 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16809 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16810 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16811 else
16812 pdma_phys_bpl1 = 0;
16813 sgl_pg_pairs->sgl_pg1_addr_lo =
16814 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16815 sgl_pg_pairs->sgl_pg1_addr_hi =
16816 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16817 /* Keep the first xritag on the list */
16818 if (pg_pairs == 0)
16819 xritag_start = psb->cur_iocbq.sli4_xritag;
16820 sgl_pg_pairs++;
16821 pg_pairs++;
16822 }
16823 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16824 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16825 /* Perform endian conversion if necessary */
16826 sgl->word0 = cpu_to_le32(sgl->word0);
16827
16828 if (!phba->sli4_hba.intr_enable)
16829 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16830 else {
James Smarta183a152011-10-10 21:32:43 -040016831 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016832 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16833 }
16834 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16835 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16836 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16837 if (rc != MBX_TIMEOUT)
16838 lpfc_sli4_mbox_cmd_free(phba, mbox);
16839 if (shdr_status || shdr_add_status || rc) {
16840 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16841 "2564 POST_SGL_BLOCK mailbox command failed "
16842 "status x%x add_status x%x mbx status x%x\n",
16843 shdr_status, shdr_add_status, rc);
16844 rc = -ENXIO;
16845 }
16846 return rc;
16847}
16848
16849/**
16850 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16851 * @phba: pointer to lpfc_hba struct that the frame was received on
16852 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16853 *
16854 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16855 * valid type of frame that the LPFC driver will handle. This function will
16856 * return a zero if the frame is a valid frame or a non zero value when the
16857 * frame does not pass the check.
16858 **/
16859static int
16860lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16861{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016862 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016863 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016864 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016865
James Smartae9e28f2017-05-15 15:20:51 -070016866#define FC_RCTL_MDS_DIAGS 0xF4
16867
James Smart4f774512009-05-22 14:52:35 -040016868 switch (fc_hdr->fh_r_ctl) {
16869 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16870 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16871 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16872 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16873 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16874 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16875 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16876 case FC_RCTL_DD_CMD_STATUS: /* command status */
16877 case FC_RCTL_ELS_REQ: /* extended link services request */
16878 case FC_RCTL_ELS_REP: /* extended link services reply */
16879 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16880 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16881 case FC_RCTL_BA_NOP: /* basic link service NOP */
16882 case FC_RCTL_BA_ABTS: /* basic link service abort */
16883 case FC_RCTL_BA_RMC: /* remove connection */
16884 case FC_RCTL_BA_ACC: /* basic accept */
16885 case FC_RCTL_BA_RJT: /* basic reject */
16886 case FC_RCTL_BA_PRMT:
16887 case FC_RCTL_ACK_1: /* acknowledge_1 */
16888 case FC_RCTL_ACK_0: /* acknowledge_0 */
16889 case FC_RCTL_P_RJT: /* port reject */
16890 case FC_RCTL_F_RJT: /* fabric reject */
16891 case FC_RCTL_P_BSY: /* port busy */
16892 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16893 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16894 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016895 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016896 case FC_RCTL_END: /* end */
16897 break;
16898 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16899 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16900 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16901 return lpfc_fc_frame_check(phba, fc_hdr);
16902 default:
16903 goto drop;
16904 }
James Smartae9e28f2017-05-15 15:20:51 -070016905
16906#define FC_TYPE_VENDOR_UNIQUE 0xFF
16907
James Smart4f774512009-05-22 14:52:35 -040016908 switch (fc_hdr->fh_type) {
16909 case FC_TYPE_BLS:
16910 case FC_TYPE_ELS:
16911 case FC_TYPE_FCP:
16912 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016913 case FC_TYPE_NVME:
James Smartae9e28f2017-05-15 15:20:51 -070016914 case FC_TYPE_VENDOR_UNIQUE:
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);
17744}
17745
17746static void
17747lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17748 struct hbq_dmabuf *dmabuf)
17749{
17750 struct fc_frame_header *fc_hdr;
17751 struct lpfc_hba *phba = vport->phba;
17752 struct lpfc_iocbq *iocbq = NULL;
17753 union lpfc_wqe *wqe;
17754 struct lpfc_dmabuf *pcmd = NULL;
17755 uint32_t frame_len;
17756 int rc;
17757
17758 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17759 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17760
17761 /* Send the received frame back */
17762 iocbq = lpfc_sli_get_iocbq(phba);
17763 if (!iocbq)
17764 goto exit;
17765
17766 /* Allocate buffer for command payload */
17767 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17768 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017769 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017770 &pcmd->phys);
17771 if (!pcmd || !pcmd->virt)
17772 goto exit;
17773
17774 INIT_LIST_HEAD(&pcmd->list);
17775
17776 /* copyin the payload */
17777 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17778
17779 /* fill in BDE's for command */
17780 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17781 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17782 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17783 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17784
17785 iocbq->context2 = pcmd;
17786 iocbq->vport = vport;
17787 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17788 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17789
17790 /*
17791 * Setup rest of the iocb as though it were a WQE
17792 * Build the SEND_FRAME WQE
17793 */
17794 wqe = (union lpfc_wqe *)&iocbq->iocb;
17795
17796 wqe->send_frame.frame_len = frame_len;
17797 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17798 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17799 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17800 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17801 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17802 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17803
17804 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17805 iocbq->iocb.ulpLe = 1;
17806 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17807 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17808 if (rc == IOCB_ERROR)
17809 goto exit;
17810
17811 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17812 return;
17813
17814exit:
17815 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17816 "2023 Unable to process MDS loopback frame\n");
17817 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017818 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017819 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017820 if (iocbq)
17821 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017822 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17823}
17824
James Smart4f774512009-05-22 14:52:35 -040017825/**
17826 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17827 * @phba: Pointer to HBA context object.
17828 *
17829 * This function is called with no lock held. This function processes all
17830 * the received buffers and gives it to upper layers when a received buffer
17831 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017832 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017833 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17834 * appropriate receive function when the final frame in a sequence is received.
17835 **/
James Smart4d9ab992009-10-02 15:16:39 -040017836void
17837lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17838 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017839{
James Smart4d9ab992009-10-02 15:16:39 -040017840 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017841 struct fc_frame_header *fc_hdr;
17842 struct lpfc_vport *vport;
17843 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017844 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017845
James Smart4f774512009-05-22 14:52:35 -040017846 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017847 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017848
James Smart4d9ab992009-10-02 15:16:39 -040017849 /* check to see if this a valid type of frame */
17850 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17851 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17852 return;
17853 }
James Smart2ea259e2017-02-12 13:52:27 -080017854
James Smart7851fe22011-07-22 18:36:52 -040017855 if ((bf_get(lpfc_cqe_code,
17856 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17857 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17858 &dmabuf->cq_event.cqe.rcqe_cmpl);
17859 else
17860 fcfi = bf_get(lpfc_rcqe_fcf_id,
17861 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017862
James Smartae9e28f2017-05-15 15:20:51 -070017863 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17864 vport = phba->pport;
17865 /* Handle MDS Loopback frames */
17866 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17867 return;
17868 }
17869
James Smart895427b2017-02-12 13:52:30 -080017870 /* d_id this frame is directed to */
17871 did = sli4_did_from_fc_hdr(fc_hdr);
17872
17873 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017874 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017875 /* throw out the frame */
17876 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17877 return;
17878 }
James Smart939723a2012-05-09 21:19:03 -040017879
James Smart939723a2012-05-09 21:19:03 -040017880 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17881 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17882 (did != Fabric_DID)) {
17883 /*
17884 * Throw out the frame if we are not pt2pt.
17885 * The pt2pt protocol allows for discovery frames
17886 * to be received without a registered VPI.
17887 */
17888 if (!(vport->fc_flag & FC_PT2PT) ||
17889 (phba->link_state == LPFC_HBA_READY)) {
17890 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17891 return;
17892 }
17893 }
17894
James Smart6669f9b2009-10-02 15:16:45 -040017895 /* Handle the basic abort sequence (BA_ABTS) event */
17896 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17897 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17898 return;
17899 }
17900
James Smart4d9ab992009-10-02 15:16:39 -040017901 /* Link this frame */
17902 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17903 if (!seq_dmabuf) {
17904 /* unable to add frame to vport - throw it out */
17905 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17906 return;
17907 }
17908 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017909 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017910 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017911
James Smart6669f9b2009-10-02 15:16:45 -040017912 /* Send the complete sequence to the upper layer protocol */
17913 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017914}
James Smart6fb120a2009-05-22 14:52:59 -040017915
17916/**
17917 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17918 * @phba: pointer to lpfc hba data structure.
17919 *
17920 * This routine is invoked to post rpi header templates to the
17921 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017922 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17923 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017924 *
17925 * This routine does not require any locks. It's usage is expected
17926 * to be driver load or reset recovery when the driver is
17927 * sequential.
17928 *
17929 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017930 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017931 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017932 * When this error occurs, the driver is not guaranteed
17933 * to have any rpi regions posted to the device and
17934 * must either attempt to repost the regions or take a
17935 * fatal error.
17936 **/
17937int
17938lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17939{
17940 struct lpfc_rpi_hdr *rpi_page;
17941 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017942 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017943
James Smart6d368e52011-05-24 11:44:12 -040017944 /* SLI4 ports that support extents do not require RPI headers. */
17945 if (!phba->sli4_hba.rpi_hdrs_in_use)
17946 goto exit;
17947 if (phba->sli4_hba.extents_in_use)
17948 return -EIO;
17949
James Smart6fb120a2009-05-22 14:52:59 -040017950 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017951 /*
17952 * Assign the rpi headers a physical rpi only if the driver
17953 * has not initialized those resources. A port reset only
17954 * needs the headers posted.
17955 */
17956 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17957 LPFC_RPI_RSRC_RDY)
17958 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17959
James Smart6fb120a2009-05-22 14:52:59 -040017960 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17961 if (rc != MBX_SUCCESS) {
17962 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17963 "2008 Error %d posting all rpi "
17964 "headers\n", rc);
17965 rc = -EIO;
17966 break;
17967 }
17968 }
17969
James Smart6d368e52011-05-24 11:44:12 -040017970 exit:
17971 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17972 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017973 return rc;
17974}
17975
17976/**
17977 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17978 * @phba: pointer to lpfc hba data structure.
17979 * @rpi_page: pointer to the rpi memory region.
17980 *
17981 * This routine is invoked to post a single rpi header to the
17982 * HBA consistent with the SLI-4 interface spec. This memory region
17983 * maps up to 64 rpi context regions.
17984 *
17985 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017986 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017987 * -ENOMEM - No available memory
17988 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017989 **/
17990int
17991lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17992{
17993 LPFC_MBOXQ_t *mboxq;
17994 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17995 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017996 uint32_t shdr_status, shdr_add_status;
17997 union lpfc_sli4_cfg_shdr *shdr;
17998
James Smart6d368e52011-05-24 11:44:12 -040017999 /* SLI4 ports that support extents do not require RPI headers. */
18000 if (!phba->sli4_hba.rpi_hdrs_in_use)
18001 return rc;
18002 if (phba->sli4_hba.extents_in_use)
18003 return -EIO;
18004
James Smart6fb120a2009-05-22 14:52:59 -040018005 /* The port is notified of the header region via a mailbox command. */
18006 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18007 if (!mboxq) {
18008 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18009 "2001 Unable to allocate memory for issuing "
18010 "SLI_CONFIG_SPECIAL mailbox command\n");
18011 return -ENOMEM;
18012 }
18013
18014 /* Post all rpi memory regions to the port. */
18015 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040018016 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18017 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18018 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050018019 sizeof(struct lpfc_sli4_cfg_mhdr),
18020 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040018021
18022
18023 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040018024 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18025 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040018026 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18027 hdr_tmpl, rpi_page->page_count);
18028
James Smart6fb120a2009-05-22 14:52:59 -040018029 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18030 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040018031 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040018032 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18033 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18034 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18035 if (rc != MBX_TIMEOUT)
18036 mempool_free(mboxq, phba->mbox_mem_pool);
18037 if (shdr_status || shdr_add_status || rc) {
18038 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18039 "2514 POST_RPI_HDR mailbox failed with "
18040 "status x%x add_status x%x, mbx status x%x\n",
18041 shdr_status, shdr_add_status, rc);
18042 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070018043 } else {
18044 /*
18045 * The next_rpi stores the next logical module-64 rpi value used
18046 * to post physical rpis in subsequent rpi postings.
18047 */
18048 spin_lock_irq(&phba->hbalock);
18049 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18050 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040018051 }
18052 return rc;
18053}
18054
18055/**
18056 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18057 * @phba: pointer to lpfc hba data structure.
18058 *
18059 * This routine is invoked to post rpi header templates to the
18060 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018061 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18062 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018063 *
18064 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018065 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040018066 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
18067 **/
18068int
18069lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18070{
James Smart6d368e52011-05-24 11:44:12 -040018071 unsigned long rpi;
18072 uint16_t max_rpi, rpi_limit;
18073 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018074 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040018075 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040018076
James Smart6fb120a2009-05-22 14:52:59 -040018077 /*
James Smart6d368e52011-05-24 11:44:12 -040018078 * Fetch the next logical rpi. Because this index is logical,
18079 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040018080 */
James Smart4902b382013-10-10 12:20:35 -040018081 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040018082 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18083 rpi_limit = phba->sli4_hba.next_rpi;
18084
James Smart6d368e52011-05-24 11:44:12 -040018085 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18086 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040018087 rpi = LPFC_RPI_ALLOC_ERROR;
18088 else {
18089 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18090 phba->sli4_hba.max_cfg_param.rpi_used++;
18091 phba->sli4_hba.rpi_count++;
18092 }
James Smartbe6bb942015-04-07 15:07:22 -040018093 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18094 "0001 rpi:%x max:%x lim:%x\n",
18095 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040018096
18097 /*
18098 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040018099 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040018100 */
18101 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18102 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040018103 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018104 return rpi;
18105 }
18106
18107 /*
James Smart6d368e52011-05-24 11:44:12 -040018108 * RPI header postings are not required for SLI4 ports capable of
18109 * extents.
18110 */
18111 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040018112 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040018113 return rpi;
18114 }
18115
18116 /*
James Smart6fb120a2009-05-22 14:52:59 -040018117 * If the driver is running low on rpi resources, allocate another
18118 * page now. Note that the next_rpi value is used because
18119 * it represents how many are actually in use whereas max_rpi notes
18120 * how many are supported max by the device.
18121 */
James Smart6d368e52011-05-24 11:44:12 -040018122 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040018123 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018124 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18125 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18126 if (!rpi_hdr) {
18127 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18128 "2002 Error Could not grow rpi "
18129 "count\n");
18130 } else {
James Smart6d368e52011-05-24 11:44:12 -040018131 lrpi = rpi_hdr->start_rpi;
18132 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040018133 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18134 }
18135 }
18136
18137 return rpi;
18138}
18139
18140/**
18141 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18142 * @phba: pointer to lpfc hba data structure.
18143 *
18144 * This routine is invoked to release an rpi to the pool of
18145 * available rpis maintained by the driver.
18146 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018147static void
James Smartd7c47992010-06-08 18:31:54 -040018148__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18149{
18150 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18151 phba->sli4_hba.rpi_count--;
18152 phba->sli4_hba.max_cfg_param.rpi_used--;
18153 }
18154}
18155
18156/**
18157 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18158 * @phba: pointer to lpfc hba data structure.
18159 *
18160 * This routine is invoked to release an rpi to the pool of
18161 * available rpis maintained by the driver.
18162 **/
18163void
James Smart6fb120a2009-05-22 14:52:59 -040018164lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18165{
18166 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040018167 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040018168 spin_unlock_irq(&phba->hbalock);
18169}
18170
18171/**
18172 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18173 * @phba: pointer to lpfc hba data structure.
18174 *
18175 * This routine is invoked to remove the memory region that
18176 * provided rpi via a bitmask.
18177 **/
18178void
18179lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18180{
18181 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040018182 kfree(phba->sli4_hba.rpi_ids);
18183 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040018184}
18185
18186/**
18187 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18188 * @phba: pointer to lpfc hba data structure.
18189 *
18190 * This routine is invoked to remove the memory region that
18191 * provided rpi via a bitmask.
18192 **/
18193int
James Smart6b5151f2012-01-18 16:24:06 -050018194lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18195 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040018196{
18197 LPFC_MBOXQ_t *mboxq;
18198 struct lpfc_hba *phba = ndlp->phba;
18199 int rc;
18200
18201 /* The port is notified of the header region via a mailbox command. */
18202 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18203 if (!mboxq)
18204 return -ENOMEM;
18205
18206 /* Post all rpi memory regions to the port. */
18207 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050018208 if (cmpl) {
18209 mboxq->mbox_cmpl = cmpl;
18210 mboxq->context1 = arg;
18211 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050018212 } else
18213 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050018214 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040018215 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18216 if (rc == MBX_NOT_FINISHED) {
18217 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18218 "2010 Resume RPI Mailbox failed "
18219 "status %d, mbxStatus x%x\n", rc,
18220 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18221 mempool_free(mboxq, phba->mbox_mem_pool);
18222 return -EIO;
18223 }
18224 return 0;
18225}
18226
18227/**
18228 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050018229 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040018230 *
James Smart76a95d72010-11-20 23:11:48 -050018231 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040018232 *
18233 * Returns:
18234 * 0 success
18235 * -Evalue otherwise
18236 **/
18237int
James Smart76a95d72010-11-20 23:11:48 -050018238lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040018239{
18240 LPFC_MBOXQ_t *mboxq;
18241 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040018242 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040018243 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050018244 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040018245 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18246 if (!mboxq)
18247 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050018248 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040018249 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040018250 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040018251 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050018252 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040018253 "2022 INIT VPI Mailbox failed "
18254 "status %d, mbxStatus x%x\n", rc,
18255 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040018256 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040018257 }
James Smart6a9c52c2009-10-02 15:16:51 -040018258 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050018259 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040018260
18261 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040018262}
18263
18264/**
18265 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
18266 * @phba: pointer to lpfc hba data structure.
18267 * @mboxq: Pointer to mailbox object.
18268 *
18269 * This routine is invoked to manually add a single FCF record. The caller
18270 * must pass a completely initialized FCF_Record. This routine takes
18271 * care of the nonembedded mailbox operations.
18272 **/
18273static void
18274lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
18275{
18276 void *virt_addr;
18277 union lpfc_sli4_cfg_shdr *shdr;
18278 uint32_t shdr_status, shdr_add_status;
18279
18280 virt_addr = mboxq->sge_array->addr[0];
18281 /* The IOCTL status is embedded in the mailbox subheader. */
18282 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18283 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18284 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18285
18286 if ((shdr_status || shdr_add_status) &&
18287 (shdr_status != STATUS_FCF_IN_USE))
18288 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18289 "2558 ADD_FCF_RECORD mailbox failed with "
18290 "status x%x add_status x%x\n",
18291 shdr_status, shdr_add_status);
18292
18293 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18294}
18295
18296/**
18297 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18298 * @phba: pointer to lpfc hba data structure.
18299 * @fcf_record: pointer to the initialized fcf record to add.
18300 *
18301 * This routine is invoked to manually add a single FCF record. The caller
18302 * must pass a completely initialized FCF_Record. This routine takes
18303 * care of the nonembedded mailbox operations.
18304 **/
18305int
18306lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18307{
18308 int rc = 0;
18309 LPFC_MBOXQ_t *mboxq;
18310 uint8_t *bytep;
18311 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040018312 struct lpfc_mbx_sge sge;
18313 uint32_t alloc_len, req_len;
18314 uint32_t fcfindex;
18315
18316 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18317 if (!mboxq) {
18318 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18319 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18320 return -ENOMEM;
18321 }
18322
18323 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18324 sizeof(uint32_t);
18325
18326 /* Allocate DMA memory and set up the non-embedded mailbox command */
18327 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18328 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18329 req_len, LPFC_SLI4_MBX_NEMBED);
18330 if (alloc_len < req_len) {
18331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18332 "2523 Allocated DMA memory size (x%x) is "
18333 "less than the requested DMA memory "
18334 "size (x%x)\n", alloc_len, req_len);
18335 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18336 return -ENOMEM;
18337 }
18338
18339 /*
18340 * Get the first SGE entry from the non-embedded DMA memory. This
18341 * routine only uses a single SGE.
18342 */
18343 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018344 virt_addr = mboxq->sge_array->addr[0];
18345 /*
18346 * Configure the FCF record for FCFI 0. This is the driver's
18347 * hardcoded default and gets used in nonFIP mode.
18348 */
18349 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18350 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18351 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18352
18353 /*
18354 * Copy the fcf_index and the FCF Record Data. The data starts after
18355 * the FCoE header plus word10. The data copy needs to be endian
18356 * correct.
18357 */
18358 bytep += sizeof(uint32_t);
18359 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18360 mboxq->vport = phba->pport;
18361 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18362 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18363 if (rc == MBX_NOT_FINISHED) {
18364 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18365 "2515 ADD_FCF_RECORD mailbox failed with "
18366 "status 0x%x\n", rc);
18367 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18368 rc = -EIO;
18369 } else
18370 rc = 0;
18371
18372 return rc;
18373}
18374
18375/**
18376 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18377 * @phba: pointer to lpfc hba data structure.
18378 * @fcf_record: pointer to the fcf record to write the default data.
18379 * @fcf_index: FCF table entry index.
18380 *
18381 * This routine is invoked to build the driver's default FCF record. The
18382 * values used are hardcoded. This routine handles memory initialization.
18383 *
18384 **/
18385void
18386lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18387 struct fcf_record *fcf_record,
18388 uint16_t fcf_index)
18389{
18390 memset(fcf_record, 0, sizeof(struct fcf_record));
18391 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18392 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18393 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18394 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18395 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18396 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18397 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18398 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18399 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18400 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18401 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18402 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18403 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018404 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018405 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18406 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18407 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18408 /* Set the VLAN bit map */
18409 if (phba->valid_vlan) {
18410 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18411 = 1 << (phba->vlan_id % 8);
18412 }
18413}
18414
18415/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018416 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018417 * @phba: pointer to lpfc hba data structure.
18418 * @fcf_index: FCF table entry offset.
18419 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018420 * This routine is invoked to scan the entire FCF table by reading FCF
18421 * record and processing it one at a time starting from the @fcf_index
18422 * for initial FCF discovery or fast FCF failover rediscovery.
18423 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018424 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018425 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018426 **/
18427int
James Smart0c9ab6f2010-02-26 14:15:57 -050018428lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018429{
18430 int rc = 0, error;
18431 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018432
James Smart32b97932009-07-19 10:01:21 -040018433 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018434 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018435 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18436 if (!mboxq) {
18437 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18438 "2000 Failed to allocate mbox for "
18439 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018440 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018441 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018442 }
James Smartecfd03c2010-02-12 14:41:27 -050018443 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018444 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018445 if (rc) {
18446 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018447 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018448 }
James Smartecfd03c2010-02-12 14:41:27 -050018449 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018450 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018451 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018452
18453 spin_lock_irq(&phba->hbalock);
18454 phba->hba_flag |= FCF_TS_INPROG;
18455 spin_unlock_irq(&phba->hbalock);
18456
James Smart6fb120a2009-05-22 14:52:59 -040018457 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018458 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018459 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018460 else {
James Smart38b92ef2010-08-04 16:11:39 -040018461 /* Reset eligible FCF count for new scan */
18462 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018463 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018464 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018465 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018466fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018467 if (error) {
18468 if (mboxq)
18469 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018470 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018471 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018472 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018473 spin_unlock_irq(&phba->hbalock);
18474 }
James Smart6fb120a2009-05-22 14:52:59 -040018475 return error;
18476}
James Smarta0c87cb2009-07-19 10:01:10 -040018477
18478/**
James Smarta93ff372010-10-22 11:06:08 -040018479 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018480 * @phba: pointer to lpfc hba data structure.
18481 * @fcf_index: FCF table entry offset.
18482 *
18483 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018484 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018485 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018486 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018487 * otherwise.
18488 **/
18489int
18490lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18491{
18492 int rc = 0, error;
18493 LPFC_MBOXQ_t *mboxq;
18494
18495 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18496 if (!mboxq) {
18497 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18498 "2763 Failed to allocate mbox for "
18499 "READ_FCF cmd\n");
18500 error = -ENOMEM;
18501 goto fail_fcf_read;
18502 }
18503 /* Construct the read FCF record mailbox command */
18504 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18505 if (rc) {
18506 error = -EINVAL;
18507 goto fail_fcf_read;
18508 }
18509 /* Issue the mailbox command asynchronously */
18510 mboxq->vport = phba->pport;
18511 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18512 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18513 if (rc == MBX_NOT_FINISHED)
18514 error = -EIO;
18515 else
18516 error = 0;
18517
18518fail_fcf_read:
18519 if (error && mboxq)
18520 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18521 return error;
18522}
18523
18524/**
18525 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18526 * @phba: pointer to lpfc hba data structure.
18527 * @fcf_index: FCF table entry offset.
18528 *
18529 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018530 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018531 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018532 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018533 * otherwise.
18534 **/
18535int
18536lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18537{
18538 int rc = 0, error;
18539 LPFC_MBOXQ_t *mboxq;
18540
18541 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18542 if (!mboxq) {
18543 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18544 "2758 Failed to allocate mbox for "
18545 "READ_FCF cmd\n");
18546 error = -ENOMEM;
18547 goto fail_fcf_read;
18548 }
18549 /* Construct the read FCF record mailbox command */
18550 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18551 if (rc) {
18552 error = -EINVAL;
18553 goto fail_fcf_read;
18554 }
18555 /* Issue the mailbox command asynchronously */
18556 mboxq->vport = phba->pport;
18557 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18558 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18559 if (rc == MBX_NOT_FINISHED)
18560 error = -EIO;
18561 else
18562 error = 0;
18563
18564fail_fcf_read:
18565 if (error && mboxq)
18566 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18567 return error;
18568}
18569
18570/**
James Smartf5cb5302015-12-16 18:11:52 -050018571 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018572 * phba pointer to the lpfc_hba struct for this port.
18573 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18574 * routine when the rr_bmask is empty. The FCF indecies are put into the
18575 * rr_bmask based on their priority level. Starting from the highest priority
18576 * to the lowest. The most likely FCF candidate will be in the highest
18577 * priority group. When this routine is called it searches the fcf_pri list for
18578 * next lowest priority group and repopulates the rr_bmask with only those
18579 * fcf_indexes.
18580 * returns:
18581 * 1=success 0=failure
18582 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018583static int
James Smart7d791df2011-07-22 18:37:52 -040018584lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18585{
18586 uint16_t next_fcf_pri;
18587 uint16_t last_index;
18588 struct lpfc_fcf_pri *fcf_pri;
18589 int rc;
18590 int ret = 0;
18591
18592 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18593 LPFC_SLI4_FCF_TBL_INDX_MAX);
18594 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18595 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018596
18597 /* Verify the priority list has 2 or more entries */
18598 spin_lock_irq(&phba->hbalock);
18599 if (list_empty(&phba->fcf.fcf_pri_list) ||
18600 list_is_singular(&phba->fcf.fcf_pri_list)) {
18601 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018602 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18603 "3061 Last IDX %d\n", last_index);
18604 return 0; /* Empty rr list */
18605 }
James Smart25626692013-03-01 16:36:54 -050018606 spin_unlock_irq(&phba->hbalock);
18607
James Smart7d791df2011-07-22 18:37:52 -040018608 next_fcf_pri = 0;
18609 /*
18610 * Clear the rr_bmask and set all of the bits that are at this
18611 * priority.
18612 */
18613 memset(phba->fcf.fcf_rr_bmask, 0,
18614 sizeof(*phba->fcf.fcf_rr_bmask));
18615 spin_lock_irq(&phba->hbalock);
18616 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18617 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18618 continue;
18619 /*
18620 * the 1st priority that has not FLOGI failed
18621 * will be the highest.
18622 */
18623 if (!next_fcf_pri)
18624 next_fcf_pri = fcf_pri->fcf_rec.priority;
18625 spin_unlock_irq(&phba->hbalock);
18626 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18627 rc = lpfc_sli4_fcf_rr_index_set(phba,
18628 fcf_pri->fcf_rec.fcf_index);
18629 if (rc)
18630 return 0;
18631 }
18632 spin_lock_irq(&phba->hbalock);
18633 }
18634 /*
18635 * if next_fcf_pri was not set above and the list is not empty then
18636 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018637 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018638 */
18639 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18640 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18641 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18642 /*
18643 * the 1st priority that has not FLOGI failed
18644 * will be the highest.
18645 */
18646 if (!next_fcf_pri)
18647 next_fcf_pri = fcf_pri->fcf_rec.priority;
18648 spin_unlock_irq(&phba->hbalock);
18649 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18650 rc = lpfc_sli4_fcf_rr_index_set(phba,
18651 fcf_pri->fcf_rec.fcf_index);
18652 if (rc)
18653 return 0;
18654 }
18655 spin_lock_irq(&phba->hbalock);
18656 }
18657 } else
18658 ret = 1;
18659 spin_unlock_irq(&phba->hbalock);
18660
18661 return ret;
18662}
18663/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018664 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18665 * @phba: pointer to lpfc hba data structure.
18666 *
18667 * This routine is to get the next eligible FCF record index in a round
18668 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018669 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018670 * shall be returned, otherwise, the next eligible FCF record's index
18671 * shall be returned.
18672 **/
18673uint16_t
18674lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18675{
18676 uint16_t next_fcf_index;
18677
James Smart421c6622013-01-03 15:44:16 -050018678initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018679 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018680 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18681
James Smart7d791df2011-07-22 18:37:52 -040018682next_priority:
James Smart421c6622013-01-03 15:44:16 -050018683 /* Determine the next fcf index to check */
18684 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018685 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18686 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018687 next_fcf_index);
18688
James Smart0c9ab6f2010-02-26 14:15:57 -050018689 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018690 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18691 /*
18692 * If we have wrapped then we need to clear the bits that
18693 * have been tested so that we can detect when we should
18694 * change the priority level.
18695 */
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, 0);
James Smart7d791df2011-07-22 18:37:52 -040018698 }
18699
James Smart0c9ab6f2010-02-26 14:15:57 -050018700
James Smart3804dc82010-07-14 15:31:37 -040018701 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018702 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18703 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18704 /*
18705 * If next fcf index is not found check if there are lower
18706 * Priority level fcf's in the fcf_priority list.
18707 * Set up the rr_bmask with all of the avaiable fcf bits
18708 * at that level and continue the selection process.
18709 */
18710 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018711 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018712 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18713 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040018714 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18715 return LPFC_FCOE_FCF_NEXT_NONE;
18716 else {
18717 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18718 "3063 Only FCF available idx %d, flag %x\n",
18719 next_fcf_index,
18720 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18721 return next_fcf_index;
18722 }
James Smart3804dc82010-07-14 15:31:37 -040018723 }
18724
James Smart7d791df2011-07-22 18:37:52 -040018725 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18726 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018727 LPFC_FCF_FLOGI_FAILED) {
18728 if (list_is_singular(&phba->fcf.fcf_pri_list))
18729 return LPFC_FCOE_FCF_NEXT_NONE;
18730
James Smart7d791df2011-07-22 18:37:52 -040018731 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018732 }
James Smart7d791df2011-07-22 18:37:52 -040018733
James Smart3804dc82010-07-14 15:31:37 -040018734 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018735 "2845 Get next roundrobin failover FCF (x%x)\n",
18736 next_fcf_index);
18737
James Smart0c9ab6f2010-02-26 14:15:57 -050018738 return next_fcf_index;
18739}
18740
18741/**
18742 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18743 * @phba: pointer to lpfc hba data structure.
18744 *
18745 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018746 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018747 * does not go beyond the range of the driver allocated bmask dimension
18748 * before setting the bit.
18749 *
18750 * Returns 0 if the index bit successfully set, otherwise, it returns
18751 * -EINVAL.
18752 **/
18753int
18754lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18755{
18756 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18757 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018758 "2610 FCF (x%x) reached driver's book "
18759 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018760 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18761 return -EINVAL;
18762 }
18763 /* Set the eligible FCF record index bmask */
18764 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18765
James Smart3804dc82010-07-14 15:31:37 -040018766 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018767 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018768 "bmask\n", fcf_index);
18769
James Smart0c9ab6f2010-02-26 14:15:57 -050018770 return 0;
18771}
18772
18773/**
James Smart3804dc82010-07-14 15:31:37 -040018774 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018775 * @phba: pointer to lpfc hba data structure.
18776 *
18777 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018778 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018779 * does not go beyond the range of the driver allocated bmask dimension
18780 * before clearing the bit.
18781 **/
18782void
18783lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18784{
James Smart9a803a72013-09-06 12:17:56 -040018785 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018786 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18787 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018788 "2762 FCF (x%x) reached driver's book "
18789 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018790 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18791 return;
18792 }
18793 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018794 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018795 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18796 list) {
James Smart7d791df2011-07-22 18:37:52 -040018797 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18798 list_del_init(&fcf_pri->list);
18799 break;
18800 }
18801 }
18802 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018803 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018804
18805 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018806 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018807 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018808}
18809
18810/**
James Smartecfd03c2010-02-12 14:41:27 -050018811 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18812 * @phba: pointer to lpfc hba data structure.
18813 *
18814 * This routine is the completion routine for the rediscover FCF table mailbox
18815 * command. If the mailbox command returned failure, it will try to stop the
18816 * FCF rediscover wait timer.
18817 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018818static void
James Smartecfd03c2010-02-12 14:41:27 -050018819lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18820{
18821 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18822 uint32_t shdr_status, shdr_add_status;
18823
18824 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18825
18826 shdr_status = bf_get(lpfc_mbox_hdr_status,
18827 &redisc_fcf->header.cfg_shdr.response);
18828 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18829 &redisc_fcf->header.cfg_shdr.response);
18830 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018831 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018832 "2746 Requesting for FCF rediscovery failed "
18833 "status x%x add_status x%x\n",
18834 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018835 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018836 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018837 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018838 spin_unlock_irq(&phba->hbalock);
18839 /*
18840 * CVL event triggered FCF rediscover request failed,
18841 * last resort to re-try current registered FCF entry.
18842 */
18843 lpfc_retry_pport_discovery(phba);
18844 } else {
18845 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018846 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018847 spin_unlock_irq(&phba->hbalock);
18848 /*
18849 * DEAD FCF event triggered FCF rediscover request
18850 * failed, last resort to fail over as a link down
18851 * to FCF registration.
18852 */
18853 lpfc_sli4_fcf_dead_failthrough(phba);
18854 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018855 } else {
18856 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018857 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018858 /*
18859 * Start FCF rediscovery wait timer for pending FCF
18860 * before rescan FCF record table.
18861 */
18862 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018863 }
James Smartecfd03c2010-02-12 14:41:27 -050018864
18865 mempool_free(mbox, phba->mbox_mem_pool);
18866}
18867
18868/**
James Smart3804dc82010-07-14 15:31:37 -040018869 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018870 * @phba: pointer to lpfc hba data structure.
18871 *
18872 * This routine is invoked to request for rediscovery of the entire FCF table
18873 * by the port.
18874 **/
18875int
18876lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18877{
18878 LPFC_MBOXQ_t *mbox;
18879 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18880 int rc, length;
18881
James Smart0c9ab6f2010-02-26 14:15:57 -050018882 /* Cancel retry delay timers to all vports before FCF rediscover */
18883 lpfc_cancel_all_vport_retry_delay_timer(phba);
18884
James Smartecfd03c2010-02-12 14:41:27 -050018885 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18886 if (!mbox) {
18887 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18888 "2745 Failed to allocate mbox for "
18889 "requesting FCF rediscover.\n");
18890 return -ENOMEM;
18891 }
18892
18893 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18894 sizeof(struct lpfc_sli4_cfg_mhdr));
18895 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18896 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18897 length, LPFC_SLI4_MBX_EMBED);
18898
18899 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18900 /* Set count to 0 for invalidating the entire FCF database */
18901 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18902
18903 /* Issue the mailbox command asynchronously */
18904 mbox->vport = phba->pport;
18905 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18906 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18907
18908 if (rc == MBX_NOT_FINISHED) {
18909 mempool_free(mbox, phba->mbox_mem_pool);
18910 return -EIO;
18911 }
18912 return 0;
18913}
18914
18915/**
James Smartfc2b9892010-02-26 14:15:29 -050018916 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18917 * @phba: pointer to lpfc hba data structure.
18918 *
18919 * This function is the failover routine as a last resort to the FCF DEAD
18920 * event when driver failed to perform fast FCF failover.
18921 **/
18922void
18923lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18924{
18925 uint32_t link_state;
18926
18927 /*
18928 * Last resort as FCF DEAD event failover will treat this as
18929 * a link down, but save the link state because we don't want
18930 * it to be changed to Link Down unless it is already down.
18931 */
18932 link_state = phba->link_state;
18933 lpfc_linkdown(phba);
18934 phba->link_state = link_state;
18935
18936 /* Unregister FCF if no devices connected to it */
18937 lpfc_unregister_unused_fcf(phba);
18938}
18939
18940/**
James Smart026abb82011-12-13 13:20:45 -050018941 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018942 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018943 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018944 *
James Smart026abb82011-12-13 13:20:45 -050018945 * This function gets SLI3 port configure region 23 data through memory dump
18946 * mailbox command. When it successfully retrieves data, the size of the data
18947 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018948 **/
James Smart026abb82011-12-13 13:20:45 -050018949static uint32_t
18950lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018951{
18952 LPFC_MBOXQ_t *pmb = NULL;
18953 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018954 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018955 int rc;
18956
James Smart026abb82011-12-13 13:20:45 -050018957 if (!rgn23_data)
18958 return 0;
18959
James Smarta0c87cb2009-07-19 10:01:10 -040018960 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18961 if (!pmb) {
18962 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018963 "2600 failed to allocate mailbox memory\n");
18964 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018965 }
18966 mb = &pmb->u.mb;
18967
James Smarta0c87cb2009-07-19 10:01:10 -040018968 do {
18969 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18970 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18971
18972 if (rc != MBX_SUCCESS) {
18973 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018974 "2601 failed to read config "
18975 "region 23, rc 0x%x Status 0x%x\n",
18976 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018977 mb->un.varDmp.word_cnt = 0;
18978 }
18979 /*
18980 * dump mem may return a zero when finished or we got a
18981 * mailbox error, either way we are done.
18982 */
18983 if (mb->un.varDmp.word_cnt == 0)
18984 break;
18985 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18986 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18987
18988 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018989 rgn23_data + offset,
18990 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018991 offset += mb->un.varDmp.word_cnt;
18992 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18993
James Smart026abb82011-12-13 13:20:45 -050018994 mempool_free(pmb, phba->mbox_mem_pool);
18995 return offset;
18996}
18997
18998/**
18999 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19000 * @phba: pointer to lpfc hba data structure.
19001 * @rgn23_data: pointer to configure region 23 data.
19002 *
19003 * This function gets SLI4 port configure region 23 data through memory dump
19004 * mailbox command. When it successfully retrieves data, the size of the data
19005 * will be returned, otherwise, 0 will be returned.
19006 **/
19007static uint32_t
19008lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19009{
19010 LPFC_MBOXQ_t *mboxq = NULL;
19011 struct lpfc_dmabuf *mp = NULL;
19012 struct lpfc_mqe *mqe;
19013 uint32_t data_length = 0;
19014 int rc;
19015
19016 if (!rgn23_data)
19017 return 0;
19018
19019 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19020 if (!mboxq) {
19021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19022 "3105 failed to allocate mailbox memory\n");
19023 return 0;
19024 }
19025
19026 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19027 goto out;
19028 mqe = &mboxq->u.mqe;
19029 mp = (struct lpfc_dmabuf *) mboxq->context1;
19030 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19031 if (rc)
19032 goto out;
19033 data_length = mqe->un.mb_words[5];
19034 if (data_length == 0)
19035 goto out;
19036 if (data_length > DMP_RGN23_SIZE) {
19037 data_length = 0;
19038 goto out;
19039 }
19040 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19041out:
19042 mempool_free(mboxq, phba->mbox_mem_pool);
19043 if (mp) {
19044 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19045 kfree(mp);
19046 }
19047 return data_length;
19048}
19049
19050/**
19051 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19052 * @phba: pointer to lpfc hba data structure.
19053 *
19054 * This function read region 23 and parse TLV for port status to
19055 * decide if the user disaled the port. If the TLV indicates the
19056 * port is disabled, the hba_flag is set accordingly.
19057 **/
19058void
19059lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19060{
19061 uint8_t *rgn23_data = NULL;
19062 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19063 uint32_t offset = 0;
19064
19065 /* Get adapter Region 23 data */
19066 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19067 if (!rgn23_data)
19068 goto out;
19069
19070 if (phba->sli_rev < LPFC_SLI_REV4)
19071 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19072 else {
19073 if_type = bf_get(lpfc_sli_intf_if_type,
19074 &phba->sli4_hba.sli_intf);
19075 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19076 goto out;
19077 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19078 }
James Smarta0c87cb2009-07-19 10:01:10 -040019079
19080 if (!data_size)
19081 goto out;
19082
19083 /* Check the region signature first */
19084 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19086 "2619 Config region 23 has bad signature\n");
19087 goto out;
19088 }
19089 offset += 4;
19090
19091 /* Check the data structure version */
19092 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19094 "2620 Config region 23 has bad version\n");
19095 goto out;
19096 }
19097 offset += 4;
19098
19099 /* Parse TLV entries in the region */
19100 while (offset < data_size) {
19101 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19102 break;
19103 /*
19104 * If the TLV is not driver specific TLV or driver id is
19105 * not linux driver id, skip the record.
19106 */
19107 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19108 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19109 (rgn23_data[offset + 3] != 0)) {
19110 offset += rgn23_data[offset + 1] * 4 + 4;
19111 continue;
19112 }
19113
19114 /* Driver found a driver specific TLV in the config region */
19115 sub_tlv_len = rgn23_data[offset + 1] * 4;
19116 offset += 4;
19117 tlv_offset = 0;
19118
19119 /*
19120 * Search for configured port state sub-TLV.
19121 */
19122 while ((offset < data_size) &&
19123 (tlv_offset < sub_tlv_len)) {
19124 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19125 offset += 4;
19126 tlv_offset += 4;
19127 break;
19128 }
19129 if (rgn23_data[offset] != PORT_STE_TYPE) {
19130 offset += rgn23_data[offset + 1] * 4 + 4;
19131 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19132 continue;
19133 }
19134
19135 /* This HBA contains PORT_STE configured */
19136 if (!rgn23_data[offset + 2])
19137 phba->hba_flag |= LINK_DISABLED;
19138
19139 goto out;
19140 }
19141 }
James Smart026abb82011-12-13 13:20:45 -050019142
James Smarta0c87cb2009-07-19 10:01:10 -040019143out:
James Smarta0c87cb2009-07-19 10:01:10 -040019144 kfree(rgn23_data);
19145 return;
19146}
James Smart695a8142010-01-26 23:08:03 -050019147
19148/**
James Smart52d52442011-05-24 11:42:45 -040019149 * lpfc_wr_object - write an object to the firmware
19150 * @phba: HBA structure that indicates port to create a queue on.
19151 * @dmabuf_list: list of dmabufs to write to the port.
19152 * @size: the total byte value of the objects to write to the port.
19153 * @offset: the current offset to be used to start the transfer.
19154 *
19155 * This routine will create a wr_object mailbox command to send to the port.
19156 * the mailbox command will be constructed using the dma buffers described in
19157 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19158 * BDEs that the imbedded mailbox can support. The @offset variable will be
19159 * used to indicate the starting offset of the transfer and will also return
19160 * the offset after the write object mailbox has completed. @size is used to
19161 * determine the end of the object and whether the eof bit should be set.
19162 *
19163 * Return 0 is successful and offset will contain the the new offset to use
19164 * for the next write.
19165 * Return negative value for error cases.
19166 **/
19167int
19168lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19169 uint32_t size, uint32_t *offset)
19170{
19171 struct lpfc_mbx_wr_object *wr_object;
19172 LPFC_MBOXQ_t *mbox;
19173 int rc = 0, i = 0;
19174 uint32_t shdr_status, shdr_add_status;
19175 uint32_t mbox_tmo;
19176 union lpfc_sli4_cfg_shdr *shdr;
19177 struct lpfc_dmabuf *dmabuf;
19178 uint32_t written = 0;
19179
19180 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19181 if (!mbox)
19182 return -ENOMEM;
19183
19184 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19185 LPFC_MBOX_OPCODE_WRITE_OBJECT,
19186 sizeof(struct lpfc_mbx_wr_object) -
19187 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19188
19189 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19190 wr_object->u.request.write_offset = *offset;
19191 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19192 wr_object->u.request.object_name[0] =
19193 cpu_to_le32(wr_object->u.request.object_name[0]);
19194 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19195 list_for_each_entry(dmabuf, dmabuf_list, list) {
19196 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19197 break;
19198 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19199 wr_object->u.request.bde[i].addrHigh =
19200 putPaddrHigh(dmabuf->phys);
19201 if (written + SLI4_PAGE_SIZE >= size) {
19202 wr_object->u.request.bde[i].tus.f.bdeSize =
19203 (size - written);
19204 written += (size - written);
19205 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
19206 } else {
19207 wr_object->u.request.bde[i].tus.f.bdeSize =
19208 SLI4_PAGE_SIZE;
19209 written += SLI4_PAGE_SIZE;
19210 }
19211 i++;
19212 }
19213 wr_object->u.request.bde_count = i;
19214 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19215 if (!phba->sli4_hba.intr_enable)
19216 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19217 else {
James Smarta183a152011-10-10 21:32:43 -040019218 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040019219 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19220 }
19221 /* The IOCTL status is embedded in the mailbox subheader. */
19222 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
19223 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19224 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19225 if (rc != MBX_TIMEOUT)
19226 mempool_free(mbox, phba->mbox_mem_pool);
19227 if (shdr_status || shdr_add_status || rc) {
19228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19229 "3025 Write Object mailbox failed with "
19230 "status x%x add_status x%x, mbx status x%x\n",
19231 shdr_status, shdr_add_status, rc);
19232 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080019233 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040019234 } else
19235 *offset += wr_object->u.response.actual_write_length;
19236 return rc;
19237}
19238
19239/**
James Smart695a8142010-01-26 23:08:03 -050019240 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
19241 * @vport: pointer to vport data structure.
19242 *
19243 * This function iterate through the mailboxq and clean up all REG_LOGIN
19244 * and REG_VPI mailbox commands associated with the vport. This function
19245 * is called when driver want to restart discovery of the vport due to
19246 * a Clear Virtual Link event.
19247 **/
19248void
19249lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
19250{
19251 struct lpfc_hba *phba = vport->phba;
19252 LPFC_MBOXQ_t *mb, *nextmb;
19253 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040019254 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040019255 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040019256 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040019257 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050019258 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050019259
James Smartd439d282010-09-29 11:18:45 -040019260 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050019261 spin_lock_irq(&phba->hbalock);
19262 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
19263 if (mb->vport != vport)
19264 continue;
19265
19266 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19267 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19268 continue;
19269
James Smartd439d282010-09-29 11:18:45 -040019270 list_del(&mb->list);
19271 list_add_tail(&mb->list, &mbox_cmd_list);
19272 }
19273 /* Clean up active mailbox command with the vport */
19274 mb = phba->sli.mbox_active;
19275 if (mb && (mb->vport == vport)) {
19276 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
19277 (mb->u.mb.mbxCommand == MBX_REG_VPI))
19278 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19279 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19280 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
19281 /* Put reference count for delayed processing */
19282 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19283 /* Unregister the RPI when mailbox complete */
19284 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19285 }
19286 }
James Smart63e801c2010-11-20 23:14:19 -050019287 /* Cleanup any mailbox completions which are not yet processed */
19288 do {
19289 restart_loop = 0;
19290 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19291 /*
19292 * If this mailox is already processed or it is
19293 * for another vport ignore it.
19294 */
19295 if ((mb->vport != vport) ||
19296 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19297 continue;
19298
19299 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19300 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19301 continue;
19302
19303 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19304 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19305 ndlp = (struct lpfc_nodelist *)mb->context2;
19306 /* Unregister the RPI when mailbox complete */
19307 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19308 restart_loop = 1;
19309 spin_unlock_irq(&phba->hbalock);
19310 spin_lock(shost->host_lock);
19311 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19312 spin_unlock(shost->host_lock);
19313 spin_lock_irq(&phba->hbalock);
19314 break;
19315 }
19316 }
19317 } while (restart_loop);
19318
James Smartd439d282010-09-29 11:18:45 -040019319 spin_unlock_irq(&phba->hbalock);
19320
19321 /* Release the cleaned-up mailbox commands */
19322 while (!list_empty(&mbox_cmd_list)) {
19323 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050019324 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19325 mp = (struct lpfc_dmabuf *) (mb->context1);
19326 if (mp) {
19327 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19328 kfree(mp);
19329 }
James Smart78730cf2010-04-06 15:06:30 -040019330 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040019331 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019332 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019333 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019334 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019335 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019336 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019337 }
James Smart695a8142010-01-26 23:08:03 -050019338 }
James Smart695a8142010-01-26 23:08:03 -050019339 mempool_free(mb, phba->mbox_mem_pool);
19340 }
James Smartd439d282010-09-29 11:18:45 -040019341
19342 /* Release the ndlp with the cleaned-up active mailbox command */
19343 if (act_mbx_ndlp) {
19344 spin_lock(shost->host_lock);
19345 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19346 spin_unlock(shost->host_lock);
19347 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019348 }
James Smart695a8142010-01-26 23:08:03 -050019349}
19350
James Smart2a9bf3d2010-06-07 15:24:45 -040019351/**
19352 * lpfc_drain_txq - Drain the txq
19353 * @phba: Pointer to HBA context object.
19354 *
19355 * This function attempt to submit IOCBs on the txq
19356 * to the adapter. For SLI4 adapters, the txq contains
19357 * ELS IOCBs that have been deferred because the there
19358 * are no SGLs. This congestion can occur with large
19359 * vport counts during node discovery.
19360 **/
19361
19362uint32_t
19363lpfc_drain_txq(struct lpfc_hba *phba)
19364{
19365 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019366 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019367 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019368 unsigned long iflags = 0;
19369 char *fail_msg = NULL;
19370 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019371 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019372 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070019373 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040019374
James Smartdc19e3b2018-05-24 21:08:57 -070019375 if (phba->link_flag & LS_MDS_LOOPBACK) {
19376 /* MDS WQE are posted only to first WQ*/
19377 wq = phba->sli4_hba.fcp_wq[0];
19378 if (unlikely(!wq))
19379 return 0;
19380 pring = wq->pring;
19381 } else {
19382 wq = phba->sli4_hba.els_wq;
19383 if (unlikely(!wq))
19384 return 0;
19385 pring = lpfc_phba_elsring(phba);
19386 }
19387
19388 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019389 return 0;
James Smart895427b2017-02-12 13:52:30 -080019390
James Smart398d81c2013-05-31 17:04:19 -040019391 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019392 list_for_each_entry(piocbq, &pring->txq, list) {
19393 txq_cnt++;
19394 }
19395
19396 if (txq_cnt > pring->txq_max)
19397 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019398
James Smart398d81c2013-05-31 17:04:19 -040019399 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019400
James Smart0e9bb8d2013-03-01 16:35:12 -050019401 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019402 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019403
James Smart19ca7602010-11-20 23:11:55 -050019404 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019405 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019406 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019407 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19408 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019409 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019410 break;
19411 }
James Smart895427b2017-02-12 13:52:30 -080019412 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019413 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019414 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019415 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019416 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019417 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019418 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019419
19420 /* The xri and iocb resources secured,
19421 * attempt to issue request
19422 */
James Smart6d368e52011-05-24 11:44:12 -040019423 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019424 piocbq->sli4_xritag = sglq->sli4_xritag;
19425 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19426 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019427 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019428 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070019429 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019430 fail_msg = " - Wq is full";
19431 else
19432 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19433
19434 if (fail_msg) {
19435 /* Failed means we can't issue and need to cancel */
19436 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19437 "2822 IOCB failed %s iotag 0x%x "
19438 "xri 0x%x\n",
19439 fail_msg,
19440 piocbq->iotag, piocbq->sli4_xritag);
19441 list_add_tail(&piocbq->list, &completions);
19442 }
James Smart398d81c2013-05-31 17:04:19 -040019443 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019444 }
19445
James Smart2a9bf3d2010-06-07 15:24:45 -040019446 /* Cancel all the IOCBs that cannot be issued */
19447 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19448 IOERR_SLI_ABORTED);
19449
James Smart0e9bb8d2013-03-01 16:35:12 -050019450 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019451}
James Smart895427b2017-02-12 13:52:30 -080019452
19453/**
19454 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19455 * @phba: Pointer to HBA context object.
19456 * @pwqe: Pointer to command WQE.
19457 * @sglq: Pointer to the scatter gather queue object.
19458 *
19459 * This routine converts the bpl or bde that is in the WQE
19460 * to a sgl list for the sli4 hardware. The physical address
19461 * of the bpl/bde is converted back to a virtual address.
19462 * If the WQE contains a BPL then the list of BDE's is
19463 * converted to sli4_sge's. If the WQE contains a single
19464 * BDE then it is converted to a single sli_sge.
19465 * The WQE is still in cpu endianness so the contents of
19466 * the bpl can be used without byte swapping.
19467 *
19468 * Returns valid XRI = Success, NO_XRI = Failure.
19469 */
19470static uint16_t
19471lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19472 struct lpfc_sglq *sglq)
19473{
19474 uint16_t xritag = NO_XRI;
19475 struct ulp_bde64 *bpl = NULL;
19476 struct ulp_bde64 bde;
19477 struct sli4_sge *sgl = NULL;
19478 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080019479 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080019480 int numBdes = 0;
19481 int i = 0;
19482 uint32_t offset = 0; /* accumulated offset in the sg request list */
19483 int inbound = 0; /* number of sg reply entries inbound from firmware */
19484 uint32_t cmd;
19485
19486 if (!pwqeq || !sglq)
19487 return xritag;
19488
19489 sgl = (struct sli4_sge *)sglq->sgl;
19490 wqe = &pwqeq->wqe;
19491 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19492
19493 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19494 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19495 return sglq->sli4_xritag;
19496 numBdes = pwqeq->rsvd2;
19497 if (numBdes) {
19498 /* The addrHigh and addrLow fields within the WQE
19499 * have not been byteswapped yet so there is no
19500 * need to swap them back.
19501 */
19502 if (pwqeq->context3)
19503 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19504 else
19505 return xritag;
19506
19507 bpl = (struct ulp_bde64 *)dmabuf->virt;
19508 if (!bpl)
19509 return xritag;
19510
19511 for (i = 0; i < numBdes; i++) {
19512 /* Should already be byte swapped. */
19513 sgl->addr_hi = bpl->addrHigh;
19514 sgl->addr_lo = bpl->addrLow;
19515
19516 sgl->word2 = le32_to_cpu(sgl->word2);
19517 if ((i+1) == numBdes)
19518 bf_set(lpfc_sli4_sge_last, sgl, 1);
19519 else
19520 bf_set(lpfc_sli4_sge_last, sgl, 0);
19521 /* swap the size field back to the cpu so we
19522 * can assign it to the sgl.
19523 */
19524 bde.tus.w = le32_to_cpu(bpl->tus.w);
19525 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19526 /* The offsets in the sgl need to be accumulated
19527 * separately for the request and reply lists.
19528 * The request is always first, the reply follows.
19529 */
19530 switch (cmd) {
19531 case CMD_GEN_REQUEST64_WQE:
19532 /* add up the reply sg entries */
19533 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19534 inbound++;
19535 /* first inbound? reset the offset */
19536 if (inbound == 1)
19537 offset = 0;
19538 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19539 bf_set(lpfc_sli4_sge_type, sgl,
19540 LPFC_SGE_TYPE_DATA);
19541 offset += bde.tus.f.bdeSize;
19542 break;
19543 case CMD_FCP_TRSP64_WQE:
19544 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19545 bf_set(lpfc_sli4_sge_type, sgl,
19546 LPFC_SGE_TYPE_DATA);
19547 break;
19548 case CMD_FCP_TSEND64_WQE:
19549 case CMD_FCP_TRECEIVE64_WQE:
19550 bf_set(lpfc_sli4_sge_type, sgl,
19551 bpl->tus.f.bdeFlags);
19552 if (i < 3)
19553 offset = 0;
19554 else
19555 offset += bde.tus.f.bdeSize;
19556 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19557 break;
19558 }
19559 sgl->word2 = cpu_to_le32(sgl->word2);
19560 bpl++;
19561 sgl++;
19562 }
19563 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19564 /* The addrHigh and addrLow fields of the BDE have not
19565 * been byteswapped yet so they need to be swapped
19566 * before putting them in the sgl.
19567 */
19568 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19569 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19570 sgl->word2 = le32_to_cpu(sgl->word2);
19571 bf_set(lpfc_sli4_sge_last, sgl, 1);
19572 sgl->word2 = cpu_to_le32(sgl->word2);
19573 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19574 }
19575 return sglq->sli4_xritag;
19576}
19577
19578/**
19579 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19580 * @phba: Pointer to HBA context object.
19581 * @ring_number: Base sli ring number
19582 * @pwqe: Pointer to command WQE.
19583 **/
19584int
19585lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19586 struct lpfc_iocbq *pwqe)
19587{
James Smart205e8242018-03-05 12:04:03 -080019588 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019589 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019590 struct lpfc_queue *wq;
19591 struct lpfc_sglq *sglq;
19592 struct lpfc_sli_ring *pring;
19593 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019594 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019595
19596 /* NVME_LS and NVME_LS ABTS requests. */
19597 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19598 pring = phba->sli4_hba.nvmels_wq->pring;
19599 spin_lock_irqsave(&pring->ring_lock, iflags);
19600 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19601 if (!sglq) {
19602 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19603 return WQE_BUSY;
19604 }
19605 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19606 pwqe->sli4_xritag = sglq->sli4_xritag;
19607 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19608 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19609 return WQE_ERROR;
19610 }
19611 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19612 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019613 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19614 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019615 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019616 return ret;
James Smart895427b2017-02-12 13:52:30 -080019617 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019618
James Smart895427b2017-02-12 13:52:30 -080019619 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19620 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19621 return 0;
19622 }
19623
19624 /* NVME_FCREQ and NVME_ABTS requests */
19625 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19626 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19627 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19628
19629 spin_lock_irqsave(&pring->ring_lock, iflags);
19630 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19631 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19632 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019633 ret = lpfc_sli4_wq_put(wq, wqe);
19634 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019635 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019636 return ret;
James Smart895427b2017-02-12 13:52:30 -080019637 }
19638 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19639 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19640 return 0;
19641 }
19642
James Smartf358dd02017-02-12 13:52:34 -080019643 /* NVMET requests */
19644 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19645 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19646 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19647
19648 spin_lock_irqsave(&pring->ring_lock, iflags);
19649 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019650 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019651 if (pwqe->sli4_xritag == NO_XRI) {
19652 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19653 pwqe->sli4_xritag = sglq->sli4_xritag;
19654 }
19655 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19656 pwqe->sli4_xritag);
19657 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19658 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19659 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019660 ret = lpfc_sli4_wq_put(wq, wqe);
19661 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019662 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019663 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019664 }
19665 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19666 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19667 return 0;
19668 }
James Smart895427b2017-02-12 13:52:30 -080019669 return WQE_ERROR;
19670}