blob: 89ebb07e203dc47f7451cba28f3c54731dbff329 [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;
395 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
396 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
397 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
398 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
399 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
400 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
401}
402
403/**
James Smart4f774512009-05-22 14:52:35 -0400404 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
405 * @q: The Event Queue that the host has completed processing for.
406 * @arm: Indicates whether the host wants to arms this CQ.
407 *
408 * This routine will mark all Event Queue Entries on @q, from the last
409 * known completed entry to the last entry that was processed, as completed
410 * by clearing the valid bit for each completion queue entry. Then it will
411 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
412 * The internal host index in the @q will be updated by this routine to indicate
413 * that the host has finished processing the entries. The @arm parameter
414 * indicates that the queue should be rearmed when ringing the doorbell.
415 *
416 * This function will return the number of EQEs that were popped.
417 **/
418uint32_t
419lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
420{
421 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800422 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400423 struct lpfc_eqe *temp_eqe;
424 struct lpfc_register doorbell;
425
James Smart2e90f4b2011-12-13 13:22:37 -0500426 /* sanity check on queue memory */
427 if (unlikely(!q))
428 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800429 phba = q->phba;
James Smart2e90f4b2011-12-13 13:22:37 -0500430
James Smart4f774512009-05-22 14:52:35 -0400431 /* while there are valid entries */
432 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800433 if (!phba->sli4_hba.pc_sli4_params.eqav) {
434 temp_eqe = q->qe[q->host_index].eqe;
435 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
436 }
James Smart4f774512009-05-22 14:52:35 -0400437 released++;
438 q->host_index = ((q->host_index + 1) % q->entry_count);
439 }
440 if (unlikely(released == 0 && !arm))
441 return 0;
442
443 /* ring doorbell for number popped */
444 doorbell.word0 = 0;
445 if (arm) {
446 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
447 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
448 }
449 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
450 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500451 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
452 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
453 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800454 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500455 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
456 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
James Smart9dd35422018-02-22 08:18:41 -0800457 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400458 return released;
459}
460
461/**
James Smart27d6ac02018-02-22 08:18:42 -0800462 * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
463 * @q: The Event Queue that the host has completed processing for.
464 * @arm: Indicates whether the host wants to arms this CQ.
465 *
466 * This routine will mark all Event Queue Entries on @q, from the last
467 * known completed entry to the last entry that was processed, as completed
468 * by clearing the valid bit for each completion queue entry. Then it will
469 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
470 * The internal host index in the @q will be updated by this routine to indicate
471 * that the host has finished processing the entries. The @arm parameter
472 * indicates that the queue should be rearmed when ringing the doorbell.
473 *
474 * This function will return the number of EQEs that were popped.
475 **/
476uint32_t
477lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
478{
479 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800480 struct lpfc_hba *phba;
James Smart27d6ac02018-02-22 08:18:42 -0800481 struct lpfc_eqe *temp_eqe;
482 struct lpfc_register doorbell;
483
484 /* sanity check on queue memory */
485 if (unlikely(!q))
486 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800487 phba = q->phba;
James Smart27d6ac02018-02-22 08:18:42 -0800488
489 /* while there are valid entries */
490 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800491 if (!phba->sli4_hba.pc_sli4_params.eqav) {
492 temp_eqe = q->qe[q->host_index].eqe;
493 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
494 }
James Smart27d6ac02018-02-22 08:18:42 -0800495 released++;
496 q->host_index = ((q->host_index + 1) % q->entry_count);
497 }
498 if (unlikely(released == 0 && !arm))
499 return 0;
500
501 /* ring doorbell for number popped */
502 doorbell.word0 = 0;
503 if (arm)
504 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
505 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
506 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
507 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
508 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
509 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
510 readl(q->phba->sli4_hba.EQDBregaddr);
511 return released;
512}
513
514/**
James Smart4f774512009-05-22 14:52:35 -0400515 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
516 * @q: The Completion Queue to get the first valid CQE from
517 *
518 * This routine will get the first valid Completion Queue Entry from @q, update
519 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
520 * the Queue (no more work to do), or the Queue is full of CQEs that have been
521 * processed, but not popped back to the HBA then this routine will return NULL.
522 **/
523static struct lpfc_cqe *
524lpfc_sli4_cq_get(struct lpfc_queue *q)
525{
James Smart7365f6f2018-02-22 08:18:46 -0800526 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400527 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400528 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400529
James Smart2e90f4b2011-12-13 13:22:37 -0500530 /* sanity check on queue memory */
531 if (unlikely(!q))
532 return NULL;
James Smart7365f6f2018-02-22 08:18:46 -0800533 phba = q->phba;
534 cqe = q->qe[q->hba_index].cqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500535
James Smart4f774512009-05-22 14:52:35 -0400536 /* If the next CQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800537 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400538 return NULL;
539 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400540 idx = ((q->hba_index + 1) % q->entry_count);
541 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400542 return NULL;
543
James Smart027140e2012-08-03 12:35:44 -0400544 q->hba_index = idx;
James Smart7365f6f2018-02-22 08:18:46 -0800545 /* if the index wrapped around, toggle the valid bit */
546 if (phba->sli4_hba.pc_sli4_params.cqav && !q->hba_index)
547 q->qe_valid = (q->qe_valid) ? 0 : 1;
James Smart27f344e2014-05-07 17:16:46 -0400548
549 /*
550 * insert barrier for instruction interlock : data from the hardware
551 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800552 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
553 * instructions allowing action on content before valid bit checked,
554 * add barrier here as well. May not be needed as "content" is a
555 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400556 */
557 mb();
James Smart4f774512009-05-22 14:52:35 -0400558 return cqe;
559}
560
561/**
562 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
563 * @q: The Completion Queue that the host has completed processing for.
564 * @arm: Indicates whether the host wants to arms this CQ.
565 *
566 * This routine will mark all Completion queue entries on @q, from the last
567 * known completed entry to the last entry that was processed, as completed
568 * by clearing the valid bit for each completion queue entry. Then it will
569 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
570 * The internal host index in the @q will be updated by this routine to indicate
571 * that the host has finished processing the entries. The @arm parameter
572 * indicates that the queue should be rearmed when ringing the doorbell.
573 *
574 * This function will return the number of CQEs that were released.
575 **/
576uint32_t
577lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
578{
579 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800580 struct lpfc_hba *phba;
James Smart4f774512009-05-22 14:52:35 -0400581 struct lpfc_cqe *temp_qe;
582 struct lpfc_register doorbell;
583
James Smart2e90f4b2011-12-13 13:22:37 -0500584 /* sanity check on queue memory */
585 if (unlikely(!q))
586 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800587 phba = q->phba;
588
James Smart4f774512009-05-22 14:52:35 -0400589 /* while there are valid entries */
590 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800591 if (!phba->sli4_hba.pc_sli4_params.cqav) {
592 temp_qe = q->qe[q->host_index].cqe;
593 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
594 }
James Smart4f774512009-05-22 14:52:35 -0400595 released++;
596 q->host_index = ((q->host_index + 1) % q->entry_count);
597 }
598 if (unlikely(released == 0 && !arm))
599 return 0;
600
601 /* ring doorbell for number popped */
602 doorbell.word0 = 0;
603 if (arm)
604 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
605 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
606 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500607 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
608 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
609 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800610 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400611 return released;
612}
613
614/**
James Smart27d6ac02018-02-22 08:18:42 -0800615 * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
616 * @q: The Completion Queue that the host has completed processing for.
617 * @arm: Indicates whether the host wants to arms this CQ.
618 *
619 * This routine will mark all Completion queue entries on @q, from the last
620 * known completed entry to the last entry that was processed, as completed
621 * by clearing the valid bit for each completion queue entry. Then it will
622 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
623 * The internal host index in the @q will be updated by this routine to indicate
624 * that the host has finished processing the entries. The @arm parameter
625 * indicates that the queue should be rearmed when ringing the doorbell.
626 *
627 * This function will return the number of CQEs that were released.
628 **/
629uint32_t
630lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
631{
632 uint32_t released = 0;
James Smart7365f6f2018-02-22 08:18:46 -0800633 struct lpfc_hba *phba;
James Smart27d6ac02018-02-22 08:18:42 -0800634 struct lpfc_cqe *temp_qe;
635 struct lpfc_register doorbell;
636
637 /* sanity check on queue memory */
638 if (unlikely(!q))
639 return 0;
James Smart7365f6f2018-02-22 08:18:46 -0800640 phba = q->phba;
641
James Smart27d6ac02018-02-22 08:18:42 -0800642 /* while there are valid entries */
643 while (q->hba_index != q->host_index) {
James Smart7365f6f2018-02-22 08:18:46 -0800644 if (!phba->sli4_hba.pc_sli4_params.cqav) {
645 temp_qe = q->qe[q->host_index].cqe;
646 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
647 }
James Smart27d6ac02018-02-22 08:18:42 -0800648 released++;
649 q->host_index = ((q->host_index + 1) % q->entry_count);
650 }
651 if (unlikely(released == 0 && !arm))
652 return 0;
653
654 /* ring doorbell for number popped */
655 doorbell.word0 = 0;
656 if (arm)
657 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
658 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
659 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
660 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
661 return released;
662}
663
664/**
James Smart4f774512009-05-22 14:52:35 -0400665 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
666 * @q: The Header Receive Queue to operate on.
667 * @wqe: The Receive Queue Entry to put on the Receive queue.
668 *
669 * This routine will copy the contents of @wqe to the next available entry on
670 * the @q. This function will then ring the Receive Queue Doorbell to signal the
671 * HBA to start processing the Receive Queue Entry. This function returns the
672 * index that the rqe was copied to if successful. If no entries are available
673 * on @q then this function will return -ENOMEM.
674 * The caller is expected to hold the hbalock when calling this routine.
675 **/
James Smart895427b2017-02-12 13:52:30 -0800676int
James Smart4f774512009-05-22 14:52:35 -0400677lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
678 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
679{
James Smart2e90f4b2011-12-13 13:22:37 -0500680 struct lpfc_rqe *temp_hrqe;
681 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400682 struct lpfc_register doorbell;
James Smartcbc5de12017-12-08 17:18:04 -0800683 int hq_put_index;
684 int dq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400685
James Smart2e90f4b2011-12-13 13:22:37 -0500686 /* sanity check on queue memory */
687 if (unlikely(!hq) || unlikely(!dq))
688 return -ENOMEM;
James Smartcbc5de12017-12-08 17:18:04 -0800689 hq_put_index = hq->host_index;
690 dq_put_index = dq->host_index;
691 temp_hrqe = hq->qe[hq_put_index].rqe;
692 temp_drqe = dq->qe[dq_put_index].rqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500693
James Smart4f774512009-05-22 14:52:35 -0400694 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
695 return -EINVAL;
James Smartcbc5de12017-12-08 17:18:04 -0800696 if (hq_put_index != dq_put_index)
James Smart4f774512009-05-22 14:52:35 -0400697 return -EINVAL;
698 /* If the host has not yet processed the next entry then we are done */
James Smartcbc5de12017-12-08 17:18:04 -0800699 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400700 return -EBUSY;
James Smart48f8fdb2018-05-04 20:37:51 -0700701 lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
702 lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400703
704 /* Update the host index to point to the next slot */
James Smartcbc5de12017-12-08 17:18:04 -0800705 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
706 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700707 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400708
709 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400710 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400711 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500712 if (hq->db_format == LPFC_DB_RING_FORMAT) {
713 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
714 hq->entry_repost);
715 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
716 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
717 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
718 hq->entry_repost);
719 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
720 hq->host_index);
721 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
722 } else {
723 return -EINVAL;
724 }
725 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400726 }
James Smartcbc5de12017-12-08 17:18:04 -0800727 return hq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400728}
729
730/**
731 * lpfc_sli4_rq_release - Updates internal hba index for RQ
732 * @q: The Header Receive Queue to operate on.
733 *
734 * This routine will update the HBA index of a queue to reflect consumption of
735 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
736 * consumed an entry the host calls this function to update the queue's
737 * internal pointers. This routine returns the number of entries that were
738 * consumed by the HBA.
739 **/
740static uint32_t
741lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
742{
James Smart2e90f4b2011-12-13 13:22:37 -0500743 /* sanity check on queue memory */
744 if (unlikely(!hq) || unlikely(!dq))
745 return 0;
746
James Smart4f774512009-05-22 14:52:35 -0400747 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
748 return 0;
749 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
750 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
751 return 1;
752}
753
James Smarte59058c2008-08-24 21:49:00 -0400754/**
James Smart3621a712009-04-06 18:47:14 -0400755 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400756 * @phba: Pointer to HBA context object.
757 * @pring: Pointer to driver SLI ring object.
758 *
759 * This function returns pointer to next command iocb entry
760 * in the command ring. The caller must hold hbalock to prevent
761 * other threads consume the next command iocb.
762 * SLI-2/SLI-3 provide different sized iocbs.
763 **/
James Smarted957682007-06-17 19:56:37 -0500764static inline IOCB_t *
765lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
766{
James Smart7e56aa22012-08-03 12:35:34 -0400767 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
768 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500769}
770
James Smarte59058c2008-08-24 21:49:00 -0400771/**
James Smart3621a712009-04-06 18:47:14 -0400772 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400773 * @phba: Pointer to HBA context object.
774 * @pring: Pointer to driver SLI ring object.
775 *
776 * This function returns pointer to next response iocb entry
777 * in the response ring. The caller must hold hbalock to make sure
778 * that no other thread consume the next response iocb.
779 * SLI-2/SLI-3 provide different sized iocbs.
780 **/
James Smarted957682007-06-17 19:56:37 -0500781static inline IOCB_t *
782lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
783{
James Smart7e56aa22012-08-03 12:35:34 -0400784 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
785 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500786}
787
James Smarte59058c2008-08-24 21:49:00 -0400788/**
James Smart3621a712009-04-06 18:47:14 -0400789 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400790 * @phba: Pointer to HBA context object.
791 *
792 * This function is called with hbalock held. This function
793 * allocates a new driver iocb object from the iocb pool. If the
794 * allocation is successful, it returns pointer to the newly
795 * allocated iocb object else it returns NULL.
796 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400797struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500798__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400799{
800 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
801 struct lpfc_iocbq * iocbq = NULL;
802
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100803 lockdep_assert_held(&phba->hbalock);
804
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400805 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400806 if (iocbq)
807 phba->iocb_cnt++;
808 if (phba->iocb_cnt > phba->iocb_max)
809 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400810 return iocbq;
811}
812
James Smarte59058c2008-08-24 21:49:00 -0400813/**
James Smartda0436e2009-05-22 14:51:39 -0400814 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
815 * @phba: Pointer to HBA context object.
816 * @xritag: XRI value.
817 *
818 * This function clears the sglq pointer from the array of acive
819 * sglq's. The xritag that is passed in is used to index into the
820 * array. Before the xritag can be used it needs to be adjusted
821 * by subtracting the xribase.
822 *
823 * Returns sglq ponter = success, NULL = Failure.
824 **/
James Smart895427b2017-02-12 13:52:30 -0800825struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400826__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
827{
James Smartda0436e2009-05-22 14:51:39 -0400828 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400829
830 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
831 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400832 return sglq;
833}
834
835/**
836 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
837 * @phba: Pointer to HBA context object.
838 * @xritag: XRI value.
839 *
840 * This function returns the sglq pointer from the array of acive
841 * sglq's. The xritag that is passed in is used to index into the
842 * array. Before the xritag can be used it needs to be adjusted
843 * by subtracting the xribase.
844 *
845 * Returns sglq ponter = success, NULL = Failure.
846 **/
James Smart0f65ff62010-02-26 14:14:23 -0500847struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400848__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
849{
James Smartda0436e2009-05-22 14:51:39 -0400850 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400851
852 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400853 return sglq;
854}
855
856/**
James Smart1151e3e2011-02-16 12:39:35 -0500857 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500858 * @phba: Pointer to HBA context object.
859 * @xritag: xri used in this exchange.
860 * @rrq: The RRQ to be cleared.
861 *
James Smart19ca7602010-11-20 23:11:55 -0500862 **/
James Smart1151e3e2011-02-16 12:39:35 -0500863void
864lpfc_clr_rrq_active(struct lpfc_hba *phba,
865 uint16_t xritag,
866 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500867{
James Smart1151e3e2011-02-16 12:39:35 -0500868 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500869
James Smart1151e3e2011-02-16 12:39:35 -0500870 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
871 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500872
873 /* The target DID could have been swapped (cable swap)
874 * we should use the ndlp from the findnode if it is
875 * available.
876 */
James Smart1151e3e2011-02-16 12:39:35 -0500877 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500878 ndlp = rrq->ndlp;
879
James Smart1151e3e2011-02-16 12:39:35 -0500880 if (!ndlp)
881 goto out;
882
James Smartcff261f2013-12-17 20:29:47 -0500883 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500884 rrq->send_rrq = 0;
885 rrq->xritag = 0;
886 rrq->rrq_stop_time = 0;
887 }
James Smart1151e3e2011-02-16 12:39:35 -0500888out:
James Smart19ca7602010-11-20 23:11:55 -0500889 mempool_free(rrq, phba->rrq_pool);
890}
891
892/**
893 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
894 * @phba: Pointer to HBA context object.
895 *
896 * This function is called with hbalock held. This function
897 * Checks if stop_time (ratov from setting rrq active) has
898 * been reached, if it has and the send_rrq flag is set then
899 * it will call lpfc_send_rrq. If the send_rrq flag is not set
900 * then it will just call the routine to clear the rrq and
901 * free the rrq resource.
902 * The timer is set to the next rrq that is going to expire before
903 * leaving the routine.
904 *
905 **/
906void
907lpfc_handle_rrq_active(struct lpfc_hba *phba)
908{
909 struct lpfc_node_rrq *rrq;
910 struct lpfc_node_rrq *nextrrq;
911 unsigned long next_time;
912 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500913 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500914
915 spin_lock_irqsave(&phba->hbalock, iflags);
916 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400917 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500918 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500919 &phba->active_rrq_list, list) {
920 if (time_after(jiffies, rrq->rrq_stop_time))
921 list_move(&rrq->list, &send_rrq);
922 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500923 next_time = rrq->rrq_stop_time;
924 }
925 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500926 if ((!list_empty(&phba->active_rrq_list)) &&
927 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500928 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500929 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
930 list_del(&rrq->list);
931 if (!rrq->send_rrq)
932 /* this call will free the rrq */
933 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
934 else if (lpfc_send_rrq(phba, rrq)) {
935 /* if we send the rrq then the completion handler
936 * will clear the bit in the xribitmap.
937 */
938 lpfc_clr_rrq_active(phba, rrq->xritag,
939 rrq);
940 }
941 }
James Smart19ca7602010-11-20 23:11:55 -0500942}
943
944/**
945 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
946 * @vport: Pointer to vport context object.
947 * @xri: The xri used in the exchange.
948 * @did: The targets DID for this exchange.
949 *
950 * returns NULL = rrq not found in the phba->active_rrq_list.
951 * rrq = rrq for this xri and target.
952 **/
953struct lpfc_node_rrq *
954lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
955{
956 struct lpfc_hba *phba = vport->phba;
957 struct lpfc_node_rrq *rrq;
958 struct lpfc_node_rrq *nextrrq;
959 unsigned long iflags;
960
961 if (phba->sli_rev != LPFC_SLI_REV4)
962 return NULL;
963 spin_lock_irqsave(&phba->hbalock, iflags);
964 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
965 if (rrq->vport == vport && rrq->xritag == xri &&
966 rrq->nlp_DID == did){
967 list_del(&rrq->list);
968 spin_unlock_irqrestore(&phba->hbalock, iflags);
969 return rrq;
970 }
971 }
972 spin_unlock_irqrestore(&phba->hbalock, iflags);
973 return NULL;
974}
975
976/**
977 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
978 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500979 * @ndlp: Pointer to the lpfc_node_list structure.
980 * If ndlp is NULL Remove all active RRQs for this vport from the
981 * phba->active_rrq_list and clear the rrq.
982 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500983 **/
984void
James Smart1151e3e2011-02-16 12:39:35 -0500985lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500986
987{
988 struct lpfc_hba *phba = vport->phba;
989 struct lpfc_node_rrq *rrq;
990 struct lpfc_node_rrq *nextrrq;
991 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500992 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500993
994 if (phba->sli_rev != LPFC_SLI_REV4)
995 return;
James Smart1151e3e2011-02-16 12:39:35 -0500996 if (!ndlp) {
997 lpfc_sli4_vport_delete_els_xri_aborted(vport);
998 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500999 }
James Smart1151e3e2011-02-16 12:39:35 -05001000 spin_lock_irqsave(&phba->hbalock, iflags);
1001 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
1002 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
1003 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -05001004 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -05001005
1006 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1007 list_del(&rrq->list);
1008 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1009 }
James Smart19ca7602010-11-20 23:11:55 -05001010}
1011
1012/**
James Smart1151e3e2011-02-16 12:39:35 -05001013 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -05001014 * @phba: Pointer to HBA context object.
1015 * @ndlp: Targets nodelist pointer for this exchange.
1016 * @xritag the xri in the bitmap to test.
1017 *
1018 * This function is called with hbalock held. This function
1019 * returns 0 = rrq not active for this xri
1020 * 1 = rrq is valid for this xri.
1021 **/
James Smart1151e3e2011-02-16 12:39:35 -05001022int
1023lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -05001024 uint16_t xritag)
1025{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001026 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -05001027 if (!ndlp)
1028 return 0;
James Smartcff261f2013-12-17 20:29:47 -05001029 if (!ndlp->active_rrqs_xri_bitmap)
1030 return 0;
1031 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -05001032 return 1;
1033 else
1034 return 0;
1035}
1036
1037/**
1038 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1039 * @phba: Pointer to HBA context object.
1040 * @ndlp: nodelist pointer for this target.
1041 * @xritag: xri used in this exchange.
1042 * @rxid: Remote Exchange ID.
1043 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1044 *
1045 * This function takes the hbalock.
1046 * The active bit is always set in the active rrq xri_bitmap even
1047 * if there is no slot avaiable for the other rrq information.
1048 *
1049 * returns 0 rrq actived for this xri
1050 * < 0 No memory or invalid ndlp.
1051 **/
1052int
1053lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -05001054 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -05001055{
James Smart19ca7602010-11-20 23:11:55 -05001056 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -05001057 struct lpfc_node_rrq *rrq;
1058 int empty;
1059
1060 if (!ndlp)
1061 return -EINVAL;
1062
1063 if (!phba->cfg_enable_rrq)
1064 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001065
1066 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001067 if (phba->pport->load_flag & FC_UNLOADING) {
1068 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1069 goto out;
1070 }
1071
1072 /*
1073 * set the active bit even if there is no mem available.
1074 */
1075 if (NLP_CHK_FREE_REQ(ndlp))
1076 goto out;
1077
1078 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1079 goto out;
1080
James Smartcff261f2013-12-17 20:29:47 -05001081 if (!ndlp->active_rrqs_xri_bitmap)
1082 goto out;
1083
1084 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -05001085 goto out;
1086
James Smart19ca7602010-11-20 23:11:55 -05001087 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001088 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1089 if (!rrq) {
1090 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1091 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1092 " DID:0x%x Send:%d\n",
1093 xritag, rxid, ndlp->nlp_DID, send_rrq);
1094 return -EINVAL;
1095 }
James Smarte5771b42013-03-01 16:37:14 -05001096 if (phba->cfg_enable_rrq == 1)
1097 rrq->send_rrq = send_rrq;
1098 else
1099 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001100 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001101 rrq->rrq_stop_time = jiffies +
1102 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001103 rrq->ndlp = ndlp;
1104 rrq->nlp_DID = ndlp->nlp_DID;
1105 rrq->vport = ndlp->vport;
1106 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001107 spin_lock_irqsave(&phba->hbalock, iflags);
1108 empty = list_empty(&phba->active_rrq_list);
1109 list_add_tail(&rrq->list, &phba->active_rrq_list);
1110 phba->hba_flag |= HBA_RRQ_ACTIVE;
1111 if (empty)
1112 lpfc_worker_wake_up(phba);
1113 spin_unlock_irqrestore(&phba->hbalock, iflags);
1114 return 0;
1115out:
1116 spin_unlock_irqrestore(&phba->hbalock, iflags);
1117 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1118 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1119 " DID:0x%x Send:%d\n",
1120 xritag, rxid, ndlp->nlp_DID, send_rrq);
1121 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001122}
1123
1124/**
James Smart895427b2017-02-12 13:52:30 -08001125 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001126 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -05001127 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001128 *
James Smartdafe8ce2014-09-03 12:56:40 -04001129 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -04001130 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -04001131 * list is not empty then it is successful, it returns pointer to the newly
1132 * allocated sglq object else it returns NULL.
1133 **/
1134static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -08001135__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -04001136{
James Smart895427b2017-02-12 13:52:30 -08001137 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -04001138 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001139 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001140 struct lpfc_scsi_buf *lpfc_cmd;
1141 struct lpfc_nodelist *ndlp;
1142 int found = 0;
1143
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001144 lockdep_assert_held(&phba->hbalock);
1145
James Smart19ca7602010-11-20 23:11:55 -05001146 if (piocbq->iocb_flag & LPFC_IO_FCP) {
1147 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1148 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001149 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001150 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001151 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001152 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1153 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1154 ndlp = NULL;
1155 else
1156 ndlp = piocbq->context_un.ndlp;
1157 } else {
James Smart19ca7602010-11-20 23:11:55 -05001158 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001159 }
James Smart19ca7602010-11-20 23:11:55 -05001160
James Smart895427b2017-02-12 13:52:30 -08001161 spin_lock(&phba->sli4_hba.sgl_list_lock);
1162 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001163 start_sglq = sglq;
1164 while (!found) {
1165 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001166 break;
James Smart895427b2017-02-12 13:52:30 -08001167 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1168 test_bit(sglq->sli4_lxritag,
1169 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001170 /* This xri has an rrq outstanding for this DID.
1171 * put it back in the list and get another xri.
1172 */
James Smart895427b2017-02-12 13:52:30 -08001173 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001174 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001175 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001176 struct lpfc_sglq, list);
1177 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001178 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001179 sglq = NULL;
1180 break;
1181 } else
1182 continue;
1183 }
1184 sglq->ndlp = ndlp;
1185 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001186 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001187 sglq->state = SGL_ALLOCATED;
1188 }
James Smart895427b2017-02-12 13:52:30 -08001189 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001190 return sglq;
1191}
1192
1193/**
James Smartf358dd02017-02-12 13:52:34 -08001194 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1195 * @phba: Pointer to HBA context object.
1196 * @piocb: Pointer to the iocbq.
1197 *
1198 * This function is called with the sgl_list lock held. This function
1199 * gets a new driver sglq object from the sglq list. If the
1200 * list is not empty then it is successful, it returns pointer to the newly
1201 * allocated sglq object else it returns NULL.
1202 **/
1203struct lpfc_sglq *
1204__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1205{
1206 struct list_head *lpfc_nvmet_sgl_list;
1207 struct lpfc_sglq *sglq = NULL;
1208
1209 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1210
1211 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1212
1213 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1214 if (!sglq)
1215 return NULL;
1216 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1217 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001218 return sglq;
1219}
1220
James Smarte59058c2008-08-24 21:49:00 -04001221/**
James Smart3621a712009-04-06 18:47:14 -04001222 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001223 * @phba: Pointer to HBA context object.
1224 *
1225 * This function is called with no lock held. This function
1226 * allocates a new driver iocb object from the iocb pool. If the
1227 * allocation is successful, it returns pointer to the newly
1228 * allocated iocb object else it returns NULL.
1229 **/
James Smart2e0fef82007-06-17 19:56:36 -05001230struct lpfc_iocbq *
1231lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001232{
James Smart2e0fef82007-06-17 19:56:36 -05001233 struct lpfc_iocbq * iocbq = NULL;
1234 unsigned long iflags;
1235
1236 spin_lock_irqsave(&phba->hbalock, iflags);
1237 iocbq = __lpfc_sli_get_iocbq(phba);
1238 spin_unlock_irqrestore(&phba->hbalock, iflags);
1239 return iocbq;
1240}
1241
James Smarte59058c2008-08-24 21:49:00 -04001242/**
James Smart4f774512009-05-22 14:52:35 -04001243 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1244 * @phba: Pointer to HBA context object.
1245 * @iocbq: Pointer to driver iocb object.
1246 *
1247 * This function is called with hbalock held to release driver
1248 * iocb object to the iocb pool. The iotag in the iocb object
1249 * does not change for each use of the iocb object. This function
1250 * clears all other fields of the iocb object when it is freed.
1251 * The sqlq structure that holds the xritag and phys and virtual
1252 * mappings for the scatter gather list is retrieved from the
1253 * active array of sglq. The get of the sglq pointer also clears
1254 * the entry in the array. If the status of the IO indiactes that
1255 * this IO was aborted then the sglq entry it put on the
1256 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1257 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001258 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001259 **/
1260static void
1261__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1262{
1263 struct lpfc_sglq *sglq;
1264 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001265 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001266 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001267
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001268 lockdep_assert_held(&phba->hbalock);
1269
James Smart4f774512009-05-22 14:52:35 -04001270 if (iocbq->sli4_xritag == NO_XRI)
1271 sglq = NULL;
1272 else
James Smart6d368e52011-05-24 11:44:12 -04001273 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1274
James Smart0e9bb8d2013-03-01 16:35:12 -05001275
James Smart4f774512009-05-22 14:52:35 -04001276 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001277 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1278 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1279 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001280 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001281 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001282 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001283 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1284 spin_unlock_irqrestore(
1285 &phba->sli4_hba.sgl_list_lock, iflag);
1286 goto out;
1287 }
1288
James Smart895427b2017-02-12 13:52:30 -08001289 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001290 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1291 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001292 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1293 iflag);
James Smart341af102010-01-26 23:07:37 -05001294 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001295 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001296 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001297 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001298 } else {
James Smart895427b2017-02-12 13:52:30 -08001299 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1300 iflag);
James Smart4f774512009-05-22 14:52:35 -04001301 sglq->state = SGL_FREED;
1302 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001303 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001304 &phba->sli4_hba.lpfc_els_sgl_list);
1305 spin_unlock_irqrestore(
1306 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001307
1308 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001309 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001310 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001311 }
James Smart4f774512009-05-22 14:52:35 -04001312 }
1313
James Smartf358dd02017-02-12 13:52:34 -08001314out:
James Smart4f774512009-05-22 14:52:35 -04001315 /*
1316 * Clean all volatile data fields, preserve iotag and node struct.
1317 */
1318 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001319 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001320 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001321 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1322 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001323 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1324}
1325
James Smart2a9bf3d2010-06-07 15:24:45 -04001326
James Smart4f774512009-05-22 14:52:35 -04001327/**
James Smart3772a992009-05-22 14:50:54 -04001328 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1329 * @phba: Pointer to HBA context object.
1330 * @iocbq: Pointer to driver iocb object.
1331 *
1332 * This function is called with hbalock held to release driver
1333 * iocb object to the iocb pool. The iotag in the iocb object
1334 * does not change for each use of the iocb object. This function
1335 * clears all other fields of the iocb object when it is freed.
1336 **/
1337static void
1338__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1339{
1340 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1341
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001342 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001343
1344 /*
James Smart3772a992009-05-22 14:50:54 -04001345 * Clean all volatile data fields, preserve iotag and node struct.
1346 */
1347 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1348 iocbq->sli4_xritag = NO_XRI;
1349 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1350}
1351
1352/**
James Smart3621a712009-04-06 18:47:14 -04001353 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001354 * @phba: Pointer to HBA context object.
1355 * @iocbq: Pointer to driver iocb object.
1356 *
1357 * This function is called with hbalock held to release driver
1358 * iocb object to the iocb pool. The iotag in the iocb object
1359 * does not change for each use of the iocb object. This function
1360 * clears all other fields of the iocb object when it is freed.
1361 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001362static void
James Smart2e0fef82007-06-17 19:56:36 -05001363__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1364{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001365 lockdep_assert_held(&phba->hbalock);
1366
James Smart3772a992009-05-22 14:50:54 -04001367 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001368 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001369}
1370
James Smarte59058c2008-08-24 21:49:00 -04001371/**
James Smart3621a712009-04-06 18:47:14 -04001372 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001373 * @phba: Pointer to HBA context object.
1374 * @iocbq: Pointer to driver iocb object.
1375 *
1376 * This function is called with no lock held to release the iocb to
1377 * iocb pool.
1378 **/
James Smart2e0fef82007-06-17 19:56:36 -05001379void
1380lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1381{
1382 unsigned long iflags;
1383
1384 /*
1385 * Clean all volatile data fields, preserve iotag and node struct.
1386 */
1387 spin_lock_irqsave(&phba->hbalock, iflags);
1388 __lpfc_sli_release_iocbq(phba, iocbq);
1389 spin_unlock_irqrestore(&phba->hbalock, iflags);
1390}
1391
James Smarte59058c2008-08-24 21:49:00 -04001392/**
James Smarta257bf92009-04-06 18:48:10 -04001393 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1394 * @phba: Pointer to HBA context object.
1395 * @iocblist: List of IOCBs.
1396 * @ulpstatus: ULP status in IOCB command field.
1397 * @ulpWord4: ULP word-4 in IOCB command field.
1398 *
1399 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1400 * on the list by invoking the complete callback function associated with the
1401 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1402 * fields.
1403 **/
1404void
1405lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1406 uint32_t ulpstatus, uint32_t ulpWord4)
1407{
1408 struct lpfc_iocbq *piocb;
1409
1410 while (!list_empty(iocblist)) {
1411 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001412 if (!piocb->iocb_cmpl)
1413 lpfc_sli_release_iocbq(phba, piocb);
1414 else {
1415 piocb->iocb.ulpStatus = ulpstatus;
1416 piocb->iocb.un.ulpWord[4] = ulpWord4;
1417 (piocb->iocb_cmpl) (phba, piocb, piocb);
1418 }
1419 }
1420 return;
1421}
1422
1423/**
James Smart3621a712009-04-06 18:47:14 -04001424 * lpfc_sli_iocb_cmd_type - Get the iocb type
1425 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001426 *
1427 * This function is called by ring event handler function to get the iocb type.
1428 * This function translates the iocb command to an iocb command type used to
1429 * decide the final disposition of each completed IOCB.
1430 * The function returns
1431 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1432 * LPFC_SOL_IOCB if it is a solicited iocb completion
1433 * LPFC_ABORT_IOCB if it is an abort iocb
1434 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1435 *
1436 * The caller is not required to hold any lock.
1437 **/
dea31012005-04-17 16:05:31 -05001438static lpfc_iocb_type
1439lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1440{
1441 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1442
1443 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1444 return 0;
1445
1446 switch (iocb_cmnd) {
1447 case CMD_XMIT_SEQUENCE_CR:
1448 case CMD_XMIT_SEQUENCE_CX:
1449 case CMD_XMIT_BCAST_CN:
1450 case CMD_XMIT_BCAST_CX:
1451 case CMD_ELS_REQUEST_CR:
1452 case CMD_ELS_REQUEST_CX:
1453 case CMD_CREATE_XRI_CR:
1454 case CMD_CREATE_XRI_CX:
1455 case CMD_GET_RPI_CN:
1456 case CMD_XMIT_ELS_RSP_CX:
1457 case CMD_GET_RPI_CR:
1458 case CMD_FCP_IWRITE_CR:
1459 case CMD_FCP_IWRITE_CX:
1460 case CMD_FCP_IREAD_CR:
1461 case CMD_FCP_IREAD_CX:
1462 case CMD_FCP_ICMND_CR:
1463 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001464 case CMD_FCP_TSEND_CX:
1465 case CMD_FCP_TRSP_CX:
1466 case CMD_FCP_TRECEIVE_CX:
1467 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001468 case CMD_ADAPTER_MSG:
1469 case CMD_ADAPTER_DUMP:
1470 case CMD_XMIT_SEQUENCE64_CR:
1471 case CMD_XMIT_SEQUENCE64_CX:
1472 case CMD_XMIT_BCAST64_CN:
1473 case CMD_XMIT_BCAST64_CX:
1474 case CMD_ELS_REQUEST64_CR:
1475 case CMD_ELS_REQUEST64_CX:
1476 case CMD_FCP_IWRITE64_CR:
1477 case CMD_FCP_IWRITE64_CX:
1478 case CMD_FCP_IREAD64_CR:
1479 case CMD_FCP_IREAD64_CX:
1480 case CMD_FCP_ICMND64_CR:
1481 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001482 case CMD_FCP_TSEND64_CX:
1483 case CMD_FCP_TRSP64_CX:
1484 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001485 case CMD_GEN_REQUEST64_CR:
1486 case CMD_GEN_REQUEST64_CX:
1487 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001488 case DSSCMD_IWRITE64_CR:
1489 case DSSCMD_IWRITE64_CX:
1490 case DSSCMD_IREAD64_CR:
1491 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001492 type = LPFC_SOL_IOCB;
1493 break;
1494 case CMD_ABORT_XRI_CN:
1495 case CMD_ABORT_XRI_CX:
1496 case CMD_CLOSE_XRI_CN:
1497 case CMD_CLOSE_XRI_CX:
1498 case CMD_XRI_ABORTED_CX:
1499 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001500 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001501 type = LPFC_ABORT_IOCB;
1502 break;
1503 case CMD_RCV_SEQUENCE_CX:
1504 case CMD_RCV_ELS_REQ_CX:
1505 case CMD_RCV_SEQUENCE64_CX:
1506 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001507 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001508 case CMD_IOCB_RCV_SEQ64_CX:
1509 case CMD_IOCB_RCV_ELS64_CX:
1510 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001511 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001512 type = LPFC_UNSOL_IOCB;
1513 break;
James Smart3163f722008-02-08 18:50:25 -05001514 case CMD_IOCB_XMIT_MSEQ64_CR:
1515 case CMD_IOCB_XMIT_MSEQ64_CX:
1516 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1517 case CMD_IOCB_RCV_ELS_LIST64_CX:
1518 case CMD_IOCB_CLOSE_EXTENDED_CN:
1519 case CMD_IOCB_ABORT_EXTENDED_CN:
1520 case CMD_IOCB_RET_HBQE64_CN:
1521 case CMD_IOCB_FCP_IBIDIR64_CR:
1522 case CMD_IOCB_FCP_IBIDIR64_CX:
1523 case CMD_IOCB_FCP_ITASKMGT64_CX:
1524 case CMD_IOCB_LOGENTRY_CN:
1525 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1526 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001527 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001528 type = LPFC_UNKNOWN_IOCB;
1529 break;
dea31012005-04-17 16:05:31 -05001530 default:
1531 type = LPFC_UNKNOWN_IOCB;
1532 break;
1533 }
1534
1535 return type;
1536}
1537
James Smarte59058c2008-08-24 21:49:00 -04001538/**
James Smart3621a712009-04-06 18:47:14 -04001539 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001540 * @phba: Pointer to HBA context object.
1541 *
1542 * This function is called from SLI initialization code
1543 * to configure every ring of the HBA's SLI interface. The
1544 * caller is not required to hold any lock. This function issues
1545 * a config_ring mailbox command for each ring.
1546 * This function returns zero if successful else returns a negative
1547 * error code.
1548 **/
dea31012005-04-17 16:05:31 -05001549static int
James Smarted957682007-06-17 19:56:37 -05001550lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001551{
1552 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001553 LPFC_MBOXQ_t *pmb;
1554 MAILBOX_t *pmbox;
1555 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001556
James Smarted957682007-06-17 19:56:37 -05001557 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1558 if (!pmb)
1559 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001560 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001561 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001562 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001563 lpfc_config_ring(phba, i, pmb);
1564 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1565 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001567 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001568 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1569 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001570 rc, pmbox->mbxCommand,
1571 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001572 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001573 ret = -ENXIO;
1574 break;
dea31012005-04-17 16:05:31 -05001575 }
1576 }
James Smarted957682007-06-17 19:56:37 -05001577 mempool_free(pmb, phba->mbox_mem_pool);
1578 return ret;
dea31012005-04-17 16:05:31 -05001579}
1580
James Smarte59058c2008-08-24 21:49:00 -04001581/**
James Smart3621a712009-04-06 18:47:14 -04001582 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001583 * @phba: Pointer to HBA context object.
1584 * @pring: Pointer to driver SLI ring object.
1585 * @piocb: Pointer to the driver iocb object.
1586 *
1587 * This function is called with hbalock held. The function adds the
1588 * new iocb to txcmplq of the given ring. This function always returns
1589 * 0. If this function is called for ELS ring, this function checks if
1590 * there is a vport associated with the ELS command. This function also
1591 * starts els_tmofunc timer if this is an ELS command.
1592 **/
dea31012005-04-17 16:05:31 -05001593static int
James Smart2e0fef82007-06-17 19:56:36 -05001594lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1595 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001596{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001597 lockdep_assert_held(&phba->hbalock);
1598
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001599 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001600
dea31012005-04-17 16:05:31 -05001601 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001602 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001603
James Smart92d7f7b2007-06-17 19:56:38 -05001604 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1605 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001606 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1607 BUG_ON(!piocb->vport);
1608 if (!(piocb->vport->load_flag & FC_UNLOADING))
1609 mod_timer(&piocb->vport->els_tmofunc,
1610 jiffies +
1611 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1612 }
dea31012005-04-17 16:05:31 -05001613
James Smart2e0fef82007-06-17 19:56:36 -05001614 return 0;
dea31012005-04-17 16:05:31 -05001615}
1616
James Smarte59058c2008-08-24 21:49:00 -04001617/**
James Smart3621a712009-04-06 18:47:14 -04001618 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001619 * @phba: Pointer to HBA context object.
1620 * @pring: Pointer to driver SLI ring object.
1621 *
1622 * This function is called with hbalock held to get next
1623 * iocb in txq of the given ring. If there is any iocb in
1624 * the txq, the function returns first iocb in the list after
1625 * removing the iocb from the list, else it returns NULL.
1626 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001627struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001628lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001629{
dea31012005-04-17 16:05:31 -05001630 struct lpfc_iocbq *cmd_iocb;
1631
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001632 lockdep_assert_held(&phba->hbalock);
1633
James Smart858c9f62007-06-17 19:56:39 -05001634 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001635 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001636}
1637
James Smarte59058c2008-08-24 21:49:00 -04001638/**
James Smart3621a712009-04-06 18:47:14 -04001639 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001640 * @phba: Pointer to HBA context object.
1641 * @pring: Pointer to driver SLI ring object.
1642 *
1643 * This function is called with hbalock held and the caller must post the
1644 * iocb without releasing the lock. If the caller releases the lock,
1645 * iocb slot returned by the function is not guaranteed to be available.
1646 * The function returns pointer to the next available iocb slot if there
1647 * is available slot in the ring, else it returns NULL.
1648 * If the get index of the ring is ahead of the put index, the function
1649 * will post an error attention event to the worker thread to take the
1650 * HBA to offline state.
1651 **/
dea31012005-04-17 16:05:31 -05001652static IOCB_t *
1653lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1654{
James Smart34b02dc2008-08-24 21:49:55 -04001655 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001656 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001657
1658 lockdep_assert_held(&phba->hbalock);
1659
James Smart7e56aa22012-08-03 12:35:34 -04001660 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1661 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1662 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001663
James Smart7e56aa22012-08-03 12:35:34 -04001664 if (unlikely(pring->sli.sli3.local_getidx ==
1665 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001666
James Smart7e56aa22012-08-03 12:35:34 -04001667 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001668
James Smart7e56aa22012-08-03 12:35:34 -04001669 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001670 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001671 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001672 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001673 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001674 pring->sli.sli3.local_getidx,
1675 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001676
James Smart2e0fef82007-06-17 19:56:36 -05001677 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001678 /*
1679 * All error attention handlers are posted to
1680 * worker thread
1681 */
1682 phba->work_ha |= HA_ERATT;
1683 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001684
James Smart5e9d9b82008-06-14 22:52:53 -04001685 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001686
1687 return NULL;
1688 }
1689
James Smart7e56aa22012-08-03 12:35:34 -04001690 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001691 return NULL;
1692 }
1693
James Smarted957682007-06-17 19:56:37 -05001694 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001695}
1696
James Smarte59058c2008-08-24 21:49:00 -04001697/**
James Smart3621a712009-04-06 18:47:14 -04001698 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001699 * @phba: Pointer to HBA context object.
1700 * @iocbq: Pointer to driver iocb object.
1701 *
1702 * This function gets an iotag for the iocb. If there is no unused iotag and
1703 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1704 * array and assigns a new iotag.
1705 * The function returns the allocated iotag if successful, else returns zero.
1706 * Zero is not a valid iotag.
1707 * The caller is not required to hold any lock.
1708 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001709uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001710lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001711{
James Smart2e0fef82007-06-17 19:56:36 -05001712 struct lpfc_iocbq **new_arr;
1713 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001714 size_t new_len;
1715 struct lpfc_sli *psli = &phba->sli;
1716 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001717
James Smart2e0fef82007-06-17 19:56:36 -05001718 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001719 iotag = psli->last_iotag;
1720 if(++iotag < psli->iocbq_lookup_len) {
1721 psli->last_iotag = iotag;
1722 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001723 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001724 iocbq->iotag = iotag;
1725 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001726 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001727 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1728 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001729 spin_unlock_irq(&phba->hbalock);
Kees Cook6396bb22018-06-12 14:03:40 -07001730 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001731 GFP_KERNEL);
1732 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001733 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001734 old_arr = psli->iocbq_lookup;
1735 if (new_len <= psli->iocbq_lookup_len) {
1736 /* highly unprobable case */
1737 kfree(new_arr);
1738 iotag = psli->last_iotag;
1739 if(++iotag < psli->iocbq_lookup_len) {
1740 psli->last_iotag = iotag;
1741 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001742 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001743 iocbq->iotag = iotag;
1744 return iotag;
1745 }
James Smart2e0fef82007-06-17 19:56:36 -05001746 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001747 return 0;
1748 }
1749 if (psli->iocbq_lookup)
1750 memcpy(new_arr, old_arr,
1751 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001752 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001753 psli->iocbq_lookup = new_arr;
1754 psli->iocbq_lookup_len = new_len;
1755 psli->last_iotag = iotag;
1756 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001757 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001758 iocbq->iotag = iotag;
1759 kfree(old_arr);
1760 return iotag;
1761 }
James Smart8f6d98d2006-08-01 07:34:00 -04001762 } else
James Smart2e0fef82007-06-17 19:56:36 -05001763 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001764
James Smartbc739052010-08-04 16:11:18 -04001765 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001766 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1767 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001768
James Bottomley604a3e32005-10-29 10:28:33 -05001769 return 0;
dea31012005-04-17 16:05:31 -05001770}
1771
James Smarte59058c2008-08-24 21:49:00 -04001772/**
James Smart3621a712009-04-06 18:47:14 -04001773 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001774 * @phba: Pointer to HBA context object.
1775 * @pring: Pointer to driver SLI ring object.
1776 * @iocb: Pointer to iocb slot in the ring.
1777 * @nextiocb: Pointer to driver iocb object which need to be
1778 * posted to firmware.
1779 *
1780 * This function is called with hbalock held to post a new iocb to
1781 * the firmware. This function copies the new iocb to ring iocb slot and
1782 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1783 * a completion call back for this iocb else the function will free the
1784 * iocb object.
1785 **/
dea31012005-04-17 16:05:31 -05001786static void
1787lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1788 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1789{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001790 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001791 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001792 * Set up an iotag
dea31012005-04-17 16:05:31 -05001793 */
James Bottomley604a3e32005-10-29 10:28:33 -05001794 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001795
James Smarte2a0a9d2008-12-04 22:40:02 -05001796
James Smarta58cbd52007-08-02 11:09:43 -04001797 if (pring->ringno == LPFC_ELS_RING) {
1798 lpfc_debugfs_slow_ring_trc(phba,
1799 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1800 *(((uint32_t *) &nextiocb->iocb) + 4),
1801 *(((uint32_t *) &nextiocb->iocb) + 6),
1802 *(((uint32_t *) &nextiocb->iocb) + 7));
1803 }
1804
dea31012005-04-17 16:05:31 -05001805 /*
1806 * Issue iocb command to adapter
1807 */
James Smart92d7f7b2007-06-17 19:56:38 -05001808 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001809 wmb();
1810 pring->stats.iocb_cmd++;
1811
1812 /*
1813 * If there is no completion routine to call, we can release the
1814 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1815 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1816 */
1817 if (nextiocb->iocb_cmpl)
1818 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001819 else
James Smart2e0fef82007-06-17 19:56:36 -05001820 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001821
1822 /*
1823 * Let the HBA know what IOCB slot will be the next one the
1824 * driver will put a command into.
1825 */
James Smart7e56aa22012-08-03 12:35:34 -04001826 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1827 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001828}
1829
James Smarte59058c2008-08-24 21:49:00 -04001830/**
James Smart3621a712009-04-06 18:47:14 -04001831 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001832 * @phba: Pointer to HBA context object.
1833 * @pring: Pointer to driver SLI ring object.
1834 *
1835 * The caller is not required to hold any lock for calling this function.
1836 * This function updates the chip attention bits for the ring to inform firmware
1837 * that there are pending work to be done for this ring and requests an
1838 * interrupt when there is space available in the ring. This function is
1839 * called when the driver is unable to post more iocbs to the ring due
1840 * to unavailability of space in the ring.
1841 **/
dea31012005-04-17 16:05:31 -05001842static void
James Smart2e0fef82007-06-17 19:56:36 -05001843lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001844{
1845 int ringno = pring->ringno;
1846
1847 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1848
1849 wmb();
1850
1851 /*
1852 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1853 * The HBA will tell us when an IOCB entry is available.
1854 */
1855 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1856 readl(phba->CAregaddr); /* flush */
1857
1858 pring->stats.iocb_cmd_full++;
1859}
1860
James Smarte59058c2008-08-24 21:49:00 -04001861/**
James Smart3621a712009-04-06 18:47:14 -04001862 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001863 * @phba: Pointer to HBA context object.
1864 * @pring: Pointer to driver SLI ring object.
1865 *
1866 * This function updates the chip attention register bit for the
1867 * given ring to inform HBA that there is more work to be done
1868 * in this ring. The caller is not required to hold any lock.
1869 **/
dea31012005-04-17 16:05:31 -05001870static void
James Smart2e0fef82007-06-17 19:56:36 -05001871lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001872{
1873 int ringno = pring->ringno;
1874
1875 /*
1876 * Tell the HBA that there is work to do in this ring.
1877 */
James Smart34b02dc2008-08-24 21:49:55 -04001878 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1879 wmb();
1880 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1881 readl(phba->CAregaddr); /* flush */
1882 }
dea31012005-04-17 16:05:31 -05001883}
1884
James Smarte59058c2008-08-24 21:49:00 -04001885/**
James Smart3621a712009-04-06 18:47:14 -04001886 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001887 * @phba: Pointer to HBA context object.
1888 * @pring: Pointer to driver SLI ring object.
1889 *
1890 * This function is called with hbalock held to post pending iocbs
1891 * in the txq to the firmware. This function is called when driver
1892 * detects space available in the ring.
1893 **/
dea31012005-04-17 16:05:31 -05001894static void
James Smart2e0fef82007-06-17 19:56:36 -05001895lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001896{
1897 IOCB_t *iocb;
1898 struct lpfc_iocbq *nextiocb;
1899
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001900 lockdep_assert_held(&phba->hbalock);
1901
dea31012005-04-17 16:05:31 -05001902 /*
1903 * Check to see if:
1904 * (a) there is anything on the txq to send
1905 * (b) link is up
1906 * (c) link attention events can be processed (fcp ring only)
1907 * (d) IOCB processing is not blocked by the outstanding mbox command.
1908 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001909
1910 if (lpfc_is_link_up(phba) &&
1911 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001912 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001913 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001914
1915 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1916 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1917 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1918
1919 if (iocb)
1920 lpfc_sli_update_ring(phba, pring);
1921 else
1922 lpfc_sli_update_full_ring(phba, pring);
1923 }
1924
1925 return;
1926}
1927
James Smarte59058c2008-08-24 21:49:00 -04001928/**
James Smart3621a712009-04-06 18:47:14 -04001929 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001930 * @phba: Pointer to HBA context object.
1931 * @hbqno: HBQ number.
1932 *
1933 * This function is called with hbalock held to get the next
1934 * available slot for the given HBQ. If there is free slot
1935 * available for the HBQ it will return pointer to the next available
1936 * HBQ entry else it will return NULL.
1937 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001938static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001939lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1940{
1941 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1942
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001943 lockdep_assert_held(&phba->hbalock);
1944
James Smarted957682007-06-17 19:56:37 -05001945 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1946 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1947 hbqp->next_hbqPutIdx = 0;
1948
1949 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001950 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001951 uint32_t getidx = le32_to_cpu(raw_index);
1952
1953 hbqp->local_hbqGetIdx = getidx;
1954
1955 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1956 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001957 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001958 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001959 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001960 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001961 hbqp->entry_count);
1962
1963 phba->link_state = LPFC_HBA_ERROR;
1964 return NULL;
1965 }
1966
1967 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1968 return NULL;
1969 }
1970
James Smart51ef4c22007-08-02 11:10:31 -04001971 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1972 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001973}
1974
James Smarte59058c2008-08-24 21:49:00 -04001975/**
James Smart3621a712009-04-06 18:47:14 -04001976 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001977 * @phba: Pointer to HBA context object.
1978 *
1979 * This function is called with no lock held to free all the
1980 * hbq buffers while uninitializing the SLI interface. It also
1981 * frees the HBQ buffers returned by the firmware but not yet
1982 * processed by the upper layers.
1983 **/
James Smarted957682007-06-17 19:56:37 -05001984void
1985lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1986{
James Smart92d7f7b2007-06-17 19:56:38 -05001987 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1988 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001989 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001990 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001991
James Smart51ef4c22007-08-02 11:10:31 -04001992 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001993 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001994 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001995 for (i = 0; i < hbq_count; ++i) {
1996 list_for_each_entry_safe(dmabuf, next_dmabuf,
1997 &phba->hbqs[i].hbq_buffer_list, list) {
1998 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1999 list_del(&hbq_buf->dbuf.list);
2000 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2001 }
James Smarta8adb832007-10-27 13:37:53 -04002002 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05002003 }
James Smart3163f722008-02-08 18:50:25 -05002004
2005 /* Mark the HBQs not in use */
2006 phba->hbq_in_use = 0;
2007 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05002008}
2009
James Smarte59058c2008-08-24 21:49:00 -04002010/**
James Smart3621a712009-04-06 18:47:14 -04002011 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002012 * @phba: Pointer to HBA context object.
2013 * @hbqno: HBQ number.
2014 * @hbq_buf: Pointer to HBQ buffer.
2015 *
2016 * This function is called with the hbalock held to post a
2017 * hbq buffer to the firmware. If the function finds an empty
2018 * slot in the HBQ, it will post the buffer. The function will return
2019 * pointer to the hbq entry if it successfully post the buffer
2020 * else it will return NULL.
2021 **/
James Smart3772a992009-05-22 14:50:54 -04002022static int
James Smarted957682007-06-17 19:56:37 -05002023lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05002024 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05002025{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002026 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002027 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2028}
2029
2030/**
2031 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2032 * @phba: Pointer to HBA context object.
2033 * @hbqno: HBQ number.
2034 * @hbq_buf: Pointer to HBQ buffer.
2035 *
2036 * This function is called with the hbalock held to post a hbq buffer to the
2037 * firmware. If the function finds an empty slot in the HBQ, it will post the
2038 * buffer and place it on the hbq_buffer_list. The function will return zero if
2039 * it successfully post the buffer else it will return an error.
2040 **/
2041static int
2042lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2043 struct hbq_dmabuf *hbq_buf)
2044{
James Smarted957682007-06-17 19:56:37 -05002045 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002046 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002047
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002048 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002049 /* Get next HBQ entry slot to use */
2050 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2051 if (hbqe) {
2052 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2053
James Smart92d7f7b2007-06-17 19:56:38 -05002054 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2055 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002056 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002057 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002058 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2059 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2060 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002061 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2062 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002063 /* flush */
James Smarted957682007-06-17 19:56:37 -05002064 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002065 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002066 return 0;
2067 } else
2068 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002069}
2070
James Smart4f774512009-05-22 14:52:35 -04002071/**
2072 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2073 * @phba: Pointer to HBA context object.
2074 * @hbqno: HBQ number.
2075 * @hbq_buf: Pointer to HBQ buffer.
2076 *
2077 * This function is called with the hbalock held to post an RQE to the SLI4
2078 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2079 * the hbq_buffer_list and return zero, otherwise it will return an error.
2080 **/
2081static int
2082lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2083 struct hbq_dmabuf *hbq_buf)
2084{
2085 int rc;
2086 struct lpfc_rqe hrqe;
2087 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002088 struct lpfc_queue *hrq;
2089 struct lpfc_queue *drq;
2090
2091 if (hbqno != LPFC_ELS_HBQ)
2092 return 1;
2093 hrq = phba->sli4_hba.hdr_rq;
2094 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002095
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002096 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002097 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2098 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2099 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2100 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002101 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002102 if (rc < 0)
2103 return rc;
James Smart895427b2017-02-12 13:52:30 -08002104 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002105 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2106 return 0;
2107}
2108
James Smarte59058c2008-08-24 21:49:00 -04002109/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002110static struct lpfc_hbq_init lpfc_els_hbq = {
2111 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002112 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002113 .mask_count = 0,
2114 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002115 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002116 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002117 .init_count = 40,
2118 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002119};
James Smarted957682007-06-17 19:56:37 -05002120
James Smarte59058c2008-08-24 21:49:00 -04002121/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002122struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002123 &lpfc_els_hbq,
2124};
2125
James Smarte59058c2008-08-24 21:49:00 -04002126/**
James Smart3621a712009-04-06 18:47:14 -04002127 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002128 * @phba: Pointer to HBA context object.
2129 * @hbqno: HBQ number.
2130 * @count: Number of HBQ buffers to be posted.
2131 *
James Smartd7c255b2008-08-24 21:50:00 -04002132 * This function is called with no lock held to post more hbq buffers to the
2133 * given HBQ. The function returns the number of HBQ buffers successfully
2134 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002135 **/
James Smart311464e2007-08-02 11:10:37 -04002136static int
James Smart92d7f7b2007-06-17 19:56:38 -05002137lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2138{
James Smartd7c255b2008-08-24 21:50:00 -04002139 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002140 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002141 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002142 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002143 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002144 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002145
James Smartd7c255b2008-08-24 21:50:00 -04002146 if ((phba->hbqs[hbqno].buffer_count + count) >
2147 lpfc_hbq_defs[hbqno]->entry_count)
2148 count = lpfc_hbq_defs[hbqno]->entry_count -
2149 phba->hbqs[hbqno].buffer_count;
2150 if (!count)
2151 return 0;
2152 /* Allocate HBQ entries */
2153 for (i = 0; i < count; i++) {
2154 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2155 if (!hbq_buffer)
2156 break;
2157 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2158 }
James Smart3163f722008-02-08 18:50:25 -05002159 /* Check whether HBQ is still in use */
2160 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002161 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002162 goto err;
2163 while (!list_empty(&hbq_buf_list)) {
2164 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2165 dbuf.list);
2166 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2167 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002168 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002169 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002170 posted++;
2171 } else
James Smart51ef4c22007-08-02 11:10:31 -04002172 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002173 }
James Smart3163f722008-02-08 18:50:25 -05002174 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002175 return posted;
2176err:
2177 spin_unlock_irqrestore(&phba->hbalock, flags);
2178 while (!list_empty(&hbq_buf_list)) {
2179 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2180 dbuf.list);
2181 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2182 }
James Smart92d7f7b2007-06-17 19:56:38 -05002183 return 0;
James Smarted957682007-06-17 19:56:37 -05002184}
2185
James Smarte59058c2008-08-24 21:49:00 -04002186/**
James Smart3621a712009-04-06 18:47:14 -04002187 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002188 * @phba: Pointer to HBA context object.
2189 * @qno: HBQ number.
2190 *
2191 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002192 * is called with no lock held. The function returns the number of HBQ entries
2193 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002194 **/
James Smarted957682007-06-17 19:56:37 -05002195int
James Smart92d7f7b2007-06-17 19:56:38 -05002196lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002197{
James Smartdef9c7a2009-12-21 17:02:28 -05002198 if (phba->sli_rev == LPFC_SLI_REV4)
2199 return 0;
2200 else
2201 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2202 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002203}
2204
James Smarte59058c2008-08-24 21:49:00 -04002205/**
James Smart3621a712009-04-06 18:47:14 -04002206 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002207 * @phba: Pointer to HBA context object.
2208 * @qno: HBQ queue number.
2209 *
2210 * This function is called from SLI initialization code path with
2211 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002212 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002213 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002214static int
James Smart92d7f7b2007-06-17 19:56:38 -05002215lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002216{
James Smartdef9c7a2009-12-21 17:02:28 -05002217 if (phba->sli_rev == LPFC_SLI_REV4)
2218 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002219 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002220 else
2221 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2222 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002223}
2224
James Smarte59058c2008-08-24 21:49:00 -04002225/**
James Smart3772a992009-05-22 14:50:54 -04002226 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2227 * @phba: Pointer to HBA context object.
2228 * @hbqno: HBQ number.
2229 *
2230 * This function removes the first hbq buffer on an hbq list and returns a
2231 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2232 **/
2233static struct hbq_dmabuf *
2234lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2235{
2236 struct lpfc_dmabuf *d_buf;
2237
2238 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2239 if (!d_buf)
2240 return NULL;
2241 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2242}
2243
2244/**
James Smart2d7dbc42017-02-12 13:52:35 -08002245 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2246 * @phba: Pointer to HBA context object.
2247 * @hbqno: HBQ number.
2248 *
2249 * This function removes the first RQ buffer on an RQ buffer list and returns a
2250 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2251 **/
2252static struct rqb_dmabuf *
2253lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2254{
2255 struct lpfc_dmabuf *h_buf;
2256 struct lpfc_rqb *rqbp;
2257
2258 rqbp = hrq->rqbp;
2259 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2260 struct lpfc_dmabuf, list);
2261 if (!h_buf)
2262 return NULL;
2263 rqbp->buffer_count--;
2264 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2265}
2266
2267/**
James Smart3621a712009-04-06 18:47:14 -04002268 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002269 * @phba: Pointer to HBA context object.
2270 * @tag: Tag of the hbq buffer.
2271 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002272 * This function searches for the hbq buffer associated with the given tag in
2273 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2274 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002275 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002276static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002277lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2278{
James Smart92d7f7b2007-06-17 19:56:38 -05002279 struct lpfc_dmabuf *d_buf;
2280 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002281 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002282
James Smart51ef4c22007-08-02 11:10:31 -04002283 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002284 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002285 return NULL;
2286
James Smart3772a992009-05-22 14:50:54 -04002287 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002288 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002289 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002290 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002291 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002292 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002293 }
2294 }
James Smart3772a992009-05-22 14:50:54 -04002295 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002296 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002297 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002298 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002299 return NULL;
James Smarted957682007-06-17 19:56:37 -05002300}
2301
James Smarte59058c2008-08-24 21:49:00 -04002302/**
James Smart3621a712009-04-06 18:47:14 -04002303 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002304 * @phba: Pointer to HBA context object.
2305 * @hbq_buffer: Pointer to HBQ buffer.
2306 *
2307 * This function is called with hbalock. This function gives back
2308 * the hbq buffer to firmware. If the HBQ does not have space to
2309 * post the buffer, it will free the buffer.
2310 **/
James Smarted957682007-06-17 19:56:37 -05002311void
James Smart51ef4c22007-08-02 11:10:31 -04002312lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002313{
2314 uint32_t hbqno;
2315
James Smart51ef4c22007-08-02 11:10:31 -04002316 if (hbq_buffer) {
2317 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002318 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002319 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002320 }
2321}
2322
James Smarte59058c2008-08-24 21:49:00 -04002323/**
James Smart3621a712009-04-06 18:47:14 -04002324 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002325 * @mbxCommand: mailbox command code.
2326 *
2327 * This function is called by the mailbox event handler function to verify
2328 * that the completed mailbox command is a legitimate mailbox command. If the
2329 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2330 * and the mailbox event handler will take the HBA offline.
2331 **/
dea31012005-04-17 16:05:31 -05002332static int
2333lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2334{
2335 uint8_t ret;
2336
2337 switch (mbxCommand) {
2338 case MBX_LOAD_SM:
2339 case MBX_READ_NV:
2340 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002341 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002342 case MBX_RUN_BIU_DIAG:
2343 case MBX_INIT_LINK:
2344 case MBX_DOWN_LINK:
2345 case MBX_CONFIG_LINK:
2346 case MBX_CONFIG_RING:
2347 case MBX_RESET_RING:
2348 case MBX_READ_CONFIG:
2349 case MBX_READ_RCONFIG:
2350 case MBX_READ_SPARM:
2351 case MBX_READ_STATUS:
2352 case MBX_READ_RPI:
2353 case MBX_READ_XRI:
2354 case MBX_READ_REV:
2355 case MBX_READ_LNK_STAT:
2356 case MBX_REG_LOGIN:
2357 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002358 case MBX_CLEAR_LA:
2359 case MBX_DUMP_MEMORY:
2360 case MBX_DUMP_CONTEXT:
2361 case MBX_RUN_DIAGS:
2362 case MBX_RESTART:
2363 case MBX_UPDATE_CFG:
2364 case MBX_DOWN_LOAD:
2365 case MBX_DEL_LD_ENTRY:
2366 case MBX_RUN_PROGRAM:
2367 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002368 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002369 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002370 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002371 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002372 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002373 case MBX_LOAD_AREA:
2374 case MBX_RUN_BIU_DIAG64:
2375 case MBX_CONFIG_PORT:
2376 case MBX_READ_SPARM64:
2377 case MBX_READ_RPI64:
2378 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002379 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002380 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002381 case MBX_SET_DEBUG:
2382 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002383 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002384 case MBX_REG_VPI:
2385 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002386 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002387 case MBX_PORT_CAPABILITIES:
2388 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002389 case MBX_SLI4_CONFIG:
2390 case MBX_SLI4_REQ_FTRS:
2391 case MBX_REG_FCFI:
2392 case MBX_UNREG_FCFI:
2393 case MBX_REG_VFI:
2394 case MBX_UNREG_VFI:
2395 case MBX_INIT_VPI:
2396 case MBX_INIT_VFI:
2397 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002398 case MBX_READ_EVENT_LOG_STATUS:
2399 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002400 case MBX_SECURITY_MGMT:
2401 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002402 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002403 ret = mbxCommand;
2404 break;
2405 default:
2406 ret = MBX_SHUTDOWN;
2407 break;
2408 }
James Smart2e0fef82007-06-17 19:56:36 -05002409 return ret;
dea31012005-04-17 16:05:31 -05002410}
James Smarte59058c2008-08-24 21:49:00 -04002411
2412/**
James Smart3621a712009-04-06 18:47:14 -04002413 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002414 * @phba: Pointer to HBA context object.
2415 * @pmboxq: Pointer to mailbox command.
2416 *
2417 * This is completion handler function for mailbox commands issued from
2418 * lpfc_sli_issue_mbox_wait function. This function is called by the
2419 * mailbox event handler function with no lock held. This function
2420 * will wake up thread waiting on the wait queue pointed by context1
2421 * of the mailbox.
2422 **/
James Smart04c68492009-05-22 14:52:52 -04002423void
James Smart2e0fef82007-06-17 19:56:36 -05002424lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002425{
James Smart858c9f62007-06-17 19:56:39 -05002426 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002427 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002428
2429 /*
James Smarte29d74f2018-03-05 12:04:07 -08002430 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002431 * continued running.
2432 */
James Smart7054a602007-04-25 09:52:34 -04002433 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002434 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002435 pmbox_done = (struct completion *)pmboxq->context3;
2436 if (pmbox_done)
2437 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002438 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002439 return;
2440}
2441
James Smarte59058c2008-08-24 21:49:00 -04002442
2443/**
James Smart3621a712009-04-06 18:47:14 -04002444 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002445 * @phba: Pointer to HBA context object.
2446 * @pmb: Pointer to mailbox object.
2447 *
2448 * This function is the default mailbox completion handler. It
2449 * frees the memory resources associated with the completed mailbox
2450 * command. If the completed command is a REG_LOGIN mailbox command,
2451 * this function will issue a UREG_LOGIN to re-claim the RPI.
2452 **/
dea31012005-04-17 16:05:31 -05002453void
James Smart2e0fef82007-06-17 19:56:36 -05002454lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002455{
James Smartd439d282010-09-29 11:18:45 -04002456 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002457 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002458 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002459 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002460 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002461 int rc;
2462
dea31012005-04-17 16:05:31 -05002463 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002464
dea31012005-04-17 16:05:31 -05002465 if (mp) {
2466 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2467 kfree(mp);
2468 }
James Smart7054a602007-04-25 09:52:34 -04002469
2470 /*
2471 * If a REG_LOGIN succeeded after node is destroyed or node
2472 * is in re-discovery driver need to cleanup the RPI.
2473 */
James Smart2e0fef82007-06-17 19:56:36 -05002474 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002475 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2476 !pmb->u.mb.mbxStatus) {
2477 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002478 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002479 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002480 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002481 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002482 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2483 if (rc != MBX_NOT_FINISHED)
2484 return;
2485 }
2486
James Smart695a8142010-01-26 23:08:03 -05002487 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2488 !(phba->pport->load_flag & FC_UNLOADING) &&
2489 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002490 shost = lpfc_shost_from_vport(vport);
2491 spin_lock_irq(shost->host_lock);
2492 vport->vpi_state |= LPFC_VPI_REGISTERED;
2493 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2494 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002495 }
2496
James Smartd439d282010-09-29 11:18:45 -04002497 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2498 ndlp = (struct lpfc_nodelist *)pmb->context2;
2499 lpfc_nlp_put(ndlp);
2500 pmb->context2 = NULL;
2501 }
2502
James Smartdcf2a4e2010-09-29 11:18:53 -04002503 /* Check security permission status on INIT_LINK mailbox command */
2504 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2505 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2506 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2507 "2860 SLI authentication is required "
2508 "for INIT_LINK but has not done yet\n");
2509
James Smart04c68492009-05-22 14:52:52 -04002510 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2511 lpfc_sli4_mbox_cmd_free(phba, pmb);
2512 else
2513 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002514}
James Smartbe6bb942015-04-07 15:07:22 -04002515 /**
2516 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2517 * @phba: Pointer to HBA context object.
2518 * @pmb: Pointer to mailbox object.
2519 *
2520 * This function is the unreg rpi mailbox completion handler. It
2521 * frees the memory resources associated with the completed mailbox
2522 * command. An additional refrenece is put on the ndlp to prevent
2523 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2524 * the unreg mailbox command completes, this routine puts the
2525 * reference back.
2526 *
2527 **/
2528void
2529lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2530{
2531 struct lpfc_vport *vport = pmb->vport;
2532 struct lpfc_nodelist *ndlp;
2533
2534 ndlp = pmb->context1;
2535 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2536 if (phba->sli_rev == LPFC_SLI_REV4 &&
2537 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002538 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002539 LPFC_SLI_INTF_IF_TYPE_2)) {
2540 if (ndlp) {
2541 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2542 "0010 UNREG_LOGIN vpi:%x "
2543 "rpi:%x DID:%x map:%x %p\n",
2544 vport->vpi, ndlp->nlp_rpi,
2545 ndlp->nlp_DID,
2546 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002547 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002548 lpfc_nlp_put(ndlp);
2549 }
2550 }
2551 }
2552
2553 mempool_free(pmb, phba->mbox_mem_pool);
2554}
dea31012005-04-17 16:05:31 -05002555
James Smarte59058c2008-08-24 21:49:00 -04002556/**
James Smart3621a712009-04-06 18:47:14 -04002557 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002558 * @phba: Pointer to HBA context object.
2559 *
2560 * This function is called with no lock held. This function processes all
2561 * the completed mailbox commands and gives it to upper layers. The interrupt
2562 * service routine processes mailbox completion interrupt and adds completed
2563 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2564 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2565 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2566 * function returns the mailbox commands to the upper layer by calling the
2567 * completion handler function of each mailbox.
2568 **/
dea31012005-04-17 16:05:31 -05002569int
James Smart2e0fef82007-06-17 19:56:36 -05002570lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002571{
James Smart92d7f7b2007-06-17 19:56:38 -05002572 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002573 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002574 int rc;
2575 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002576
2577 phba->sli.slistat.mbox_event++;
2578
James Smart92d7f7b2007-06-17 19:56:38 -05002579 /* Get all completed mailboxe buffers into the cmplq */
2580 spin_lock_irq(&phba->hbalock);
2581 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2582 spin_unlock_irq(&phba->hbalock);
2583
dea31012005-04-17 16:05:31 -05002584 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002585 do {
2586 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2587 if (pmb == NULL)
2588 break;
2589
James Smart04c68492009-05-22 14:52:52 -04002590 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002591
James Smart858c9f62007-06-17 19:56:39 -05002592 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2593 if (pmb->vport) {
2594 lpfc_debugfs_disc_trc(pmb->vport,
2595 LPFC_DISC_TRC_MBOX_VPORT,
2596 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2597 (uint32_t)pmbox->mbxCommand,
2598 pmbox->un.varWords[0],
2599 pmbox->un.varWords[1]);
2600 }
2601 else {
2602 lpfc_debugfs_disc_trc(phba->pport,
2603 LPFC_DISC_TRC_MBOX,
2604 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2605 (uint32_t)pmbox->mbxCommand,
2606 pmbox->un.varWords[0],
2607 pmbox->un.varWords[1]);
2608 }
2609 }
2610
dea31012005-04-17 16:05:31 -05002611 /*
2612 * It is a fatal error if unknown mbox command completion.
2613 */
2614 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2615 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002616 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002617 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002618 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002619 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002620 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002621 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002622 lpfc_sli_config_mbox_subsys_get(phba,
2623 pmb),
2624 lpfc_sli_config_mbox_opcode_get(phba,
2625 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002626 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002627 phba->work_hs = HS_FFER3;
2628 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002629 continue;
dea31012005-04-17 16:05:31 -05002630 }
2631
dea31012005-04-17 16:05:31 -05002632 if (pmbox->mbxStatus) {
2633 phba->sli.slistat.mbox_stat_err++;
2634 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2635 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002636 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002637 LOG_MBOX | LOG_SLI,
2638 "(%d):0305 Mbox cmd cmpl "
2639 "error - RETRYing Data: x%x "
2640 "(x%x/x%x) x%x x%x x%x\n",
2641 pmb->vport ? pmb->vport->vpi : 0,
2642 pmbox->mbxCommand,
2643 lpfc_sli_config_mbox_subsys_get(phba,
2644 pmb),
2645 lpfc_sli_config_mbox_opcode_get(phba,
2646 pmb),
2647 pmbox->mbxStatus,
2648 pmbox->un.varWords[0],
2649 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002650 pmbox->mbxStatus = 0;
2651 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002652 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002653 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002654 continue;
dea31012005-04-17 16:05:31 -05002655 }
2656 }
2657
2658 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002659 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002660 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002661 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2662 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002663 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002664 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002665 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2666 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002667 pmb->mbox_cmpl,
2668 *((uint32_t *) pmbox),
2669 pmbox->un.varWords[0],
2670 pmbox->un.varWords[1],
2671 pmbox->un.varWords[2],
2672 pmbox->un.varWords[3],
2673 pmbox->un.varWords[4],
2674 pmbox->un.varWords[5],
2675 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002676 pmbox->un.varWords[7],
2677 pmbox->un.varWords[8],
2678 pmbox->un.varWords[9],
2679 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002680
James Smart92d7f7b2007-06-17 19:56:38 -05002681 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002682 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002683 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002684 return 0;
dea31012005-04-17 16:05:31 -05002685}
James Smart92d7f7b2007-06-17 19:56:38 -05002686
James Smarte59058c2008-08-24 21:49:00 -04002687/**
James Smart3621a712009-04-06 18:47:14 -04002688 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002689 * @phba: Pointer to HBA context object.
2690 * @pring: Pointer to driver SLI ring object.
2691 * @tag: buffer tag.
2692 *
2693 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2694 * is set in the tag the buffer is posted for a particular exchange,
2695 * the function will return the buffer without replacing the buffer.
2696 * If the buffer is for unsolicited ELS or CT traffic, this function
2697 * returns the buffer and also posts another buffer to the firmware.
2698 **/
James Smart76bb24e2007-10-27 13:38:00 -04002699static struct lpfc_dmabuf *
2700lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002701 struct lpfc_sli_ring *pring,
2702 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002703{
James Smart9f1e1b52008-12-04 22:39:40 -05002704 struct hbq_dmabuf *hbq_entry;
2705
James Smart76bb24e2007-10-27 13:38:00 -04002706 if (tag & QUE_BUFTAG_BIT)
2707 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002708 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2709 if (!hbq_entry)
2710 return NULL;
2711 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002712}
James Smart57127f12007-10-27 13:37:05 -04002713
James Smart3772a992009-05-22 14:50:54 -04002714/**
2715 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2716 * @phba: Pointer to HBA context object.
2717 * @pring: Pointer to driver SLI ring object.
2718 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2719 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2720 * @fch_type: the type for the first frame of the sequence.
2721 *
2722 * This function is called with no lock held. This function uses the r_ctl and
2723 * type of the received sequence to find the correct callback function to call
2724 * to process the sequence.
2725 **/
2726static int
2727lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2728 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2729 uint32_t fch_type)
2730{
2731 int i;
2732
James Smartf358dd02017-02-12 13:52:34 -08002733 switch (fch_type) {
2734 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002735 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002736 return 1;
2737 default:
2738 break;
2739 }
2740
James Smart3772a992009-05-22 14:50:54 -04002741 /* unSolicited Responses */
2742 if (pring->prt[0].profile) {
2743 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2744 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2745 saveq);
2746 return 1;
2747 }
2748 /* We must search, based on rctl / type
2749 for the right routine */
2750 for (i = 0; i < pring->num_mask; i++) {
2751 if ((pring->prt[i].rctl == fch_r_ctl) &&
2752 (pring->prt[i].type == fch_type)) {
2753 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2754 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2755 (phba, pring, saveq);
2756 return 1;
2757 }
2758 }
2759 return 0;
2760}
James Smarte59058c2008-08-24 21:49:00 -04002761
2762/**
James Smart3621a712009-04-06 18:47:14 -04002763 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002764 * @phba: Pointer to HBA context object.
2765 * @pring: Pointer to driver SLI ring object.
2766 * @saveq: Pointer to the unsolicited iocb.
2767 *
2768 * This function is called with no lock held by the ring event handler
2769 * when there is an unsolicited iocb posted to the response ring by the
2770 * firmware. This function gets the buffer associated with the iocbs
2771 * and calls the event handler for the ring. This function handles both
2772 * qring buffers and hbq buffers.
2773 * When the function returns 1 the caller can free the iocb object otherwise
2774 * upper layer functions will free the iocb objects.
2775 **/
dea31012005-04-17 16:05:31 -05002776static int
2777lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2778 struct lpfc_iocbq *saveq)
2779{
2780 IOCB_t * irsp;
2781 WORD5 * w5p;
2782 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002783 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002784 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002785
dea31012005-04-17 16:05:31 -05002786 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002787
2788 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2789 if (pring->lpfc_sli_rcv_async_status)
2790 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2791 else
2792 lpfc_printf_log(phba,
2793 KERN_WARNING,
2794 LOG_SLI,
2795 "0316 Ring %d handler: unexpected "
2796 "ASYNC_STATUS iocb received evt_code "
2797 "0x%x\n",
2798 pring->ringno,
2799 irsp->un.asyncstat.evt_code);
2800 return 1;
2801 }
2802
James Smart3163f722008-02-08 18:50:25 -05002803 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2804 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2805 if (irsp->ulpBdeCount > 0) {
2806 dmzbuf = lpfc_sli_get_buff(phba, pring,
2807 irsp->un.ulpWord[3]);
2808 lpfc_in_buf_free(phba, dmzbuf);
2809 }
2810
2811 if (irsp->ulpBdeCount > 1) {
2812 dmzbuf = lpfc_sli_get_buff(phba, pring,
2813 irsp->unsli3.sli3Words[3]);
2814 lpfc_in_buf_free(phba, dmzbuf);
2815 }
2816
2817 if (irsp->ulpBdeCount > 2) {
2818 dmzbuf = lpfc_sli_get_buff(phba, pring,
2819 irsp->unsli3.sli3Words[7]);
2820 lpfc_in_buf_free(phba, dmzbuf);
2821 }
2822
2823 return 1;
2824 }
2825
James Smart92d7f7b2007-06-17 19:56:38 -05002826 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002827 if (irsp->ulpBdeCount != 0) {
2828 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002829 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002830 if (!saveq->context2)
2831 lpfc_printf_log(phba,
2832 KERN_ERR,
2833 LOG_SLI,
2834 "0341 Ring %d Cannot find buffer for "
2835 "an unsolicited iocb. tag 0x%x\n",
2836 pring->ringno,
2837 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002838 }
2839 if (irsp->ulpBdeCount == 2) {
2840 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002841 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002842 if (!saveq->context3)
2843 lpfc_printf_log(phba,
2844 KERN_ERR,
2845 LOG_SLI,
2846 "0342 Ring %d Cannot find buffer for an"
2847 " unsolicited iocb. tag 0x%x\n",
2848 pring->ringno,
2849 irsp->unsli3.sli3Words[7]);
2850 }
2851 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002852 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002853 if (irsp->ulpBdeCount != 0) {
2854 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2855 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002856 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002857 lpfc_printf_log(phba,
2858 KERN_ERR,
2859 LOG_SLI,
2860 "0343 Ring %d Cannot find "
2861 "buffer for an unsolicited iocb"
2862 ". tag 0x%x\n", pring->ringno,
2863 irsp->un.ulpWord[3]);
2864 }
2865 if (irsp->ulpBdeCount == 2) {
2866 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2867 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002868 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002869 lpfc_printf_log(phba,
2870 KERN_ERR,
2871 LOG_SLI,
2872 "0344 Ring %d Cannot find "
2873 "buffer for an unsolicited "
2874 "iocb. tag 0x%x\n",
2875 pring->ringno,
2876 irsp->unsli3.sli3Words[7]);
2877 }
2878 }
James Smart92d7f7b2007-06-17 19:56:38 -05002879 }
James Smart9c2face2008-01-11 01:53:18 -05002880 if (irsp->ulpBdeCount != 0 &&
2881 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2882 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2883 int found = 0;
2884
2885 /* search continue save q for same XRI */
2886 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002887 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2888 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002889 list_add_tail(&saveq->list, &iocbq->list);
2890 found = 1;
2891 break;
2892 }
2893 }
2894 if (!found)
2895 list_add_tail(&saveq->clist,
2896 &pring->iocb_continue_saveq);
2897 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2898 list_del_init(&iocbq->clist);
2899 saveq = iocbq;
2900 irsp = &(saveq->iocb);
2901 } else
2902 return 0;
2903 }
2904 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2905 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2906 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002907 Rctl = FC_RCTL_ELS_REQ;
2908 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002909 } else {
2910 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2911 Rctl = w5p->hcsw.Rctl;
2912 Type = w5p->hcsw.Type;
2913
2914 /* Firmware Workaround */
2915 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2916 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2917 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002918 Rctl = FC_RCTL_ELS_REQ;
2919 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002920 w5p->hcsw.Rctl = Rctl;
2921 w5p->hcsw.Type = Type;
2922 }
2923 }
James Smart92d7f7b2007-06-17 19:56:38 -05002924
James Smart3772a992009-05-22 14:50:54 -04002925 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002926 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002927 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002928 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002929 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002930
James Smart92d7f7b2007-06-17 19:56:38 -05002931 return 1;
dea31012005-04-17 16:05:31 -05002932}
2933
James Smarte59058c2008-08-24 21:49:00 -04002934/**
James Smart3621a712009-04-06 18:47:14 -04002935 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002936 * @phba: Pointer to HBA context object.
2937 * @pring: Pointer to driver SLI ring object.
2938 * @prspiocb: Pointer to response iocb object.
2939 *
2940 * This function looks up the iocb_lookup table to get the command iocb
2941 * corresponding to the given response iocb using the iotag of the
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002942 * response iocb. This function is called with the hbalock held
2943 * for sli3 devices or the ring_lock for sli4 devices.
James Smarte59058c2008-08-24 21:49:00 -04002944 * This function returns the command iocb object if it finds the command
2945 * iocb else returns NULL.
2946 **/
dea31012005-04-17 16:05:31 -05002947static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002948lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2949 struct lpfc_sli_ring *pring,
2950 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002951{
dea31012005-04-17 16:05:31 -05002952 struct lpfc_iocbq *cmd_iocb = NULL;
2953 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002954 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002955
James Bottomley604a3e32005-10-29 10:28:33 -05002956 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002957
James Bottomley604a3e32005-10-29 10:28:33 -05002958 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2959 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002960 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002961 /* remove from txcmpl queue list */
2962 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002963 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002964 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002965 }
dea31012005-04-17 16:05:31 -05002966 }
2967
dea31012005-04-17 16:05:31 -05002968 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002969 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002970 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002971 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002972 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002973 return NULL;
2974}
2975
James Smarte59058c2008-08-24 21:49:00 -04002976/**
James Smart3772a992009-05-22 14:50:54 -04002977 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2978 * @phba: Pointer to HBA context object.
2979 * @pring: Pointer to driver SLI ring object.
2980 * @iotag: IOCB tag.
2981 *
2982 * This function looks up the iocb_lookup table to get the command iocb
2983 * corresponding to the given iotag. This function is called with the
2984 * hbalock held.
2985 * This function returns the command iocb object if it finds the command
2986 * iocb else returns NULL.
2987 **/
2988static struct lpfc_iocbq *
2989lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2990 struct lpfc_sli_ring *pring, uint16_t iotag)
2991{
James Smart895427b2017-02-12 13:52:30 -08002992 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002993
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002994 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002995 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2996 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002997 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2998 /* remove from txcmpl queue list */
2999 list_del_init(&cmd_iocb->list);
3000 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04003001 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003002 }
James Smart3772a992009-05-22 14:50:54 -04003003 }
James Smart89533e92016-10-13 15:06:15 -07003004
James Smart3772a992009-05-22 14:50:54 -04003005 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08003006 "0372 iotag x%x lookup error: max iotag (x%x) "
3007 "iocb_flag x%x\n",
3008 iotag, phba->sli.last_iotag,
3009 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003010 return NULL;
3011}
3012
3013/**
James Smart3621a712009-04-06 18:47:14 -04003014 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003015 * @phba: Pointer to HBA context object.
3016 * @pring: Pointer to driver SLI ring object.
3017 * @saveq: Pointer to the response iocb to be processed.
3018 *
3019 * This function is called by the ring event handler for non-fcp
3020 * rings when there is a new response iocb in the response ring.
3021 * The caller is not required to hold any locks. This function
3022 * gets the command iocb associated with the response iocb and
3023 * calls the completion handler for the command iocb. If there
3024 * is no completion handler, the function will free the resources
3025 * associated with command iocb. If the response iocb is for
3026 * an already aborted command iocb, the status of the completion
3027 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3028 * This function always returns 1.
3029 **/
dea31012005-04-17 16:05:31 -05003030static int
James Smart2e0fef82007-06-17 19:56:36 -05003031lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003032 struct lpfc_iocbq *saveq)
3033{
James Smart2e0fef82007-06-17 19:56:36 -05003034 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003035 int rc = 1;
3036 unsigned long iflag;
3037
3038 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003039 if (phba->sli_rev == LPFC_SLI_REV4)
3040 spin_lock_irqsave(&pring->ring_lock, iflag);
3041 else
3042 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003043 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
Dick Kennedy341b2aa2017-11-07 12:59:02 -08003044 if (phba->sli_rev == LPFC_SLI_REV4)
3045 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3046 else
3047 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart2e0fef82007-06-17 19:56:36 -05003048
dea31012005-04-17 16:05:31 -05003049 if (cmdiocbp) {
3050 if (cmdiocbp->iocb_cmpl) {
3051 /*
James Smartea2151b2008-09-07 11:52:10 -04003052 * If an ELS command failed send an event to mgmt
3053 * application.
3054 */
3055 if (saveq->iocb.ulpStatus &&
3056 (pring->ringno == LPFC_ELS_RING) &&
3057 (cmdiocbp->iocb.ulpCommand ==
3058 CMD_ELS_REQUEST64_CR))
3059 lpfc_send_els_failure_event(phba,
3060 cmdiocbp, saveq);
3061
3062 /*
dea31012005-04-17 16:05:31 -05003063 * Post all ELS completions to the worker thread.
3064 * All other are passed to the completion callback.
3065 */
3066 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003067 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3068 (cmdiocbp->iocb_flag &
3069 LPFC_DRIVER_ABORTED)) {
3070 spin_lock_irqsave(&phba->hbalock,
3071 iflag);
James Smart07951072007-04-25 09:51:38 -04003072 cmdiocbp->iocb_flag &=
3073 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003074 spin_unlock_irqrestore(&phba->hbalock,
3075 iflag);
James Smart07951072007-04-25 09:51:38 -04003076 saveq->iocb.ulpStatus =
3077 IOSTAT_LOCAL_REJECT;
3078 saveq->iocb.un.ulpWord[4] =
3079 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003080
3081 /* Firmware could still be in progress
3082 * of DMAing payload, so don't free data
3083 * buffer till after a hbeat.
3084 */
James Smart341af102010-01-26 23:07:37 -05003085 spin_lock_irqsave(&phba->hbalock,
3086 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003087 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003088 spin_unlock_irqrestore(&phba->hbalock,
3089 iflag);
3090 }
James Smart0f65ff62010-02-26 14:14:23 -05003091 if (phba->sli_rev == LPFC_SLI_REV4) {
3092 if (saveq->iocb_flag &
3093 LPFC_EXCHANGE_BUSY) {
3094 /* Set cmdiocb flag for the
3095 * exchange busy so sgl (xri)
3096 * will not be released until
3097 * the abort xri is received
3098 * from hba.
3099 */
3100 spin_lock_irqsave(
3101 &phba->hbalock, iflag);
3102 cmdiocbp->iocb_flag |=
3103 LPFC_EXCHANGE_BUSY;
3104 spin_unlock_irqrestore(
3105 &phba->hbalock, iflag);
3106 }
3107 if (cmdiocbp->iocb_flag &
3108 LPFC_DRIVER_ABORTED) {
3109 /*
3110 * Clear LPFC_DRIVER_ABORTED
3111 * bit in case it was driver
3112 * initiated abort.
3113 */
3114 spin_lock_irqsave(
3115 &phba->hbalock, iflag);
3116 cmdiocbp->iocb_flag &=
3117 ~LPFC_DRIVER_ABORTED;
3118 spin_unlock_irqrestore(
3119 &phba->hbalock, iflag);
3120 cmdiocbp->iocb.ulpStatus =
3121 IOSTAT_LOCAL_REJECT;
3122 cmdiocbp->iocb.un.ulpWord[4] =
3123 IOERR_ABORT_REQUESTED;
3124 /*
3125 * For SLI4, irsiocb contains
3126 * NO_XRI in sli_xritag, it
3127 * shall not affect releasing
3128 * sgl (xri) process.
3129 */
3130 saveq->iocb.ulpStatus =
3131 IOSTAT_LOCAL_REJECT;
3132 saveq->iocb.un.ulpWord[4] =
3133 IOERR_SLI_ABORTED;
3134 spin_lock_irqsave(
3135 &phba->hbalock, iflag);
3136 saveq->iocb_flag |=
3137 LPFC_DELAY_MEM_FREE;
3138 spin_unlock_irqrestore(
3139 &phba->hbalock, iflag);
3140 }
James Smart07951072007-04-25 09:51:38 -04003141 }
dea31012005-04-17 16:05:31 -05003142 }
James Smart2e0fef82007-06-17 19:56:36 -05003143 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003144 } else
3145 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003146 } else {
3147 /*
3148 * Unknown initiating command based on the response iotag.
3149 * This could be the case on the ELS ring because of
3150 * lpfc_els_abort().
3151 */
3152 if (pring->ringno != LPFC_ELS_RING) {
3153 /*
3154 * Ring <ringno> handler: unexpected completion IoTag
3155 * <IoTag>
3156 */
James Smarta257bf92009-04-06 18:48:10 -04003157 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003158 "0322 Ring %d handler: "
3159 "unexpected completion IoTag x%x "
3160 "Data: x%x x%x x%x x%x\n",
3161 pring->ringno,
3162 saveq->iocb.ulpIoTag,
3163 saveq->iocb.ulpStatus,
3164 saveq->iocb.un.ulpWord[4],
3165 saveq->iocb.ulpCommand,
3166 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003167 }
3168 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003169
dea31012005-04-17 16:05:31 -05003170 return rc;
3171}
3172
James Smarte59058c2008-08-24 21:49:00 -04003173/**
James Smart3621a712009-04-06 18:47:14 -04003174 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003175 * @phba: Pointer to HBA context object.
3176 * @pring: Pointer to driver SLI ring object.
3177 *
3178 * This function is called from the iocb ring event handlers when
3179 * put pointer is ahead of the get pointer for a ring. This function signal
3180 * an error attention condition to the worker thread and the worker
3181 * thread will transition the HBA to offline state.
3182 **/
James Smart2e0fef82007-06-17 19:56:36 -05003183static void
3184lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003185{
James Smart34b02dc2008-08-24 21:49:55 -04003186 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003187 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003188 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003189 * rsp ring <portRspMax>
3190 */
3191 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003192 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003193 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003194 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003195 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003196
James Smart2e0fef82007-06-17 19:56:36 -05003197 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003198
3199 /*
3200 * All error attention handlers are posted to
3201 * worker thread
3202 */
3203 phba->work_ha |= HA_ERATT;
3204 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003205
James Smart5e9d9b82008-06-14 22:52:53 -04003206 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003207
3208 return;
3209}
3210
James Smarte59058c2008-08-24 21:49:00 -04003211/**
James Smart3621a712009-04-06 18:47:14 -04003212 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003213 * @ptr: Pointer to address of HBA context object.
3214 *
3215 * This function is invoked by the Error Attention polling timer when the
3216 * timer times out. It will check the SLI Error Attention register for
3217 * possible attention events. If so, it will post an Error Attention event
3218 * and wake up worker thread to process it. Otherwise, it will set up the
3219 * Error Attention polling timer for the next poll.
3220 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003221void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003222{
3223 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003224 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003225 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003226
Kees Cookf22eb4d2017-09-06 20:24:26 -07003227 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003228
James Smartaa6fbb72012-08-03 12:36:03 -04003229 /* Here we will also keep track of interrupts per sec of the hba */
3230 sli_intr = phba->sli.slistat.sli_intr;
3231
3232 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3233 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3234 sli_intr);
3235 else
3236 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3237
James Smart65791f12016-07-06 12:35:56 -07003238 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3239 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003240 phba->sli.slistat.sli_ips = cnt;
3241
3242 phba->sli.slistat.sli_prev_intr = sli_intr;
3243
James Smart93996272008-08-24 21:50:30 -04003244 /* Check chip HA register for error event */
3245 eratt = lpfc_sli_check_eratt(phba);
3246
3247 if (eratt)
3248 /* Tell the worker thread there is work to do */
3249 lpfc_worker_wake_up(phba);
3250 else
3251 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003252 mod_timer(&phba->eratt_poll,
3253 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003254 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003255 return;
3256}
3257
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003258
James Smarte59058c2008-08-24 21:49:00 -04003259/**
James Smart3621a712009-04-06 18:47:14 -04003260 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003261 * @phba: Pointer to HBA context object.
3262 * @pring: Pointer to driver SLI ring object.
3263 * @mask: Host attention register mask for this ring.
3264 *
3265 * This function is called from the interrupt context when there is a ring
3266 * event for the fcp ring. The caller does not hold any lock.
3267 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003268 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003269 * LE bit set. The function will call the completion handler of the command iocb
3270 * if the response iocb indicates a completion for a command iocb or it is
3271 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3272 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003273 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003274 * to check it explicitly.
3275 */
3276int
James Smart2e0fef82007-06-17 19:56:36 -05003277lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3278 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003279{
James Smart34b02dc2008-08-24 21:49:55 -04003280 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003281 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003282 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003283 struct lpfc_iocbq *cmdiocbq = NULL;
3284 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003285 uint32_t status;
3286 uint32_t portRspPut, portRspMax;
3287 int rc = 1;
3288 lpfc_iocb_type type;
3289 unsigned long iflag;
3290 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003291
James Smart2e0fef82007-06-17 19:56:36 -05003292 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003293 pring->stats.iocb_event++;
3294
dea31012005-04-17 16:05:31 -05003295 /*
3296 * The next available response entry should never exceed the maximum
3297 * entries. If it does, treat it as an adapter hardware error.
3298 */
James Smart7e56aa22012-08-03 12:35:34 -04003299 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003300 portRspPut = le32_to_cpu(pgp->rspPutInx);
3301 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003302 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003303 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003304 return 1;
3305 }
James Smart45ed1192009-10-02 15:17:02 -04003306 if (phba->fcp_ring_in_use) {
3307 spin_unlock_irqrestore(&phba->hbalock, iflag);
3308 return 1;
3309 } else
3310 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003311
3312 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003313 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003314 /*
3315 * Fetch an entry off the ring and copy it into a local data
3316 * structure. The copy involves a byte-swap since the
3317 * network byte order and pci byte orders are different.
3318 */
James Smarted957682007-06-17 19:56:37 -05003319 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003320 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003321
James Smart7e56aa22012-08-03 12:35:34 -04003322 if (++pring->sli.sli3.rspidx >= portRspMax)
3323 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003324
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003325 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3326 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003327 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003328 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003329 irsp = &rspiocbq.iocb;
3330
dea31012005-04-17 16:05:31 -05003331 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3332 pring->stats.iocb_rsp++;
3333 rsp_cmpl++;
3334
3335 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003336 /*
3337 * If resource errors reported from HBA, reduce
3338 * queuedepths of the SCSI device.
3339 */
3340 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003341 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3342 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003343 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003344 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003345 spin_lock_irqsave(&phba->hbalock, iflag);
3346 }
3347
dea31012005-04-17 16:05:31 -05003348 /* Rsp ring <ringno> error: IOCB */
3349 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003350 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003351 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003352 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003353 irsp->un.ulpWord[0],
3354 irsp->un.ulpWord[1],
3355 irsp->un.ulpWord[2],
3356 irsp->un.ulpWord[3],
3357 irsp->un.ulpWord[4],
3358 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003359 *(uint32_t *)&irsp->un1,
3360 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003361 }
3362
3363 switch (type) {
3364 case LPFC_ABORT_IOCB:
3365 case LPFC_SOL_IOCB:
3366 /*
3367 * Idle exchange closed via ABTS from port. No iocb
3368 * resources need to be recovered.
3369 */
3370 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003371 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003372 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003373 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003374 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003375 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003376 break;
3377 }
3378
James Bottomley604a3e32005-10-29 10:28:33 -05003379 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3380 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003381 if (unlikely(!cmdiocbq))
3382 break;
3383 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3384 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3385 if (cmdiocbq->iocb_cmpl) {
3386 spin_unlock_irqrestore(&phba->hbalock, iflag);
3387 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3388 &rspiocbq);
3389 spin_lock_irqsave(&phba->hbalock, iflag);
3390 }
dea31012005-04-17 16:05:31 -05003391 break;
James Smarta4bc3372006-12-02 13:34:16 -05003392 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003393 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003394 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003395 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003396 break;
dea31012005-04-17 16:05:31 -05003397 default:
3398 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3399 char adaptermsg[LPFC_MAX_ADPTMSG];
3400 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3401 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3402 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003403 dev_warn(&((phba->pcidev)->dev),
3404 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003405 phba->brd_no, adaptermsg);
3406 } else {
3407 /* Unknown IOCB command */
3408 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003409 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003410 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003411 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003412 irsp->ulpStatus,
3413 irsp->ulpIoTag,
3414 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003415 }
3416 break;
3417 }
3418
3419 /*
3420 * The response IOCB has been processed. Update the ring
3421 * pointer in SLIM. If the port response put pointer has not
3422 * been updated, sync the pgp->rspPutInx and fetch the new port
3423 * response put pointer.
3424 */
James Smart7e56aa22012-08-03 12:35:34 -04003425 writel(pring->sli.sli3.rspidx,
3426 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003427
James Smart7e56aa22012-08-03 12:35:34 -04003428 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003429 portRspPut = le32_to_cpu(pgp->rspPutInx);
3430 }
3431
3432 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3433 pring->stats.iocb_rsp_full++;
3434 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3435 writel(status, phba->CAregaddr);
3436 readl(phba->CAregaddr);
3437 }
3438 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3439 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3440 pring->stats.iocb_cmd_empty++;
3441
3442 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003443 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003444 lpfc_sli_resume_iocb(phba, pring);
3445
3446 if ((pring->lpfc_sli_cmd_available))
3447 (pring->lpfc_sli_cmd_available) (phba, pring);
3448
3449 }
3450
James Smart45ed1192009-10-02 15:17:02 -04003451 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003452 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003453 return rc;
3454}
3455
James Smarte59058c2008-08-24 21:49:00 -04003456/**
James Smart3772a992009-05-22 14:50:54 -04003457 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3458 * @phba: Pointer to HBA context object.
3459 * @pring: Pointer to driver SLI ring object.
3460 * @rspiocbp: Pointer to driver response IOCB object.
3461 *
3462 * This function is called from the worker thread when there is a slow-path
3463 * response IOCB to process. This function chains all the response iocbs until
3464 * seeing the iocb with the LE bit set. The function will call
3465 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3466 * completion of a command iocb. The function will call the
3467 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3468 * The function frees the resources or calls the completion handler if this
3469 * iocb is an abort completion. The function returns NULL when the response
3470 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3471 * this function shall chain the iocb on to the iocb_continueq and return the
3472 * response iocb passed in.
3473 **/
3474static struct lpfc_iocbq *
3475lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3476 struct lpfc_iocbq *rspiocbp)
3477{
3478 struct lpfc_iocbq *saveq;
3479 struct lpfc_iocbq *cmdiocbp;
3480 struct lpfc_iocbq *next_iocb;
3481 IOCB_t *irsp = NULL;
3482 uint32_t free_saveq;
3483 uint8_t iocb_cmd_type;
3484 lpfc_iocb_type type;
3485 unsigned long iflag;
3486 int rc;
3487
3488 spin_lock_irqsave(&phba->hbalock, iflag);
3489 /* First add the response iocb to the countinueq list */
3490 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3491 pring->iocb_continueq_cnt++;
3492
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003493 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003494 irsp = &rspiocbp->iocb;
3495 if (irsp->ulpLe) {
3496 /*
3497 * By default, the driver expects to free all resources
3498 * associated with this iocb completion.
3499 */
3500 free_saveq = 1;
3501 saveq = list_get_first(&pring->iocb_continueq,
3502 struct lpfc_iocbq, list);
3503 irsp = &(saveq->iocb);
3504 list_del_init(&pring->iocb_continueq);
3505 pring->iocb_continueq_cnt = 0;
3506
3507 pring->stats.iocb_rsp++;
3508
3509 /*
3510 * If resource errors reported from HBA, reduce
3511 * queuedepths of the SCSI device.
3512 */
3513 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003514 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3515 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003516 spin_unlock_irqrestore(&phba->hbalock, iflag);
3517 phba->lpfc_rampdown_queue_depth(phba);
3518 spin_lock_irqsave(&phba->hbalock, iflag);
3519 }
3520
3521 if (irsp->ulpStatus) {
3522 /* Rsp ring <ringno> error: IOCB */
3523 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3524 "0328 Rsp Ring %d error: "
3525 "IOCB Data: "
3526 "x%x x%x x%x x%x "
3527 "x%x x%x x%x x%x "
3528 "x%x x%x x%x x%x "
3529 "x%x x%x x%x x%x\n",
3530 pring->ringno,
3531 irsp->un.ulpWord[0],
3532 irsp->un.ulpWord[1],
3533 irsp->un.ulpWord[2],
3534 irsp->un.ulpWord[3],
3535 irsp->un.ulpWord[4],
3536 irsp->un.ulpWord[5],
3537 *(((uint32_t *) irsp) + 6),
3538 *(((uint32_t *) irsp) + 7),
3539 *(((uint32_t *) irsp) + 8),
3540 *(((uint32_t *) irsp) + 9),
3541 *(((uint32_t *) irsp) + 10),
3542 *(((uint32_t *) irsp) + 11),
3543 *(((uint32_t *) irsp) + 12),
3544 *(((uint32_t *) irsp) + 13),
3545 *(((uint32_t *) irsp) + 14),
3546 *(((uint32_t *) irsp) + 15));
3547 }
3548
3549 /*
3550 * Fetch the IOCB command type and call the correct completion
3551 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3552 * get freed back to the lpfc_iocb_list by the discovery
3553 * kernel thread.
3554 */
3555 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3556 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3557 switch (type) {
3558 case LPFC_SOL_IOCB:
3559 spin_unlock_irqrestore(&phba->hbalock, iflag);
3560 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3561 spin_lock_irqsave(&phba->hbalock, iflag);
3562 break;
3563
3564 case LPFC_UNSOL_IOCB:
3565 spin_unlock_irqrestore(&phba->hbalock, iflag);
3566 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3567 spin_lock_irqsave(&phba->hbalock, iflag);
3568 if (!rc)
3569 free_saveq = 0;
3570 break;
3571
3572 case LPFC_ABORT_IOCB:
3573 cmdiocbp = NULL;
3574 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3575 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3576 saveq);
3577 if (cmdiocbp) {
3578 /* Call the specified completion routine */
3579 if (cmdiocbp->iocb_cmpl) {
3580 spin_unlock_irqrestore(&phba->hbalock,
3581 iflag);
3582 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3583 saveq);
3584 spin_lock_irqsave(&phba->hbalock,
3585 iflag);
3586 } else
3587 __lpfc_sli_release_iocbq(phba,
3588 cmdiocbp);
3589 }
3590 break;
3591
3592 case LPFC_UNKNOWN_IOCB:
3593 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3594 char adaptermsg[LPFC_MAX_ADPTMSG];
3595 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3596 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3597 MAX_MSG_DATA);
3598 dev_warn(&((phba->pcidev)->dev),
3599 "lpfc%d: %s\n",
3600 phba->brd_no, adaptermsg);
3601 } else {
3602 /* Unknown IOCB command */
3603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3604 "0335 Unknown IOCB "
3605 "command Data: x%x "
3606 "x%x x%x x%x\n",
3607 irsp->ulpCommand,
3608 irsp->ulpStatus,
3609 irsp->ulpIoTag,
3610 irsp->ulpContext);
3611 }
3612 break;
3613 }
3614
3615 if (free_saveq) {
3616 list_for_each_entry_safe(rspiocbp, next_iocb,
3617 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003618 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003619 __lpfc_sli_release_iocbq(phba, rspiocbp);
3620 }
3621 __lpfc_sli_release_iocbq(phba, saveq);
3622 }
3623 rspiocbp = NULL;
3624 }
3625 spin_unlock_irqrestore(&phba->hbalock, iflag);
3626 return rspiocbp;
3627}
3628
3629/**
3630 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003631 * @phba: Pointer to HBA context object.
3632 * @pring: Pointer to driver SLI ring object.
3633 * @mask: Host attention register mask for this ring.
3634 *
James Smart3772a992009-05-22 14:50:54 -04003635 * This routine wraps the actual slow_ring event process routine from the
3636 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003637 **/
James Smart3772a992009-05-22 14:50:54 -04003638void
James Smart2e0fef82007-06-17 19:56:36 -05003639lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3640 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003641{
James Smart3772a992009-05-22 14:50:54 -04003642 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3643}
3644
3645/**
3646 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3647 * @phba: Pointer to HBA context object.
3648 * @pring: Pointer to driver SLI ring object.
3649 * @mask: Host attention register mask for this ring.
3650 *
3651 * This function is called from the worker thread when there is a ring event
3652 * for non-fcp rings. The caller does not hold any lock. The function will
3653 * remove each response iocb in the response ring and calls the handle
3654 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3655 **/
3656static void
3657lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3658 struct lpfc_sli_ring *pring, uint32_t mask)
3659{
James Smart34b02dc2008-08-24 21:49:55 -04003660 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003661 IOCB_t *entry;
3662 IOCB_t *irsp = NULL;
3663 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003664 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003665 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003666 uint32_t status;
dea31012005-04-17 16:05:31 -05003667
James Smart34b02dc2008-08-24 21:49:55 -04003668 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003669 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003670 pring->stats.iocb_event++;
3671
dea31012005-04-17 16:05:31 -05003672 /*
3673 * The next available response entry should never exceed the maximum
3674 * entries. If it does, treat it as an adapter hardware error.
3675 */
James Smart7e56aa22012-08-03 12:35:34 -04003676 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003677 portRspPut = le32_to_cpu(pgp->rspPutInx);
3678 if (portRspPut >= portRspMax) {
3679 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003680 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003681 * rsp ring <portRspMax>
3682 */
James Smarted957682007-06-17 19:56:37 -05003683 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003684 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003685 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003686 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003687
James Smart2e0fef82007-06-17 19:56:36 -05003688 phba->link_state = LPFC_HBA_ERROR;
3689 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003690
3691 phba->work_hs = HS_FFER3;
3692 lpfc_handle_eratt(phba);
3693
James Smart3772a992009-05-22 14:50:54 -04003694 return;
dea31012005-04-17 16:05:31 -05003695 }
3696
3697 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003698 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003699 /*
3700 * Build a completion list and call the appropriate handler.
3701 * The process is to get the next available response iocb, get
3702 * a free iocb from the list, copy the response data into the
3703 * free iocb, insert to the continuation list, and update the
3704 * next response index to slim. This process makes response
3705 * iocb's in the ring available to DMA as fast as possible but
3706 * pays a penalty for a copy operation. Since the iocb is
3707 * only 32 bytes, this penalty is considered small relative to
3708 * the PCI reads for register values and a slim write. When
3709 * the ulpLe field is set, the entire Command has been
3710 * received.
3711 */
James Smarted957682007-06-17 19:56:37 -05003712 entry = lpfc_resp_iocb(phba, pring);
3713
James Smart858c9f62007-06-17 19:56:39 -05003714 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003715 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003716 if (rspiocbp == NULL) {
3717 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003718 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003719 break;
3720 }
3721
James Smarted957682007-06-17 19:56:37 -05003722 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3723 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003724 irsp = &rspiocbp->iocb;
3725
James Smart7e56aa22012-08-03 12:35:34 -04003726 if (++pring->sli.sli3.rspidx >= portRspMax)
3727 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003728
James Smarta58cbd52007-08-02 11:09:43 -04003729 if (pring->ringno == LPFC_ELS_RING) {
3730 lpfc_debugfs_slow_ring_trc(phba,
3731 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3732 *(((uint32_t *) irsp) + 4),
3733 *(((uint32_t *) irsp) + 6),
3734 *(((uint32_t *) irsp) + 7));
3735 }
3736
James Smart7e56aa22012-08-03 12:35:34 -04003737 writel(pring->sli.sli3.rspidx,
3738 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003739
James Smart3772a992009-05-22 14:50:54 -04003740 spin_unlock_irqrestore(&phba->hbalock, iflag);
3741 /* Handle the response IOCB */
3742 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3743 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003744
3745 /*
3746 * If the port response put pointer has not been updated, sync
3747 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3748 * response put pointer.
3749 */
James Smart7e56aa22012-08-03 12:35:34 -04003750 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003751 portRspPut = le32_to_cpu(pgp->rspPutInx);
3752 }
James Smart7e56aa22012-08-03 12:35:34 -04003753 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003754
James Smart92d7f7b2007-06-17 19:56:38 -05003755 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003756 /* At least one response entry has been freed */
3757 pring->stats.iocb_rsp_full++;
3758 /* SET RxRE_RSP in Chip Att register */
3759 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3760 writel(status, phba->CAregaddr);
3761 readl(phba->CAregaddr); /* flush */
3762 }
3763 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3764 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3765 pring->stats.iocb_cmd_empty++;
3766
3767 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003768 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003769 lpfc_sli_resume_iocb(phba, pring);
3770
3771 if ((pring->lpfc_sli_cmd_available))
3772 (pring->lpfc_sli_cmd_available) (phba, pring);
3773
3774 }
3775
James Smart2e0fef82007-06-17 19:56:36 -05003776 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003777 return;
dea31012005-04-17 16:05:31 -05003778}
3779
James Smarte59058c2008-08-24 21:49:00 -04003780/**
James Smart4f774512009-05-22 14:52:35 -04003781 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3782 * @phba: Pointer to HBA context object.
3783 * @pring: Pointer to driver SLI ring object.
3784 * @mask: Host attention register mask for this ring.
3785 *
3786 * This function is called from the worker thread when there is a pending
3787 * ELS response iocb on the driver internal slow-path response iocb worker
3788 * queue. The caller does not hold any lock. The function will remove each
3789 * response iocb from the response worker queue and calls the handle
3790 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3791 **/
3792static void
3793lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3794 struct lpfc_sli_ring *pring, uint32_t mask)
3795{
3796 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003797 struct hbq_dmabuf *dmabuf;
3798 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003799 unsigned long iflag;
3800
James Smart45ed1192009-10-02 15:17:02 -04003801 spin_lock_irqsave(&phba->hbalock, iflag);
3802 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3803 spin_unlock_irqrestore(&phba->hbalock, iflag);
3804 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003805 /* Get the response iocb from the head of work queue */
3806 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003807 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003808 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003809 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003810
3811 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3812 case CQE_CODE_COMPL_WQE:
3813 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3814 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003815 /* Translate ELS WCQE to response IOCBQ */
3816 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3817 irspiocbq);
3818 if (irspiocbq)
3819 lpfc_sli_sp_handle_rspiocb(phba, pring,
3820 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003821 break;
3822 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003823 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003824 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3825 cq_event);
3826 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3827 break;
3828 default:
3829 break;
3830 }
James Smart4f774512009-05-22 14:52:35 -04003831 }
3832}
3833
3834/**
James Smart3621a712009-04-06 18:47:14 -04003835 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003836 * @phba: Pointer to HBA context object.
3837 * @pring: Pointer to driver SLI ring object.
3838 *
3839 * This function aborts all iocbs in the given ring and frees all the iocb
3840 * objects in txq. This function issues an abort iocb for all the iocb commands
3841 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3842 * the return of this function. The caller is not required to hold any locks.
3843 **/
James Smart2e0fef82007-06-17 19:56:36 -05003844void
dea31012005-04-17 16:05:31 -05003845lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3846{
James Smart2534ba72007-04-25 09:52:20 -04003847 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003848 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003849
James Smart92d7f7b2007-06-17 19:56:38 -05003850 if (pring->ringno == LPFC_ELS_RING) {
3851 lpfc_fabric_abort_hba(phba);
3852 }
3853
dea31012005-04-17 16:05:31 -05003854 /* Error everything on txq and txcmplq
3855 * First do the txq.
3856 */
James Smartdb55fba2014-04-04 13:52:02 -04003857 if (phba->sli_rev >= LPFC_SLI_REV4) {
3858 spin_lock_irq(&pring->ring_lock);
3859 list_splice_init(&pring->txq, &completions);
3860 pring->txq_cnt = 0;
3861 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003862
James Smartdb55fba2014-04-04 13:52:02 -04003863 spin_lock_irq(&phba->hbalock);
3864 /* Next issue ABTS for everything on the txcmplq */
3865 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3866 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3867 spin_unlock_irq(&phba->hbalock);
3868 } else {
3869 spin_lock_irq(&phba->hbalock);
3870 list_splice_init(&pring->txq, &completions);
3871 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003872
James Smartdb55fba2014-04-04 13:52:02 -04003873 /* Next issue ABTS for everything on the txcmplq */
3874 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3875 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3876 spin_unlock_irq(&phba->hbalock);
3877 }
James Smart2534ba72007-04-25 09:52:20 -04003878
James Smarta257bf92009-04-06 18:48:10 -04003879 /* Cancel all the IOCBs from the completions list */
3880 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3881 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003882}
3883
James Smarte59058c2008-08-24 21:49:00 -04003884/**
James Smart895427b2017-02-12 13:52:30 -08003885 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3886 * @phba: Pointer to HBA context object.
3887 * @pring: Pointer to driver SLI ring object.
3888 *
3889 * This function aborts all iocbs in the given ring and frees all the iocb
3890 * objects in txq. This function issues an abort iocb for all the iocb commands
3891 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3892 * the return of this function. The caller is not required to hold any locks.
3893 **/
3894void
3895lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3896{
3897 LIST_HEAD(completions);
3898 struct lpfc_iocbq *iocb, *next_iocb;
3899
3900 if (pring->ringno == LPFC_ELS_RING)
3901 lpfc_fabric_abort_hba(phba);
3902
3903 spin_lock_irq(&phba->hbalock);
3904 /* Next issue ABTS for everything on the txcmplq */
3905 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3906 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3907 spin_unlock_irq(&phba->hbalock);
3908}
3909
3910
3911/**
James Smartdb55fba2014-04-04 13:52:02 -04003912 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3913 * @phba: Pointer to HBA context object.
3914 * @pring: Pointer to driver SLI ring object.
3915 *
3916 * This function aborts all iocbs in FCP rings and frees all the iocb
3917 * objects in txq. This function issues an abort iocb for all the iocb commands
3918 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3919 * the return of this function. The caller is not required to hold any locks.
3920 **/
3921void
3922lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3923{
3924 struct lpfc_sli *psli = &phba->sli;
3925 struct lpfc_sli_ring *pring;
3926 uint32_t i;
3927
3928 /* Look on all the FCP Rings for the iotag */
3929 if (phba->sli_rev >= LPFC_SLI_REV4) {
3930 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003931 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003932 lpfc_sli_abort_iocb_ring(phba, pring);
3933 }
3934 } else {
James Smart895427b2017-02-12 13:52:30 -08003935 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003936 lpfc_sli_abort_iocb_ring(phba, pring);
3937 }
3938}
3939
James Smart895427b2017-02-12 13:52:30 -08003940/**
3941 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3942 * @phba: Pointer to HBA context object.
3943 *
3944 * This function aborts all wqes in NVME rings. This function issues an
3945 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3946 * the txcmplq is not guaranteed to complete before the return of this
3947 * function. The caller is not required to hold any locks.
3948 **/
3949void
3950lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3951{
3952 struct lpfc_sli_ring *pring;
3953 uint32_t i;
3954
3955 if (phba->sli_rev < LPFC_SLI_REV4)
3956 return;
3957
3958 /* Abort all IO on each NVME ring. */
3959 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3960 pring = phba->sli4_hba.nvme_wq[i]->pring;
3961 lpfc_sli_abort_wqe_ring(phba, pring);
3962 }
3963}
3964
James Smartdb55fba2014-04-04 13:52:02 -04003965
3966/**
James Smart3621a712009-04-06 18:47:14 -04003967 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003968 * @phba: Pointer to HBA context object.
3969 *
3970 * This function flushes all iocbs in the fcp ring and frees all the iocb
3971 * objects in txq and txcmplq. This function will not issue abort iocbs
3972 * for all the iocb commands in txcmplq, they will just be returned with
3973 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3974 * slot has been permanently disabled.
3975 **/
3976void
3977lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3978{
3979 LIST_HEAD(txq);
3980 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003981 struct lpfc_sli *psli = &phba->sli;
3982 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003983 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08003984 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04003985
3986 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003987 /* Indicate the I/O queues are flushed */
3988 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003989 spin_unlock_irq(&phba->hbalock);
3990
James Smartdb55fba2014-04-04 13:52:02 -04003991 /* Look on all the FCP Rings for the iotag */
3992 if (phba->sli_rev >= LPFC_SLI_REV4) {
3993 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003994 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003995
James Smartdb55fba2014-04-04 13:52:02 -04003996 spin_lock_irq(&pring->ring_lock);
3997 /* Retrieve everything on txq */
3998 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003999 list_for_each_entry_safe(piocb, next_iocb,
4000 &pring->txcmplq, list)
4001 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004002 /* Retrieve everything on the txcmplq */
4003 list_splice_init(&pring->txcmplq, &txcmplq);
4004 pring->txq_cnt = 0;
4005 pring->txcmplq_cnt = 0;
4006 spin_unlock_irq(&pring->ring_lock);
4007
4008 /* Flush the txq */
4009 lpfc_sli_cancel_iocbs(phba, &txq,
4010 IOSTAT_LOCAL_REJECT,
4011 IOERR_SLI_DOWN);
4012 /* Flush the txcmpq */
4013 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4014 IOSTAT_LOCAL_REJECT,
4015 IOERR_SLI_DOWN);
4016 }
4017 } else {
James Smart895427b2017-02-12 13:52:30 -08004018 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004019
4020 spin_lock_irq(&phba->hbalock);
4021 /* Retrieve everything on txq */
4022 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004023 list_for_each_entry_safe(piocb, next_iocb,
4024 &pring->txcmplq, list)
4025 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004026 /* Retrieve everything on the txcmplq */
4027 list_splice_init(&pring->txcmplq, &txcmplq);
4028 pring->txq_cnt = 0;
4029 pring->txcmplq_cnt = 0;
4030 spin_unlock_irq(&phba->hbalock);
4031
4032 /* Flush the txq */
4033 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4034 IOERR_SLI_DOWN);
4035 /* Flush the txcmpq */
4036 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4037 IOERR_SLI_DOWN);
4038 }
James Smarta8e497d2008-08-24 21:50:11 -04004039}
4040
4041/**
James Smart895427b2017-02-12 13:52:30 -08004042 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4043 * @phba: Pointer to HBA context object.
4044 *
4045 * This function flushes all wqes in the nvme rings and frees all resources
4046 * in the txcmplq. This function does not issue abort wqes for the IO
4047 * commands in txcmplq, they will just be returned with
4048 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4049 * slot has been permanently disabled.
4050 **/
4051void
4052lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4053{
4054 LIST_HEAD(txcmplq);
4055 struct lpfc_sli_ring *pring;
4056 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004057 struct lpfc_iocbq *piocb, *next_iocb;
James Smart895427b2017-02-12 13:52:30 -08004058
4059 if (phba->sli_rev < LPFC_SLI_REV4)
4060 return;
4061
4062 /* Hint to other driver operations that a flush is in progress. */
4063 spin_lock_irq(&phba->hbalock);
4064 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4065 spin_unlock_irq(&phba->hbalock);
4066
4067 /* Cycle through all NVME rings and complete each IO with
4068 * a local driver reason code. This is a flush so no
4069 * abort exchange to FW.
4070 */
4071 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
4072 pring = phba->sli4_hba.nvme_wq[i]->pring;
4073
James Smart895427b2017-02-12 13:52:30 -08004074 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -08004075 list_for_each_entry_safe(piocb, next_iocb,
4076 &pring->txcmplq, list)
4077 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4078 /* Retrieve everything on the txcmplq */
James Smart895427b2017-02-12 13:52:30 -08004079 list_splice_init(&pring->txcmplq, &txcmplq);
4080 pring->txcmplq_cnt = 0;
4081 spin_unlock_irq(&pring->ring_lock);
4082
4083 /* Flush the txcmpq &&&PAE */
4084 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4085 IOSTAT_LOCAL_REJECT,
4086 IOERR_SLI_DOWN);
4087 }
4088}
4089
4090/**
James Smart3772a992009-05-22 14:50:54 -04004091 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004092 * @phba: Pointer to HBA context object.
4093 * @mask: Bit mask to be checked.
4094 *
4095 * This function reads the host status register and compares
4096 * with the provided bit mask to check if HBA completed
4097 * the restart. This function will wait in a loop for the
4098 * HBA to complete restart. If the HBA does not restart within
4099 * 15 iterations, the function will reset the HBA again. The
4100 * function returns 1 when HBA fail to restart otherwise returns
4101 * zero.
4102 **/
James Smart3772a992009-05-22 14:50:54 -04004103static int
4104lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004105{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004106 uint32_t status;
4107 int i = 0;
4108 int retval = 0;
dea31012005-04-17 16:05:31 -05004109
Jamie Wellnitz41415862006-02-28 19:25:27 -05004110 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004111 if (lpfc_readl(phba->HSregaddr, &status))
4112 return 1;
dea31012005-04-17 16:05:31 -05004113
Jamie Wellnitz41415862006-02-28 19:25:27 -05004114 /*
4115 * Check status register every 100ms for 5 retries, then every
4116 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4117 * every 2.5 sec for 4.
4118 * Break our of the loop if errors occurred during init.
4119 */
4120 while (((status & mask) != mask) &&
4121 !(status & HS_FFERM) &&
4122 i++ < 20) {
dea31012005-04-17 16:05:31 -05004123
Jamie Wellnitz41415862006-02-28 19:25:27 -05004124 if (i <= 5)
4125 msleep(10);
4126 else if (i <= 10)
4127 msleep(500);
4128 else
4129 msleep(2500);
dea31012005-04-17 16:05:31 -05004130
Jamie Wellnitz41415862006-02-28 19:25:27 -05004131 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004132 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004133 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004134 lpfc_sli_brdrestart(phba);
4135 }
4136 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004137 if (lpfc_readl(phba->HSregaddr, &status)) {
4138 retval = 1;
4139 break;
4140 }
dea31012005-04-17 16:05:31 -05004141 }
dea31012005-04-17 16:05:31 -05004142
Jamie Wellnitz41415862006-02-28 19:25:27 -05004143 /* Check to see if any errors occurred during init */
4144 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004145 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4146 "2751 Adapter failed to restart, "
4147 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4148 status,
4149 readl(phba->MBslimaddr + 0xa8),
4150 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004151 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004152 retval = 1;
4153 }
dea31012005-04-17 16:05:31 -05004154
Jamie Wellnitz41415862006-02-28 19:25:27 -05004155 return retval;
dea31012005-04-17 16:05:31 -05004156}
4157
James Smartda0436e2009-05-22 14:51:39 -04004158/**
4159 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4160 * @phba: Pointer to HBA context object.
4161 * @mask: Bit mask to be checked.
4162 *
4163 * This function checks the host status register to check if HBA is
4164 * ready. This function will wait in a loop for the HBA to be ready
4165 * If the HBA is not ready , the function will will reset the HBA PCI
4166 * function again. The function returns 1 when HBA fail to be ready
4167 * otherwise returns zero.
4168 **/
4169static int
4170lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4171{
4172 uint32_t status;
4173 int retval = 0;
4174
4175 /* Read the HBA Host Status Register */
4176 status = lpfc_sli4_post_status_check(phba);
4177
4178 if (status) {
4179 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4180 lpfc_sli_brdrestart(phba);
4181 status = lpfc_sli4_post_status_check(phba);
4182 }
4183
4184 /* Check to see if any errors occurred during init */
4185 if (status) {
4186 phba->link_state = LPFC_HBA_ERROR;
4187 retval = 1;
4188 } else
4189 phba->sli4_hba.intr_enable = 0;
4190
4191 return retval;
4192}
4193
4194/**
4195 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4196 * @phba: Pointer to HBA context object.
4197 * @mask: Bit mask to be checked.
4198 *
4199 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4200 * from the API jump table function pointer from the lpfc_hba struct.
4201 **/
4202int
4203lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4204{
4205 return phba->lpfc_sli_brdready(phba, mask);
4206}
4207
James Smart92908312006-03-07 15:04:13 -05004208#define BARRIER_TEST_PATTERN (0xdeadbeef)
4209
James Smarte59058c2008-08-24 21:49:00 -04004210/**
James Smart3621a712009-04-06 18:47:14 -04004211 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004212 * @phba: Pointer to HBA context object.
4213 *
James Smart1b511972011-12-13 13:23:09 -05004214 * This function is called before resetting an HBA. This function is called
4215 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004216 **/
James Smart2e0fef82007-06-17 19:56:36 -05004217void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004218{
James Smart65a29c12006-07-06 15:50:50 -04004219 uint32_t __iomem *resp_buf;
4220 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004221 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004222 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004223 int i;
4224 uint8_t hdrtype;
4225
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004226 lockdep_assert_held(&phba->hbalock);
4227
James Smart92908312006-03-07 15:04:13 -05004228 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4229 if (hdrtype != 0x80 ||
4230 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4231 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4232 return;
4233
4234 /*
4235 * Tell the other part of the chip to suspend temporarily all
4236 * its DMA activity.
4237 */
James Smart65a29c12006-07-06 15:50:50 -04004238 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004239
4240 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004241 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4242 return;
James Smart92908312006-03-07 15:04:13 -05004243 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4244 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004245 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004246
James Smart9940b972011-03-11 16:06:12 -05004247 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4248 return;
4249 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004250 /* Clear Chip error bit */
4251 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004252 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004253 }
4254
4255 mbox = 0;
4256 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4257 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4258
4259 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004260 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004261 writel(mbox, mbox_buf);
4262
James Smart9940b972011-03-11 16:06:12 -05004263 for (i = 0; i < 50; i++) {
4264 if (lpfc_readl((resp_buf + 1), &resp_data))
4265 return;
4266 if (resp_data != ~(BARRIER_TEST_PATTERN))
4267 mdelay(1);
4268 else
4269 break;
4270 }
4271 resp_data = 0;
4272 if (lpfc_readl((resp_buf + 1), &resp_data))
4273 return;
4274 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004275 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004276 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004277 goto restore_hc;
4278 else
4279 goto clear_errat;
4280 }
4281
4282 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004283 resp_data = 0;
4284 for (i = 0; i < 500; i++) {
4285 if (lpfc_readl(resp_buf, &resp_data))
4286 return;
4287 if (resp_data != mbox)
4288 mdelay(1);
4289 else
4290 break;
4291 }
James Smart92908312006-03-07 15:04:13 -05004292
4293clear_errat:
4294
James Smart9940b972011-03-11 16:06:12 -05004295 while (++i < 500) {
4296 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4297 return;
4298 if (!(ha_copy & HA_ERATT))
4299 mdelay(1);
4300 else
4301 break;
4302 }
James Smart92908312006-03-07 15:04:13 -05004303
4304 if (readl(phba->HAregaddr) & HA_ERATT) {
4305 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004306 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004307 }
4308
4309restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004310 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004311 writel(hc_copy, phba->HCregaddr);
4312 readl(phba->HCregaddr); /* flush */
4313}
4314
James Smarte59058c2008-08-24 21:49:00 -04004315/**
James Smart3621a712009-04-06 18:47:14 -04004316 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004317 * @phba: Pointer to HBA context object.
4318 *
4319 * This function issues a kill_board mailbox command and waits for
4320 * the error attention interrupt. This function is called for stopping
4321 * the firmware processing. The caller is not required to hold any
4322 * locks. This function calls lpfc_hba_down_post function to free
4323 * any pending commands after the kill. The function will return 1 when it
4324 * fails to kill the board else will return 0.
4325 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004326int
James Smart2e0fef82007-06-17 19:56:36 -05004327lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004328{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004329 struct lpfc_sli *psli;
4330 LPFC_MBOXQ_t *pmb;
4331 uint32_t status;
4332 uint32_t ha_copy;
4333 int retval;
4334 int i = 0;
4335
4336 psli = &phba->sli;
4337
4338 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004339 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004340 "0329 Kill HBA Data: x%x x%x\n",
4341 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004342
James Smart98c9ea52007-10-27 13:37:33 -04004343 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4344 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004345 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004346
4347 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004348 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004349 if (lpfc_readl(phba->HCregaddr, &status)) {
4350 spin_unlock_irq(&phba->hbalock);
4351 mempool_free(pmb, phba->mbox_mem_pool);
4352 return 1;
4353 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004354 status &= ~HC_ERINT_ENA;
4355 writel(status, phba->HCregaddr);
4356 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004357 phba->link_flag |= LS_IGNORE_ERATT;
4358 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004359
4360 lpfc_kill_board(phba, pmb);
4361 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4362 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4363
4364 if (retval != MBX_SUCCESS) {
4365 if (retval != MBX_BUSY)
4366 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004367 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4368 "2752 KILL_BOARD command failed retval %d\n",
4369 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004370 spin_lock_irq(&phba->hbalock);
4371 phba->link_flag &= ~LS_IGNORE_ERATT;
4372 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004373 return 1;
4374 }
4375
James Smartf4b4c682009-05-22 14:53:12 -04004376 spin_lock_irq(&phba->hbalock);
4377 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4378 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004379
Jamie Wellnitz41415862006-02-28 19:25:27 -05004380 mempool_free(pmb, phba->mbox_mem_pool);
4381
4382 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4383 * attention every 100ms for 3 seconds. If we don't get ERATT after
4384 * 3 seconds we still set HBA_ERROR state because the status of the
4385 * board is now undefined.
4386 */
James Smart9940b972011-03-11 16:06:12 -05004387 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4388 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004389 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4390 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004391 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4392 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004393 }
4394
4395 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004396 if (ha_copy & HA_ERATT) {
4397 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004398 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004399 }
James Smart2e0fef82007-06-17 19:56:36 -05004400 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004401 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004402 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004403 phba->link_flag &= ~LS_IGNORE_ERATT;
4404 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004405
Jamie Wellnitz41415862006-02-28 19:25:27 -05004406 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004407 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004408
James Smart2e0fef82007-06-17 19:56:36 -05004409 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004410}
4411
James Smarte59058c2008-08-24 21:49:00 -04004412/**
James Smart3772a992009-05-22 14:50:54 -04004413 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004414 * @phba: Pointer to HBA context object.
4415 *
4416 * This function resets the HBA by writing HC_INITFF to the control
4417 * register. After the HBA resets, this function resets all the iocb ring
4418 * indices. This function disables PCI layer parity checking during
4419 * the reset.
4420 * This function returns 0 always.
4421 * The caller is not required to hold any locks.
4422 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004423int
James Smart2e0fef82007-06-17 19:56:36 -05004424lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004425{
4426 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004427 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004428 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004429 int i;
dea31012005-04-17 16:05:31 -05004430
Jamie Wellnitz41415862006-02-28 19:25:27 -05004431 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004432
Jamie Wellnitz41415862006-02-28 19:25:27 -05004433 /* Reset HBA */
4434 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004435 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004436 (phba->pport) ? phba->pport->port_state : 0,
4437 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004438
4439 /* perform board reset */
4440 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004441 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004442 if (phba->pport) {
4443 phba->pport->fc_myDID = 0;
4444 phba->pport->fc_prevDID = 0;
4445 }
dea31012005-04-17 16:05:31 -05004446
Jamie Wellnitz41415862006-02-28 19:25:27 -05004447 /* Turn off parity checking and serr during the physical reset */
4448 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4449 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4450 (cfg_value &
4451 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4452
James Smart3772a992009-05-22 14:50:54 -04004453 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4454
Jamie Wellnitz41415862006-02-28 19:25:27 -05004455 /* Now toggle INITFF bit in the Host Control Register */
4456 writel(HC_INITFF, phba->HCregaddr);
4457 mdelay(1);
4458 readl(phba->HCregaddr); /* flush */
4459 writel(0, phba->HCregaddr);
4460 readl(phba->HCregaddr); /* flush */
4461
4462 /* Restore PCI cmd register */
4463 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004464
4465 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004466 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004467 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004468 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004469 pring->sli.sli3.rspidx = 0;
4470 pring->sli.sli3.next_cmdidx = 0;
4471 pring->sli.sli3.local_getidx = 0;
4472 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004473 pring->missbufcnt = 0;
4474 }
dea31012005-04-17 16:05:31 -05004475
James Smart2e0fef82007-06-17 19:56:36 -05004476 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004477 return 0;
4478}
4479
James Smarte59058c2008-08-24 21:49:00 -04004480/**
James Smartda0436e2009-05-22 14:51:39 -04004481 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4482 * @phba: Pointer to HBA context object.
4483 *
4484 * This function resets a SLI4 HBA. This function disables PCI layer parity
4485 * checking during resets the device. The caller is not required to hold
4486 * any locks.
4487 *
4488 * This function returns 0 always.
4489 **/
4490int
4491lpfc_sli4_brdreset(struct lpfc_hba *phba)
4492{
4493 struct lpfc_sli *psli = &phba->sli;
4494 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004495 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004496
4497 /* Reset HBA */
4498 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004499 "0295 Reset HBA Data: x%x x%x x%x\n",
4500 phba->pport->port_state, psli->sli_flag,
4501 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004502
4503 /* perform board reset */
4504 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004505 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004506 phba->pport->fc_myDID = 0;
4507 phba->pport->fc_prevDID = 0;
4508
James Smartda0436e2009-05-22 14:51:39 -04004509 spin_lock_irq(&phba->hbalock);
4510 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4511 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004512 spin_unlock_irq(&phba->hbalock);
4513
James Smart02936352014-04-04 13:52:12 -04004514 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4515 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4516 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4517 return rc;
4518 }
4519
James Smartda0436e2009-05-22 14:51:39 -04004520 /* Now physically reset the device */
4521 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4522 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004523
4524 /* Turn off parity checking and serr during the physical reset */
4525 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4526 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4527 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4528
James Smart88318812012-09-29 11:29:29 -04004529 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004530 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004531
James Smartbe858b62010-12-15 17:57:20 -05004532 /* Restore PCI cmd register */
4533 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4534
James Smart27b01b82012-05-09 21:19:44 -04004535 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004536}
4537
4538/**
4539 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004540 * @phba: Pointer to HBA context object.
4541 *
4542 * This function is called in the SLI initialization code path to
4543 * restart the HBA. The caller is not required to hold any lock.
4544 * This function writes MBX_RESTART mailbox command to the SLIM and
4545 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4546 * function to free any pending commands. The function enables
4547 * POST only during the first initialization. The function returns zero.
4548 * The function does not guarantee completion of MBX_RESTART mailbox
4549 * command before the return of this function.
4550 **/
James Smartda0436e2009-05-22 14:51:39 -04004551static int
4552lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004553{
4554 MAILBOX_t *mb;
4555 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004556 volatile uint32_t word0;
4557 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004558 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004559
James Smart2e0fef82007-06-17 19:56:36 -05004560 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004561
James Smart0d878412009-10-02 15:16:56 -04004562 /* Take PCIe device Advanced Error Reporting (AER) state */
4563 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4564
Jamie Wellnitz41415862006-02-28 19:25:27 -05004565 psli = &phba->sli;
4566
4567 /* Restart HBA */
4568 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004569 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004570 (phba->pport) ? phba->pport->port_state : 0,
4571 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004572
4573 word0 = 0;
4574 mb = (MAILBOX_t *) &word0;
4575 mb->mbxCommand = MBX_RESTART;
4576 mb->mbxHc = 1;
4577
James Smart92908312006-03-07 15:04:13 -05004578 lpfc_reset_barrier(phba);
4579
Jamie Wellnitz41415862006-02-28 19:25:27 -05004580 to_slim = phba->MBslimaddr;
4581 writel(*(uint32_t *) mb, to_slim);
4582 readl(to_slim); /* flush */
4583
4584 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004585 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004586 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004587 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004588 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004589 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004590 writel(*(uint32_t *) mb, to_slim);
4591 readl(to_slim); /* flush */
4592
4593 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004594 if (phba->pport)
4595 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004596 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004597 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004598 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004599
James Smart64ba8812006-08-02 15:24:34 -04004600 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004601 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04004602
James Smarteaf15d52008-12-04 22:39:29 -05004603 /* Give the INITFF and Post time to settle. */
4604 mdelay(100);
dea31012005-04-17 16:05:31 -05004605
James Smart0d878412009-10-02 15:16:56 -04004606 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4607 if (hba_aer_enabled)
4608 pci_disable_pcie_error_reporting(phba->pcidev);
4609
Jamie Wellnitz41415862006-02-28 19:25:27 -05004610 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004611
4612 return 0;
4613}
4614
James Smarte59058c2008-08-24 21:49:00 -04004615/**
James Smartda0436e2009-05-22 14:51:39 -04004616 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4617 * @phba: Pointer to HBA context object.
4618 *
4619 * This function is called in the SLI initialization code path to restart
4620 * a SLI4 HBA. The caller is not required to hold any lock.
4621 * At the end of the function, it calls lpfc_hba_down_post function to
4622 * free any pending commands.
4623 **/
4624static int
4625lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4626{
4627 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004628 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004629 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004630
4631 /* Restart HBA */
4632 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4633 "0296 Restart HBA Data: x%x x%x\n",
4634 phba->pport->port_state, psli->sli_flag);
4635
James Smart75baf692010-06-08 18:31:21 -04004636 /* Take PCIe device Advanced Error Reporting (AER) state */
4637 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4638
James Smart27b01b82012-05-09 21:19:44 -04004639 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004640
4641 spin_lock_irq(&phba->hbalock);
4642 phba->pport->stopped = 0;
4643 phba->link_state = LPFC_INIT_START;
4644 phba->hba_flag = 0;
4645 spin_unlock_irq(&phba->hbalock);
4646
4647 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004648 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04004649
James Smart75baf692010-06-08 18:31:21 -04004650 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4651 if (hba_aer_enabled)
4652 pci_disable_pcie_error_reporting(phba->pcidev);
4653
James Smartda0436e2009-05-22 14:51:39 -04004654 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004655 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004656
James Smart27b01b82012-05-09 21:19:44 -04004657 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004658}
4659
4660/**
4661 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4662 * @phba: Pointer to HBA context object.
4663 *
4664 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4665 * API jump table function pointer from the lpfc_hba struct.
4666**/
4667int
4668lpfc_sli_brdrestart(struct lpfc_hba *phba)
4669{
4670 return phba->lpfc_sli_brdrestart(phba);
4671}
4672
4673/**
James Smart3621a712009-04-06 18:47:14 -04004674 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004675 * @phba: Pointer to HBA context object.
4676 *
4677 * This function is called after a HBA restart to wait for successful
4678 * restart of the HBA. Successful restart of the HBA is indicated by
4679 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4680 * iteration, the function will restart the HBA again. The function returns
4681 * zero if HBA successfully restarted else returns negative error code.
4682 **/
James Smart4492b732017-04-27 15:08:26 -07004683int
dea31012005-04-17 16:05:31 -05004684lpfc_sli_chipset_init(struct lpfc_hba *phba)
4685{
4686 uint32_t status, i = 0;
4687
4688 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004689 if (lpfc_readl(phba->HSregaddr, &status))
4690 return -EIO;
dea31012005-04-17 16:05:31 -05004691
4692 /* Check status register to see what current state is */
4693 i = 0;
4694 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4695
James Smartdcf2a4e2010-09-29 11:18:53 -04004696 /* Check every 10ms for 10 retries, then every 100ms for 90
4697 * retries, then every 1 sec for 50 retires for a total of
4698 * ~60 seconds before reset the board again and check every
4699 * 1 sec for 50 retries. The up to 60 seconds before the
4700 * board ready is required by the Falcon FIPS zeroization
4701 * complete, and any reset the board in between shall cause
4702 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004703 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004704 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004705 /* Adapter failed to init, timeout, status reg
4706 <status> */
James Smarted957682007-06-17 19:56:37 -05004707 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004708 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004709 "timeout, status reg x%x, "
4710 "FW Data: A8 x%x AC x%x\n", status,
4711 readl(phba->MBslimaddr + 0xa8),
4712 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004713 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004714 return -ETIMEDOUT;
4715 }
4716
4717 /* Check to see if any errors occurred during init */
4718 if (status & HS_FFERM) {
4719 /* ERROR: During chipset initialization */
4720 /* Adapter failed to init, chipset, status reg
4721 <status> */
James Smarted957682007-06-17 19:56:37 -05004722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004723 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004724 "chipset, status reg x%x, "
4725 "FW Data: A8 x%x AC x%x\n", status,
4726 readl(phba->MBslimaddr + 0xa8),
4727 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004728 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004729 return -EIO;
4730 }
4731
James Smartdcf2a4e2010-09-29 11:18:53 -04004732 if (i <= 10)
dea31012005-04-17 16:05:31 -05004733 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004734 else if (i <= 100)
4735 msleep(100);
4736 else
4737 msleep(1000);
dea31012005-04-17 16:05:31 -05004738
James Smartdcf2a4e2010-09-29 11:18:53 -04004739 if (i == 150) {
4740 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004741 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004742 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004743 }
4744 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004745 if (lpfc_readl(phba->HSregaddr, &status))
4746 return -EIO;
dea31012005-04-17 16:05:31 -05004747 }
4748
4749 /* Check to see if any errors occurred during init */
4750 if (status & HS_FFERM) {
4751 /* ERROR: During chipset initialization */
4752 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004754 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004755 "status reg x%x, "
4756 "FW Data: A8 x%x AC x%x\n", status,
4757 readl(phba->MBslimaddr + 0xa8),
4758 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004759 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004760 return -EIO;
4761 }
4762
4763 /* Clear all interrupt enable conditions */
4764 writel(0, phba->HCregaddr);
4765 readl(phba->HCregaddr); /* flush */
4766
4767 /* setup host attn register */
4768 writel(0xffffffff, phba->HAregaddr);
4769 readl(phba->HAregaddr); /* flush */
4770 return 0;
4771}
4772
James Smarte59058c2008-08-24 21:49:00 -04004773/**
James Smart3621a712009-04-06 18:47:14 -04004774 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004775 *
4776 * This function calculates and returns the number of HBQs required to be
4777 * configured.
4778 **/
James Smart78b2d852007-08-02 11:10:21 -04004779int
James Smarted957682007-06-17 19:56:37 -05004780lpfc_sli_hbq_count(void)
4781{
James Smart92d7f7b2007-06-17 19:56:38 -05004782 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004783}
4784
James Smarte59058c2008-08-24 21:49:00 -04004785/**
James Smart3621a712009-04-06 18:47:14 -04004786 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004787 *
4788 * This function adds the number of hbq entries in every HBQ to get
4789 * the total number of hbq entries required for the HBA and returns
4790 * the total count.
4791 **/
James Smarted957682007-06-17 19:56:37 -05004792static int
4793lpfc_sli_hbq_entry_count(void)
4794{
4795 int hbq_count = lpfc_sli_hbq_count();
4796 int count = 0;
4797 int i;
4798
4799 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004800 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004801 return count;
4802}
4803
James Smarte59058c2008-08-24 21:49:00 -04004804/**
James Smart3621a712009-04-06 18:47:14 -04004805 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004806 *
4807 * This function calculates amount of memory required for all hbq entries
4808 * to be configured and returns the total memory required.
4809 **/
dea31012005-04-17 16:05:31 -05004810int
James Smarted957682007-06-17 19:56:37 -05004811lpfc_sli_hbq_size(void)
4812{
4813 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4814}
4815
James Smarte59058c2008-08-24 21:49:00 -04004816/**
James Smart3621a712009-04-06 18:47:14 -04004817 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004818 * @phba: Pointer to HBA context object.
4819 *
4820 * This function is called during the SLI initialization to configure
4821 * all the HBQs and post buffers to the HBQ. The caller is not
4822 * required to hold any locks. This function will return zero if successful
4823 * else it will return negative error code.
4824 **/
James Smarted957682007-06-17 19:56:37 -05004825static int
4826lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4827{
4828 int hbq_count = lpfc_sli_hbq_count();
4829 LPFC_MBOXQ_t *pmb;
4830 MAILBOX_t *pmbox;
4831 uint32_t hbqno;
4832 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004833
James Smart92d7f7b2007-06-17 19:56:38 -05004834 /* Get a Mailbox buffer to setup mailbox
4835 * commands for HBA initialization
4836 */
James Smarted957682007-06-17 19:56:37 -05004837 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4838
4839 if (!pmb)
4840 return -ENOMEM;
4841
James Smart04c68492009-05-22 14:52:52 -04004842 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004843
4844 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4845 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004846 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004847
4848 hbq_entry_index = 0;
4849 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4850 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4851 phba->hbqs[hbqno].hbqPutIdx = 0;
4852 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4853 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004854 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004855 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4856 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004857 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4858
4859 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4860 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4861 mbxStatus <status>, ring <num> */
4862
4863 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004864 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004865 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004866 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004867 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004868 pmbox->mbxStatus, hbqno);
4869
4870 phba->link_state = LPFC_HBA_ERROR;
4871 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004872 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004873 }
4874 }
4875 phba->hbq_count = hbq_count;
4876
James Smarted957682007-06-17 19:56:37 -05004877 mempool_free(pmb, phba->mbox_mem_pool);
4878
James Smart92d7f7b2007-06-17 19:56:38 -05004879 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004880 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4881 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004882 return 0;
4883}
4884
James Smarte59058c2008-08-24 21:49:00 -04004885/**
James Smart4f774512009-05-22 14:52:35 -04004886 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4887 * @phba: Pointer to HBA context object.
4888 *
4889 * This function is called during the SLI initialization to configure
4890 * all the HBQs and post buffers to the HBQ. The caller is not
4891 * required to hold any locks. This function will return zero if successful
4892 * else it will return negative error code.
4893 **/
4894static int
4895lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4896{
4897 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004898 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4899 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004900 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004901 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004902 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004903 return 0;
4904}
4905
4906/**
James Smart3621a712009-04-06 18:47:14 -04004907 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004908 * @phba: Pointer to HBA context object.
4909 * @sli_mode: sli mode - 2/3
4910 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004911 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004912 * to issue config_port mailbox command. This function restarts the
4913 * HBA firmware and issues a config_port mailbox command to configure
4914 * the SLI interface in the sli mode specified by sli_mode
4915 * variable. The caller is not required to hold any locks.
4916 * The function returns 0 if successful, else returns negative error
4917 * code.
4918 **/
James Smart93996272008-08-24 21:50:30 -04004919int
4920lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004921{
4922 LPFC_MBOXQ_t *pmb;
4923 uint32_t resetcount = 0, rc = 0, done = 0;
4924
4925 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4926 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004927 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004928 return -ENOMEM;
4929 }
4930
James Smarted957682007-06-17 19:56:37 -05004931 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004932 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004933 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004934 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004935 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004936 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004937 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004938 rc = lpfc_sli_chipset_init(phba);
4939 if (rc)
4940 break;
4941
James Smart2e0fef82007-06-17 19:56:36 -05004942 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004943 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004944 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004945 resetcount++;
4946
James Smarted957682007-06-17 19:56:37 -05004947 /* Call pre CONFIG_PORT mailbox command initialization. A
4948 * value of 0 means the call was successful. Any other
4949 * nonzero value is a failure, but if ERESTART is returned,
4950 * the driver may reset the HBA and try again.
4951 */
dea31012005-04-17 16:05:31 -05004952 rc = lpfc_config_port_prep(phba);
4953 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004954 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004955 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004956 } else if (rc)
dea31012005-04-17 16:05:31 -05004957 break;
James Smart6d368e52011-05-24 11:44:12 -04004958
James Smart2e0fef82007-06-17 19:56:36 -05004959 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004960 lpfc_config_port(phba, pmb);
4961 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004962 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4963 LPFC_SLI3_HBQ_ENABLED |
4964 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004965 LPFC_SLI3_BG_ENABLED |
4966 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004967 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004968 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004969 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004970 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004971 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004972 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004973 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004974 spin_unlock_irq(&phba->hbalock);
4975 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004976 } else {
4977 /* Allow asynchronous mailbox command to go through */
4978 spin_lock_irq(&phba->hbalock);
4979 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4980 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004981 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004982
4983 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4984 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4985 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4986 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004987 }
dea31012005-04-17 16:05:31 -05004988 }
James Smarted957682007-06-17 19:56:37 -05004989 if (!done) {
4990 rc = -EINVAL;
4991 goto do_prep_failed;
4992 }
James Smart04c68492009-05-22 14:52:52 -04004993 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4994 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004995 rc = -ENXIO;
4996 goto do_prep_failed;
4997 }
James Smart04c68492009-05-22 14:52:52 -04004998 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004999 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005000 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5001 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5002 phba->max_vpi : phba->max_vports;
5003
James Smart34b02dc2008-08-24 21:49:55 -04005004 } else
5005 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04005006 phba->fips_level = 0;
5007 phba->fips_spec_rev = 0;
5008 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04005009 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04005010 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
5011 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5012 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5013 "2850 Security Crypto Active. FIPS x%d "
5014 "(Spec Rev: x%d)",
5015 phba->fips_level, phba->fips_spec_rev);
5016 }
5017 if (pmb->u.mb.un.varCfgPort.sec_err) {
5018 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5019 "2856 Config Port Security Crypto "
5020 "Error: x%x ",
5021 pmb->u.mb.un.varCfgPort.sec_err);
5022 }
James Smart04c68492009-05-22 14:52:52 -04005023 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005024 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005025 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005026 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005027
5028 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5029 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005030
James Smartf44ac122018-03-05 12:04:08 -08005031 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5032 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5033 phba->cfg_enable_bg = 0;
5034 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smarte2a0a9d2008-12-04 22:40:02 -05005035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5036 "0443 Adapter did not grant "
5037 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005038 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005039 }
James Smart34b02dc2008-08-24 21:49:55 -04005040 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005041 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005042 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005043 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005044 }
James Smart92d7f7b2007-06-17 19:56:38 -05005045do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005046 mempool_free(pmb, phba->mbox_mem_pool);
5047 return rc;
5048}
5049
James Smarte59058c2008-08-24 21:49:00 -04005050
5051/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005052 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005053 * @phba: Pointer to HBA context object.
5054 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005055 * This function is the main SLI initialization function. This function
5056 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005057 * error attention handler code. Caller is not required to hold any
5058 * locks. This function issues config_port mailbox command to configure
5059 * the SLI, setup iocb rings and HBQ rings. In the end the function
5060 * calls the config_port_post function to issue init_link mailbox
5061 * command and to start the discovery. The function will return zero
5062 * if successful, else it will return negative error code.
5063 **/
James Smarted957682007-06-17 19:56:37 -05005064int
5065lpfc_sli_hba_setup(struct lpfc_hba *phba)
5066{
5067 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005068 int mode = 3, i;
5069 int longs;
James Smarted957682007-06-17 19:56:37 -05005070
James Smart12247e82016-07-06 12:36:09 -07005071 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005072 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005073 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005075 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005076 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005077 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005078 break;
5079 }
James Smarted957682007-06-17 19:56:37 -05005080 mode = 2;
5081 break;
5082 case 0:
5083 case 3:
5084 break;
5085 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005086 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005087 "1819 Unrecognized sli_mode parameter: %d.\n",
5088 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005089
5090 break;
5091 }
James Smartb5c53952016-03-31 14:12:30 -07005092 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005093
James Smart93996272008-08-24 21:50:30 -04005094 rc = lpfc_sli_config_port(phba, mode);
5095
James Smart12247e82016-07-06 12:36:09 -07005096 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005097 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005098 "1820 Unable to select SLI-3. "
5099 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005100 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005101 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005102 else if (rc && mode == 2)
5103 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005104 if (rc)
dea31012005-04-17 16:05:31 -05005105 goto lpfc_sli_hba_setup_error;
5106
James Smart0d878412009-10-02 15:16:56 -04005107 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5108 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5109 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5110 if (!rc) {
5111 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5112 "2709 This device supports "
5113 "Advanced Error Reporting (AER)\n");
5114 spin_lock_irq(&phba->hbalock);
5115 phba->hba_flag |= HBA_AER_ENABLED;
5116 spin_unlock_irq(&phba->hbalock);
5117 } else {
5118 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5119 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005120 "Advanced Error Reporting (AER): %d\n",
5121 rc);
James Smart0d878412009-10-02 15:16:56 -04005122 phba->cfg_aer_support = 0;
5123 }
5124 }
5125
James Smarted957682007-06-17 19:56:37 -05005126 if (phba->sli_rev == 3) {
5127 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5128 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005129 } else {
5130 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5131 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005132 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005133 }
5134
5135 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005136 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5137 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005138 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005139
5140 if (rc)
5141 goto lpfc_sli_hba_setup_error;
5142
James Smart6d368e52011-05-24 11:44:12 -04005143 /* Initialize VPIs. */
5144 if (phba->sli_rev == LPFC_SLI_REV3) {
5145 /*
5146 * The VPI bitmask and physical ID array are allocated
5147 * and initialized once only - at driver load. A port
5148 * reset doesn't need to reinitialize this memory.
5149 */
5150 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5151 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005152 phba->vpi_bmask = kcalloc(longs,
5153 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005154 GFP_KERNEL);
5155 if (!phba->vpi_bmask) {
5156 rc = -ENOMEM;
5157 goto lpfc_sli_hba_setup_error;
5158 }
5159
Kees Cook6396bb22018-06-12 14:03:40 -07005160 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5161 sizeof(uint16_t),
5162 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005163 if (!phba->vpi_ids) {
5164 kfree(phba->vpi_bmask);
5165 rc = -ENOMEM;
5166 goto lpfc_sli_hba_setup_error;
5167 }
5168 for (i = 0; i < phba->max_vpi; i++)
5169 phba->vpi_ids[i] = i;
5170 }
5171 }
5172
James Smart93996272008-08-24 21:50:30 -04005173 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005174 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5175 rc = lpfc_sli_hbq_setup(phba);
5176 if (rc)
5177 goto lpfc_sli_hba_setup_error;
5178 }
James Smart04c68492009-05-22 14:52:52 -04005179 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005180 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005181 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005182
5183 rc = lpfc_config_port_post(phba);
5184 if (rc)
5185 goto lpfc_sli_hba_setup_error;
5186
James Smarted957682007-06-17 19:56:37 -05005187 return rc;
5188
James Smart92d7f7b2007-06-17 19:56:38 -05005189lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005190 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005192 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005193 return rc;
5194}
5195
James Smartda0436e2009-05-22 14:51:39 -04005196/**
5197 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5198 * @phba: Pointer to HBA context object.
5199 * @mboxq: mailbox pointer.
5200 * This function issue a dump mailbox command to read config region
5201 * 23 and parse the records in the region and populate driver
5202 * data structure.
5203 **/
5204static int
James Smartff78d8f2011-12-13 13:21:35 -05005205lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005206{
James Smartff78d8f2011-12-13 13:21:35 -05005207 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005208 struct lpfc_dmabuf *mp;
5209 struct lpfc_mqe *mqe;
5210 uint32_t data_length;
5211 int rc;
5212
5213 /* Program the default value of vlan_id and fc_map */
5214 phba->valid_vlan = 0;
5215 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5216 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5217 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5218
James Smartff78d8f2011-12-13 13:21:35 -05005219 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5220 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005221 return -ENOMEM;
5222
James Smartff78d8f2011-12-13 13:21:35 -05005223 mqe = &mboxq->u.mqe;
5224 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5225 rc = -ENOMEM;
5226 goto out_free_mboxq;
5227 }
5228
James Smartda0436e2009-05-22 14:51:39 -04005229 mp = (struct lpfc_dmabuf *) mboxq->context1;
5230 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5231
5232 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5233 "(%d):2571 Mailbox cmd x%x Status x%x "
5234 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5235 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5236 "CQ: x%x x%x x%x x%x\n",
5237 mboxq->vport ? mboxq->vport->vpi : 0,
5238 bf_get(lpfc_mqe_command, mqe),
5239 bf_get(lpfc_mqe_status, mqe),
5240 mqe->un.mb_words[0], mqe->un.mb_words[1],
5241 mqe->un.mb_words[2], mqe->un.mb_words[3],
5242 mqe->un.mb_words[4], mqe->un.mb_words[5],
5243 mqe->un.mb_words[6], mqe->un.mb_words[7],
5244 mqe->un.mb_words[8], mqe->un.mb_words[9],
5245 mqe->un.mb_words[10], mqe->un.mb_words[11],
5246 mqe->un.mb_words[12], mqe->un.mb_words[13],
5247 mqe->un.mb_words[14], mqe->un.mb_words[15],
5248 mqe->un.mb_words[16], mqe->un.mb_words[50],
5249 mboxq->mcqe.word0,
5250 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5251 mboxq->mcqe.trailer);
5252
5253 if (rc) {
5254 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5255 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005256 rc = -EIO;
5257 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005258 }
5259 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005260 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005261 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5262 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005263 rc = -EIO;
5264 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005265 }
James Smartda0436e2009-05-22 14:51:39 -04005266
5267 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5268 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5269 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005270 rc = 0;
5271
5272out_free_mboxq:
5273 mempool_free(mboxq, phba->mbox_mem_pool);
5274 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005275}
5276
5277/**
5278 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5279 * @phba: pointer to lpfc hba data structure.
5280 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5281 * @vpd: pointer to the memory to hold resulting port vpd data.
5282 * @vpd_size: On input, the number of bytes allocated to @vpd.
5283 * On output, the number of data bytes in @vpd.
5284 *
5285 * This routine executes a READ_REV SLI4 mailbox command. In
5286 * addition, this routine gets the port vpd data.
5287 *
5288 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005289 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005290 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005291 **/
5292static int
5293lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5294 uint8_t *vpd, uint32_t *vpd_size)
5295{
5296 int rc = 0;
5297 uint32_t dma_size;
5298 struct lpfc_dmabuf *dmabuf;
5299 struct lpfc_mqe *mqe;
5300
5301 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5302 if (!dmabuf)
5303 return -ENOMEM;
5304
5305 /*
5306 * Get a DMA buffer for the vpd data resulting from the READ_REV
5307 * mailbox command.
5308 */
5309 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005310 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5311 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005312 if (!dmabuf->virt) {
5313 kfree(dmabuf);
5314 return -ENOMEM;
5315 }
James Smartda0436e2009-05-22 14:51:39 -04005316
5317 /*
5318 * The SLI4 implementation of READ_REV conflicts at word1,
5319 * bits 31:16 and SLI4 adds vpd functionality not present
5320 * in SLI3. This code corrects the conflicts.
5321 */
5322 lpfc_read_rev(phba, mboxq);
5323 mqe = &mboxq->u.mqe;
5324 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5325 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5326 mqe->un.read_rev.word1 &= 0x0000FFFF;
5327 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5328 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5329
5330 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5331 if (rc) {
5332 dma_free_coherent(&phba->pcidev->dev, dma_size,
5333 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005334 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005335 return -EIO;
5336 }
5337
James Smartda0436e2009-05-22 14:51:39 -04005338 /*
5339 * The available vpd length cannot be bigger than the
5340 * DMA buffer passed to the port. Catch the less than
5341 * case and update the caller's size.
5342 */
5343 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5344 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5345
James Smartd7c47992010-06-08 18:31:54 -04005346 memcpy(vpd, dmabuf->virt, *vpd_size);
5347
James Smartda0436e2009-05-22 14:51:39 -04005348 dma_free_coherent(&phba->pcidev->dev, dma_size,
5349 dmabuf->virt, dmabuf->phys);
5350 kfree(dmabuf);
5351 return 0;
5352}
5353
5354/**
James Smartcd1c8302011-10-10 21:33:25 -04005355 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5356 * @phba: pointer to lpfc hba data structure.
5357 *
5358 * This routine retrieves SLI4 device physical port name this PCI function
5359 * is attached to.
5360 *
5361 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005362 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005363 * otherwise - failed to retrieve physical port name
5364 **/
5365static int
5366lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5367{
5368 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005369 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5370 struct lpfc_controller_attribute *cntl_attr;
5371 struct lpfc_mbx_get_port_name *get_port_name;
5372 void *virtaddr = NULL;
5373 uint32_t alloclen, reqlen;
5374 uint32_t shdr_status, shdr_add_status;
5375 union lpfc_sli4_cfg_shdr *shdr;
5376 char cport_name = 0;
5377 int rc;
5378
5379 /* We assume nothing at this point */
5380 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5381 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5382
5383 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5384 if (!mboxq)
5385 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005386 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005387 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5388 lpfc_sli4_read_config(phba);
5389 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5390 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005391
5392 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5393 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5394 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5395 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5396 LPFC_SLI4_MBX_NEMBED);
5397 if (alloclen < reqlen) {
5398 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5399 "3084 Allocated DMA memory size (%d) is "
5400 "less than the requested DMA memory size "
5401 "(%d)\n", alloclen, reqlen);
5402 rc = -ENOMEM;
5403 goto out_free_mboxq;
5404 }
5405 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5406 virtaddr = mboxq->sge_array->addr[0];
5407 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5408 shdr = &mbx_cntl_attr->cfg_shdr;
5409 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5410 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5411 if (shdr_status || shdr_add_status || rc) {
5412 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5413 "3085 Mailbox x%x (x%x/x%x) failed, "
5414 "rc:x%x, status:x%x, add_status:x%x\n",
5415 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5416 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5417 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5418 rc, shdr_status, shdr_add_status);
5419 rc = -ENXIO;
5420 goto out_free_mboxq;
5421 }
5422 cntl_attr = &mbx_cntl_attr->cntl_attr;
5423 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5424 phba->sli4_hba.lnk_info.lnk_tp =
5425 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5426 phba->sli4_hba.lnk_info.lnk_no =
5427 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5428 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5429 "3086 lnk_type:%d, lnk_numb:%d\n",
5430 phba->sli4_hba.lnk_info.lnk_tp,
5431 phba->sli4_hba.lnk_info.lnk_no);
5432
5433retrieve_ppname:
5434 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5435 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5436 sizeof(struct lpfc_mbx_get_port_name) -
5437 sizeof(struct lpfc_sli4_cfg_mhdr),
5438 LPFC_SLI4_MBX_EMBED);
5439 get_port_name = &mboxq->u.mqe.un.get_port_name;
5440 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5441 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5442 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5443 phba->sli4_hba.lnk_info.lnk_tp);
5444 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5445 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5446 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5447 if (shdr_status || shdr_add_status || rc) {
5448 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5449 "3087 Mailbox x%x (x%x/x%x) failed: "
5450 "rc:x%x, status:x%x, add_status:x%x\n",
5451 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5452 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5453 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5454 rc, shdr_status, shdr_add_status);
5455 rc = -ENXIO;
5456 goto out_free_mboxq;
5457 }
5458 switch (phba->sli4_hba.lnk_info.lnk_no) {
5459 case LPFC_LINK_NUMBER_0:
5460 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5461 &get_port_name->u.response);
5462 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5463 break;
5464 case LPFC_LINK_NUMBER_1:
5465 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5466 &get_port_name->u.response);
5467 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5468 break;
5469 case LPFC_LINK_NUMBER_2:
5470 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5471 &get_port_name->u.response);
5472 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5473 break;
5474 case LPFC_LINK_NUMBER_3:
5475 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5476 &get_port_name->u.response);
5477 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5478 break;
5479 default:
5480 break;
5481 }
5482
5483 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5484 phba->Port[0] = cport_name;
5485 phba->Port[1] = '\0';
5486 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5487 "3091 SLI get port name: %s\n", phba->Port);
5488 }
5489
5490out_free_mboxq:
5491 if (rc != MBX_TIMEOUT) {
5492 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5493 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5494 else
5495 mempool_free(mboxq, phba->mbox_mem_pool);
5496 }
5497 return rc;
5498}
5499
5500/**
James Smartda0436e2009-05-22 14:51:39 -04005501 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5502 * @phba: pointer to lpfc hba data structure.
5503 *
5504 * This routine is called to explicitly arm the SLI4 device's completion and
5505 * event queues
5506 **/
5507static void
5508lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5509{
James Smart895427b2017-02-12 13:52:30 -08005510 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005511 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartda0436e2009-05-22 14:51:39 -04005512
James Smartb71413d2018-02-22 08:18:40 -08005513 sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5514 sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5515 if (sli4_hba->nvmels_cq)
5516 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
James Smart895427b2017-02-12 13:52:30 -08005517 LPFC_QUEUE_REARM);
5518
James Smartb71413d2018-02-22 08:18:40 -08005519 if (sli4_hba->fcp_cq)
James Smart895427b2017-02-12 13:52:30 -08005520 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005521 sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
James Smart895427b2017-02-12 13:52:30 -08005522 LPFC_QUEUE_REARM);
5523
James Smartb71413d2018-02-22 08:18:40 -08005524 if (sli4_hba->nvme_cq)
James Smart895427b2017-02-12 13:52:30 -08005525 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005526 sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
James Smart895427b2017-02-12 13:52:30 -08005527 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005528
James Smartf38fa0b2014-04-04 13:52:21 -04005529 if (phba->cfg_fof)
James Smartb71413d2018-02-22 08:18:40 -08005530 sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005531
James Smartb71413d2018-02-22 08:18:40 -08005532 if (sli4_hba->hba_eq)
James Smart895427b2017-02-12 13:52:30 -08005533 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005534 sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5535 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005536
James Smart2d7dbc42017-02-12 13:52:35 -08005537 if (phba->nvmet_support) {
5538 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smartb71413d2018-02-22 08:18:40 -08005539 sli4_hba->sli4_cq_release(
5540 sli4_hba->nvmet_cqset[qidx],
James Smart2d7dbc42017-02-12 13:52:35 -08005541 LPFC_QUEUE_REARM);
5542 }
James Smart2e90f4b2011-12-13 13:22:37 -05005543 }
James Smart1ba981f2014-02-20 09:56:45 -05005544
5545 if (phba->cfg_fof)
James Smartb71413d2018-02-22 08:18:40 -08005546 sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005547}
5548
5549/**
James Smart6d368e52011-05-24 11:44:12 -04005550 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5551 * @phba: Pointer to HBA context object.
5552 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005553 * @extnt_count: buffer to hold port available extent count.
5554 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005555 *
James Smartb76f2dc2011-07-22 18:37:42 -04005556 * This function calls the port and retrievs the number of available
5557 * extents and their size for a particular extent type.
5558 *
5559 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005560 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005561int
James Smart6d368e52011-05-24 11:44:12 -04005562lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5563 uint16_t *extnt_count, uint16_t *extnt_size)
5564{
5565 int rc = 0;
5566 uint32_t length;
5567 uint32_t mbox_tmo;
5568 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5569 LPFC_MBOXQ_t *mbox;
5570
5571 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5572 if (!mbox)
5573 return -ENOMEM;
5574
5575 /* Find out how many extents are available for this resource type */
5576 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5577 sizeof(struct lpfc_sli4_cfg_mhdr));
5578 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5579 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5580 length, LPFC_SLI4_MBX_EMBED);
5581
5582 /* Send an extents count of 0 - the GET doesn't use it. */
5583 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5584 LPFC_SLI4_MBX_EMBED);
5585 if (unlikely(rc)) {
5586 rc = -EIO;
5587 goto err_exit;
5588 }
5589
5590 if (!phba->sli4_hba.intr_enable)
5591 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5592 else {
James Smarta183a152011-10-10 21:32:43 -04005593 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005594 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5595 }
5596 if (unlikely(rc)) {
5597 rc = -EIO;
5598 goto err_exit;
5599 }
5600
5601 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5602 if (bf_get(lpfc_mbox_hdr_status,
5603 &rsrc_info->header.cfg_shdr.response)) {
5604 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5605 "2930 Failed to get resource extents "
5606 "Status 0x%x Add'l Status 0x%x\n",
5607 bf_get(lpfc_mbox_hdr_status,
5608 &rsrc_info->header.cfg_shdr.response),
5609 bf_get(lpfc_mbox_hdr_add_status,
5610 &rsrc_info->header.cfg_shdr.response));
5611 rc = -EIO;
5612 goto err_exit;
5613 }
5614
5615 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5616 &rsrc_info->u.rsp);
5617 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5618 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005619
5620 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5621 "3162 Retrieved extents type-%d from port: count:%d, "
5622 "size:%d\n", type, *extnt_count, *extnt_size);
5623
5624err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005625 mempool_free(mbox, phba->mbox_mem_pool);
5626 return rc;
5627}
5628
5629/**
5630 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5631 * @phba: Pointer to HBA context object.
5632 * @type: The extent type to check.
5633 *
5634 * This function reads the current available extents from the port and checks
5635 * if the extent count or extent size has changed since the last access.
5636 * Callers use this routine post port reset to understand if there is a
5637 * extent reprovisioning requirement.
5638 *
5639 * Returns:
5640 * -Error: error indicates problem.
5641 * 1: Extent count or size has changed.
5642 * 0: No changes.
5643 **/
5644static int
5645lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5646{
5647 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5648 uint16_t size_diff, rsrc_ext_size;
5649 int rc = 0;
5650 struct lpfc_rsrc_blks *rsrc_entry;
5651 struct list_head *rsrc_blk_list = NULL;
5652
5653 size_diff = 0;
5654 curr_ext_cnt = 0;
5655 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5656 &rsrc_ext_cnt,
5657 &rsrc_ext_size);
5658 if (unlikely(rc))
5659 return -EIO;
5660
5661 switch (type) {
5662 case LPFC_RSC_TYPE_FCOE_RPI:
5663 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5664 break;
5665 case LPFC_RSC_TYPE_FCOE_VPI:
5666 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5667 break;
5668 case LPFC_RSC_TYPE_FCOE_XRI:
5669 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5670 break;
5671 case LPFC_RSC_TYPE_FCOE_VFI:
5672 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5673 break;
5674 default:
5675 break;
5676 }
5677
5678 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5679 curr_ext_cnt++;
5680 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5681 size_diff++;
5682 }
5683
5684 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5685 rc = 1;
5686
5687 return rc;
5688}
5689
5690/**
5691 * lpfc_sli4_cfg_post_extnts -
5692 * @phba: Pointer to HBA context object.
5693 * @extnt_cnt - number of available extents.
5694 * @type - the extent type (rpi, xri, vfi, vpi).
5695 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5696 * @mbox - pointer to the caller's allocated mailbox structure.
5697 *
5698 * This function executes the extents allocation request. It also
5699 * takes care of the amount of memory needed to allocate or get the
5700 * allocated extents. It is the caller's responsibility to evaluate
5701 * the response.
5702 *
5703 * Returns:
5704 * -Error: Error value describes the condition found.
5705 * 0: if successful
5706 **/
5707static int
James Smart8a9d2e82012-05-09 21:16:12 -04005708lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005709 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5710{
5711 int rc = 0;
5712 uint32_t req_len;
5713 uint32_t emb_len;
5714 uint32_t alloc_len, mbox_tmo;
5715
5716 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005717 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005718
5719 /*
5720 * Calculate the size of an embedded mailbox. The uint32_t
5721 * accounts for extents-specific word.
5722 */
5723 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5724 sizeof(uint32_t);
5725
5726 /*
5727 * Presume the allocation and response will fit into an embedded
5728 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5729 */
5730 *emb = LPFC_SLI4_MBX_EMBED;
5731 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005732 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005733 sizeof(union lpfc_sli4_cfg_shdr) +
5734 sizeof(uint32_t);
5735 *emb = LPFC_SLI4_MBX_NEMBED;
5736 }
5737
5738 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5739 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5740 req_len, *emb);
5741 if (alloc_len < req_len) {
5742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005743 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005744 "less than the requested DMA memory "
5745 "size (x%x)\n", alloc_len, req_len);
5746 return -ENOMEM;
5747 }
James Smart8a9d2e82012-05-09 21:16:12 -04005748 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005749 if (unlikely(rc))
5750 return -EIO;
5751
5752 if (!phba->sli4_hba.intr_enable)
5753 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5754 else {
James Smarta183a152011-10-10 21:32:43 -04005755 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005756 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5757 }
5758
5759 if (unlikely(rc))
5760 rc = -EIO;
5761 return rc;
5762}
5763
5764/**
5765 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5766 * @phba: Pointer to HBA context object.
5767 * @type: The resource extent type to allocate.
5768 *
5769 * This function allocates the number of elements for the specified
5770 * resource type.
5771 **/
5772static int
5773lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5774{
5775 bool emb = false;
5776 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5777 uint16_t rsrc_id, rsrc_start, j, k;
5778 uint16_t *ids;
5779 int i, rc;
5780 unsigned long longs;
5781 unsigned long *bmask;
5782 struct lpfc_rsrc_blks *rsrc_blks;
5783 LPFC_MBOXQ_t *mbox;
5784 uint32_t length;
5785 struct lpfc_id_range *id_array = NULL;
5786 void *virtaddr = NULL;
5787 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5788 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5789 struct list_head *ext_blk_list;
5790
5791 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5792 &rsrc_cnt,
5793 &rsrc_size);
5794 if (unlikely(rc))
5795 return -EIO;
5796
5797 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5798 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5799 "3009 No available Resource Extents "
5800 "for resource type 0x%x: Count: 0x%x, "
5801 "Size 0x%x\n", type, rsrc_cnt,
5802 rsrc_size);
5803 return -ENOMEM;
5804 }
5805
James Smart8a9d2e82012-05-09 21:16:12 -04005806 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5807 "2903 Post resource extents type-0x%x: "
5808 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005809
5810 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5811 if (!mbox)
5812 return -ENOMEM;
5813
James Smart8a9d2e82012-05-09 21:16:12 -04005814 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005815 if (unlikely(rc)) {
5816 rc = -EIO;
5817 goto err_exit;
5818 }
5819
5820 /*
5821 * Figure out where the response is located. Then get local pointers
5822 * to the response data. The port does not guarantee to respond to
5823 * all extents counts request so update the local variable with the
5824 * allocated count from the port.
5825 */
5826 if (emb == LPFC_SLI4_MBX_EMBED) {
5827 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5828 id_array = &rsrc_ext->u.rsp.id[0];
5829 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5830 } else {
5831 virtaddr = mbox->sge_array->addr[0];
5832 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5833 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5834 id_array = &n_rsrc->id;
5835 }
5836
5837 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5838 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5839
5840 /*
5841 * Based on the resource size and count, correct the base and max
5842 * resource values.
5843 */
5844 length = sizeof(struct lpfc_rsrc_blks);
5845 switch (type) {
5846 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005847 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005848 sizeof(unsigned long),
5849 GFP_KERNEL);
5850 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5851 rc = -ENOMEM;
5852 goto err_exit;
5853 }
Kees Cook6396bb22018-06-12 14:03:40 -07005854 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005855 sizeof(uint16_t),
5856 GFP_KERNEL);
5857 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5858 kfree(phba->sli4_hba.rpi_bmask);
5859 rc = -ENOMEM;
5860 goto err_exit;
5861 }
5862
5863 /*
5864 * The next_rpi was initialized with the maximum available
5865 * count but the port may allocate a smaller number. Catch
5866 * that case and update the next_rpi.
5867 */
5868 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5869
5870 /* Initialize local ptrs for common extent processing later. */
5871 bmask = phba->sli4_hba.rpi_bmask;
5872 ids = phba->sli4_hba.rpi_ids;
5873 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5874 break;
5875 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07005876 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005877 GFP_KERNEL);
5878 if (unlikely(!phba->vpi_bmask)) {
5879 rc = -ENOMEM;
5880 goto err_exit;
5881 }
Kees Cook6396bb22018-06-12 14:03:40 -07005882 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04005883 GFP_KERNEL);
5884 if (unlikely(!phba->vpi_ids)) {
5885 kfree(phba->vpi_bmask);
5886 rc = -ENOMEM;
5887 goto err_exit;
5888 }
5889
5890 /* Initialize local ptrs for common extent processing later. */
5891 bmask = phba->vpi_bmask;
5892 ids = phba->vpi_ids;
5893 ext_blk_list = &phba->lpfc_vpi_blk_list;
5894 break;
5895 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07005896 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005897 sizeof(unsigned long),
5898 GFP_KERNEL);
5899 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5900 rc = -ENOMEM;
5901 goto err_exit;
5902 }
James Smart8a9d2e82012-05-09 21:16:12 -04005903 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07005904 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005905 sizeof(uint16_t),
5906 GFP_KERNEL);
5907 if (unlikely(!phba->sli4_hba.xri_ids)) {
5908 kfree(phba->sli4_hba.xri_bmask);
5909 rc = -ENOMEM;
5910 goto err_exit;
5911 }
5912
5913 /* Initialize local ptrs for common extent processing later. */
5914 bmask = phba->sli4_hba.xri_bmask;
5915 ids = phba->sli4_hba.xri_ids;
5916 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5917 break;
5918 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07005919 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04005920 sizeof(unsigned long),
5921 GFP_KERNEL);
5922 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5923 rc = -ENOMEM;
5924 goto err_exit;
5925 }
Kees Cook6396bb22018-06-12 14:03:40 -07005926 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005927 sizeof(uint16_t),
5928 GFP_KERNEL);
5929 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5930 kfree(phba->sli4_hba.vfi_bmask);
5931 rc = -ENOMEM;
5932 goto err_exit;
5933 }
5934
5935 /* Initialize local ptrs for common extent processing later. */
5936 bmask = phba->sli4_hba.vfi_bmask;
5937 ids = phba->sli4_hba.vfi_ids;
5938 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5939 break;
5940 default:
5941 /* Unsupported Opcode. Fail call. */
5942 id_array = NULL;
5943 bmask = NULL;
5944 ids = NULL;
5945 ext_blk_list = NULL;
5946 goto err_exit;
5947 }
5948
5949 /*
5950 * Complete initializing the extent configuration with the
5951 * allocated ids assigned to this function. The bitmask serves
5952 * as an index into the array and manages the available ids. The
5953 * array just stores the ids communicated to the port via the wqes.
5954 */
5955 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5956 if ((i % 2) == 0)
5957 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5958 &id_array[k]);
5959 else
5960 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5961 &id_array[k]);
5962
5963 rsrc_blks = kzalloc(length, GFP_KERNEL);
5964 if (unlikely(!rsrc_blks)) {
5965 rc = -ENOMEM;
5966 kfree(bmask);
5967 kfree(ids);
5968 goto err_exit;
5969 }
5970 rsrc_blks->rsrc_start = rsrc_id;
5971 rsrc_blks->rsrc_size = rsrc_size;
5972 list_add_tail(&rsrc_blks->list, ext_blk_list);
5973 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005974 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005975 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005976 lpfc_sli4_get_iocb_cnt(phba);
5977 phba->sli4_hba.nvme_xri_start =
5978 phba->sli4_hba.scsi_xri_start +
5979 phba->sli4_hba.scsi_xri_max;
5980 }
James Smart6d368e52011-05-24 11:44:12 -04005981
5982 while (rsrc_id < (rsrc_start + rsrc_size)) {
5983 ids[j] = rsrc_id;
5984 rsrc_id++;
5985 j++;
5986 }
5987 /* Entire word processed. Get next word.*/
5988 if ((i % 2) == 1)
5989 k++;
5990 }
5991 err_exit:
5992 lpfc_sli4_mbox_cmd_free(phba, mbox);
5993 return rc;
5994}
5995
James Smart895427b2017-02-12 13:52:30 -08005996
5997
James Smart6d368e52011-05-24 11:44:12 -04005998/**
5999 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6000 * @phba: Pointer to HBA context object.
6001 * @type: the extent's type.
6002 *
6003 * This function deallocates all extents of a particular resource type.
6004 * SLI4 does not allow for deallocating a particular extent range. It
6005 * is the caller's responsibility to release all kernel memory resources.
6006 **/
6007static int
6008lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6009{
6010 int rc;
6011 uint32_t length, mbox_tmo = 0;
6012 LPFC_MBOXQ_t *mbox;
6013 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6014 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6015
6016 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6017 if (!mbox)
6018 return -ENOMEM;
6019
6020 /*
6021 * This function sends an embedded mailbox because it only sends the
6022 * the resource type. All extents of this type are released by the
6023 * port.
6024 */
6025 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6026 sizeof(struct lpfc_sli4_cfg_mhdr));
6027 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6028 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6029 length, LPFC_SLI4_MBX_EMBED);
6030
6031 /* Send an extents count of 0 - the dealloc doesn't use it. */
6032 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6033 LPFC_SLI4_MBX_EMBED);
6034 if (unlikely(rc)) {
6035 rc = -EIO;
6036 goto out_free_mbox;
6037 }
6038 if (!phba->sli4_hba.intr_enable)
6039 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6040 else {
James Smarta183a152011-10-10 21:32:43 -04006041 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006042 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6043 }
6044 if (unlikely(rc)) {
6045 rc = -EIO;
6046 goto out_free_mbox;
6047 }
6048
6049 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6050 if (bf_get(lpfc_mbox_hdr_status,
6051 &dealloc_rsrc->header.cfg_shdr.response)) {
6052 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6053 "2919 Failed to release resource extents "
6054 "for type %d - Status 0x%x Add'l Status 0x%x. "
6055 "Resource memory not released.\n",
6056 type,
6057 bf_get(lpfc_mbox_hdr_status,
6058 &dealloc_rsrc->header.cfg_shdr.response),
6059 bf_get(lpfc_mbox_hdr_add_status,
6060 &dealloc_rsrc->header.cfg_shdr.response));
6061 rc = -EIO;
6062 goto out_free_mbox;
6063 }
6064
6065 /* Release kernel memory resources for the specific type. */
6066 switch (type) {
6067 case LPFC_RSC_TYPE_FCOE_VPI:
6068 kfree(phba->vpi_bmask);
6069 kfree(phba->vpi_ids);
6070 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6071 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6072 &phba->lpfc_vpi_blk_list, list) {
6073 list_del_init(&rsrc_blk->list);
6074 kfree(rsrc_blk);
6075 }
James Smart16a3a202013-04-17 20:14:38 -04006076 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006077 break;
6078 case LPFC_RSC_TYPE_FCOE_XRI:
6079 kfree(phba->sli4_hba.xri_bmask);
6080 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006081 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6082 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6083 list_del_init(&rsrc_blk->list);
6084 kfree(rsrc_blk);
6085 }
6086 break;
6087 case LPFC_RSC_TYPE_FCOE_VFI:
6088 kfree(phba->sli4_hba.vfi_bmask);
6089 kfree(phba->sli4_hba.vfi_ids);
6090 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6091 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6092 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6093 list_del_init(&rsrc_blk->list);
6094 kfree(rsrc_blk);
6095 }
6096 break;
6097 case LPFC_RSC_TYPE_FCOE_RPI:
6098 /* RPI bitmask and physical id array are cleaned up earlier. */
6099 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6100 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6101 list_del_init(&rsrc_blk->list);
6102 kfree(rsrc_blk);
6103 }
6104 break;
6105 default:
6106 break;
6107 }
6108
6109 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6110
6111 out_free_mbox:
6112 mempool_free(mbox, phba->mbox_mem_pool);
6113 return rc;
6114}
6115
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006116static void
James Smart7bdedb32016-07-06 12:36:00 -07006117lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6118 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006119{
James Smart65791f12016-07-06 12:35:56 -07006120 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006121
James Smart65791f12016-07-06 12:35:56 -07006122 len = sizeof(struct lpfc_mbx_set_feature) -
6123 sizeof(struct lpfc_sli4_cfg_mhdr);
6124 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6125 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6126 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006127
James Smart7bdedb32016-07-06 12:36:00 -07006128 switch (feature) {
6129 case LPFC_SET_UE_RECOVERY:
6130 bf_set(lpfc_mbx_set_feature_UER,
6131 &mbox->u.mqe.un.set_feature, 1);
6132 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6133 mbox->u.mqe.un.set_feature.param_len = 8;
6134 break;
6135 case LPFC_SET_MDS_DIAGS:
6136 bf_set(lpfc_mbx_set_feature_mds,
6137 &mbox->u.mqe.un.set_feature, 1);
6138 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006139 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006140 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6141 mbox->u.mqe.un.set_feature.param_len = 8;
6142 break;
James Smart65791f12016-07-06 12:35:56 -07006143 }
James Smart7bdedb32016-07-06 12:36:00 -07006144
6145 return;
James Smart65791f12016-07-06 12:35:56 -07006146}
6147
James Smart6d368e52011-05-24 11:44:12 -04006148/**
6149 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6150 * @phba: Pointer to HBA context object.
6151 *
6152 * This function allocates all SLI4 resource identifiers.
6153 **/
6154int
6155lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6156{
6157 int i, rc, error = 0;
6158 uint16_t count, base;
6159 unsigned long longs;
6160
James Smartff78d8f2011-12-13 13:21:35 -05006161 if (!phba->sli4_hba.rpi_hdrs_in_use)
6162 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006163 if (phba->sli4_hba.extents_in_use) {
6164 /*
6165 * The port supports resource extents. The XRI, VPI, VFI, RPI
6166 * resource extent count must be read and allocated before
6167 * provisioning the resource id arrays.
6168 */
6169 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6170 LPFC_IDX_RSRC_RDY) {
6171 /*
6172 * Extent-based resources are set - the driver could
6173 * be in a port reset. Figure out if any corrective
6174 * actions need to be taken.
6175 */
6176 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6177 LPFC_RSC_TYPE_FCOE_VFI);
6178 if (rc != 0)
6179 error++;
6180 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6181 LPFC_RSC_TYPE_FCOE_VPI);
6182 if (rc != 0)
6183 error++;
6184 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6185 LPFC_RSC_TYPE_FCOE_XRI);
6186 if (rc != 0)
6187 error++;
6188 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6189 LPFC_RSC_TYPE_FCOE_RPI);
6190 if (rc != 0)
6191 error++;
6192
6193 /*
6194 * It's possible that the number of resources
6195 * provided to this port instance changed between
6196 * resets. Detect this condition and reallocate
6197 * resources. Otherwise, there is no action.
6198 */
6199 if (error) {
6200 lpfc_printf_log(phba, KERN_INFO,
6201 LOG_MBOX | LOG_INIT,
6202 "2931 Detected extent resource "
6203 "change. Reallocating all "
6204 "extents.\n");
6205 rc = lpfc_sli4_dealloc_extent(phba,
6206 LPFC_RSC_TYPE_FCOE_VFI);
6207 rc = lpfc_sli4_dealloc_extent(phba,
6208 LPFC_RSC_TYPE_FCOE_VPI);
6209 rc = lpfc_sli4_dealloc_extent(phba,
6210 LPFC_RSC_TYPE_FCOE_XRI);
6211 rc = lpfc_sli4_dealloc_extent(phba,
6212 LPFC_RSC_TYPE_FCOE_RPI);
6213 } else
6214 return 0;
6215 }
6216
6217 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6218 if (unlikely(rc))
6219 goto err_exit;
6220
6221 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6222 if (unlikely(rc))
6223 goto err_exit;
6224
6225 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6226 if (unlikely(rc))
6227 goto err_exit;
6228
6229 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6230 if (unlikely(rc))
6231 goto err_exit;
6232 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6233 LPFC_IDX_RSRC_RDY);
6234 return rc;
6235 } else {
6236 /*
6237 * The port does not support resource extents. The XRI, VPI,
6238 * VFI, RPI resource ids were determined from READ_CONFIG.
6239 * Just allocate the bitmasks and provision the resource id
6240 * arrays. If a port reset is active, the resources don't
6241 * need any action - just exit.
6242 */
6243 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006244 LPFC_IDX_RSRC_RDY) {
6245 lpfc_sli4_dealloc_resource_identifiers(phba);
6246 lpfc_sli4_remove_rpis(phba);
6247 }
James Smart6d368e52011-05-24 11:44:12 -04006248 /* RPIs. */
6249 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006250 if (count <= 0) {
6251 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6252 "3279 Invalid provisioning of "
6253 "rpi:%d\n", count);
6254 rc = -EINVAL;
6255 goto err_exit;
6256 }
James Smart6d368e52011-05-24 11:44:12 -04006257 base = phba->sli4_hba.max_cfg_param.rpi_base;
6258 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006259 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006260 sizeof(unsigned long),
6261 GFP_KERNEL);
6262 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6263 rc = -ENOMEM;
6264 goto err_exit;
6265 }
Kees Cook6396bb22018-06-12 14:03:40 -07006266 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006267 GFP_KERNEL);
6268 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6269 rc = -ENOMEM;
6270 goto free_rpi_bmask;
6271 }
6272
6273 for (i = 0; i < count; i++)
6274 phba->sli4_hba.rpi_ids[i] = base + i;
6275
6276 /* VPIs. */
6277 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006278 if (count <= 0) {
6279 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6280 "3280 Invalid provisioning of "
6281 "vpi:%d\n", count);
6282 rc = -EINVAL;
6283 goto free_rpi_ids;
6284 }
James Smart6d368e52011-05-24 11:44:12 -04006285 base = phba->sli4_hba.max_cfg_param.vpi_base;
6286 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006287 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006288 GFP_KERNEL);
6289 if (unlikely(!phba->vpi_bmask)) {
6290 rc = -ENOMEM;
6291 goto free_rpi_ids;
6292 }
Kees Cook6396bb22018-06-12 14:03:40 -07006293 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006294 GFP_KERNEL);
6295 if (unlikely(!phba->vpi_ids)) {
6296 rc = -ENOMEM;
6297 goto free_vpi_bmask;
6298 }
6299
6300 for (i = 0; i < count; i++)
6301 phba->vpi_ids[i] = base + i;
6302
6303 /* XRIs. */
6304 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006305 if (count <= 0) {
6306 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6307 "3281 Invalid provisioning of "
6308 "xri:%d\n", count);
6309 rc = -EINVAL;
6310 goto free_vpi_ids;
6311 }
James Smart6d368e52011-05-24 11:44:12 -04006312 base = phba->sli4_hba.max_cfg_param.xri_base;
6313 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006314 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006315 sizeof(unsigned long),
6316 GFP_KERNEL);
6317 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6318 rc = -ENOMEM;
6319 goto free_vpi_ids;
6320 }
James Smart41899be2012-03-01 22:34:19 -05006321 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006322 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006323 GFP_KERNEL);
6324 if (unlikely(!phba->sli4_hba.xri_ids)) {
6325 rc = -ENOMEM;
6326 goto free_xri_bmask;
6327 }
6328
6329 for (i = 0; i < count; i++)
6330 phba->sli4_hba.xri_ids[i] = base + i;
6331
6332 /* VFIs. */
6333 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006334 if (count <= 0) {
6335 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6336 "3282 Invalid provisioning of "
6337 "vfi:%d\n", count);
6338 rc = -EINVAL;
6339 goto free_xri_ids;
6340 }
James Smart6d368e52011-05-24 11:44:12 -04006341 base = phba->sli4_hba.max_cfg_param.vfi_base;
6342 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006343 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006344 sizeof(unsigned long),
6345 GFP_KERNEL);
6346 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6347 rc = -ENOMEM;
6348 goto free_xri_ids;
6349 }
Kees Cook6396bb22018-06-12 14:03:40 -07006350 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006351 GFP_KERNEL);
6352 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6353 rc = -ENOMEM;
6354 goto free_vfi_bmask;
6355 }
6356
6357 for (i = 0; i < count; i++)
6358 phba->sli4_hba.vfi_ids[i] = base + i;
6359
6360 /*
6361 * Mark all resources ready. An HBA reset doesn't need
6362 * to reset the initialization.
6363 */
6364 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6365 LPFC_IDX_RSRC_RDY);
6366 return 0;
6367 }
6368
6369 free_vfi_bmask:
6370 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006371 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006372 free_xri_ids:
6373 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006374 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006375 free_xri_bmask:
6376 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006377 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006378 free_vpi_ids:
6379 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006380 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006381 free_vpi_bmask:
6382 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006383 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006384 free_rpi_ids:
6385 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006386 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006387 free_rpi_bmask:
6388 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006389 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006390 err_exit:
6391 return rc;
6392}
6393
6394/**
6395 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6396 * @phba: Pointer to HBA context object.
6397 *
6398 * This function allocates the number of elements for the specified
6399 * resource type.
6400 **/
6401int
6402lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6403{
6404 if (phba->sli4_hba.extents_in_use) {
6405 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6406 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6407 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6408 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6409 } else {
6410 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006411 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006412 kfree(phba->vpi_ids);
6413 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6414 kfree(phba->sli4_hba.xri_bmask);
6415 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006416 kfree(phba->sli4_hba.vfi_bmask);
6417 kfree(phba->sli4_hba.vfi_ids);
6418 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6419 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6420 }
6421
6422 return 0;
6423}
6424
6425/**
James Smartb76f2dc2011-07-22 18:37:42 -04006426 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6427 * @phba: Pointer to HBA context object.
6428 * @type: The resource extent type.
6429 * @extnt_count: buffer to hold port extent count response
6430 * @extnt_size: buffer to hold port extent size response.
6431 *
6432 * This function calls the port to read the host allocated extents
6433 * for a particular type.
6434 **/
6435int
6436lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6437 uint16_t *extnt_cnt, uint16_t *extnt_size)
6438{
6439 bool emb;
6440 int rc = 0;
6441 uint16_t curr_blks = 0;
6442 uint32_t req_len, emb_len;
6443 uint32_t alloc_len, mbox_tmo;
6444 struct list_head *blk_list_head;
6445 struct lpfc_rsrc_blks *rsrc_blk;
6446 LPFC_MBOXQ_t *mbox;
6447 void *virtaddr = NULL;
6448 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6449 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6450 union lpfc_sli4_cfg_shdr *shdr;
6451
6452 switch (type) {
6453 case LPFC_RSC_TYPE_FCOE_VPI:
6454 blk_list_head = &phba->lpfc_vpi_blk_list;
6455 break;
6456 case LPFC_RSC_TYPE_FCOE_XRI:
6457 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6458 break;
6459 case LPFC_RSC_TYPE_FCOE_VFI:
6460 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6461 break;
6462 case LPFC_RSC_TYPE_FCOE_RPI:
6463 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6464 break;
6465 default:
6466 return -EIO;
6467 }
6468
6469 /* Count the number of extents currently allocatd for this type. */
6470 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6471 if (curr_blks == 0) {
6472 /*
6473 * The GET_ALLOCATED mailbox does not return the size,
6474 * just the count. The size should be just the size
6475 * stored in the current allocated block and all sizes
6476 * for an extent type are the same so set the return
6477 * value now.
6478 */
6479 *extnt_size = rsrc_blk->rsrc_size;
6480 }
6481 curr_blks++;
6482 }
6483
James Smartb76f2dc2011-07-22 18:37:42 -04006484 /*
6485 * Calculate the size of an embedded mailbox. The uint32_t
6486 * accounts for extents-specific word.
6487 */
6488 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6489 sizeof(uint32_t);
6490
6491 /*
6492 * Presume the allocation and response will fit into an embedded
6493 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6494 */
6495 emb = LPFC_SLI4_MBX_EMBED;
6496 req_len = emb_len;
6497 if (req_len > emb_len) {
6498 req_len = curr_blks * sizeof(uint16_t) +
6499 sizeof(union lpfc_sli4_cfg_shdr) +
6500 sizeof(uint32_t);
6501 emb = LPFC_SLI4_MBX_NEMBED;
6502 }
6503
6504 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6505 if (!mbox)
6506 return -ENOMEM;
6507 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6508
6509 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6510 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6511 req_len, emb);
6512 if (alloc_len < req_len) {
6513 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6514 "2983 Allocated DMA memory size (x%x) is "
6515 "less than the requested DMA memory "
6516 "size (x%x)\n", alloc_len, req_len);
6517 rc = -ENOMEM;
6518 goto err_exit;
6519 }
6520 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6521 if (unlikely(rc)) {
6522 rc = -EIO;
6523 goto err_exit;
6524 }
6525
6526 if (!phba->sli4_hba.intr_enable)
6527 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6528 else {
James Smarta183a152011-10-10 21:32:43 -04006529 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006530 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6531 }
6532
6533 if (unlikely(rc)) {
6534 rc = -EIO;
6535 goto err_exit;
6536 }
6537
6538 /*
6539 * Figure out where the response is located. Then get local pointers
6540 * to the response data. The port does not guarantee to respond to
6541 * all extents counts request so update the local variable with the
6542 * allocated count from the port.
6543 */
6544 if (emb == LPFC_SLI4_MBX_EMBED) {
6545 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6546 shdr = &rsrc_ext->header.cfg_shdr;
6547 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6548 } else {
6549 virtaddr = mbox->sge_array->addr[0];
6550 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6551 shdr = &n_rsrc->cfg_shdr;
6552 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6553 }
6554
6555 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6556 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6557 "2984 Failed to read allocated resources "
6558 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6559 type,
6560 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6561 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6562 rc = -EIO;
6563 goto err_exit;
6564 }
6565 err_exit:
6566 lpfc_sli4_mbox_cmd_free(phba, mbox);
6567 return rc;
6568}
6569
6570/**
James Smart0ef69962017-04-21 16:04:50 -07006571 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006572 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006573 * @pring: Pointer to driver SLI ring object.
6574 * @sgl_list: linked link of sgl buffers to post
6575 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006576 *
James Smart895427b2017-02-12 13:52:30 -08006577 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006578 * repost them to the port by using SGL block post. This is needed after a
6579 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006580 * of buffer sgls which contains contiguous xris and uses the non-embedded
6581 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006582 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6583 * mailbox command for posting.
6584 *
6585 * Returns: 0 = success, non-zero failure.
6586 **/
6587static int
James Smart895427b2017-02-12 13:52:30 -08006588lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6589 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006590{
6591 struct lpfc_sglq *sglq_entry = NULL;
6592 struct lpfc_sglq *sglq_entry_next = NULL;
6593 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006594 int status, total_cnt;
6595 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006596 int last_xritag = NO_XRI;
6597 LIST_HEAD(prep_sgl_list);
6598 LIST_HEAD(blck_sgl_list);
6599 LIST_HEAD(allc_sgl_list);
6600 LIST_HEAD(post_sgl_list);
6601 LIST_HEAD(free_sgl_list);
6602
James Smart38c20672013-03-01 16:37:44 -05006603 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006604 spin_lock(&phba->sli4_hba.sgl_list_lock);
6605 list_splice_init(sgl_list, &allc_sgl_list);
6606 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006607 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006608
James Smart895427b2017-02-12 13:52:30 -08006609 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006610 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6611 &allc_sgl_list, list) {
6612 list_del_init(&sglq_entry->list);
6613 block_cnt++;
6614 if ((last_xritag != NO_XRI) &&
6615 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6616 /* a hole in xri block, form a sgl posting block */
6617 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6618 post_cnt = block_cnt - 1;
6619 /* prepare list for next posting block */
6620 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6621 block_cnt = 1;
6622 } else {
6623 /* prepare list for next posting block */
6624 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6625 /* enough sgls for non-embed sgl mbox command */
6626 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6627 list_splice_init(&prep_sgl_list,
6628 &blck_sgl_list);
6629 post_cnt = block_cnt;
6630 block_cnt = 0;
6631 }
6632 }
6633 num_posted++;
6634
6635 /* keep track of last sgl's xritag */
6636 last_xritag = sglq_entry->sli4_xritag;
6637
James Smart895427b2017-02-12 13:52:30 -08006638 /* end of repost sgl list condition for buffers */
6639 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006640 if (post_cnt == 0) {
6641 list_splice_init(&prep_sgl_list,
6642 &blck_sgl_list);
6643 post_cnt = block_cnt;
6644 } else if (block_cnt == 1) {
6645 status = lpfc_sli4_post_sgl(phba,
6646 sglq_entry->phys, 0,
6647 sglq_entry->sli4_xritag);
6648 if (!status) {
6649 /* successful, put sgl to posted list */
6650 list_add_tail(&sglq_entry->list,
6651 &post_sgl_list);
6652 } else {
6653 /* Failure, put sgl to free list */
6654 lpfc_printf_log(phba, KERN_WARNING,
6655 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006656 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006657 "sgl, xritag:x%x\n",
6658 sglq_entry->sli4_xritag);
6659 list_add_tail(&sglq_entry->list,
6660 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006661 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006662 }
6663 }
6664 }
6665
6666 /* continue until a nembed page worth of sgls */
6667 if (post_cnt == 0)
6668 continue;
6669
James Smart895427b2017-02-12 13:52:30 -08006670 /* post the buffer list sgls as a block */
6671 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6672 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006673
6674 if (!status) {
6675 /* success, put sgl list to posted sgl list */
6676 list_splice_init(&blck_sgl_list, &post_sgl_list);
6677 } else {
6678 /* Failure, put sgl list to free sgl list */
6679 sglq_entry_first = list_first_entry(&blck_sgl_list,
6680 struct lpfc_sglq,
6681 list);
6682 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006683 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006684 "xritag:x%x-x%x\n",
6685 sglq_entry_first->sli4_xritag,
6686 (sglq_entry_first->sli4_xritag +
6687 post_cnt - 1));
6688 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006689 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006690 }
6691
6692 /* don't reset xirtag due to hole in xri block */
6693 if (block_cnt == 0)
6694 last_xritag = NO_XRI;
6695
James Smart895427b2017-02-12 13:52:30 -08006696 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006697 post_cnt = 0;
6698 }
6699
James Smart895427b2017-02-12 13:52:30 -08006700 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006701 lpfc_free_sgl_list(phba, &free_sgl_list);
6702
James Smart895427b2017-02-12 13:52:30 -08006703 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006704 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006705 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006706 spin_lock(&phba->sli4_hba.sgl_list_lock);
6707 list_splice_init(&post_sgl_list, sgl_list);
6708 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006709 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006710 } else {
6711 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006712 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006713 return -EIO;
6714 }
James Smart895427b2017-02-12 13:52:30 -08006715
6716 /* return the number of XRIs actually posted */
6717 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006718}
6719
James Smart61bda8f2016-10-13 15:06:05 -07006720void
6721lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6722{
6723 uint32_t len;
6724
6725 len = sizeof(struct lpfc_mbx_set_host_data) -
6726 sizeof(struct lpfc_sli4_cfg_mhdr);
6727 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6728 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6729 LPFC_SLI4_MBX_EMBED);
6730
6731 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006732 mbox->u.mqe.un.set_host_data.param_len =
6733 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006734 snprintf(mbox->u.mqe.un.set_host_data.data,
6735 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6736 "Linux %s v"LPFC_DRIVER_VERSION,
6737 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6738}
6739
James Smarta8cf5df2017-05-15 15:20:46 -07006740int
James Smart6c621a22017-05-15 15:20:45 -07006741lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07006742 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07006743{
6744 int rc, i;
6745 struct lpfc_rqe hrqe;
6746 struct lpfc_rqe drqe;
6747 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08006748 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07006749 struct rqb_dmabuf *rqb_buffer;
6750 LIST_HEAD(rqb_buf_list);
6751
James Smart411de512018-01-30 15:58:52 -08006752 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006753 rqbp = hrq->rqbp;
6754 for (i = 0; i < count; i++) {
6755 /* IF RQ is already full, don't bother */
6756 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6757 break;
6758 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6759 if (!rqb_buffer)
6760 break;
6761 rqb_buffer->hrq = hrq;
6762 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07006763 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07006764 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6765 }
6766 while (!list_empty(&rqb_buf_list)) {
6767 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6768 hbuf.list);
6769
6770 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6771 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6772 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6773 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6774 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6775 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08006776 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6777 "6421 Cannot post to HRQ %d: %x %x %x "
6778 "DRQ %x %x\n",
6779 hrq->queue_id,
6780 hrq->host_index,
6781 hrq->hba_index,
6782 hrq->entry_count,
6783 drq->host_index,
6784 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07006785 rqbp->rqb_free_buffer(phba, rqb_buffer);
6786 } else {
6787 list_add_tail(&rqb_buffer->hbuf.list,
6788 &rqbp->rqb_buffer_list);
6789 rqbp->buffer_count++;
6790 }
6791 }
James Smart411de512018-01-30 15:58:52 -08006792 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006793 return 1;
6794}
6795
James Smart8a9d2e82012-05-09 21:16:12 -04006796/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08006797 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04006798 * @phba: Pointer to HBA context object.
6799 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08006800 * This function is the main SLI4 device initialization PCI function. This
6801 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04006802 * HBA error attention handler code. Caller is not required to hold any
6803 * locks.
6804 **/
6805int
6806lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6807{
James Smart6c621a22017-05-15 15:20:45 -07006808 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04006809 LPFC_MBOXQ_t *mboxq;
6810 struct lpfc_mqe *mqe;
6811 uint8_t *vpd;
6812 uint32_t vpd_size;
6813 uint32_t ftr_rsp = 0;
6814 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6815 struct lpfc_vport *vport = phba->pport;
6816 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006817 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006818
6819 /* Perform a PCI function reset to start from clean */
6820 rc = lpfc_pci_function_reset(phba);
6821 if (unlikely(rc))
6822 return -ENODEV;
6823
6824 /* Check the HBA Host Status Register for readyness */
6825 rc = lpfc_sli4_post_status_check(phba);
6826 if (unlikely(rc))
6827 return -ENODEV;
6828 else {
6829 spin_lock_irq(&phba->hbalock);
6830 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6831 spin_unlock_irq(&phba->hbalock);
6832 }
6833
6834 /*
6835 * Allocate a single mailbox container for initializing the
6836 * port.
6837 */
6838 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6839 if (!mboxq)
6840 return -ENOMEM;
6841
James Smartda0436e2009-05-22 14:51:39 -04006842 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006843 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006844 vpd = kzalloc(vpd_size, GFP_KERNEL);
6845 if (!vpd) {
6846 rc = -ENOMEM;
6847 goto out_free_mbox;
6848 }
6849
6850 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006851 if (unlikely(rc)) {
6852 kfree(vpd);
6853 goto out_free_mbox;
6854 }
James Smart572709e2013-07-15 18:32:43 -04006855
James Smartda0436e2009-05-22 14:51:39 -04006856 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006857 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006858 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006859 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006860 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6861 } else {
James Smart76a95d72010-11-20 23:11:48 -05006862 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006863 }
James Smart45ed1192009-10-02 15:17:02 -04006864
6865 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6866 LPFC_DCBX_CEE_MODE)
6867 phba->hba_flag |= HBA_FIP_SUPPORT;
6868 else
6869 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6870
James Smart4f2e66c2012-05-09 21:17:07 -04006871 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6872
James Smartc31098c2011-04-16 11:03:33 -04006873 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006874 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6875 "0376 READ_REV Error. SLI Level %d "
6876 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006877 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006878 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006879 kfree(vpd);
6880 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006881 }
James Smartcd1c8302011-10-10 21:33:25 -04006882
6883 /*
James Smartff78d8f2011-12-13 13:21:35 -05006884 * Continue initialization with default values even if driver failed
6885 * to read FCoE param config regions, only read parameters if the
6886 * board is FCoE
6887 */
6888 if (phba->hba_flag & HBA_FCOE_MODE &&
6889 lpfc_sli4_read_fcoe_params(phba))
6890 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6891 "2570 Failed to read FCoE parameters\n");
6892
6893 /*
James Smartcd1c8302011-10-10 21:33:25 -04006894 * Retrieve sli4 device physical port name, failure of doing it
6895 * is considered as non-fatal.
6896 */
6897 rc = lpfc_sli4_retrieve_pport_name(phba);
6898 if (!rc)
6899 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6900 "3080 Successful retrieving SLI4 device "
6901 "physical port name: %s.\n", phba->Port);
6902
James Smartda0436e2009-05-22 14:51:39 -04006903 /*
6904 * Evaluate the read rev and vpd data. Populate the driver
6905 * state with the results. If this routine fails, the failure
6906 * is not fatal as the driver will use generic values.
6907 */
6908 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6909 if (unlikely(!rc)) {
6910 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6911 "0377 Error %d parsing vpd. "
6912 "Using defaults.\n", rc);
6913 rc = 0;
6914 }
James Smart76a95d72010-11-20 23:11:48 -05006915 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006916
James Smartf1126682009-06-10 17:22:44 -04006917 /* Save information as VPD data */
6918 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6919 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08006920
6921 /*
6922 * This is because first G7 ASIC doesn't support the standard
6923 * 0x5a NVME cmd descriptor type/subtype
6924 */
6925 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6926 LPFC_SLI_INTF_IF_TYPE_6) &&
6927 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
6928 (phba->vpd.rev.smRev == 0) &&
6929 (phba->cfg_nvme_embed_cmd == 1))
6930 phba->cfg_nvme_embed_cmd = 0;
6931
James Smartf1126682009-06-10 17:22:44 -04006932 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6933 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6934 &mqe->un.read_rev);
6935 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6936 &mqe->un.read_rev);
6937 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6938 &mqe->un.read_rev);
6939 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6940 &mqe->un.read_rev);
6941 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6942 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6943 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6944 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6945 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6946 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6947 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6948 "(%d):0380 READ_REV Status x%x "
6949 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6950 mboxq->vport ? mboxq->vport->vpi : 0,
6951 bf_get(lpfc_mqe_status, mqe),
6952 phba->vpd.rev.opFwName,
6953 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6954 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006955
James Smart572709e2013-07-15 18:32:43 -04006956 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6957 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6958 if (phba->pport->cfg_lun_queue_depth > rc) {
6959 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6960 "3362 LUN queue depth changed from %d to %d\n",
6961 phba->pport->cfg_lun_queue_depth, rc);
6962 phba->pport->cfg_lun_queue_depth = rc;
6963 }
6964
James Smart65791f12016-07-06 12:35:56 -07006965 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006966 LPFC_SLI_INTF_IF_TYPE_0) {
6967 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6968 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6969 if (rc == MBX_SUCCESS) {
6970 phba->hba_flag |= HBA_RECOVERABLE_UE;
6971 /* Set 1Sec interval to detect UE */
6972 phba->eratt_poll_interval = 1;
6973 phba->sli4_hba.ue_to_sr = bf_get(
6974 lpfc_mbx_set_feature_UESR,
6975 &mboxq->u.mqe.un.set_feature);
6976 phba->sli4_hba.ue_to_rp = bf_get(
6977 lpfc_mbx_set_feature_UERP,
6978 &mboxq->u.mqe.un.set_feature);
6979 }
6980 }
6981
6982 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6983 /* Enable MDS Diagnostics only if the SLI Port supports it */
6984 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6985 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6986 if (rc != MBX_SUCCESS)
6987 phba->mds_diags_support = 0;
6988 }
James Smart572709e2013-07-15 18:32:43 -04006989
James Smartda0436e2009-05-22 14:51:39 -04006990 /*
6991 * Discover the port's supported feature set and match it against the
6992 * hosts requests.
6993 */
6994 lpfc_request_features(phba, mboxq);
6995 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6996 if (unlikely(rc)) {
6997 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006998 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006999 }
7000
7001 /*
7002 * The port must support FCP initiator mode as this is the
7003 * only mode running in the host.
7004 */
7005 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7006 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7007 "0378 No support for fcpi mode.\n");
7008 ftr_rsp++;
7009 }
James Smart0bc2b7c2018-02-22 08:18:48 -08007010
7011 /* Performance Hints are ONLY for FCoE */
7012 if (phba->hba_flag & HBA_FCOE_MODE) {
7013 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7014 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7015 else
7016 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7017 }
7018
James Smartda0436e2009-05-22 14:51:39 -04007019 /*
7020 * If the port cannot support the host's requested features
7021 * then turn off the global config parameters to disable the
7022 * feature in the driver. This is not a fatal error.
7023 */
James Smartf44ac122018-03-05 12:04:08 -08007024 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7025 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7026 phba->cfg_enable_bg = 0;
7027 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04007028 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08007029 }
James Smartbf086112011-08-21 21:48:13 -04007030 }
James Smartda0436e2009-05-22 14:51:39 -04007031
7032 if (phba->max_vpi && phba->cfg_enable_npiv &&
7033 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7034 ftr_rsp++;
7035
7036 if (ftr_rsp) {
7037 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7038 "0379 Feature Mismatch Data: x%08x %08x "
7039 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7040 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7041 phba->cfg_enable_npiv, phba->max_vpi);
7042 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7043 phba->cfg_enable_bg = 0;
7044 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7045 phba->cfg_enable_npiv = 0;
7046 }
7047
7048 /* These SLI3 features are assumed in SLI4 */
7049 spin_lock_irq(&phba->hbalock);
7050 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7051 spin_unlock_irq(&phba->hbalock);
7052
James Smart6d368e52011-05-24 11:44:12 -04007053 /*
7054 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7055 * calls depends on these resources to complete port setup.
7056 */
7057 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7058 if (rc) {
7059 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7060 "2920 Failed to alloc Resource IDs "
7061 "rc = x%x\n", rc);
7062 goto out_free_mbox;
7063 }
7064
James Smart61bda8f2016-10-13 15:06:05 -07007065 lpfc_set_host_data(phba, mboxq);
7066
7067 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7068 if (rc) {
7069 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7070 "2134 Failed to set host os driver version %x",
7071 rc);
7072 }
7073
James Smartda0436e2009-05-22 14:51:39 -04007074 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007075 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7076 if (rc) {
7077 phba->link_state = LPFC_HBA_ERROR;
7078 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007079 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007080 }
7081
James Smartda0436e2009-05-22 14:51:39 -04007082 mboxq->vport = vport;
7083 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7084 mp = (struct lpfc_dmabuf *) mboxq->context1;
7085 if (rc == MBX_SUCCESS) {
7086 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7087 rc = 0;
7088 }
7089
7090 /*
7091 * This memory was allocated by the lpfc_read_sparam routine. Release
7092 * it to the mbuf pool.
7093 */
7094 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7095 kfree(mp);
7096 mboxq->context1 = NULL;
7097 if (unlikely(rc)) {
7098 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7099 "0382 READ_SPARAM command failed "
7100 "status %d, mbxStatus x%x\n",
7101 rc, bf_get(lpfc_mqe_status, mqe));
7102 phba->link_state = LPFC_HBA_ERROR;
7103 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007104 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007105 }
7106
James Smart05580562011-05-24 11:40:48 -04007107 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007108
7109 /* Update the fc_host data structures with new wwn. */
7110 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7111 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7112
James Smart895427b2017-02-12 13:52:30 -08007113 /* Create all the SLI4 queues */
7114 rc = lpfc_sli4_queue_create(phba);
7115 if (rc) {
7116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7117 "3089 Failed to allocate queues\n");
7118 rc = -ENODEV;
7119 goto out_free_mbox;
7120 }
7121 /* Set up all the queues to the device */
7122 rc = lpfc_sli4_queue_setup(phba);
7123 if (unlikely(rc)) {
7124 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7125 "0381 Error %d during queue setup.\n ", rc);
7126 goto out_stop_timers;
7127 }
7128 /* Initialize the driver internal SLI layer lists. */
7129 lpfc_sli4_setup(phba);
7130 lpfc_sli4_queue_init(phba);
7131
7132 /* update host els xri-sgl sizes and mappings */
7133 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007134 if (unlikely(rc)) {
7135 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7136 "1400 Failed to update xri-sgl size and "
7137 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007138 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007139 }
7140
James Smart8a9d2e82012-05-09 21:16:12 -04007141 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007142 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7143 phba->sli4_hba.els_xri_cnt);
7144 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007145 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7146 "0582 Error %d during els sgl post "
7147 "operation\n", rc);
7148 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007149 goto out_destroy_queue;
7150 }
7151 phba->sli4_hba.els_xri_cnt = rc;
7152
James Smartf358dd02017-02-12 13:52:34 -08007153 if (phba->nvmet_support) {
7154 /* update host nvmet xri-sgl sizes and mappings */
7155 rc = lpfc_sli4_nvmet_sgl_update(phba);
7156 if (unlikely(rc)) {
7157 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7158 "6308 Failed to update nvmet-sgl size "
7159 "and mapping: %d\n", rc);
7160 goto out_destroy_queue;
7161 }
7162
7163 /* register the nvmet sgl pool to the port */
7164 rc = lpfc_sli4_repost_sgl_list(
7165 phba,
7166 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7167 phba->sli4_hba.nvmet_xri_cnt);
7168 if (unlikely(rc < 0)) {
7169 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7170 "3117 Error %d during nvmet "
7171 "sgl post\n", rc);
7172 rc = -ENODEV;
7173 goto out_destroy_queue;
7174 }
7175 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007176
7177 cnt = phba->cfg_iocb_cnt * 1024;
7178 /* We need 1 iocbq for every SGL, for IO processing */
7179 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08007180 } else {
James Smart895427b2017-02-12 13:52:30 -08007181 /* update host scsi xri-sgl sizes and mappings */
7182 rc = lpfc_sli4_scsi_sgl_update(phba);
7183 if (unlikely(rc)) {
7184 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7185 "6309 Failed to update scsi-sgl size "
7186 "and mapping: %d\n", rc);
7187 goto out_destroy_queue;
7188 }
7189
7190 /* update host nvme xri-sgl sizes and mappings */
7191 rc = lpfc_sli4_nvme_sgl_update(phba);
7192 if (unlikely(rc)) {
7193 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7194 "6082 Failed to update nvme-sgl size "
7195 "and mapping: %d\n", rc);
7196 goto out_destroy_queue;
7197 }
James Smart6c621a22017-05-15 15:20:45 -07007198
7199 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07007200 }
7201
7202 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007203 /* Initialize and populate the iocb list per host */
7204 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007205 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07007206 phba->cfg_iocb_cnt, cnt);
7207 rc = lpfc_init_iocb_list(phba, cnt);
7208 if (rc) {
7209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007210 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007211 goto out_destroy_queue;
7212 }
James Smart8a9d2e82012-05-09 21:16:12 -04007213 }
7214
James Smart11e644e2017-06-15 22:56:48 -07007215 if (phba->nvmet_support)
7216 lpfc_nvmet_create_targetport(phba);
7217
James Smart2d7dbc42017-02-12 13:52:35 -08007218 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007219 /* Post initial buffers to all RQs created */
7220 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7221 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7222 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7223 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7224 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007225 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007226 rqbp->buffer_count = 0;
7227
James Smart2d7dbc42017-02-12 13:52:35 -08007228 lpfc_post_rq_buffer(
7229 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7230 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007231 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007232 }
7233 }
7234
James Smart895427b2017-02-12 13:52:30 -08007235 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7236 /* register the allocated scsi sgl pool to the port */
7237 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7238 if (unlikely(rc)) {
7239 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7240 "0383 Error %d during scsi sgl post "
7241 "operation\n", rc);
7242 /* Some Scsi buffers were moved to abort scsi list */
7243 /* A pci function reset will repost them */
7244 rc = -ENODEV;
7245 goto out_destroy_queue;
7246 }
James Smartda0436e2009-05-22 14:51:39 -04007247 }
7248
James Smart01649562017-02-12 13:52:32 -08007249 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7250 (phba->nvmet_support == 0)) {
7251
7252 /* register the allocated nvme sgl pool to the port */
7253 rc = lpfc_repost_nvme_sgl_list(phba);
7254 if (unlikely(rc)) {
7255 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7256 "6116 Error %d during nvme sgl post "
7257 "operation\n", rc);
7258 /* Some NVME buffers were moved to abort nvme list */
7259 /* A pci function reset will repost them */
7260 rc = -ENODEV;
7261 goto out_destroy_queue;
7262 }
James Smartda0436e2009-05-22 14:51:39 -04007263 }
7264
7265 /* Post the rpi header region to the device. */
7266 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7267 if (unlikely(rc)) {
7268 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7269 "0393 Error %d during rpi post operation\n",
7270 rc);
7271 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007272 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007273 }
James Smart97f2ecf2012-03-01 22:35:23 -05007274 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007275
James Smart895427b2017-02-12 13:52:30 -08007276 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007277 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007278 /*
7279 * The FC Port needs to register FCFI (index 0)
7280 */
7281 lpfc_reg_fcfi(phba, mboxq);
7282 mboxq->vport = phba->pport;
7283 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7284 if (rc != MBX_SUCCESS)
7285 goto out_unset_queue;
7286 rc = 0;
7287 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7288 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007289 } else {
7290 /* We are a NVME Target mode with MRQ > 1 */
7291
7292 /* First register the FCFI */
7293 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7294 mboxq->vport = phba->pport;
7295 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7296 if (rc != MBX_SUCCESS)
7297 goto out_unset_queue;
7298 rc = 0;
7299 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7300 &mboxq->u.mqe.un.reg_fcfi_mrq);
7301
7302 /* Next register the MRQs */
7303 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7304 mboxq->vport = phba->pport;
7305 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7306 if (rc != MBX_SUCCESS)
7307 goto out_unset_queue;
7308 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007309 }
7310 /* Check if the port is configured to be disabled */
7311 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007312 }
7313
7314 /* Arm the CQs and then EQs on device */
7315 lpfc_sli4_arm_cqeq_intr(phba);
7316
7317 /* Indicate device interrupt mode */
7318 phba->sli4_hba.intr_enable = 1;
7319
7320 /* Allow asynchronous mailbox command to go through */
7321 spin_lock_irq(&phba->hbalock);
7322 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7323 spin_unlock_irq(&phba->hbalock);
7324
7325 /* Post receive buffers to the device */
7326 lpfc_sli4_rb_setup(phba);
7327
James Smartfc2b9892010-02-26 14:15:29 -05007328 /* Reset HBA FCF states after HBA reset */
7329 phba->fcf.fcf_flag = 0;
7330 phba->fcf.current_rec.flag = 0;
7331
James Smartda0436e2009-05-22 14:51:39 -04007332 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007333 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007334 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007335
7336 /* Start heart beat timer */
7337 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007338 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007339 phba->hb_outstanding = 0;
7340 phba->last_completion_time = jiffies;
7341
7342 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007343 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007344 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007345
James Smart75baf692010-06-08 18:31:21 -04007346 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7347 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7348 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7349 if (!rc) {
7350 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7351 "2829 This device supports "
7352 "Advanced Error Reporting (AER)\n");
7353 spin_lock_irq(&phba->hbalock);
7354 phba->hba_flag |= HBA_AER_ENABLED;
7355 spin_unlock_irq(&phba->hbalock);
7356 } else {
7357 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7358 "2830 This device does not support "
7359 "Advanced Error Reporting (AER)\n");
7360 phba->cfg_aer_support = 0;
7361 }
James Smart0a96e972011-07-22 18:37:28 -04007362 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007363 }
7364
James Smartda0436e2009-05-22 14:51:39 -04007365 /*
7366 * The port is ready, set the host's link state to LINK_DOWN
7367 * in preparation for link interrupts.
7368 */
James Smartda0436e2009-05-22 14:51:39 -04007369 spin_lock_irq(&phba->hbalock);
7370 phba->link_state = LPFC_LINK_DOWN;
7371 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007372 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7373 (phba->hba_flag & LINK_DISABLED)) {
7374 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7375 "3103 Adapter Link is disabled.\n");
7376 lpfc_down_link(phba, mboxq);
7377 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7378 if (rc != MBX_SUCCESS) {
7379 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7380 "3104 Adapter failed to issue "
7381 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7382 goto out_unset_queue;
7383 }
7384 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007385 /* don't perform init_link on SLI4 FC port loopback test */
7386 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7387 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7388 if (rc)
7389 goto out_unset_queue;
7390 }
James Smart5350d872011-10-10 21:33:49 -04007391 }
7392 mempool_free(mboxq, phba->mbox_mem_pool);
7393 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007394out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007395 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007396 lpfc_sli4_queue_unset(phba);
7397out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007398 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007399 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007400out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007401 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007402out_free_mbox:
7403 mempool_free(mboxq, phba->mbox_mem_pool);
7404 return rc;
7405}
James Smarte59058c2008-08-24 21:49:00 -04007406
7407/**
James Smart3621a712009-04-06 18:47:14 -04007408 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007409 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007410 *
James Smarte59058c2008-08-24 21:49:00 -04007411 * This is the callback function for mailbox timer. The mailbox
7412 * timer is armed when a new mailbox command is issued and the timer
7413 * is deleted when the mailbox complete. The function is called by
7414 * the kernel timer code when a mailbox does not complete within
7415 * expected time. This function wakes up the worker thread to
7416 * process the mailbox timeout and returns. All the processing is
7417 * done by the worker thread function lpfc_mbox_timeout_handler.
7418 **/
dea31012005-04-17 16:05:31 -05007419void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007420lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007421{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007422 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007423 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007424 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007425
James Smart2e0fef82007-06-17 19:56:36 -05007426 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007427 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007428 if (!tmo_posted)
7429 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7430 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7431
James Smart5e9d9b82008-06-14 22:52:53 -04007432 if (!tmo_posted)
7433 lpfc_worker_wake_up(phba);
7434 return;
dea31012005-04-17 16:05:31 -05007435}
7436
James Smarte8d3c3b2013-10-10 12:21:30 -04007437/**
7438 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7439 * are pending
7440 * @phba: Pointer to HBA context object.
7441 *
7442 * This function checks if any mailbox completions are present on the mailbox
7443 * completion queue.
7444 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007445static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007446lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7447{
7448
7449 uint32_t idx;
7450 struct lpfc_queue *mcq;
7451 struct lpfc_mcqe *mcqe;
7452 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08007453 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04007454
7455 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7456 return false;
7457
7458 /* Check for completions on mailbox completion queue */
7459
7460 mcq = phba->sli4_hba.mbx_cq;
7461 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08007462 qe_valid = mcq->qe_valid;
7463 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
James Smarte8d3c3b2013-10-10 12:21:30 -04007464 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7465 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7466 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7467 pending_completions = true;
7468 break;
7469 }
7470 idx = (idx + 1) % mcq->entry_count;
7471 if (mcq->hba_index == idx)
7472 break;
James Smart7365f6f2018-02-22 08:18:46 -08007473
7474 /* if the index wrapped around, toggle the valid bit */
7475 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7476 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04007477 }
7478 return pending_completions;
7479
7480}
7481
7482/**
7483 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7484 * that were missed.
7485 * @phba: Pointer to HBA context object.
7486 *
7487 * For sli4, it is possible to miss an interrupt. As such mbox completions
7488 * maybe missed causing erroneous mailbox timeouts to occur. This function
7489 * checks to see if mbox completions are on the mailbox completion queue
7490 * and will process all the completions associated with the eq for the
7491 * mailbox completion queue.
7492 **/
7493bool
7494lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7495{
James Smartb71413d2018-02-22 08:18:40 -08007496 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04007497 uint32_t eqidx;
7498 struct lpfc_queue *fpeq = NULL;
7499 struct lpfc_eqe *eqe;
7500 bool mbox_pending;
7501
7502 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7503 return false;
7504
7505 /* Find the eq associated with the mcq */
7506
James Smartb71413d2018-02-22 08:18:40 -08007507 if (sli4_hba->hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007508 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smartb71413d2018-02-22 08:18:40 -08007509 if (sli4_hba->hba_eq[eqidx]->queue_id ==
7510 sli4_hba->mbx_cq->assoc_qid) {
7511 fpeq = sli4_hba->hba_eq[eqidx];
James Smarte8d3c3b2013-10-10 12:21:30 -04007512 break;
7513 }
7514 if (!fpeq)
7515 return false;
7516
7517 /* Turn off interrupts from this EQ */
7518
James Smartb71413d2018-02-22 08:18:40 -08007519 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04007520
7521 /* Check to see if a mbox completion is pending */
7522
7523 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7524
7525 /*
7526 * If a mbox completion is pending, process all the events on EQ
7527 * associated with the mbox completion queue (this could include
7528 * mailbox commands, async events, els commands, receive queue data
7529 * and fcp commands)
7530 */
7531
7532 if (mbox_pending)
7533 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7534 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7535 fpeq->EQ_processed++;
7536 }
7537
7538 /* Always clear and re-arm the EQ */
7539
James Smartb71413d2018-02-22 08:18:40 -08007540 sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04007541
7542 return mbox_pending;
7543
7544}
James Smarte59058c2008-08-24 21:49:00 -04007545
7546/**
James Smart3621a712009-04-06 18:47:14 -04007547 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007548 * @phba: Pointer to HBA context object.
7549 *
7550 * This function is called from worker thread when a mailbox command times out.
7551 * The caller is not required to hold any locks. This function will reset the
7552 * HBA and recover all the pending commands.
7553 **/
dea31012005-04-17 16:05:31 -05007554void
7555lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7556{
James Smart2e0fef82007-06-17 19:56:36 -05007557 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007558 MAILBOX_t *mb = NULL;
7559
James Smart1dcb58e2007-04-25 09:51:30 -04007560 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007561
James Smarte8d3c3b2013-10-10 12:21:30 -04007562 /* If the mailbox completed, process the completion and return */
7563 if (lpfc_sli4_process_missed_mbox_completions(phba))
7564 return;
7565
James Smarteb016562014-09-03 12:58:06 -04007566 if (pmbox != NULL)
7567 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007568 /* Check the pmbox pointer first. There is a race condition
7569 * between the mbox timeout handler getting executed in the
7570 * worklist and the mailbox actually completing. When this
7571 * race condition occurs, the mbox_active will be NULL.
7572 */
7573 spin_lock_irq(&phba->hbalock);
7574 if (pmbox == NULL) {
7575 lpfc_printf_log(phba, KERN_WARNING,
7576 LOG_MBOX | LOG_SLI,
7577 "0353 Active Mailbox cleared - mailbox timeout "
7578 "exiting\n");
7579 spin_unlock_irq(&phba->hbalock);
7580 return;
7581 }
7582
dea31012005-04-17 16:05:31 -05007583 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007584 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007585 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007586 mb->mbxCommand,
7587 phba->pport->port_state,
7588 phba->sli.sli_flag,
7589 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007590 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007591
James Smart1dcb58e2007-04-25 09:51:30 -04007592 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7593 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007594 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007595 */
James Smart2e0fef82007-06-17 19:56:36 -05007596 spin_lock_irq(&phba->pport->work_port_lock);
7597 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7598 spin_unlock_irq(&phba->pport->work_port_lock);
7599 spin_lock_irq(&phba->hbalock);
7600 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007601 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007602 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007603
James Smartdb55fba2014-04-04 13:52:02 -04007604 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007605
7606 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007607 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007608
7609 /* Reset the HBA device */
7610 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007611}
7612
James Smarte59058c2008-08-24 21:49:00 -04007613/**
James Smart3772a992009-05-22 14:50:54 -04007614 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007615 * @phba: Pointer to HBA context object.
7616 * @pmbox: Pointer to mailbox object.
7617 * @flag: Flag indicating how the mailbox need to be processed.
7618 *
7619 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007620 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7621 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007622 * The mailbox command can be submitted in polling mode, in which case
7623 * this function will wait in a polling loop for the completion of the
7624 * mailbox.
7625 * If the mailbox is submitted in no_wait mode (not polling) the
7626 * function will submit the command and returns immediately without waiting
7627 * for the mailbox completion. The no_wait is supported only when HBA
7628 * is in SLI2/SLI3 mode - interrupts are enabled.
7629 * The SLI interface allows only one mailbox pending at a time. If the
7630 * mailbox is issued in polling mode and there is already a mailbox
7631 * pending, then the function will return an error. If the mailbox is issued
7632 * in NO_WAIT mode and there is a mailbox pending already, the function
7633 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7634 * The sli layer owns the mailbox object until the completion of mailbox
7635 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7636 * return codes the caller owns the mailbox command after the return of
7637 * the function.
7638 **/
James Smart3772a992009-05-22 14:50:54 -04007639static int
7640lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7641 uint32_t flag)
dea31012005-04-17 16:05:31 -05007642{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007643 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007644 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007645 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007646 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007647 int i;
James Smart09372822008-01-11 01:52:54 -05007648 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007649 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007650 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007651 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007652 int processing_queue = 0;
7653
7654 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7655 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007656 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007657 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007658 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7659 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7660 return MBX_SUCCESS;
7661 }
James Smart58da1ff2008-04-07 10:15:56 -04007662 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007663 pmbox = lpfc_mbox_get(phba);
7664 if (!pmbox) {
7665 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7666 return MBX_SUCCESS;
7667 }
7668 }
dea31012005-04-17 16:05:31 -05007669
James Smarted957682007-06-17 19:56:37 -05007670 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007671 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007672 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007673 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007674 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007675 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007676 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007677 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007678 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007679 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007680 }
7681 }
7682
Linas Vepstas8d63f372007-02-14 14:28:36 -06007683 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007684 if (unlikely(pci_channel_offline(phba->pcidev))) {
7685 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7686 goto out_not_finished;
7687 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007688
James Smarta257bf92009-04-06 18:48:10 -04007689 /* If HBA has a deferred error attention, fail the iocb. */
7690 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7691 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7692 goto out_not_finished;
7693 }
7694
dea31012005-04-17 16:05:31 -05007695 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007696
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007697 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007698 status = MBX_SUCCESS;
7699
James Smart2e0fef82007-06-17 19:56:36 -05007700 if (phba->link_state == LPFC_HBA_ERROR) {
7701 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007702
7703 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007704 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7705 "(%d):0311 Mailbox command x%x cannot "
7706 "issue Data: x%x x%x\n",
7707 pmbox->vport ? pmbox->vport->vpi : 0,
7708 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007709 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007710 }
7711
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007712 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007713 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7714 !(hc_copy & HC_MBINT_ENA)) {
7715 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7716 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007717 "(%d):2528 Mailbox command x%x cannot "
7718 "issue Data: x%x x%x\n",
7719 pmbox->vport ? pmbox->vport->vpi : 0,
7720 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007721 goto out_not_finished;
7722 }
James Smart92908312006-03-07 15:04:13 -05007723 }
7724
dea31012005-04-17 16:05:31 -05007725 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7726 /* Polling for a mbox command when another one is already active
7727 * is not allowed in SLI. Also, the driver must have established
7728 * SLI2 mode to queue and process multiple mbox commands.
7729 */
7730
7731 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007732 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007733
7734 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007735 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7736 "(%d):2529 Mailbox command x%x "
7737 "cannot issue Data: x%x x%x\n",
7738 pmbox->vport ? pmbox->vport->vpi : 0,
7739 pmbox->u.mb.mbxCommand,
7740 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007741 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007742 }
7743
James Smart3772a992009-05-22 14:50:54 -04007744 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007745 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007746 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007747 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7748 "(%d):2530 Mailbox command x%x "
7749 "cannot issue Data: x%x x%x\n",
7750 pmbox->vport ? pmbox->vport->vpi : 0,
7751 pmbox->u.mb.mbxCommand,
7752 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007753 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007754 }
7755
dea31012005-04-17 16:05:31 -05007756 /* Another mailbox command is still being processed, queue this
7757 * command to be processed later.
7758 */
7759 lpfc_mbox_put(phba, pmbox);
7760
7761 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007762 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007763 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007764 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007765 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07007766 mbx->mbxCommand,
7767 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007768 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007769
7770 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007771 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007772
James Smart858c9f62007-06-17 19:56:39 -05007773 if (pmbox->vport) {
7774 lpfc_debugfs_disc_trc(pmbox->vport,
7775 LPFC_DISC_TRC_MBOX_VPORT,
7776 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007777 (uint32_t)mbx->mbxCommand,
7778 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007779 }
7780 else {
7781 lpfc_debugfs_disc_trc(phba->pport,
7782 LPFC_DISC_TRC_MBOX,
7783 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007784 (uint32_t)mbx->mbxCommand,
7785 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007786 }
7787
James Smart2e0fef82007-06-17 19:56:36 -05007788 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007789 }
7790
dea31012005-04-17 16:05:31 -05007791 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7792
7793 /* If we are not polling, we MUST be in SLI2 mode */
7794 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007795 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007796 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007797 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007798 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007799 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007800 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7801 "(%d):2531 Mailbox command x%x "
7802 "cannot issue Data: x%x x%x\n",
7803 pmbox->vport ? pmbox->vport->vpi : 0,
7804 pmbox->u.mb.mbxCommand,
7805 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007806 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007807 }
7808 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007809 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7810 1000);
7811 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007812 }
7813
7814 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007815 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007816 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007817 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007818 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07007819 mbx->mbxCommand,
7820 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007821 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007822
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007823 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007824 if (pmbox->vport) {
7825 lpfc_debugfs_disc_trc(pmbox->vport,
7826 LPFC_DISC_TRC_MBOX_VPORT,
7827 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007828 (uint32_t)mbx->mbxCommand,
7829 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007830 }
7831 else {
7832 lpfc_debugfs_disc_trc(phba->pport,
7833 LPFC_DISC_TRC_MBOX,
7834 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007835 (uint32_t)mbx->mbxCommand,
7836 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007837 }
7838 }
7839
dea31012005-04-17 16:05:31 -05007840 psli->slistat.mbox_cmd++;
7841 evtctr = psli->slistat.mbox_event;
7842
7843 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007844 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007845
James Smart3772a992009-05-22 14:50:54 -04007846 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007847 /* Populate mbox extension offset word. */
7848 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007849 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007850 = (uint8_t *)phba->mbox_ext
7851 - (uint8_t *)phba->mbox;
7852 }
7853
7854 /* Copy the mailbox extension data */
7855 if (pmbox->in_ext_byte_len && pmbox->context2) {
7856 lpfc_sli_pcimem_bcopy(pmbox->context2,
7857 (uint8_t *)phba->mbox_ext,
7858 pmbox->in_ext_byte_len);
7859 }
7860 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007861 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007862 } else {
James Smart7a470272010-03-15 11:25:20 -04007863 /* Populate mbox extension offset word. */
7864 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007865 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007866 = MAILBOX_HBA_EXT_OFFSET;
7867
7868 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007869 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007870 lpfc_memcpy_to_slim(phba->MBslimaddr +
7871 MAILBOX_HBA_EXT_OFFSET,
7872 pmbox->context2, pmbox->in_ext_byte_len);
7873
James Smart895427b2017-02-12 13:52:30 -08007874 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007875 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007876 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7877 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007878
7879 /* First copy mbox command data to HBA SLIM, skip past first
7880 word */
7881 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007882 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007883 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7884
7885 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007886 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007887 to_slim = phba->MBslimaddr;
7888 writel(ldata, to_slim);
7889 readl(to_slim); /* flush */
7890
James Smart895427b2017-02-12 13:52:30 -08007891 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007892 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007893 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007894 }
7895
7896 wmb();
dea31012005-04-17 16:05:31 -05007897
7898 switch (flag) {
7899 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007900 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007901 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007902 /* Interrupt board to do it */
7903 writel(CA_MBATT, phba->CAregaddr);
7904 readl(phba->CAregaddr); /* flush */
7905 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007906 break;
7907
7908 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007909 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007910 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007911 /* Interrupt board to do it */
7912 writel(CA_MBATT, phba->CAregaddr);
7913 readl(phba->CAregaddr); /* flush */
7914
James Smart3772a992009-05-22 14:50:54 -04007915 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007916 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007917 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007918 word0 = le32_to_cpu(word0);
7919 } else {
7920 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007921 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7922 spin_unlock_irqrestore(&phba->hbalock,
7923 drvr_flag);
7924 goto out_not_finished;
7925 }
dea31012005-04-17 16:05:31 -05007926 }
7927
7928 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007929 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7930 spin_unlock_irqrestore(&phba->hbalock,
7931 drvr_flag);
7932 goto out_not_finished;
7933 }
James Smarta183a152011-10-10 21:32:43 -04007934 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7935 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007936 i = 0;
dea31012005-04-17 16:05:31 -05007937 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007938 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7939 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007940 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007941 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007942 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007943 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007944 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007945 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007946 }
7947
7948 /* Check if we took a mbox interrupt while we were
7949 polling */
7950 if (((word0 & OWN_CHIP) != OWN_CHIP)
7951 && (evtctr != psli->slistat.mbox_event))
7952 break;
7953
James Smart09372822008-01-11 01:52:54 -05007954 if (i++ > 10) {
7955 spin_unlock_irqrestore(&phba->hbalock,
7956 drvr_flag);
7957 msleep(1);
7958 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7959 }
dea31012005-04-17 16:05:31 -05007960
James Smart3772a992009-05-22 14:50:54 -04007961 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007962 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007963 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007964 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007965 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007966 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007967 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007968 /* Check real SLIM for any errors */
7969 slimword0 = readl(phba->MBslimaddr);
7970 slimmb = (MAILBOX_t *) & slimword0;
7971 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7972 && slimmb->mbxStatus) {
7973 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007974 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007975 word0 = slimword0;
7976 }
7977 }
7978 } else {
7979 /* First copy command data */
7980 word0 = readl(phba->MBslimaddr);
7981 }
7982 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007983 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7984 spin_unlock_irqrestore(&phba->hbalock,
7985 drvr_flag);
7986 goto out_not_finished;
7987 }
dea31012005-04-17 16:05:31 -05007988 }
7989
James Smart3772a992009-05-22 14:50:54 -04007990 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007991 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007992 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7993 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007994 /* Copy the mailbox extension data */
7995 if (pmbox->out_ext_byte_len && pmbox->context2) {
7996 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7997 pmbox->context2,
7998 pmbox->out_ext_byte_len);
7999 }
dea31012005-04-17 16:05:31 -05008000 } else {
8001 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008002 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08008003 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008004 /* Copy the mailbox extension data */
8005 if (pmbox->out_ext_byte_len && pmbox->context2) {
8006 lpfc_memcpy_from_slim(pmbox->context2,
8007 phba->MBslimaddr +
8008 MAILBOX_HBA_EXT_OFFSET,
8009 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05008010 }
8011 }
8012
8013 writel(HA_MBATT, phba->HAregaddr);
8014 readl(phba->HAregaddr); /* flush */
8015
8016 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008017 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05008018 }
8019
James Smart2e0fef82007-06-17 19:56:36 -05008020 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8021 return status;
James Smart58da1ff2008-04-07 10:15:56 -04008022
8023out_not_finished:
8024 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04008025 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04008026 lpfc_mbox_cmpl_put(phba, pmbox);
8027 }
8028 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008029}
8030
James Smarte59058c2008-08-24 21:49:00 -04008031/**
James Smartf1126682009-06-10 17:22:44 -04008032 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8033 * @phba: Pointer to HBA context object.
8034 *
8035 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8036 * the driver internal pending mailbox queue. It will then try to wait out the
8037 * possible outstanding mailbox command before return.
8038 *
8039 * Returns:
8040 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8041 * the outstanding mailbox command timed out.
8042 **/
8043static int
8044lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8045{
8046 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008047 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008048 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008049
8050 /* Mark the asynchronous mailbox command posting as blocked */
8051 spin_lock_irq(&phba->hbalock);
8052 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008053 /* Determine how long we might wait for the active mailbox
8054 * command to be gracefully completed by firmware.
8055 */
James Smarta183a152011-10-10 21:32:43 -04008056 if (phba->sli.mbox_active)
8057 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8058 phba->sli.mbox_active) *
8059 1000) + jiffies;
8060 spin_unlock_irq(&phba->hbalock);
8061
James Smarte8d3c3b2013-10-10 12:21:30 -04008062 /* Make sure the mailbox is really active */
8063 if (timeout)
8064 lpfc_sli4_process_missed_mbox_completions(phba);
8065
James Smartf1126682009-06-10 17:22:44 -04008066 /* Wait for the outstnading mailbox command to complete */
8067 while (phba->sli.mbox_active) {
8068 /* Check active mailbox complete status every 2ms */
8069 msleep(2);
8070 if (time_after(jiffies, timeout)) {
8071 /* Timeout, marked the outstanding cmd not complete */
8072 rc = 1;
8073 break;
8074 }
8075 }
8076
8077 /* Can not cleanly block async mailbox command, fails it */
8078 if (rc) {
8079 spin_lock_irq(&phba->hbalock);
8080 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8081 spin_unlock_irq(&phba->hbalock);
8082 }
8083 return rc;
8084}
8085
8086/**
8087 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8088 * @phba: Pointer to HBA context object.
8089 *
8090 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8091 * commands from the driver internal pending mailbox queue. It makes sure
8092 * that there is no outstanding mailbox command before resuming posting
8093 * asynchronous mailbox commands. If, for any reason, there is outstanding
8094 * mailbox command, it will try to wait it out before resuming asynchronous
8095 * mailbox command posting.
8096 **/
8097static void
8098lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8099{
8100 struct lpfc_sli *psli = &phba->sli;
8101
8102 spin_lock_irq(&phba->hbalock);
8103 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8104 /* Asynchronous mailbox posting is not blocked, do nothing */
8105 spin_unlock_irq(&phba->hbalock);
8106 return;
8107 }
8108
8109 /* Outstanding synchronous mailbox command is guaranteed to be done,
8110 * successful or timeout, after timing-out the outstanding mailbox
8111 * command shall always be removed, so just unblock posting async
8112 * mailbox command and resume
8113 */
8114 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8115 spin_unlock_irq(&phba->hbalock);
8116
8117 /* wake up worker thread to post asynchronlous mailbox command */
8118 lpfc_worker_wake_up(phba);
8119}
8120
8121/**
James Smart2d843ed2012-09-29 11:29:06 -04008122 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8123 * @phba: Pointer to HBA context object.
8124 * @mboxq: Pointer to mailbox object.
8125 *
8126 * The function waits for the bootstrap mailbox register ready bit from
8127 * port for twice the regular mailbox command timeout value.
8128 *
8129 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8130 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8131 **/
8132static int
8133lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8134{
8135 uint32_t db_ready;
8136 unsigned long timeout;
8137 struct lpfc_register bmbx_reg;
8138
8139 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8140 * 1000) + jiffies;
8141
8142 do {
8143 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8144 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8145 if (!db_ready)
8146 msleep(2);
8147
8148 if (time_after(jiffies, timeout))
8149 return MBXERR_ERROR;
8150 } while (!db_ready);
8151
8152 return 0;
8153}
8154
8155/**
James Smartda0436e2009-05-22 14:51:39 -04008156 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8157 * @phba: Pointer to HBA context object.
8158 * @mboxq: Pointer to mailbox object.
8159 *
8160 * The function posts a mailbox to the port. The mailbox is expected
8161 * to be comletely filled in and ready for the port to operate on it.
8162 * This routine executes a synchronous completion operation on the
8163 * mailbox by polling for its completion.
8164 *
8165 * The caller must not be holding any locks when calling this routine.
8166 *
8167 * Returns:
8168 * MBX_SUCCESS - mailbox posted successfully
8169 * Any of the MBX error values.
8170 **/
8171static int
8172lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8173{
8174 int rc = MBX_SUCCESS;
8175 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008176 uint32_t mcqe_status;
8177 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008178 struct lpfc_sli *psli = &phba->sli;
8179 struct lpfc_mqe *mb = &mboxq->u.mqe;
8180 struct lpfc_bmbx_create *mbox_rgn;
8181 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008182
8183 /*
8184 * Only one mailbox can be active to the bootstrap mailbox region
8185 * at a time and there is no queueing provided.
8186 */
8187 spin_lock_irqsave(&phba->hbalock, iflag);
8188 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8189 spin_unlock_irqrestore(&phba->hbalock, iflag);
8190 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008191 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008192 "cannot issue Data: x%x x%x\n",
8193 mboxq->vport ? mboxq->vport->vpi : 0,
8194 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008195 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8196 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008197 psli->sli_flag, MBX_POLL);
8198 return MBXERR_ERROR;
8199 }
8200 /* The server grabs the token and owns it until release */
8201 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8202 phba->sli.mbox_active = mboxq;
8203 spin_unlock_irqrestore(&phba->hbalock, iflag);
8204
James Smart2d843ed2012-09-29 11:29:06 -04008205 /* wait for bootstrap mbox register for readyness */
8206 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8207 if (rc)
8208 goto exit;
8209
James Smartda0436e2009-05-22 14:51:39 -04008210 /*
8211 * Initialize the bootstrap memory region to avoid stale data areas
8212 * in the mailbox post. Then copy the caller's mailbox contents to
8213 * the bmbx mailbox region.
8214 */
8215 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8216 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07008217 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8218 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008219
8220 /* Post the high mailbox dma address to the port and wait for ready. */
8221 dma_address = &phba->sli4_hba.bmbx.dma_address;
8222 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8223
James Smart2d843ed2012-09-29 11:29:06 -04008224 /* wait for bootstrap mbox register for hi-address write done */
8225 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8226 if (rc)
8227 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008228
8229 /* Post the low mailbox dma address to the port. */
8230 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008231
James Smart2d843ed2012-09-29 11:29:06 -04008232 /* wait for bootstrap mbox register for low address write done */
8233 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8234 if (rc)
8235 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008236
8237 /*
8238 * Read the CQ to ensure the mailbox has completed.
8239 * If so, update the mailbox status so that the upper layers
8240 * can complete the request normally.
8241 */
James Smart48f8fdb2018-05-04 20:37:51 -07008242 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8243 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008244 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07008245 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8246 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04008247 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008248 /*
8249 * When the CQE status indicates a failure and the mailbox status
8250 * indicates success then copy the CQE status into the mailbox status
8251 * (and prefix it with x4000).
8252 */
James Smartda0436e2009-05-22 14:51:39 -04008253 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008254 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8255 bf_set(lpfc_mqe_status, mb,
8256 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008257 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008258 } else
8259 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008260
8261 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008262 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008263 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8264 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008265 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8266 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8267 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008268 bf_get(lpfc_mqe_status, mb),
8269 mb->un.mb_words[0], mb->un.mb_words[1],
8270 mb->un.mb_words[2], mb->un.mb_words[3],
8271 mb->un.mb_words[4], mb->un.mb_words[5],
8272 mb->un.mb_words[6], mb->un.mb_words[7],
8273 mb->un.mb_words[8], mb->un.mb_words[9],
8274 mb->un.mb_words[10], mb->un.mb_words[11],
8275 mb->un.mb_words[12], mboxq->mcqe.word0,
8276 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8277 mboxq->mcqe.trailer);
8278exit:
8279 /* We are holding the token, no needed for lock when release */
8280 spin_lock_irqsave(&phba->hbalock, iflag);
8281 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8282 phba->sli.mbox_active = NULL;
8283 spin_unlock_irqrestore(&phba->hbalock, iflag);
8284 return rc;
8285}
8286
8287/**
8288 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8289 * @phba: Pointer to HBA context object.
8290 * @pmbox: Pointer to mailbox object.
8291 * @flag: Flag indicating how the mailbox need to be processed.
8292 *
8293 * This function is called by discovery code and HBA management code to submit
8294 * a mailbox command to firmware with SLI-4 interface spec.
8295 *
8296 * Return codes the caller owns the mailbox command after the return of the
8297 * function.
8298 **/
8299static int
8300lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8301 uint32_t flag)
8302{
8303 struct lpfc_sli *psli = &phba->sli;
8304 unsigned long iflags;
8305 int rc;
8306
James Smartb76f2dc2011-07-22 18:37:42 -04008307 /* dump from issue mailbox command if setup */
8308 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8309
James Smart8fa38512009-07-19 10:01:03 -04008310 rc = lpfc_mbox_dev_check(phba);
8311 if (unlikely(rc)) {
8312 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008313 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008314 "cannot issue Data: x%x x%x\n",
8315 mboxq->vport ? mboxq->vport->vpi : 0,
8316 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008317 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8318 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008319 psli->sli_flag, flag);
8320 goto out_not_finished;
8321 }
8322
James Smartda0436e2009-05-22 14:51:39 -04008323 /* Detect polling mode and jump to a handler */
8324 if (!phba->sli4_hba.intr_enable) {
8325 if (flag == MBX_POLL)
8326 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8327 else
8328 rc = -EIO;
8329 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008330 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008331 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008332 "(x%x/x%x) failure: "
8333 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8334 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008335 mboxq->vport ? mboxq->vport->vpi : 0,
8336 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008337 lpfc_sli_config_mbox_subsys_get(phba,
8338 mboxq),
8339 lpfc_sli_config_mbox_opcode_get(phba,
8340 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008341 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8342 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8343 bf_get(lpfc_mcqe_ext_status,
8344 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008345 psli->sli_flag, flag);
8346 return rc;
8347 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008348 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8349 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008350 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008351 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008352 mboxq->vport ? mboxq->vport->vpi : 0,
8353 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008354 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8355 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008356 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008357 /* Try to block the asynchronous mailbox posting */
8358 rc = lpfc_sli4_async_mbox_block(phba);
8359 if (!rc) {
8360 /* Successfully blocked, now issue sync mbox cmd */
8361 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8362 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008363 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008364 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008365 "(%d):2597 Sync Mailbox command "
8366 "x%x (x%x/x%x) failure: "
8367 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8368 "Data: x%x x%x\n,",
8369 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008370 mboxq->u.mb.mbxCommand,
8371 lpfc_sli_config_mbox_subsys_get(phba,
8372 mboxq),
8373 lpfc_sli_config_mbox_opcode_get(phba,
8374 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008375 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8376 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8377 bf_get(lpfc_mcqe_ext_status,
8378 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008379 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008380 /* Unblock the async mailbox posting afterward */
8381 lpfc_sli4_async_mbox_unblock(phba);
8382 }
8383 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008384 }
8385
8386 /* Now, interrupt mode asynchrous mailbox command */
8387 rc = lpfc_mbox_cmd_check(phba, mboxq);
8388 if (rc) {
8389 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008390 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008391 "cannot issue Data: x%x x%x\n",
8392 mboxq->vport ? mboxq->vport->vpi : 0,
8393 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008394 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8395 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008396 psli->sli_flag, flag);
8397 goto out_not_finished;
8398 }
James Smartda0436e2009-05-22 14:51:39 -04008399
8400 /* Put the mailbox command to the driver internal FIFO */
8401 psli->slistat.mbox_busy++;
8402 spin_lock_irqsave(&phba->hbalock, iflags);
8403 lpfc_mbox_put(phba, mboxq);
8404 spin_unlock_irqrestore(&phba->hbalock, iflags);
8405 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8406 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008407 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008408 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8409 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008410 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8411 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008412 phba->pport->port_state,
8413 psli->sli_flag, MBX_NOWAIT);
8414 /* Wake up worker thread to transport mailbox command from head */
8415 lpfc_worker_wake_up(phba);
8416
8417 return MBX_BUSY;
8418
8419out_not_finished:
8420 return MBX_NOT_FINISHED;
8421}
8422
8423/**
8424 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8425 * @phba: Pointer to HBA context object.
8426 *
8427 * This function is called by worker thread to send a mailbox command to
8428 * SLI4 HBA firmware.
8429 *
8430 **/
8431int
8432lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8433{
8434 struct lpfc_sli *psli = &phba->sli;
8435 LPFC_MBOXQ_t *mboxq;
8436 int rc = MBX_SUCCESS;
8437 unsigned long iflags;
8438 struct lpfc_mqe *mqe;
8439 uint32_t mbx_cmnd;
8440
8441 /* Check interrupt mode before post async mailbox command */
8442 if (unlikely(!phba->sli4_hba.intr_enable))
8443 return MBX_NOT_FINISHED;
8444
8445 /* Check for mailbox command service token */
8446 spin_lock_irqsave(&phba->hbalock, iflags);
8447 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8448 spin_unlock_irqrestore(&phba->hbalock, iflags);
8449 return MBX_NOT_FINISHED;
8450 }
8451 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8452 spin_unlock_irqrestore(&phba->hbalock, iflags);
8453 return MBX_NOT_FINISHED;
8454 }
8455 if (unlikely(phba->sli.mbox_active)) {
8456 spin_unlock_irqrestore(&phba->hbalock, iflags);
8457 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8458 "0384 There is pending active mailbox cmd\n");
8459 return MBX_NOT_FINISHED;
8460 }
8461 /* Take the mailbox command service token */
8462 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8463
8464 /* Get the next mailbox command from head of queue */
8465 mboxq = lpfc_mbox_get(phba);
8466
8467 /* If no more mailbox command waiting for post, we're done */
8468 if (!mboxq) {
8469 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8470 spin_unlock_irqrestore(&phba->hbalock, iflags);
8471 return MBX_SUCCESS;
8472 }
8473 phba->sli.mbox_active = mboxq;
8474 spin_unlock_irqrestore(&phba->hbalock, iflags);
8475
8476 /* Check device readiness for posting mailbox command */
8477 rc = lpfc_mbox_dev_check(phba);
8478 if (unlikely(rc))
8479 /* Driver clean routine will clean up pending mailbox */
8480 goto out_not_finished;
8481
8482 /* Prepare the mbox command to be posted */
8483 mqe = &mboxq->u.mqe;
8484 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8485
8486 /* Start timer for the mbox_tmo and log some mailbox post messages */
8487 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008488 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008489
8490 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008491 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008492 "x%x x%x\n",
8493 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008494 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8495 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008496 phba->pport->port_state, psli->sli_flag);
8497
8498 if (mbx_cmnd != MBX_HEARTBEAT) {
8499 if (mboxq->vport) {
8500 lpfc_debugfs_disc_trc(mboxq->vport,
8501 LPFC_DISC_TRC_MBOX_VPORT,
8502 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8503 mbx_cmnd, mqe->un.mb_words[0],
8504 mqe->un.mb_words[1]);
8505 } else {
8506 lpfc_debugfs_disc_trc(phba->pport,
8507 LPFC_DISC_TRC_MBOX,
8508 "MBOX Send: cmd:x%x mb:x%x x%x",
8509 mbx_cmnd, mqe->un.mb_words[0],
8510 mqe->un.mb_words[1]);
8511 }
8512 }
8513 psli->slistat.mbox_cmd++;
8514
8515 /* Post the mailbox command to the port */
8516 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8517 if (rc != MBX_SUCCESS) {
8518 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008519 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008520 "cannot issue Data: x%x x%x\n",
8521 mboxq->vport ? mboxq->vport->vpi : 0,
8522 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008523 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8524 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008525 psli->sli_flag, MBX_NOWAIT);
8526 goto out_not_finished;
8527 }
8528
8529 return rc;
8530
8531out_not_finished:
8532 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008533 if (phba->sli.mbox_active) {
8534 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8535 __lpfc_mbox_cmpl_put(phba, mboxq);
8536 /* Release the token */
8537 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8538 phba->sli.mbox_active = NULL;
8539 }
James Smartda0436e2009-05-22 14:51:39 -04008540 spin_unlock_irqrestore(&phba->hbalock, iflags);
8541
8542 return MBX_NOT_FINISHED;
8543}
8544
8545/**
8546 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8547 * @phba: Pointer to HBA context object.
8548 * @pmbox: Pointer to mailbox object.
8549 * @flag: Flag indicating how the mailbox need to be processed.
8550 *
8551 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8552 * the API jump table function pointer from the lpfc_hba struct.
8553 *
8554 * Return codes the caller owns the mailbox command after the return of the
8555 * function.
8556 **/
8557int
8558lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8559{
8560 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8561}
8562
8563/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008564 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008565 * @phba: The hba struct for which this call is being executed.
8566 * @dev_grp: The HBA PCI-Device group number.
8567 *
8568 * This routine sets up the mbox interface API function jump table in @phba
8569 * struct.
8570 * Returns: 0 - success, -ENODEV - failure.
8571 **/
8572int
8573lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8574{
8575
8576 switch (dev_grp) {
8577 case LPFC_PCI_DEV_LP:
8578 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8579 phba->lpfc_sli_handle_slow_ring_event =
8580 lpfc_sli_handle_slow_ring_event_s3;
8581 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8582 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8583 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8584 break;
8585 case LPFC_PCI_DEV_OC:
8586 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8587 phba->lpfc_sli_handle_slow_ring_event =
8588 lpfc_sli_handle_slow_ring_event_s4;
8589 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8590 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8591 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8592 break;
8593 default:
8594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8595 "1420 Invalid HBA PCI-device group: 0x%x\n",
8596 dev_grp);
8597 return -ENODEV;
8598 break;
8599 }
8600 return 0;
8601}
8602
8603/**
James Smart3621a712009-04-06 18:47:14 -04008604 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008605 * @phba: Pointer to HBA context object.
8606 * @pring: Pointer to driver SLI ring object.
8607 * @piocb: Pointer to address of newly added command iocb.
8608 *
8609 * This function is called with hbalock held to add a command
8610 * iocb to the txq when SLI layer cannot submit the command iocb
8611 * to the ring.
8612 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008613void
James Smart92d7f7b2007-06-17 19:56:38 -05008614__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008615 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008616{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008617 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008618 /* Insert the caller's iocb in the txq tail for later processing. */
8619 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008620}
8621
James Smarte59058c2008-08-24 21:49:00 -04008622/**
James Smart3621a712009-04-06 18:47:14 -04008623 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008624 * @phba: Pointer to HBA context object.
8625 * @pring: Pointer to driver SLI ring object.
8626 * @piocb: Pointer to address of newly added command iocb.
8627 *
8628 * This function is called with hbalock held before a new
8629 * iocb is submitted to the firmware. This function checks
8630 * txq to flush the iocbs in txq to Firmware before
8631 * submitting new iocbs to the Firmware.
8632 * If there are iocbs in the txq which need to be submitted
8633 * to firmware, lpfc_sli_next_iocb returns the first element
8634 * of the txq after dequeuing it from txq.
8635 * If there is no iocb in the txq then the function will return
8636 * *piocb and *piocb is set to NULL. Caller needs to check
8637 * *piocb to find if there are more commands in the txq.
8638 **/
dea31012005-04-17 16:05:31 -05008639static struct lpfc_iocbq *
8640lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008641 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008642{
8643 struct lpfc_iocbq * nextiocb;
8644
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008645 lockdep_assert_held(&phba->hbalock);
8646
dea31012005-04-17 16:05:31 -05008647 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8648 if (!nextiocb) {
8649 nextiocb = *piocb;
8650 *piocb = NULL;
8651 }
8652
8653 return nextiocb;
8654}
8655
James Smarte59058c2008-08-24 21:49:00 -04008656/**
James Smart3772a992009-05-22 14:50:54 -04008657 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008658 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008659 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008660 * @piocb: Pointer to command iocb.
8661 * @flag: Flag indicating if this command can be put into txq.
8662 *
James Smart3772a992009-05-22 14:50:54 -04008663 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8664 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8665 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8666 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8667 * this function allows only iocbs for posting buffers. This function finds
8668 * next available slot in the command ring and posts the command to the
8669 * available slot and writes the port attention register to request HBA start
8670 * processing new iocb. If there is no slot available in the ring and
8671 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8672 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008673 *
James Smart3772a992009-05-22 14:50:54 -04008674 * This function is called with hbalock held. The function will return success
8675 * after it successfully submit the iocb to firmware or after adding to the
8676 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008677 **/
James Smart98c9ea52007-10-27 13:37:33 -04008678static int
James Smart3772a992009-05-22 14:50:54 -04008679__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008680 struct lpfc_iocbq *piocb, uint32_t flag)
8681{
8682 struct lpfc_iocbq *nextiocb;
8683 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008684 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008685
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008686 lockdep_assert_held(&phba->hbalock);
8687
James Smart92d7f7b2007-06-17 19:56:38 -05008688 if (piocb->iocb_cmpl && (!piocb->vport) &&
8689 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8690 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8691 lpfc_printf_log(phba, KERN_ERR,
8692 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008693 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008694 piocb->iocb.ulpCommand);
8695 dump_stack();
8696 return IOCB_ERROR;
8697 }
8698
8699
Linas Vepstas8d63f372007-02-14 14:28:36 -06008700 /* If the PCI channel is in offline state, do not post iocbs. */
8701 if (unlikely(pci_channel_offline(phba->pcidev)))
8702 return IOCB_ERROR;
8703
James Smarta257bf92009-04-06 18:48:10 -04008704 /* If HBA has a deferred error attention, fail the iocb. */
8705 if (unlikely(phba->hba_flag & DEFER_ERATT))
8706 return IOCB_ERROR;
8707
dea31012005-04-17 16:05:31 -05008708 /*
8709 * We should never get an IOCB if we are in a < LINK_DOWN state
8710 */
James Smart2e0fef82007-06-17 19:56:36 -05008711 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008712 return IOCB_ERROR;
8713
8714 /*
8715 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008716 * outstanding event.
dea31012005-04-17 16:05:31 -05008717 */
James Smart0b727fe2007-10-27 13:37:25 -04008718 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008719 goto iocb_busy;
8720
James Smart2e0fef82007-06-17 19:56:36 -05008721 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008722 /*
James Smart2680eea2007-04-25 09:52:55 -04008723 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008724 * can be issued if the link is not up.
8725 */
8726 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008727 case CMD_GEN_REQUEST64_CR:
8728 case CMD_GEN_REQUEST64_CX:
8729 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8730 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008731 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008732 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8733 MENLO_TRANSPORT_TYPE))
8734
8735 goto iocb_busy;
8736 break;
dea31012005-04-17 16:05:31 -05008737 case CMD_QUE_RING_BUF_CN:
8738 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008739 /*
8740 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8741 * completion, iocb_cmpl MUST be 0.
8742 */
8743 if (piocb->iocb_cmpl)
8744 piocb->iocb_cmpl = NULL;
8745 /*FALLTHROUGH*/
8746 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008747 case CMD_CLOSE_XRI_CN:
8748 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008749 break;
8750 default:
8751 goto iocb_busy;
8752 }
8753
8754 /*
8755 * For FCP commands, we must be in a state where we can process link
8756 * attention events.
8757 */
James Smart895427b2017-02-12 13:52:30 -08008758 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008759 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008760 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008761 }
dea31012005-04-17 16:05:31 -05008762
dea31012005-04-17 16:05:31 -05008763 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8764 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8765 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8766
8767 if (iocb)
8768 lpfc_sli_update_ring(phba, pring);
8769 else
8770 lpfc_sli_update_full_ring(phba, pring);
8771
8772 if (!piocb)
8773 return IOCB_SUCCESS;
8774
8775 goto out_busy;
8776
8777 iocb_busy:
8778 pring->stats.iocb_cmd_delay++;
8779
8780 out_busy:
8781
8782 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008783 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008784 return IOCB_SUCCESS;
8785 }
8786
8787 return IOCB_BUSY;
8788}
8789
James Smart3772a992009-05-22 14:50:54 -04008790/**
James Smart4f774512009-05-22 14:52:35 -04008791 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8792 * @phba: Pointer to HBA context object.
8793 * @piocb: Pointer to command iocb.
8794 * @sglq: Pointer to the scatter gather queue object.
8795 *
8796 * This routine converts the bpl or bde that is in the IOCB
8797 * to a sgl list for the sli4 hardware. The physical address
8798 * of the bpl/bde is converted back to a virtual address.
8799 * If the IOCB contains a BPL then the list of BDE's is
8800 * converted to sli4_sge's. If the IOCB contains a single
8801 * BDE then it is converted to a single sli_sge.
8802 * The IOCB is still in cpu endianess so the contents of
8803 * the bpl can be used without byte swapping.
8804 *
8805 * Returns valid XRI = Success, NO_XRI = Failure.
8806**/
8807static uint16_t
8808lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8809 struct lpfc_sglq *sglq)
8810{
8811 uint16_t xritag = NO_XRI;
8812 struct ulp_bde64 *bpl = NULL;
8813 struct ulp_bde64 bde;
8814 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008815 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008816 IOCB_t *icmd;
8817 int numBdes = 0;
8818 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008819 uint32_t offset = 0; /* accumulated offset in the sg request list */
8820 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008821
8822 if (!piocbq || !sglq)
8823 return xritag;
8824
8825 sgl = (struct sli4_sge *)sglq->sgl;
8826 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008827 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8828 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008829 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8830 numBdes = icmd->un.genreq64.bdl.bdeSize /
8831 sizeof(struct ulp_bde64);
8832 /* The addrHigh and addrLow fields within the IOCB
8833 * have not been byteswapped yet so there is no
8834 * need to swap them back.
8835 */
James Smart1b511972011-12-13 13:23:09 -05008836 if (piocbq->context3)
8837 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8838 else
8839 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008840
James Smart1b511972011-12-13 13:23:09 -05008841 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008842 if (!bpl)
8843 return xritag;
8844
8845 for (i = 0; i < numBdes; i++) {
8846 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008847 sgl->addr_hi = bpl->addrHigh;
8848 sgl->addr_lo = bpl->addrLow;
8849
James Smart05580562011-05-24 11:40:48 -04008850 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008851 if ((i+1) == numBdes)
8852 bf_set(lpfc_sli4_sge_last, sgl, 1);
8853 else
8854 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008855 /* swap the size field back to the cpu so we
8856 * can assign it to the sgl.
8857 */
8858 bde.tus.w = le32_to_cpu(bpl->tus.w);
8859 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008860 /* The offsets in the sgl need to be accumulated
8861 * separately for the request and reply lists.
8862 * The request is always first, the reply follows.
8863 */
8864 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8865 /* add up the reply sg entries */
8866 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8867 inbound++;
8868 /* first inbound? reset the offset */
8869 if (inbound == 1)
8870 offset = 0;
8871 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008872 bf_set(lpfc_sli4_sge_type, sgl,
8873 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008874 offset += bde.tus.f.bdeSize;
8875 }
James Smart546fc852011-03-11 16:06:29 -05008876 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008877 bpl++;
8878 sgl++;
8879 }
8880 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8881 /* The addrHigh and addrLow fields of the BDE have not
8882 * been byteswapped yet so they need to be swapped
8883 * before putting them in the sgl.
8884 */
8885 sgl->addr_hi =
8886 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8887 sgl->addr_lo =
8888 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008889 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008890 bf_set(lpfc_sli4_sge_last, sgl, 1);
8891 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008892 sgl->sge_len =
8893 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008894 }
8895 return sglq->sli4_xritag;
8896}
8897
8898/**
James Smart4f774512009-05-22 14:52:35 -04008899 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8900 * @phba: Pointer to HBA context object.
8901 * @piocb: Pointer to command iocb.
8902 * @wqe: Pointer to the work queue entry.
8903 *
8904 * This routine converts the iocb command to its Work Queue Entry
8905 * equivalent. The wqe pointer should not have any fields set when
8906 * this routine is called because it will memcpy over them.
8907 * This routine does not set the CQ_ID or the WQEC bits in the
8908 * wqe.
8909 *
8910 * Returns: 0 = Success, IOCB_ERROR = Failure.
8911 **/
8912static int
8913lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08008914 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04008915{
James Smart5ffc2662009-11-18 15:39:44 -05008916 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008917 uint8_t ct = 0;
8918 uint32_t fip;
8919 uint32_t abort_tag;
8920 uint8_t command_type = ELS_COMMAND_NON_FIP;
8921 uint8_t cmnd;
8922 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008923 uint16_t abrt_iotag;
8924 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008925 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008926 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008927 int numBdes, i;
8928 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008929 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008930 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008931 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008932
James Smart45ed1192009-10-02 15:17:02 -04008933 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008934 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008935 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008936 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008937 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008938 command_type = ELS_COMMAND_FIP;
8939 else
8940 command_type = ELS_COMMAND_NON_FIP;
8941
James Smartb5c53952016-03-31 14:12:30 -07008942 if (phba->fcp_embed_io)
8943 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008944 /* Some of the fields are in the right position already */
8945 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07008946 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8947 /* The ct field has moved so reset */
8948 wqe->generic.wqe_com.word7 = 0;
8949 wqe->generic.wqe_com.word10 = 0;
8950 }
James Smartb5c53952016-03-31 14:12:30 -07008951
8952 abort_tag = (uint32_t) iocbq->iotag;
8953 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008954 /* words0-2 bpl convert bde */
8955 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008956 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8957 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008958 bpl = (struct ulp_bde64 *)
8959 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8960 if (!bpl)
8961 return IOCB_ERROR;
8962
8963 /* Should already be byte swapped. */
8964 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8965 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8966 /* swap the size field back to the cpu so we
8967 * can assign it to the sgl.
8968 */
8969 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008970 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8971 total_len = 0;
8972 for (i = 0; i < numBdes; i++) {
8973 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8974 total_len += bde.tus.f.bdeSize;
8975 }
James Smart4f774512009-05-22 14:52:35 -04008976 } else
James Smart5ffc2662009-11-18 15:39:44 -05008977 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008978
8979 iocbq->iocb.ulpIoTag = iocbq->iotag;
8980 cmnd = iocbq->iocb.ulpCommand;
8981
8982 switch (iocbq->iocb.ulpCommand) {
8983 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008984 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8985 ndlp = iocbq->context_un.ndlp;
8986 else
8987 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008988 if (!iocbq->iocb.ulpLe) {
8989 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8990 "2007 Only Limited Edition cmd Format"
8991 " supported 0x%x\n",
8992 iocbq->iocb.ulpCommand);
8993 return IOCB_ERROR;
8994 }
James Smartff78d8f2011-12-13 13:21:35 -05008995
James Smart5ffc2662009-11-18 15:39:44 -05008996 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008997 /* Els_reguest64 has a TMO */
8998 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8999 iocbq->iocb.ulpTimeout);
9000 /* Need a VF for word 4 set the vf bit*/
9001 bf_set(els_req64_vf, &wqe->els_req, 0);
9002 /* And a VFID for word 12 */
9003 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04009004 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04009005 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9006 iocbq->iocb.ulpContext);
9007 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9008 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04009009 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05009010 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05009011 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9012 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05009013 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9014 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05009015 if_type = bf_get(lpfc_sli_intf_if_type,
9016 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009017 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05009018 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05009019 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05009020 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05009021 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05009022 *pcmd == ELS_CMD_PLOGI)) {
9023 bf_set(els_req64_sp, &wqe->els_req, 1);
9024 bf_set(els_req64_sid, &wqe->els_req,
9025 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009026 if ((*pcmd == ELS_CMD_FLOGI) &&
9027 !(phba->fc_topology ==
9028 LPFC_TOPOLOGY_LOOP))
9029 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009030 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9031 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009032 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009033 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009034 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9035 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9036 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9037 }
James Smartc8685952009-11-18 15:39:16 -05009038 }
James Smart6d368e52011-05-24 11:44:12 -04009039 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9040 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009041 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9042 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9043 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9044 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9045 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9046 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009047 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009048 break;
James Smart5ffc2662009-11-18 15:39:44 -05009049 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009050 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9051 iocbq->iocb.un.ulpWord[3]);
9052 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009053 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009054 /* The entire sequence is transmitted for this IOCB */
9055 xmit_len = total_len;
9056 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009057 if (phba->link_flag & LS_LOOPBACK_MODE)
9058 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04009059 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009060 /* word3 iocb=io_tag32 wqe=reserved */
9061 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009062 /* word4 relative_offset memcpy */
9063 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009064 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9065 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9066 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9067 LPFC_WQE_IOD_WRITE);
9068 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9069 LPFC_WQE_LENLOC_WORD12);
9070 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009071 wqe->xmit_sequence.xmit_len = xmit_len;
9072 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009073 break;
James Smart4f774512009-05-22 14:52:35 -04009074 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009075 /* word3 iocb=iotag32 wqe=seq_payload_len */
9076 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009077 /* word4 iocb=rsvd wqe=rsvd */
9078 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9079 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009080 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009081 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009082 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9083 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9084 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9085 LPFC_WQE_LENLOC_WORD3);
9086 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009087 break;
James Smart4f774512009-05-22 14:52:35 -04009088 case CMD_FCP_IWRITE64_CR:
9089 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009090 /* word3 iocb=iotag wqe=payload_offset_len */
9091 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009092 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9093 xmit_len + sizeof(struct fcp_rsp));
9094 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9095 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009096 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9097 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9098 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9099 iocbq->iocb.ulpFCP2Rcvy);
9100 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9101 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009102 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9103 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9104 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009105 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009106 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009107 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9108 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009109 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9110 if (iocbq->priority) {
9111 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9112 (iocbq->priority << 1));
9113 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009114 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9115 (phba->cfg_XLanePriority << 1));
9116 }
9117 }
James Smartb5c53952016-03-31 14:12:30 -07009118 /* Note, word 10 is already initialized to 0 */
9119
James Smart0bc2b7c2018-02-22 08:18:48 -08009120 /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9121 if (phba->fcp_embed_pbde)
9122 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9123 else
9124 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9125
James Smartb5c53952016-03-31 14:12:30 -07009126 if (phba->fcp_embed_io) {
9127 struct lpfc_scsi_buf *lpfc_cmd;
9128 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009129 struct fcp_cmnd *fcp_cmnd;
9130 uint32_t *ptr;
9131
9132 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009133
9134 lpfc_cmd = iocbq->context1;
9135 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9136 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9137
9138 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009139 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009140 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009141 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9142 wqe->generic.bde.addrHigh = 0;
9143 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009144
James Smart205e8242018-03-05 12:04:03 -08009145 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9146 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009147
9148 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009149 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009150 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9151 }
James Smart7851fe22011-07-22 18:36:52 -04009152 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009153 case CMD_FCP_IREAD64_CR:
9154 /* word3 iocb=iotag wqe=payload_offset_len */
9155 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009156 bf_set(payload_offset_len, &wqe->fcp_iread,
9157 xmit_len + sizeof(struct fcp_rsp));
9158 bf_set(cmd_buff_len, &wqe->fcp_iread,
9159 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009160 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9161 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9162 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9163 iocbq->iocb.ulpFCP2Rcvy);
9164 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009165 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009166 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9167 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9168 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009169 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009170 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009171 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9172 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009173 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9174 if (iocbq->priority) {
9175 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9176 (iocbq->priority << 1));
9177 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009178 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9179 (phba->cfg_XLanePriority << 1));
9180 }
9181 }
James Smartb5c53952016-03-31 14:12:30 -07009182 /* Note, word 10 is already initialized to 0 */
9183
James Smart0bc2b7c2018-02-22 08:18:48 -08009184 /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9185 if (phba->fcp_embed_pbde)
9186 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9187 else
9188 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9189
James Smartb5c53952016-03-31 14:12:30 -07009190 if (phba->fcp_embed_io) {
9191 struct lpfc_scsi_buf *lpfc_cmd;
9192 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009193 struct fcp_cmnd *fcp_cmnd;
9194 uint32_t *ptr;
9195
9196 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009197
9198 lpfc_cmd = iocbq->context1;
9199 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9200 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9201
9202 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009203 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009204 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009205 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9206 wqe->generic.bde.addrHigh = 0;
9207 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009208
James Smart205e8242018-03-05 12:04:03 -08009209 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9210 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009211
9212 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009213 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009214 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9215 }
James Smart7851fe22011-07-22 18:36:52 -04009216 break;
James Smartf1126682009-06-10 17:22:44 -04009217 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009218 /* word3 iocb=iotag wqe=payload_offset_len */
9219 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9220 bf_set(payload_offset_len, &wqe->fcp_icmd,
9221 xmit_len + sizeof(struct fcp_rsp));
9222 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9223 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009224 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009225 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009226 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009227 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9228 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9229 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9230 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9231 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009232 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9233 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009234 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9235 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009236 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9237 if (iocbq->priority) {
9238 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9239 (iocbq->priority << 1));
9240 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009241 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9242 (phba->cfg_XLanePriority << 1));
9243 }
9244 }
James Smartb5c53952016-03-31 14:12:30 -07009245 /* Note, word 10 is already initialized to 0 */
9246
9247 if (phba->fcp_embed_io) {
9248 struct lpfc_scsi_buf *lpfc_cmd;
9249 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009250 struct fcp_cmnd *fcp_cmnd;
9251 uint32_t *ptr;
9252
9253 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009254
9255 lpfc_cmd = iocbq->context1;
9256 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9257 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9258
9259 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009260 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009261 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009262 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9263 wqe->generic.bde.addrHigh = 0;
9264 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009265
James Smart205e8242018-03-05 12:04:03 -08009266 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9267 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009268
9269 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009270 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009271 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9272 }
James Smart7851fe22011-07-22 18:36:52 -04009273 break;
James Smart4f774512009-05-22 14:52:35 -04009274 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009275 /* For this command calculate the xmit length of the
9276 * request bde.
9277 */
9278 xmit_len = 0;
9279 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9280 sizeof(struct ulp_bde64);
9281 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009282 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009283 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9284 break;
James Smart63e801c2010-11-20 23:14:19 -05009285 xmit_len += bde.tus.f.bdeSize;
9286 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009287 /* word3 iocb=IO_TAG wqe=request_payload_len */
9288 wqe->gen_req.request_payload_len = xmit_len;
9289 /* word4 iocb=parameter wqe=relative_offset memcpy */
9290 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009291 /* word6 context tag copied in memcpy */
9292 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9293 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9294 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9295 "2015 Invalid CT %x command 0x%x\n",
9296 ct, iocbq->iocb.ulpCommand);
9297 return IOCB_ERROR;
9298 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009299 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9300 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9301 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9302 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9303 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9304 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9305 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9306 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009307 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009308 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009309 break;
James Smart4f774512009-05-22 14:52:35 -04009310 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009311 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009312 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009313 /* word3 iocb=iotag32 wqe=response_payload_len */
9314 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009315 /* word4 */
9316 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009317 /* word5 iocb=rsvd wge=did */
9318 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009319 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9320
9321 if_type = bf_get(lpfc_sli_intf_if_type,
9322 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009323 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009324 if (iocbq->vport->fc_flag & FC_PT2PT) {
9325 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9326 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9327 iocbq->vport->fc_myDID);
9328 if (iocbq->vport->fc_myDID == Fabric_DID) {
9329 bf_set(wqe_els_did,
9330 &wqe->xmit_els_rsp.wqe_dest, 0);
9331 }
9332 }
9333 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009334 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9335 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9336 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9337 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009338 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009339 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009340 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009341 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009342 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9343 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9344 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9345 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9346 LPFC_WQE_LENLOC_WORD3);
9347 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009348 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9349 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009350 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9351 iocbq->context2)->virt);
9352 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009353 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9354 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009355 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009356 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9357 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009358 phba->vpi_ids[phba->pport->vpi]);
9359 }
James Smart4f774512009-05-22 14:52:35 -04009360 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009361 break;
James Smart4f774512009-05-22 14:52:35 -04009362 case CMD_CLOSE_XRI_CN:
9363 case CMD_ABORT_XRI_CN:
9364 case CMD_ABORT_XRI_CX:
9365 /* words 0-2 memcpy should be 0 rserved */
9366 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009367 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9368 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9369 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9370 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9371 } else
9372 fip = 0;
9373
9374 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009375 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009376 * The link is down, or the command was ELS_FIP
9377 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009378 * on the wire.
9379 */
9380 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9381 else
9382 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9383 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009384 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9385 wqe->abort_cmd.rsrvd5 = 0;
9386 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009387 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9388 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009389 /*
9390 * The abort handler will send us CMD_ABORT_XRI_CN or
9391 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9392 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009393 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9394 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9395 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9396 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009397 cmnd = CMD_ABORT_XRI_CX;
9398 command_type = OTHER_COMMAND;
9399 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009400 break;
James Smart6669f9b2009-10-02 15:16:45 -04009401 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009402 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009403 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009404 * we re-construct this WQE here based on information in
9405 * iocbq from scratch.
9406 */
9407 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009408 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009409 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009410 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9411 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009412 LPFC_ABTS_UNSOL_INT) {
9413 /* ABTS sent by initiator to CT exchange, the
9414 * RX_ID field will be filled with the newly
9415 * allocated responder XRI.
9416 */
9417 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9418 iocbq->sli4_xritag);
9419 } else {
9420 /* ABTS sent by responder to CT exchange, the
9421 * RX_ID field will be filled with the responder
9422 * RX_ID from ABTS.
9423 */
9424 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009425 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009426 }
James Smart6669f9b2009-10-02 15:16:45 -04009427 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9428 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009429
9430 /* Use CT=VPI */
9431 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9432 ndlp->nlp_DID);
9433 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9434 iocbq->iocb.ulpContext);
9435 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009436 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009437 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009438 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9439 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9440 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009441 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9442 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009443 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9444 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9445 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9446 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9447 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9448 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9449 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9450 }
9451
James Smart7851fe22011-07-22 18:36:52 -04009452 break;
James Smartae9e28f2017-05-15 15:20:51 -07009453 case CMD_SEND_FRAME:
9454 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9455 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9456 return 0;
James Smart4f774512009-05-22 14:52:35 -04009457 case CMD_XRI_ABORTED_CX:
9458 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009459 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9460 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9461 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9462 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9463 default:
9464 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9465 "2014 Invalid command 0x%x\n",
9466 iocbq->iocb.ulpCommand);
9467 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009468 break;
James Smart4f774512009-05-22 14:52:35 -04009469 }
James Smart6d368e52011-05-24 11:44:12 -04009470
James Smart8012cc32012-10-31 14:44:49 -04009471 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9472 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9473 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9474 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9475 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9476 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9477 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9478 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009479 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9480 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9481 wqe->generic.wqe_com.abort_tag = abort_tag;
9482 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9483 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9484 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9485 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009486 return 0;
9487}
9488
9489/**
9490 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9491 * @phba: Pointer to HBA context object.
9492 * @ring_number: SLI ring number to issue iocb on.
9493 * @piocb: Pointer to command iocb.
9494 * @flag: Flag indicating if this command can be put into txq.
9495 *
9496 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9497 * an iocb command to an HBA with SLI-4 interface spec.
9498 *
9499 * This function is called with hbalock held. The function will return success
9500 * after it successfully submit the iocb to firmware or after adding to the
9501 * txq.
9502 **/
9503static int
9504__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9505 struct lpfc_iocbq *piocb, uint32_t flag)
9506{
9507 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -08009508 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -05009509 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009510 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009511
James Smart895427b2017-02-12 13:52:30 -08009512 /* Get the WQ */
9513 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9514 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9515 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9516 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9517 else
9518 wq = phba->sli4_hba.oas_wq;
9519 } else {
9520 wq = phba->sli4_hba.els_wq;
9521 }
9522
9523 /* Get corresponding ring */
9524 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009525
James Smartb5c53952016-03-31 14:12:30 -07009526 /*
9527 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -07009528 */
James Smartb5c53952016-03-31 14:12:30 -07009529
James Smart895427b2017-02-12 13:52:30 -08009530 lockdep_assert_held(&phba->hbalock);
9531
James Smart4f774512009-05-22 14:52:35 -04009532 if (piocb->sli4_xritag == NO_XRI) {
9533 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009534 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009535 sglq = NULL;
9536 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009537 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009538 if (!(flag & SLI_IOCB_RET_IOCB)) {
9539 __lpfc_sli_ringtx_put(phba,
9540 pring, piocb);
9541 return IOCB_SUCCESS;
9542 } else {
9543 return IOCB_BUSY;
9544 }
9545 } else {
James Smart895427b2017-02-12 13:52:30 -08009546 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009547 if (!sglq) {
9548 if (!(flag & SLI_IOCB_RET_IOCB)) {
9549 __lpfc_sli_ringtx_put(phba,
9550 pring,
9551 piocb);
9552 return IOCB_SUCCESS;
9553 } else
9554 return IOCB_BUSY;
9555 }
9556 }
James Smart4f774512009-05-22 14:52:35 -04009557 }
James Smart2ea259e2017-02-12 13:52:27 -08009558 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009559 /* These IO's already have an XRI and a mapped sgl. */
9560 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009561 else {
James Smart6d368e52011-05-24 11:44:12 -04009562 /*
9563 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009564 * sglq is on the active list
9565 */
James Smartedccdc12013-01-03 15:43:45 -05009566 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009567 if (!sglq)
9568 return IOCB_ERROR;
9569 }
9570
9571 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009572 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009573 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009574 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009575 return IOCB_ERROR;
9576 }
9577
James Smart205e8242018-03-05 12:04:03 -08009578 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -04009579 return IOCB_ERROR;
9580
James Smart205e8242018-03-05 12:04:03 -08009581 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -08009582 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009583 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9584
9585 return 0;
9586}
9587
9588/**
James Smart3772a992009-05-22 14:50:54 -04009589 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9590 *
9591 * This routine wraps the actual lockless version for issusing IOCB function
9592 * pointer from the lpfc_hba struct.
9593 *
9594 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009595 * IOCB_ERROR - Error
9596 * IOCB_SUCCESS - Success
9597 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009598 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009599int
James Smart3772a992009-05-22 14:50:54 -04009600__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9601 struct lpfc_iocbq *piocb, uint32_t flag)
9602{
9603 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9604}
9605
9606/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009607 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009608 * @phba: The hba struct for which this call is being executed.
9609 * @dev_grp: The HBA PCI-Device group number.
9610 *
9611 * This routine sets up the SLI interface API function jump table in @phba
9612 * struct.
9613 * Returns: 0 - success, -ENODEV - failure.
9614 **/
9615int
9616lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9617{
9618
9619 switch (dev_grp) {
9620 case LPFC_PCI_DEV_LP:
9621 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9622 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9623 break;
James Smart4f774512009-05-22 14:52:35 -04009624 case LPFC_PCI_DEV_OC:
9625 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9626 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9627 break;
James Smart3772a992009-05-22 14:50:54 -04009628 default:
9629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9630 "1419 Invalid HBA PCI-device group: 0x%x\n",
9631 dev_grp);
9632 return -ENODEV;
9633 break;
9634 }
9635 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9636 return 0;
9637}
James Smart92d7f7b2007-06-17 19:56:38 -05009638
James Smarta1efe162015-05-21 13:55:20 -04009639/**
James Smart895427b2017-02-12 13:52:30 -08009640 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009641 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009642 * @piocb: Pointer to command iocb.
9643 *
James Smart895427b2017-02-12 13:52:30 -08009644 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9645 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009646 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009647 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009648 */
James Smart895427b2017-02-12 13:52:30 -08009649struct lpfc_sli_ring *
9650lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009651{
James Smart895427b2017-02-12 13:52:30 -08009652 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009653 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009654 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009655 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009656 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009657 /*
James Smart895427b2017-02-12 13:52:30 -08009658 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009659 * be setup based on what work queue we used.
9660 */
Dick Kennedy8e036a92017-09-29 17:34:35 -07009661 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart895427b2017-02-12 13:52:30 -08009662 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009663 lpfc_sli4_scmd_to_wqidx_distr(phba,
9664 piocb->context1);
Dick Kennedy8e036a92017-09-29 17:34:35 -07009665 piocb->hba_wqidx = piocb->hba_wqidx %
9666 phba->cfg_fcp_io_channel;
9667 }
James Smart895427b2017-02-12 13:52:30 -08009668 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009669 } else {
9670 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009671 return NULL;
9672 piocb->hba_wqidx = 0;
9673 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009674 }
James Smart895427b2017-02-12 13:52:30 -08009675 } else {
9676 if (unlikely(!phba->sli4_hba.els_wq))
9677 return NULL;
9678 piocb->hba_wqidx = 0;
9679 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009680 }
James Smart9bd2bff52014-09-03 12:57:30 -04009681}
9682
James Smarte59058c2008-08-24 21:49:00 -04009683/**
James Smart3621a712009-04-06 18:47:14 -04009684 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009685 * @phba: Pointer to HBA context object.
9686 * @pring: Pointer to driver SLI ring object.
9687 * @piocb: Pointer to command iocb.
9688 * @flag: Flag indicating if this command can be put into txq.
9689 *
9690 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9691 * function. This function gets the hbalock and calls
9692 * __lpfc_sli_issue_iocb function and will return the error returned
9693 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9694 * functions which do not hold hbalock.
9695 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009696int
James Smart3772a992009-05-22 14:50:54 -04009697lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009698 struct lpfc_iocbq *piocb, uint32_t flag)
9699{
James Smart895427b2017-02-12 13:52:30 -08009700 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009701 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009702 struct lpfc_queue *fpeq;
9703 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009704 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009705 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009706
James Smart7e56aa22012-08-03 12:35:34 -04009707 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009708 pring = lpfc_sli4_calc_ring(phba, piocb);
9709 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009710 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009711
James Smart9bd2bff52014-09-03 12:57:30 -04009712 spin_lock_irqsave(&pring->ring_lock, iflags);
9713 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9714 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009715
James Smart9bd2bff52014-09-03 12:57:30 -04009716 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009717 idx = piocb->hba_wqidx;
9718 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009719
James Smart895427b2017-02-12 13:52:30 -08009720 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009721
James Smart9bd2bff52014-09-03 12:57:30 -04009722 /* Get associated EQ with this index */
9723 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009724
James Smart9bd2bff52014-09-03 12:57:30 -04009725 /* Turn off interrupts from this EQ */
James Smartb71413d2018-02-22 08:18:40 -08009726 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009727
James Smart9bd2bff52014-09-03 12:57:30 -04009728 /*
9729 * Process all the events on FCP EQ
9730 */
9731 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9732 lpfc_sli4_hba_handle_eqe(phba,
9733 eqe, idx);
9734 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009735 }
James Smartba20c852012-08-03 12:36:52 -04009736
James Smart9bd2bff52014-09-03 12:57:30 -04009737 /* Always clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -08009738 phba->sli4_hba.sli4_eq_release(fpeq,
James Smart9bd2bff52014-09-03 12:57:30 -04009739 LPFC_QUEUE_REARM);
9740 }
James Smart895427b2017-02-12 13:52:30 -08009741 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009742 }
James Smart7e56aa22012-08-03 12:35:34 -04009743 } else {
9744 /* For now, SLI2/3 will still use hbalock */
9745 spin_lock_irqsave(&phba->hbalock, iflags);
9746 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9747 spin_unlock_irqrestore(&phba->hbalock, iflags);
9748 }
James Smart92d7f7b2007-06-17 19:56:38 -05009749 return rc;
9750}
9751
James Smarte59058c2008-08-24 21:49:00 -04009752/**
James Smart3621a712009-04-06 18:47:14 -04009753 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009754 * @phba: Pointer to HBA context object.
9755 *
9756 * This function is called while driver attaches with the
9757 * HBA to setup the extra ring. The extra ring is used
9758 * only when driver needs to support target mode functionality
9759 * or IP over FC functionalities.
9760 *
James Smart895427b2017-02-12 13:52:30 -08009761 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009762 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009763static int
9764lpfc_extra_ring_setup( struct lpfc_hba *phba)
9765{
9766 struct lpfc_sli *psli;
9767 struct lpfc_sli_ring *pring;
9768
9769 psli = &phba->sli;
9770
9771 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009772
9773 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009774 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009775 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9776 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9777 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9778 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009779
James Smarta4bc3372006-12-02 13:34:16 -05009780 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009781 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009782
James Smart7e56aa22012-08-03 12:35:34 -04009783 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9784 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9785 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9786 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009787
9788 /* Setup default profile for this ring */
9789 pring->iotag_max = 4096;
9790 pring->num_mask = 1;
9791 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009792 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9793 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009794 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9795 return 0;
9796}
9797
James Smartcb69f7d2011-12-13 13:21:57 -05009798/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9799 * @phba: Pointer to HBA context object.
9800 * @iocbq: Pointer to iocb object.
9801 *
9802 * The async_event handler calls this routine when it receives
9803 * an ASYNC_STATUS_CN event from the port. The port generates
9804 * this event when an Abort Sequence request to an rport fails
9805 * twice in succession. The abort could be originated by the
9806 * driver or by the port. The ABTS could have been for an ELS
9807 * or FCP IO. The port only generates this event when an ABTS
9808 * fails to complete after one retry.
9809 */
9810static void
9811lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9812 struct lpfc_iocbq *iocbq)
9813{
9814 struct lpfc_nodelist *ndlp = NULL;
9815 uint16_t rpi = 0, vpi = 0;
9816 struct lpfc_vport *vport = NULL;
9817
9818 /* The rpi in the ulpContext is vport-sensitive. */
9819 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9820 rpi = iocbq->iocb.ulpContext;
9821
9822 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9823 "3092 Port generated ABTS async event "
9824 "on vpi %d rpi %d status 0x%x\n",
9825 vpi, rpi, iocbq->iocb.ulpStatus);
9826
9827 vport = lpfc_find_vport_by_vpid(phba, vpi);
9828 if (!vport)
9829 goto err_exit;
9830 ndlp = lpfc_findnode_rpi(vport, rpi);
9831 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9832 goto err_exit;
9833
9834 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9835 lpfc_sli_abts_recover_port(vport, ndlp);
9836 return;
9837
9838 err_exit:
9839 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9840 "3095 Event Context not found, no "
9841 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9842 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9843 vpi, rpi);
9844}
9845
9846/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9847 * @phba: pointer to HBA context object.
9848 * @ndlp: nodelist pointer for the impacted rport.
9849 * @axri: pointer to the wcqe containing the failed exchange.
9850 *
9851 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9852 * port. The port generates this event when an abort exchange request to an
9853 * rport fails twice in succession with no reply. The abort could be originated
9854 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9855 */
9856void
9857lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9858 struct lpfc_nodelist *ndlp,
9859 struct sli4_wcqe_xri_aborted *axri)
9860{
9861 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009862 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009863
James Smart6b5151f2012-01-18 16:24:06 -05009864 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009865 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9866 "3115 Node Context not found, driver "
9867 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009868 return;
9869 }
9870
James Smartcb69f7d2011-12-13 13:21:57 -05009871 vport = ndlp->vport;
9872 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9873 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009874 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009875 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009876 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009877 bf_get(lpfc_wcqe_xa_status, axri),
9878 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009879
James Smart5c1db2a2012-03-01 22:34:36 -05009880 /*
9881 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9882 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9883 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9884 */
James Smarte3d2b802012-08-14 14:25:43 -04009885 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009886 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9887 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009888 lpfc_sli_abts_recover_port(vport, ndlp);
9889}
9890
James Smarte59058c2008-08-24 21:49:00 -04009891/**
James Smart3621a712009-04-06 18:47:14 -04009892 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009893 * @phba: Pointer to HBA context object.
9894 * @pring: Pointer to driver SLI ring object.
9895 * @iocbq: Pointer to iocb object.
9896 *
9897 * This function is called by the slow ring event handler
9898 * function when there is an ASYNC event iocb in the ring.
9899 * This function is called with no lock held.
9900 * Currently this function handles only temperature related
9901 * ASYNC events. The function decodes the temperature sensor
9902 * event message and posts events for the management applications.
9903 **/
James Smart98c9ea52007-10-27 13:37:33 -04009904static void
James Smart57127f12007-10-27 13:37:05 -04009905lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9906 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9907{
9908 IOCB_t *icmd;
9909 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009910 struct temp_event temp_event_data;
9911 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009912 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009913
9914 icmd = &iocbq->iocb;
9915 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009916
James Smartcb69f7d2011-12-13 13:21:57 -05009917 switch (evt_code) {
9918 case ASYNC_TEMP_WARN:
9919 case ASYNC_TEMP_SAFE:
9920 temp_event_data.data = (uint32_t) icmd->ulpContext;
9921 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9922 if (evt_code == ASYNC_TEMP_WARN) {
9923 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9924 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9925 "0347 Adapter is very hot, please take "
9926 "corrective action. temperature : %d Celsius\n",
9927 (uint32_t) icmd->ulpContext);
9928 } else {
9929 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9930 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9931 "0340 Adapter temperature is OK now. "
9932 "temperature : %d Celsius\n",
9933 (uint32_t) icmd->ulpContext);
9934 }
9935
9936 /* Send temperature change event to applications */
9937 shost = lpfc_shost_from_vport(phba->pport);
9938 fc_host_post_vendor_event(shost, fc_get_event_number(),
9939 sizeof(temp_event_data), (char *) &temp_event_data,
9940 LPFC_NL_VENDOR_ID);
9941 break;
9942 case ASYNC_STATUS_CN:
9943 lpfc_sli_abts_err_handler(phba, iocbq);
9944 break;
9945 default:
James Smarta257bf92009-04-06 18:48:10 -04009946 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009947 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009948 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009949 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009950 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9951 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9952 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9953 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009954 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009955 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9956 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9957 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9958 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9959
James Smartcb69f7d2011-12-13 13:21:57 -05009960 break;
James Smart57127f12007-10-27 13:37:05 -04009961 }
James Smart57127f12007-10-27 13:37:05 -04009962}
9963
9964
James Smarte59058c2008-08-24 21:49:00 -04009965/**
James Smart895427b2017-02-12 13:52:30 -08009966 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009967 * @phba: Pointer to HBA context object.
9968 *
9969 * lpfc_sli_setup sets up rings of the SLI interface with
9970 * number of iocbs per ring and iotags. This function is
9971 * called while driver attach to the HBA and before the
9972 * interrupts are enabled. So there is no need for locking.
9973 *
9974 * This function always returns 0.
9975 **/
dea31012005-04-17 16:05:31 -05009976int
James Smart895427b2017-02-12 13:52:30 -08009977lpfc_sli4_setup(struct lpfc_hba *phba)
9978{
9979 struct lpfc_sli_ring *pring;
9980
9981 pring = phba->sli4_hba.els_wq->pring;
9982 pring->num_mask = LPFC_MAX_RING_MASK;
9983 pring->prt[0].profile = 0; /* Mask 0 */
9984 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9985 pring->prt[0].type = FC_TYPE_ELS;
9986 pring->prt[0].lpfc_sli_rcv_unsol_event =
9987 lpfc_els_unsol_event;
9988 pring->prt[1].profile = 0; /* Mask 1 */
9989 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9990 pring->prt[1].type = FC_TYPE_ELS;
9991 pring->prt[1].lpfc_sli_rcv_unsol_event =
9992 lpfc_els_unsol_event;
9993 pring->prt[2].profile = 0; /* Mask 2 */
9994 /* NameServer Inquiry */
9995 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9996 /* NameServer */
9997 pring->prt[2].type = FC_TYPE_CT;
9998 pring->prt[2].lpfc_sli_rcv_unsol_event =
9999 lpfc_ct_unsol_event;
10000 pring->prt[3].profile = 0; /* Mask 3 */
10001 /* NameServer response */
10002 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10003 /* NameServer */
10004 pring->prt[3].type = FC_TYPE_CT;
10005 pring->prt[3].lpfc_sli_rcv_unsol_event =
10006 lpfc_ct_unsol_event;
10007 return 0;
10008}
10009
10010/**
10011 * lpfc_sli_setup - SLI ring setup function
10012 * @phba: Pointer to HBA context object.
10013 *
10014 * lpfc_sli_setup sets up rings of the SLI interface with
10015 * number of iocbs per ring and iotags. This function is
10016 * called while driver attach to the HBA and before the
10017 * interrupts are enabled. So there is no need for locking.
10018 *
10019 * This function always returns 0. SLI3 only.
10020 **/
10021int
dea31012005-04-17 16:05:31 -050010022lpfc_sli_setup(struct lpfc_hba *phba)
10023{
James Smarted957682007-06-17 19:56:37 -050010024 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050010025 struct lpfc_sli *psli = &phba->sli;
10026 struct lpfc_sli_ring *pring;
10027
James Smart2a76a282012-08-03 12:35:54 -040010028 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010029 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010030
James Bottomley604a3e32005-10-29 10:28:33 -050010031 psli->iocbq_lookup = NULL;
10032 psli->iocbq_lookup_len = 0;
10033 psli->last_iotag = 0;
10034
dea31012005-04-17 16:05:31 -050010035 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010036 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010037 switch (i) {
10038 case LPFC_FCP_RING: /* ring 0 - FCP */
10039 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010040 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10041 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10042 pring->sli.sli3.numCiocb +=
10043 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10044 pring->sli.sli3.numRiocb +=
10045 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10046 pring->sli.sli3.numCiocb +=
10047 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10048 pring->sli.sli3.numRiocb +=
10049 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10050 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010051 SLI3_IOCB_CMD_SIZE :
10052 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010053 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010054 SLI3_IOCB_RSP_SIZE :
10055 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010056 pring->iotag_ctr = 0;
10057 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010058 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010059 pring->fast_iotag = pring->iotag_max;
10060 pring->num_mask = 0;
10061 break;
James Smarta4bc3372006-12-02 13:34:16 -050010062 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010063 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010064 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10065 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10066 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010067 SLI3_IOCB_CMD_SIZE :
10068 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010069 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010070 SLI3_IOCB_RSP_SIZE :
10071 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010072 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010073 pring->num_mask = 0;
10074 break;
10075 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10076 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010077 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10078 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10079 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010080 SLI3_IOCB_CMD_SIZE :
10081 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010082 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010083 SLI3_IOCB_RSP_SIZE :
10084 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010085 pring->fast_iotag = 0;
10086 pring->iotag_ctr = 0;
10087 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010088 pring->lpfc_sli_rcv_async_status =
10089 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010090 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010091 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010092 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10093 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010094 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010095 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010096 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010097 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10098 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010099 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010100 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010101 pring->prt[2].profile = 0; /* Mask 2 */
10102 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010103 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010104 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010105 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010106 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010107 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010108 pring->prt[3].profile = 0; /* Mask 3 */
10109 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010110 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010111 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010112 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010113 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010114 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010115 break;
10116 }
James Smart7e56aa22012-08-03 12:35:34 -040010117 totiocbsize += (pring->sli.sli3.numCiocb *
10118 pring->sli.sli3.sizeCiocb) +
10119 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010120 }
James Smarted957682007-06-17 19:56:37 -050010121 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010122 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010123 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10124 "SLI2 SLIM Data: x%x x%lx\n",
10125 phba->brd_no, totiocbsize,
10126 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010127 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010128 if (phba->cfg_multi_ring_support == 2)
10129 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010130
10131 return 0;
10132}
10133
James Smarte59058c2008-08-24 21:49:00 -040010134/**
James Smart895427b2017-02-12 13:52:30 -080010135 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010136 * @phba: Pointer to HBA context object.
10137 *
James Smart895427b2017-02-12 13:52:30 -080010138 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010139 * ring. This function also initializes ring indices of each ring.
10140 * This function is called during the initialization of the SLI
10141 * interface of an HBA.
10142 * This function is called with no lock held and always returns
10143 * 1.
10144 **/
James Smart895427b2017-02-12 13:52:30 -080010145void
10146lpfc_sli4_queue_init(struct lpfc_hba *phba)
10147{
10148 struct lpfc_sli *psli;
10149 struct lpfc_sli_ring *pring;
10150 int i;
10151
10152 psli = &phba->sli;
10153 spin_lock_irq(&phba->hbalock);
10154 INIT_LIST_HEAD(&psli->mboxq);
10155 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10156 /* Initialize list headers for txq and txcmplq as double linked lists */
10157 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10158 pring = phba->sli4_hba.fcp_wq[i]->pring;
10159 pring->flag = 0;
10160 pring->ringno = LPFC_FCP_RING;
10161 INIT_LIST_HEAD(&pring->txq);
10162 INIT_LIST_HEAD(&pring->txcmplq);
10163 INIT_LIST_HEAD(&pring->iocb_continueq);
10164 spin_lock_init(&pring->ring_lock);
10165 }
10166 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10167 pring = phba->sli4_hba.nvme_wq[i]->pring;
10168 pring->flag = 0;
10169 pring->ringno = LPFC_FCP_RING;
10170 INIT_LIST_HEAD(&pring->txq);
10171 INIT_LIST_HEAD(&pring->txcmplq);
10172 INIT_LIST_HEAD(&pring->iocb_continueq);
10173 spin_lock_init(&pring->ring_lock);
10174 }
10175 pring = phba->sli4_hba.els_wq->pring;
10176 pring->flag = 0;
10177 pring->ringno = LPFC_ELS_RING;
10178 INIT_LIST_HEAD(&pring->txq);
10179 INIT_LIST_HEAD(&pring->txcmplq);
10180 INIT_LIST_HEAD(&pring->iocb_continueq);
10181 spin_lock_init(&pring->ring_lock);
10182
10183 if (phba->cfg_nvme_io_channel) {
10184 pring = phba->sli4_hba.nvmels_wq->pring;
10185 pring->flag = 0;
10186 pring->ringno = LPFC_ELS_RING;
10187 INIT_LIST_HEAD(&pring->txq);
10188 INIT_LIST_HEAD(&pring->txcmplq);
10189 INIT_LIST_HEAD(&pring->iocb_continueq);
10190 spin_lock_init(&pring->ring_lock);
10191 }
10192
10193 if (phba->cfg_fof) {
10194 pring = phba->sli4_hba.oas_wq->pring;
10195 pring->flag = 0;
10196 pring->ringno = LPFC_FCP_RING;
10197 INIT_LIST_HEAD(&pring->txq);
10198 INIT_LIST_HEAD(&pring->txcmplq);
10199 INIT_LIST_HEAD(&pring->iocb_continueq);
10200 spin_lock_init(&pring->ring_lock);
10201 }
10202
10203 spin_unlock_irq(&phba->hbalock);
10204}
10205
10206/**
10207 * lpfc_sli_queue_init - Queue initialization function
10208 * @phba: Pointer to HBA context object.
10209 *
10210 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10211 * ring. This function also initializes ring indices of each ring.
10212 * This function is called during the initialization of the SLI
10213 * interface of an HBA.
10214 * This function is called with no lock held and always returns
10215 * 1.
10216 **/
10217void
10218lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010219{
10220 struct lpfc_sli *psli;
10221 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010222 int i;
dea31012005-04-17 16:05:31 -050010223
10224 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010225 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010226 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010227 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010228 /* Initialize list headers for txq and txcmplq as double linked lists */
10229 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010230 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010231 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010232 pring->sli.sli3.next_cmdidx = 0;
10233 pring->sli.sli3.local_getidx = 0;
10234 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010235 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010236 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010237 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010238 pring->flag = 0;
10239 INIT_LIST_HEAD(&pring->txq);
10240 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010241 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010242 }
James Smart2e0fef82007-06-17 19:56:36 -050010243 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010244}
10245
James Smarte59058c2008-08-24 21:49:00 -040010246/**
James Smart04c68492009-05-22 14:52:52 -040010247 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10248 * @phba: Pointer to HBA context object.
10249 *
10250 * This routine flushes the mailbox command subsystem. It will unconditionally
10251 * flush all the mailbox commands in the three possible stages in the mailbox
10252 * command sub-system: pending mailbox command queue; the outstanding mailbox
10253 * command; and completed mailbox command queue. It is caller's responsibility
10254 * to make sure that the driver is in the proper state to flush the mailbox
10255 * command sub-system. Namely, the posting of mailbox commands into the
10256 * pending mailbox command queue from the various clients must be stopped;
10257 * either the HBA is in a state that it will never works on the outstanding
10258 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10259 * mailbox command has been completed.
10260 **/
10261static void
10262lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10263{
10264 LIST_HEAD(completions);
10265 struct lpfc_sli *psli = &phba->sli;
10266 LPFC_MBOXQ_t *pmb;
10267 unsigned long iflag;
10268
10269 /* Flush all the mailbox commands in the mbox system */
10270 spin_lock_irqsave(&phba->hbalock, iflag);
10271 /* The pending mailbox command queue */
10272 list_splice_init(&phba->sli.mboxq, &completions);
10273 /* The outstanding active mailbox command */
10274 if (psli->mbox_active) {
10275 list_add_tail(&psli->mbox_active->list, &completions);
10276 psli->mbox_active = NULL;
10277 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10278 }
10279 /* The completed mailbox command queue */
10280 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10281 spin_unlock_irqrestore(&phba->hbalock, iflag);
10282
10283 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10284 while (!list_empty(&completions)) {
10285 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10286 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10287 if (pmb->mbox_cmpl)
10288 pmb->mbox_cmpl(phba, pmb);
10289 }
10290}
10291
10292/**
James Smart3621a712009-04-06 18:47:14 -040010293 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010294 * @vport: Pointer to virtual port object.
10295 *
10296 * lpfc_sli_host_down is called to clean up the resources
10297 * associated with a vport before destroying virtual
10298 * port data structures.
10299 * This function does following operations:
10300 * - Free discovery resources associated with this virtual
10301 * port.
10302 * - Free iocbs associated with this virtual port in
10303 * the txq.
10304 * - Send abort for all iocb commands associated with this
10305 * vport in txcmplq.
10306 *
10307 * This function is called with no lock held and always returns 1.
10308 **/
dea31012005-04-17 16:05:31 -050010309int
James Smart92d7f7b2007-06-17 19:56:38 -050010310lpfc_sli_host_down(struct lpfc_vport *vport)
10311{
James Smart858c9f62007-06-17 19:56:39 -050010312 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010313 struct lpfc_hba *phba = vport->phba;
10314 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010315 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010316 struct lpfc_sli_ring *pring;
10317 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010318 int i;
10319 unsigned long flags = 0;
10320 uint16_t prev_pring_flag;
10321
10322 lpfc_cleanup_discovery_resources(vport);
10323
10324 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010325
James Smart895427b2017-02-12 13:52:30 -080010326 /*
10327 * Error everything on the txq since these iocbs
10328 * have not been given to the FW yet.
10329 * Also issue ABTS for everything on the txcmplq
10330 */
10331 if (phba->sli_rev != LPFC_SLI_REV4) {
10332 for (i = 0; i < psli->num_rings; i++) {
10333 pring = &psli->sli3_ring[i];
10334 prev_pring_flag = pring->flag;
10335 /* Only slow rings */
10336 if (pring->ringno == LPFC_ELS_RING) {
10337 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10338 /* Set the lpfc data pending flag */
10339 set_bit(LPFC_DATA_READY, &phba->data_flags);
10340 }
10341 list_for_each_entry_safe(iocb, next_iocb,
10342 &pring->txq, list) {
10343 if (iocb->vport != vport)
10344 continue;
10345 list_move_tail(&iocb->list, &completions);
10346 }
10347 list_for_each_entry_safe(iocb, next_iocb,
10348 &pring->txcmplq, list) {
10349 if (iocb->vport != vport)
10350 continue;
10351 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10352 }
10353 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010354 }
James Smart895427b2017-02-12 13:52:30 -080010355 } else {
10356 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10357 pring = qp->pring;
10358 if (!pring)
10359 continue;
10360 if (pring == phba->sli4_hba.els_wq->pring) {
10361 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10362 /* Set the lpfc data pending flag */
10363 set_bit(LPFC_DATA_READY, &phba->data_flags);
10364 }
10365 prev_pring_flag = pring->flag;
10366 spin_lock_irq(&pring->ring_lock);
10367 list_for_each_entry_safe(iocb, next_iocb,
10368 &pring->txq, list) {
10369 if (iocb->vport != vport)
10370 continue;
10371 list_move_tail(&iocb->list, &completions);
10372 }
10373 spin_unlock_irq(&pring->ring_lock);
10374 list_for_each_entry_safe(iocb, next_iocb,
10375 &pring->txcmplq, list) {
10376 if (iocb->vport != vport)
10377 continue;
10378 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10379 }
10380 pring->flag = prev_pring_flag;
10381 }
James Smart92d7f7b2007-06-17 19:56:38 -050010382 }
James Smart92d7f7b2007-06-17 19:56:38 -050010383 spin_unlock_irqrestore(&phba->hbalock, flags);
10384
James Smarta257bf92009-04-06 18:48:10 -040010385 /* Cancel all the IOCBs from the completions list */
10386 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10387 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010388 return 1;
10389}
10390
James Smarte59058c2008-08-24 21:49:00 -040010391/**
James Smart3621a712009-04-06 18:47:14 -040010392 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010393 * @phba: Pointer to HBA context object.
10394 *
10395 * This function cleans up all iocb, buffers, mailbox commands
10396 * while shutting down the HBA. This function is called with no
10397 * lock held and always returns 1.
10398 * This function does the following to cleanup driver resources:
10399 * - Free discovery resources for each virtual port
10400 * - Cleanup any pending fabric iocbs
10401 * - Iterate through the iocb txq and free each entry
10402 * in the list.
10403 * - Free up any buffer posted to the HBA
10404 * - Free mailbox commands in the mailbox queue.
10405 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010406int
James Smart2e0fef82007-06-17 19:56:36 -050010407lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010408{
James Smart2534ba72007-04-25 09:52:20 -040010409 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010410 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010411 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010412 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010413 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010414 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010415 int i;
10416
10417 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010418 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010419
dea31012005-04-17 16:05:31 -050010420 lpfc_hba_down_prep(phba);
10421
James Smart92d7f7b2007-06-17 19:56:38 -050010422 lpfc_fabric_abort_hba(phba);
10423
James Smart2e0fef82007-06-17 19:56:36 -050010424 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010425
James Smart895427b2017-02-12 13:52:30 -080010426 /*
10427 * Error everything on the txq since these iocbs
10428 * have not been given to the FW yet.
10429 */
10430 if (phba->sli_rev != LPFC_SLI_REV4) {
10431 for (i = 0; i < psli->num_rings; i++) {
10432 pring = &psli->sli3_ring[i];
10433 /* Only slow rings */
10434 if (pring->ringno == LPFC_ELS_RING) {
10435 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10436 /* Set the lpfc data pending flag */
10437 set_bit(LPFC_DATA_READY, &phba->data_flags);
10438 }
10439 list_splice_init(&pring->txq, &completions);
10440 }
10441 } else {
10442 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10443 pring = qp->pring;
10444 if (!pring)
10445 continue;
10446 spin_lock_irq(&pring->ring_lock);
10447 list_splice_init(&pring->txq, &completions);
10448 spin_unlock_irq(&pring->ring_lock);
10449 if (pring == phba->sli4_hba.els_wq->pring) {
10450 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10451 /* Set the lpfc data pending flag */
10452 set_bit(LPFC_DATA_READY, &phba->data_flags);
10453 }
10454 }
dea31012005-04-17 16:05:31 -050010455 }
James Smart2e0fef82007-06-17 19:56:36 -050010456 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010457
James Smarta257bf92009-04-06 18:48:10 -040010458 /* Cancel all the IOCBs from the completions list */
10459 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10460 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010461
James Smart0ff10d42008-01-11 01:52:36 -050010462 spin_lock_irqsave(&phba->hbalock, flags);
10463 list_splice_init(&phba->elsbuf, &completions);
10464 phba->elsbuf_cnt = 0;
10465 phba->elsbuf_prev_cnt = 0;
10466 spin_unlock_irqrestore(&phba->hbalock, flags);
10467
10468 while (!list_empty(&completions)) {
10469 list_remove_head(&completions, buf_ptr,
10470 struct lpfc_dmabuf, list);
10471 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10472 kfree(buf_ptr);
10473 }
10474
dea31012005-04-17 16:05:31 -050010475 /* Return any active mbox cmds */
10476 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010477
James Smartda0436e2009-05-22 14:51:39 -040010478 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010479 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010480 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010481
James Smartda0436e2009-05-22 14:51:39 -040010482 return 1;
10483}
James Smart92d7f7b2007-06-17 19:56:38 -050010484
James Smartda0436e2009-05-22 14:51:39 -040010485/**
James Smart3621a712009-04-06 18:47:14 -040010486 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010487 * @srcp: Source memory pointer.
10488 * @destp: Destination memory pointer.
10489 * @cnt: Number of words required to be copied.
10490 *
10491 * This function is used for copying data between driver memory
10492 * and the SLI memory. This function also changes the endianness
10493 * of each word if native endianness is different from SLI
10494 * endianness. This function can be called with or without
10495 * lock.
10496 **/
dea31012005-04-17 16:05:31 -050010497void
10498lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10499{
10500 uint32_t *src = srcp;
10501 uint32_t *dest = destp;
10502 uint32_t ldata;
10503 int i;
10504
10505 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10506 ldata = *src;
10507 ldata = le32_to_cpu(ldata);
10508 *dest = ldata;
10509 src++;
10510 dest++;
10511 }
10512}
10513
James Smarte59058c2008-08-24 21:49:00 -040010514
10515/**
James Smarta0c87cb2009-07-19 10:01:10 -040010516 * lpfc_sli_bemem_bcopy - SLI memory copy function
10517 * @srcp: Source memory pointer.
10518 * @destp: Destination memory pointer.
10519 * @cnt: Number of words required to be copied.
10520 *
10521 * This function is used for copying data between a data structure
10522 * with big endian representation to local endianness.
10523 * This function can be called with or without lock.
10524 **/
10525void
10526lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10527{
10528 uint32_t *src = srcp;
10529 uint32_t *dest = destp;
10530 uint32_t ldata;
10531 int i;
10532
10533 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10534 ldata = *src;
10535 ldata = be32_to_cpu(ldata);
10536 *dest = ldata;
10537 src++;
10538 dest++;
10539 }
10540}
10541
10542/**
James Smart3621a712009-04-06 18:47:14 -040010543 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010544 * @phba: Pointer to HBA context object.
10545 * @pring: Pointer to driver SLI ring object.
10546 * @mp: Pointer to driver buffer object.
10547 *
10548 * This function is called with no lock held.
10549 * It always return zero after adding the buffer to the postbufq
10550 * buffer list.
10551 **/
dea31012005-04-17 16:05:31 -050010552int
James Smart2e0fef82007-06-17 19:56:36 -050010553lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10554 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010555{
10556 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10557 later */
James Smart2e0fef82007-06-17 19:56:36 -050010558 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010559 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010560 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010561 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010562 return 0;
10563}
10564
James Smarte59058c2008-08-24 21:49:00 -040010565/**
James Smart3621a712009-04-06 18:47:14 -040010566 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010567 * @phba: Pointer to HBA context object.
10568 *
10569 * When HBQ is enabled, buffers are searched based on tags. This function
10570 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10571 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10572 * does not conflict with tags of buffer posted for unsolicited events.
10573 * The function returns the allocated tag. The function is called with
10574 * no locks held.
10575 **/
James Smart76bb24e2007-10-27 13:38:00 -040010576uint32_t
10577lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10578{
10579 spin_lock_irq(&phba->hbalock);
10580 phba->buffer_tag_count++;
10581 /*
10582 * Always set the QUE_BUFTAG_BIT to distiguish between
10583 * a tag assigned by HBQ.
10584 */
10585 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10586 spin_unlock_irq(&phba->hbalock);
10587 return phba->buffer_tag_count;
10588}
10589
James Smarte59058c2008-08-24 21:49:00 -040010590/**
James Smart3621a712009-04-06 18:47:14 -040010591 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010592 * @phba: Pointer to HBA context object.
10593 * @pring: Pointer to driver SLI ring object.
10594 * @tag: Buffer tag.
10595 *
10596 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10597 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10598 * iocb is posted to the response ring with the tag of the buffer.
10599 * This function searches the pring->postbufq list using the tag
10600 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10601 * iocb. If the buffer is found then lpfc_dmabuf object of the
10602 * buffer is returned to the caller else NULL is returned.
10603 * This function is called with no lock held.
10604 **/
James Smart76bb24e2007-10-27 13:38:00 -040010605struct lpfc_dmabuf *
10606lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10607 uint32_t tag)
10608{
10609 struct lpfc_dmabuf *mp, *next_mp;
10610 struct list_head *slp = &pring->postbufq;
10611
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010612 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010613 spin_lock_irq(&phba->hbalock);
10614 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10615 if (mp->buffer_tag == tag) {
10616 list_del_init(&mp->list);
10617 pring->postbufq_cnt--;
10618 spin_unlock_irq(&phba->hbalock);
10619 return mp;
10620 }
10621 }
10622
10623 spin_unlock_irq(&phba->hbalock);
10624 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010625 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010626 "ring %d Data x%lx x%p x%p x%x\n",
10627 pring->ringno, (unsigned long) tag,
10628 slp->next, slp->prev, pring->postbufq_cnt);
10629
10630 return NULL;
10631}
dea31012005-04-17 16:05:31 -050010632
James Smarte59058c2008-08-24 21:49:00 -040010633/**
James Smart3621a712009-04-06 18:47:14 -040010634 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010635 * @phba: Pointer to HBA context object.
10636 * @pring: Pointer to driver SLI ring object.
10637 * @phys: DMA address of the buffer.
10638 *
10639 * This function searches the buffer list using the dma_address
10640 * of unsolicited event to find the driver's lpfc_dmabuf object
10641 * corresponding to the dma_address. The function returns the
10642 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10643 * This function is called by the ct and els unsolicited event
10644 * handlers to get the buffer associated with the unsolicited
10645 * event.
10646 *
10647 * This function is called with no lock held.
10648 **/
dea31012005-04-17 16:05:31 -050010649struct lpfc_dmabuf *
10650lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10651 dma_addr_t phys)
10652{
10653 struct lpfc_dmabuf *mp, *next_mp;
10654 struct list_head *slp = &pring->postbufq;
10655
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010656 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010657 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010658 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10659 if (mp->phys == phys) {
10660 list_del_init(&mp->list);
10661 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010662 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010663 return mp;
10664 }
10665 }
10666
James Smart2e0fef82007-06-17 19:56:36 -050010667 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010668 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010669 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010670 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010671 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010672 slp->next, slp->prev, pring->postbufq_cnt);
10673 return NULL;
10674}
10675
James Smarte59058c2008-08-24 21:49:00 -040010676/**
James Smart3621a712009-04-06 18:47:14 -040010677 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010678 * @phba: Pointer to HBA context object.
10679 * @cmdiocb: Pointer to driver command iocb object.
10680 * @rspiocb: Pointer to driver response iocb object.
10681 *
10682 * This function is the completion handler for the abort iocbs for
10683 * ELS commands. This function is called from the ELS ring event
10684 * handler with no lock held. This function frees memory resources
10685 * associated with the abort iocb.
10686 **/
dea31012005-04-17 16:05:31 -050010687static void
James Smart2e0fef82007-06-17 19:56:36 -050010688lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10689 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010690{
James Smart2e0fef82007-06-17 19:56:36 -050010691 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010692 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010693 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010694
10695 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010696
10697 /*
10698 * Assume that the port already completed and returned, or
10699 * will return the iocb. Just Log the message.
10700 */
James Smart2680eea2007-04-25 09:52:55 -040010701 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10702 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10703
James Smart2e0fef82007-06-17 19:56:36 -050010704 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010705 if (phba->sli_rev < LPFC_SLI_REV4) {
10706 if (abort_iotag != 0 &&
10707 abort_iotag <= phba->sli.last_iotag)
10708 abort_iocb =
10709 phba->sli.iocbq_lookup[abort_iotag];
10710 } else
10711 /* For sli4 the abort_tag is the XRI,
10712 * so the abort routine puts the iotag of the iocb
10713 * being aborted in the context field of the abort
10714 * IOCB.
10715 */
10716 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010717
James Smart2a9bf3d2010-06-07 15:24:45 -040010718 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10719 "0327 Cannot abort els iocb %p "
10720 "with tag %x context %x, abort status %x, "
10721 "abort code %x\n",
10722 abort_iocb, abort_iotag, abort_context,
10723 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010724
James Smartff78d8f2011-12-13 13:21:35 -050010725 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010726 }
James Bottomley604a3e32005-10-29 10:28:33 -050010727 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010728 return;
10729}
10730
James Smarte59058c2008-08-24 21:49:00 -040010731/**
James Smart3621a712009-04-06 18:47:14 -040010732 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010733 * @phba: Pointer to HBA context object.
10734 * @cmdiocb: Pointer to driver command iocb object.
10735 * @rspiocb: Pointer to driver response iocb object.
10736 *
10737 * The function is called from SLI ring event handler with no
10738 * lock held. This function is the completion handler for ELS commands
10739 * which are aborted. The function frees memory resources used for
10740 * the aborted ELS commands.
10741 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010742static void
10743lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10744 struct lpfc_iocbq *rspiocb)
10745{
10746 IOCB_t *irsp = &rspiocb->iocb;
10747
10748 /* ELS cmd tag <ulpIoTag> completes */
10749 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010750 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010751 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010752 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010753 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010754 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10755 lpfc_ct_free_iocb(phba, cmdiocb);
10756 else
10757 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010758 return;
10759}
10760
James Smarte59058c2008-08-24 21:49:00 -040010761/**
James Smart5af5eee2010-10-22 11:06:38 -040010762 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010763 * @phba: Pointer to HBA context object.
10764 * @pring: Pointer to driver SLI ring object.
10765 * @cmdiocb: Pointer to driver command iocb object.
10766 *
James Smart5af5eee2010-10-22 11:06:38 -040010767 * This function issues an abort iocb for the provided command iocb down to
10768 * the port. Other than the case the outstanding command iocb is an abort
10769 * request, this function issues abort out unconditionally. This function is
10770 * called with hbalock held. The function returns 0 when it fails due to
10771 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010772 **/
James Smart5af5eee2010-10-22 11:06:38 -040010773static int
10774lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010775 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010776{
James Smart2e0fef82007-06-17 19:56:36 -050010777 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010778 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010779 IOCB_t *icmd = NULL;
10780 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010781 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010782 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010783
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010784 lockdep_assert_held(&phba->hbalock);
10785
James Smart92d7f7b2007-06-17 19:56:38 -050010786 /*
10787 * There are certain command types we don't want to abort. And we
10788 * don't want to abort commands that are already in the process of
10789 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010790 */
10791 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010792 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010793 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10794 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010795 return 0;
10796
dea31012005-04-17 16:05:31 -050010797 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010798 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010799 if (abtsiocbp == NULL)
10800 return 0;
dea31012005-04-17 16:05:31 -050010801
James Smart07951072007-04-25 09:51:38 -040010802 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010803 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010804 */
10805 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10806
dea31012005-04-17 16:05:31 -050010807 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010808 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10809 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010810 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010811 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010812 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10813 }
James Smartda0436e2009-05-22 14:51:39 -040010814 else
10815 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010816 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010817 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010818
James Smart5ffc2662009-11-18 15:39:44 -050010819 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010820 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010821 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10822 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010823 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10824 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010825
James Smart2e0fef82007-06-17 19:56:36 -050010826 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010827 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10828 else
10829 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10830
10831 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010832 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010833
James Smarte8b62012007-08-02 11:10:09 -040010834 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10835 "0339 Abort xri x%x, original iotag x%x, "
10836 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010837 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010838 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010839 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010840
10841 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010842 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10843 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010844 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010845 /* Note: both hbalock and ring_lock need to be set here */
10846 spin_lock_irqsave(&pring->ring_lock, iflags);
10847 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10848 abtsiocbp, 0);
10849 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10850 } else {
10851 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10852 abtsiocbp, 0);
10853 }
James Smart07951072007-04-25 09:51:38 -040010854
James Smartd7c255b2008-08-24 21:50:00 -040010855 if (retval)
10856 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010857
10858 /*
10859 * Caller to this routine should check for IOCB_ERROR
10860 * and handle it properly. This routine no longer removes
10861 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10862 */
10863 return retval;
10864}
10865
10866/**
10867 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10868 * @phba: Pointer to HBA context object.
10869 * @pring: Pointer to driver SLI ring object.
10870 * @cmdiocb: Pointer to driver command iocb object.
10871 *
10872 * This function issues an abort iocb for the provided command iocb. In case
10873 * of unloading, the abort iocb will not be issued to commands on the ELS
10874 * ring. Instead, the callback function shall be changed to those commands
10875 * so that nothing happens when them finishes. This function is called with
10876 * hbalock held. The function returns 0 when the command iocb is an abort
10877 * request.
10878 **/
10879int
10880lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10881 struct lpfc_iocbq *cmdiocb)
10882{
10883 struct lpfc_vport *vport = cmdiocb->vport;
10884 int retval = IOCB_ERROR;
10885 IOCB_t *icmd = NULL;
10886
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010887 lockdep_assert_held(&phba->hbalock);
10888
James Smart5af5eee2010-10-22 11:06:38 -040010889 /*
10890 * There are certain command types we don't want to abort. And we
10891 * don't want to abort commands that are already in the process of
10892 * being aborted.
10893 */
10894 icmd = &cmdiocb->iocb;
10895 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10896 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10897 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10898 return 0;
10899
Dick Kennedy1234a6d2017-09-29 17:34:29 -070010900 if (!pring) {
10901 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10902 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10903 else
10904 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10905 goto abort_iotag_exit;
10906 }
10907
James Smart5af5eee2010-10-22 11:06:38 -040010908 /*
10909 * If we're unloading, don't abort iocb on the ELS ring, but change
10910 * the callback so that nothing happens when it finishes.
10911 */
10912 if ((vport->load_flag & FC_UNLOADING) &&
10913 (pring->ringno == LPFC_ELS_RING)) {
10914 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10915 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10916 else
10917 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10918 goto abort_iotag_exit;
10919 }
10920
10921 /* Now, we try to issue the abort to the cmdiocb out */
10922 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10923
James Smart07951072007-04-25 09:51:38 -040010924abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010925 /*
10926 * Caller to this routine should check for IOCB_ERROR
10927 * and handle it properly. This routine no longer removes
10928 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010929 */
James Smart2e0fef82007-06-17 19:56:36 -050010930 return retval;
dea31012005-04-17 16:05:31 -050010931}
10932
James Smarte59058c2008-08-24 21:49:00 -040010933/**
James Smart895427b2017-02-12 13:52:30 -080010934 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10935 * @phba: Pointer to HBA context object.
10936 * @pring: Pointer to driver SLI ring object.
10937 * @cmdiocb: Pointer to driver command iocb object.
10938 *
10939 * This function issues an abort iocb for the provided command iocb down to
10940 * the port. Other than the case the outstanding command iocb is an abort
10941 * request, this function issues abort out unconditionally. This function is
10942 * called with hbalock held. The function returns 0 when it fails due to
10943 * memory allocation failure or when the command iocb is an abort request.
10944 **/
10945static int
10946lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10947 struct lpfc_iocbq *cmdiocb)
10948{
10949 struct lpfc_vport *vport = cmdiocb->vport;
10950 struct lpfc_iocbq *abtsiocbp;
James Smart205e8242018-03-05 12:04:03 -080010951 union lpfc_wqe128 *abts_wqe;
James Smart895427b2017-02-12 13:52:30 -080010952 int retval;
10953
10954 /*
10955 * There are certain command types we don't want to abort. And we
10956 * don't want to abort commands that are already in the process of
10957 * being aborted.
10958 */
10959 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10960 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10961 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10962 return 0;
10963
10964 /* issue ABTS for this io based on iotag */
10965 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10966 if (abtsiocbp == NULL)
10967 return 0;
10968
10969 /* This signals the response to set the correct status
10970 * before calling the completion handler
10971 */
10972 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10973
10974 /* Complete prepping the abort wqe and issue to the FW. */
10975 abts_wqe = &abtsiocbp->wqe;
10976 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10977 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10978
10979 /* Explicitly set reserved fields to zero.*/
10980 abts_wqe->abort_cmd.rsrvd4 = 0;
10981 abts_wqe->abort_cmd.rsrvd5 = 0;
10982
10983 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10984 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10985 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10986
10987 /* word 7 */
10988 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10989 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10990 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10991 cmdiocb->iocb.ulpClass);
10992
10993 /* word 8 - tell the FW to abort the IO associated with this
10994 * outstanding exchange ID.
10995 */
10996 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10997
10998 /* word 9 - this is the iotag for the abts_wqe completion. */
10999 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
11000 abtsiocbp->iotag);
11001
11002 /* word 10 */
11003 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
11004 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
11005 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
11006
11007 /* word 11 */
11008 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11009 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
11010 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
11011
11012 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11013 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
11014 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080011015 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080011016 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070011017 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080011018 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11019 "6147 Failed abts issue_wqe with status x%x "
11020 "for oxid x%x\n",
11021 retval, cmdiocb->sli4_xritag);
11022 lpfc_sli_release_iocbq(phba, abtsiocbp);
11023 return retval;
11024 }
11025
11026 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11027 "6148 Drv Abort NVME Request Issued for "
11028 "ox_id x%x on reqtag x%x\n",
11029 cmdiocb->sli4_xritag,
11030 abtsiocbp->iotag);
11031
11032 return retval;
11033}
11034
11035/**
James Smart5af5eee2010-10-22 11:06:38 -040011036 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11037 * @phba: pointer to lpfc HBA data structure.
11038 *
11039 * This routine will abort all pending and outstanding iocbs to an HBA.
11040 **/
11041void
11042lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11043{
11044 struct lpfc_sli *psli = &phba->sli;
11045 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011046 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011047 int i;
11048
James Smart895427b2017-02-12 13:52:30 -080011049 if (phba->sli_rev != LPFC_SLI_REV4) {
11050 for (i = 0; i < psli->num_rings; i++) {
11051 pring = &psli->sli3_ring[i];
11052 lpfc_sli_abort_iocb_ring(phba, pring);
11053 }
11054 return;
11055 }
11056 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11057 pring = qp->pring;
11058 if (!pring)
11059 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011060 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011061 }
11062}
11063
11064/**
James Smart3621a712009-04-06 18:47:14 -040011065 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011066 * @iocbq: Pointer to driver iocb object.
11067 * @vport: Pointer to driver virtual port object.
11068 * @tgt_id: SCSI ID of the target.
11069 * @lun_id: LUN ID of the scsi device.
11070 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11071 *
James Smart3621a712009-04-06 18:47:14 -040011072 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011073 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11074 * 0 if the filtering criteria is met for the given iocb and will return
11075 * 1 if the filtering criteria is not met.
11076 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11077 * given iocb is for the SCSI device specified by vport, tgt_id and
11078 * lun_id parameter.
11079 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11080 * given iocb is for the SCSI target specified by vport and tgt_id
11081 * parameters.
11082 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11083 * given iocb is for the SCSI host associated with the given vport.
11084 * This function is called with no locks held.
11085 **/
dea31012005-04-17 16:05:31 -050011086static int
James Smart51ef4c22007-08-02 11:10:31 -040011087lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11088 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011089 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011090{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011091 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011092 int rc = 1;
11093
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011094 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
11095 return rc;
11096
James Smart51ef4c22007-08-02 11:10:31 -040011097 if (iocbq->vport != vport)
11098 return rc;
11099
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011100 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011101
James Smart495a7142008-06-14 22:52:59 -040011102 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011103 return rc;
11104
11105 switch (ctx_cmd) {
11106 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040011107 if ((lpfc_cmd->rdata->pnode) &&
11108 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11109 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011110 rc = 0;
11111 break;
11112 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040011113 if ((lpfc_cmd->rdata->pnode) &&
11114 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011115 rc = 0;
11116 break;
dea31012005-04-17 16:05:31 -050011117 case LPFC_CTX_HOST:
11118 rc = 0;
11119 break;
11120 default:
11121 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011122 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011123 break;
11124 }
11125
11126 return rc;
11127}
11128
James Smarte59058c2008-08-24 21:49:00 -040011129/**
James Smart3621a712009-04-06 18:47:14 -040011130 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011131 * @vport: Pointer to virtual port.
11132 * @tgt_id: SCSI ID of the target.
11133 * @lun_id: LUN ID of the scsi device.
11134 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11135 *
11136 * This function returns number of FCP commands pending for the vport.
11137 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11138 * commands pending on the vport associated with SCSI device specified
11139 * by tgt_id and lun_id parameters.
11140 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11141 * commands pending on the vport associated with SCSI target specified
11142 * by tgt_id parameter.
11143 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11144 * commands pending on the vport.
11145 * This function returns the number of iocbs which satisfy the filter.
11146 * This function is called without any lock held.
11147 **/
dea31012005-04-17 16:05:31 -050011148int
James Smart51ef4c22007-08-02 11:10:31 -040011149lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11150 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011151{
James Smart51ef4c22007-08-02 11:10:31 -040011152 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011153 struct lpfc_iocbq *iocbq;
11154 int sum, i;
dea31012005-04-17 16:05:31 -050011155
Johannes Thumshirn31979002016-07-18 16:06:03 +020011156 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011157 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11158 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011159
James Smart51ef4c22007-08-02 11:10:31 -040011160 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11161 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011162 sum++;
dea31012005-04-17 16:05:31 -050011163 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011164 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011165
dea31012005-04-17 16:05:31 -050011166 return sum;
11167}
11168
James Smarte59058c2008-08-24 21:49:00 -040011169/**
James Smart3621a712009-04-06 18:47:14 -040011170 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011171 * @phba: Pointer to HBA context object
11172 * @cmdiocb: Pointer to command iocb object.
11173 * @rspiocb: Pointer to response iocb object.
11174 *
11175 * This function is called when an aborted FCP iocb completes. This
11176 * function is called by the ring event handler with no lock held.
11177 * This function frees the iocb.
11178 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011179void
James Smart2e0fef82007-06-17 19:56:36 -050011180lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11181 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011182{
James Smartcb69f7d2011-12-13 13:21:57 -050011183 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011184 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011185 "original iotag x%x, abort cmd iotag x%x "
11186 "status 0x%x, reason 0x%x\n",
11187 cmdiocb->iocb.un.acxri.abortContextTag,
11188 cmdiocb->iocb.un.acxri.abortIoTag,
11189 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11190 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011191 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011192 return;
11193}
11194
James Smarte59058c2008-08-24 21:49:00 -040011195/**
James Smart3621a712009-04-06 18:47:14 -040011196 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011197 * @vport: Pointer to virtual port.
11198 * @pring: Pointer to driver SLI ring object.
11199 * @tgt_id: SCSI ID of the target.
11200 * @lun_id: LUN ID of the scsi device.
11201 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11202 *
11203 * This function sends an abort command for every SCSI command
11204 * associated with the given virtual port pending on the ring
11205 * filtered by lpfc_sli_validate_fcp_iocb function.
11206 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11207 * FCP iocbs associated with lun specified by tgt_id and lun_id
11208 * parameters
11209 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11210 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11211 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11212 * FCP iocbs associated with virtual port.
11213 * This function returns number of iocbs it failed to abort.
11214 * This function is called with no locks held.
11215 **/
dea31012005-04-17 16:05:31 -050011216int
James Smart51ef4c22007-08-02 11:10:31 -040011217lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11218 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011219{
James Smart51ef4c22007-08-02 11:10:31 -040011220 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011221 struct lpfc_iocbq *iocbq;
11222 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011223 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011224 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011225 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011226 int i;
dea31012005-04-17 16:05:31 -050011227
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011228 for (i = 1; i <= phba->sli.last_iotag; i++) {
11229 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011230
James Smart51ef4c22007-08-02 11:10:31 -040011231 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011232 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011233 continue;
11234
James Smartafbd8d82013-09-06 12:22:13 -040011235 /*
11236 * If the iocbq is already being aborted, don't take a second
11237 * action, but do count it.
11238 */
11239 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11240 continue;
11241
dea31012005-04-17 16:05:31 -050011242 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011243 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011244 if (abtsiocb == NULL) {
11245 errcnt++;
11246 continue;
11247 }
dea31012005-04-17 16:05:31 -050011248
James Smartafbd8d82013-09-06 12:22:13 -040011249 /* indicate the IO is being aborted by the driver. */
11250 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11251
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011252 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011253 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11254 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011255 if (phba->sli_rev == LPFC_SLI_REV4)
11256 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11257 else
11258 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011259 abtsiocb->iocb.ulpLe = 1;
11260 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011261 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011262
James Smart5ffc2662009-11-18 15:39:44 -050011263 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011264 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011265 if (iocbq->iocb_flag & LPFC_IO_FCP)
11266 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011267 if (iocbq->iocb_flag & LPFC_IO_FOF)
11268 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011269
James Smart2e0fef82007-06-17 19:56:36 -050011270 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011271 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11272 else
11273 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11274
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011275 /* Setup callback routine and issue the command. */
11276 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011277 if (phba->sli_rev == LPFC_SLI_REV4) {
11278 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11279 if (!pring_s4)
11280 continue;
11281 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11282 abtsiocb, 0);
11283 } else
11284 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11285 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011286 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011287 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011288 errcnt++;
11289 continue;
11290 }
11291 }
11292
11293 return errcnt;
11294}
11295
James Smarte59058c2008-08-24 21:49:00 -040011296/**
James Smart98912dda2014-04-04 13:52:31 -040011297 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11298 * @vport: Pointer to virtual port.
11299 * @pring: Pointer to driver SLI ring object.
11300 * @tgt_id: SCSI ID of the target.
11301 * @lun_id: LUN ID of the scsi device.
11302 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11303 *
11304 * This function sends an abort command for every SCSI command
11305 * associated with the given virtual port pending on the ring
11306 * filtered by lpfc_sli_validate_fcp_iocb function.
11307 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11308 * FCP iocbs associated with lun specified by tgt_id and lun_id
11309 * parameters
11310 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11311 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11312 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11313 * FCP iocbs associated with virtual port.
11314 * This function returns number of iocbs it aborted .
11315 * This function is called with no locks held right after a taskmgmt
11316 * command is sent.
11317 **/
11318int
11319lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11320 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11321{
11322 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011323 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011324 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011325 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011326 struct lpfc_iocbq *iocbq;
11327 IOCB_t *icmd;
11328 int sum, i, ret_val;
11329 unsigned long iflags;
11330 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011331
James Smart59c68ea2018-04-09 14:24:25 -070011332 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011333
11334 /* all I/Os are in process of being flushed */
11335 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070011336 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011337 return 0;
11338 }
11339 sum = 0;
11340
11341 for (i = 1; i <= phba->sli.last_iotag; i++) {
11342 iocbq = phba->sli.iocbq_lookup[i];
11343
11344 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11345 cmd) != 0)
11346 continue;
11347
11348 /*
11349 * If the iocbq is already being aborted, don't take a second
11350 * action, but do count it.
11351 */
11352 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11353 continue;
11354
11355 /* issue ABTS for this IOCB based on iotag */
11356 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11357 if (abtsiocbq == NULL)
11358 continue;
11359
11360 icmd = &iocbq->iocb;
11361 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11362 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11363 if (phba->sli_rev == LPFC_SLI_REV4)
11364 abtsiocbq->iocb.un.acxri.abortIoTag =
11365 iocbq->sli4_xritag;
11366 else
11367 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11368 abtsiocbq->iocb.ulpLe = 1;
11369 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11370 abtsiocbq->vport = vport;
11371
11372 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011373 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011374 if (iocbq->iocb_flag & LPFC_IO_FCP)
11375 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011376 if (iocbq->iocb_flag & LPFC_IO_FOF)
11377 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011378
James Smart8c50d252014-09-03 12:58:16 -040011379 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11380 ndlp = lpfc_cmd->rdata->pnode;
11381
11382 if (lpfc_is_link_up(phba) &&
11383 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011384 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11385 else
11386 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11387
11388 /* Setup callback routine and issue the command. */
11389 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11390
11391 /*
11392 * Indicate the IO is being aborted by the driver and set
11393 * the caller's flag into the aborted IO.
11394 */
11395 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11396
11397 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart59c68ea2018-04-09 14:24:25 -070011398 pring_s4 = lpfc_sli4_calc_ring(phba, abtsiocbq);
11399 if (!pring_s4)
James Smart895427b2017-02-12 13:52:30 -080011400 continue;
James Smart98912dda2014-04-04 13:52:31 -040011401 /* Note: both hbalock and ring_lock must be set here */
James Smart59c68ea2018-04-09 14:24:25 -070011402 spin_lock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011403 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11404 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070011405 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011406 } else {
11407 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11408 abtsiocbq, 0);
11409 }
11410
11411
11412 if (ret_val == IOCB_ERROR)
11413 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11414 else
11415 sum++;
11416 }
James Smart59c68ea2018-04-09 14:24:25 -070011417 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011418 return sum;
11419}
11420
11421/**
James Smart3621a712009-04-06 18:47:14 -040011422 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011423 * @phba: Pointer to HBA context object.
11424 * @cmdiocbq: Pointer to command iocb.
11425 * @rspiocbq: Pointer to response iocb.
11426 *
11427 * This function is the completion handler for iocbs issued using
11428 * lpfc_sli_issue_iocb_wait function. This function is called by the
11429 * ring event handler function without any lock held. This function
11430 * can be called from both worker thread context and interrupt
11431 * context. This function also can be called from other thread which
11432 * cleans up the SLI layer objects.
11433 * This function copy the contents of the response iocb to the
11434 * response iocb memory object provided by the caller of
11435 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11436 * sleeps for the iocb completion.
11437 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011438static void
11439lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11440 struct lpfc_iocbq *cmdiocbq,
11441 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011442{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011443 wait_queue_head_t *pdone_q;
11444 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011445 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011446
James Smart2e0fef82007-06-17 19:56:36 -050011447 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011448 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11449
11450 /*
11451 * A time out has occurred for the iocb. If a time out
11452 * completion handler has been supplied, call it. Otherwise,
11453 * just free the iocbq.
11454 */
11455
11456 spin_unlock_irqrestore(&phba->hbalock, iflags);
11457 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11458 cmdiocbq->wait_iocb_cmpl = NULL;
11459 if (cmdiocbq->iocb_cmpl)
11460 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11461 else
11462 lpfc_sli_release_iocbq(phba, cmdiocbq);
11463 return;
11464 }
11465
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011466 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11467 if (cmdiocbq->context2 && rspiocbq)
11468 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11469 &rspiocbq->iocb, sizeof(IOCB_t));
11470
James Smart0f65ff62010-02-26 14:14:23 -050011471 /* Set the exchange busy flag for task management commands */
11472 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11473 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11474 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11475 cur_iocbq);
11476 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11477 }
11478
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011479 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011480 if (pdone_q)
11481 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011482 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011483 return;
11484}
11485
James Smarte59058c2008-08-24 21:49:00 -040011486/**
James Smartd11e31d2009-06-10 17:23:06 -040011487 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11488 * @phba: Pointer to HBA context object..
11489 * @piocbq: Pointer to command iocb.
11490 * @flag: Flag to test.
11491 *
11492 * This routine grabs the hbalock and then test the iocb_flag to
11493 * see if the passed in flag is set.
11494 * Returns:
11495 * 1 if flag is set.
11496 * 0 if flag is not set.
11497 **/
11498static int
11499lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11500 struct lpfc_iocbq *piocbq, uint32_t flag)
11501{
11502 unsigned long iflags;
11503 int ret;
11504
11505 spin_lock_irqsave(&phba->hbalock, iflags);
11506 ret = piocbq->iocb_flag & flag;
11507 spin_unlock_irqrestore(&phba->hbalock, iflags);
11508 return ret;
11509
11510}
11511
11512/**
James Smart3621a712009-04-06 18:47:14 -040011513 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011514 * @phba: Pointer to HBA context object..
11515 * @pring: Pointer to sli ring.
11516 * @piocb: Pointer to command iocb.
11517 * @prspiocbq: Pointer to response iocb.
11518 * @timeout: Timeout in number of seconds.
11519 *
11520 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011521 * iocb to complete. The iocb_cmpl field of the shall be used
11522 * to handle iocbs which time out. If the field is NULL, the
11523 * function shall free the iocbq structure. If more clean up is
11524 * needed, the caller is expected to provide a completion function
11525 * that will provide the needed clean up. If the iocb command is
11526 * not completed within timeout seconds, the function will either
11527 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11528 * completion function set in the iocb_cmpl field and then return
11529 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11530 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011531 * The function waits for the iocb completion using an
11532 * non-interruptible wait.
11533 * This function will sleep while waiting for iocb completion.
11534 * So, this function should not be called from any context which
11535 * does not allow sleeping. Due to the same reason, this function
11536 * cannot be called with interrupt disabled.
11537 * This function assumes that the iocb completions occur while
11538 * this function sleep. So, this function cannot be called from
11539 * the thread which process iocb completion for this ring.
11540 * This function clears the iocb_flag of the iocb object before
11541 * issuing the iocb and the iocb completion handler sets this
11542 * flag and wakes this thread when the iocb completes.
11543 * The contents of the response iocb will be copied to prspiocbq
11544 * by the completion handler when the command completes.
11545 * This function returns IOCB_SUCCESS when success.
11546 * This function is called with no lock held.
11547 **/
dea31012005-04-17 16:05:31 -050011548int
James Smart2e0fef82007-06-17 19:56:36 -050011549lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011550 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011551 struct lpfc_iocbq *piocb,
11552 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011553 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011554{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011555 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011556 long timeleft, timeout_req = 0;
11557 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011558 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011559 struct lpfc_iocbq *iocb;
11560 int txq_cnt = 0;
11561 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011562 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011563 unsigned long iflags;
11564 bool iocb_completed = true;
11565
James Smart895427b2017-02-12 13:52:30 -080011566 if (phba->sli_rev >= LPFC_SLI_REV4)
11567 pring = lpfc_sli4_calc_ring(phba, piocb);
11568 else
11569 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011570 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011571 * If the caller has provided a response iocbq buffer, then context2
11572 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011573 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011574 if (prspiocbq) {
11575 if (piocb->context2)
11576 return IOCB_ERROR;
11577 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011578 }
11579
James Smart5a0916b2013-07-15 18:31:42 -040011580 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011581 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11582 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011583 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011584
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011585 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011586 if (lpfc_readl(phba->HCregaddr, &creg_val))
11587 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011588 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11589 writel(creg_val, phba->HCregaddr);
11590 readl(phba->HCregaddr); /* flush */
11591 }
11592
James Smart2a9bf3d2010-06-07 15:24:45 -040011593 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11594 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011595 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011596 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011597 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011598 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011599 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011600 spin_lock_irqsave(&phba->hbalock, iflags);
11601 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011602
James Smart5a0916b2013-07-15 18:31:42 -040011603 /*
11604 * IOCB timed out. Inform the wake iocb wait
11605 * completion function and set local status
11606 */
11607
11608 iocb_completed = false;
11609 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11610 }
11611 spin_unlock_irqrestore(&phba->hbalock, iflags);
11612 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011613 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011614 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011615 /* Note: we are not indicating if the IOCB has a success
11616 * status or not - that's for the caller to check.
11617 * IOCB_SUCCESS means just that the command was sent and
11618 * completed. Not that it completed successfully.
11619 * */
James Smart7054a602007-04-25 09:52:34 -040011620 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011621 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011622 "0338 IOCB wait timeout error - no "
11623 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011624 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011625 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011626 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011627 "0330 IOCB wake NOT set, "
11628 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011629 timeout, (timeleft / jiffies));
11630 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011631 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011632 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011633 if (phba->cfg_log_verbose & LOG_SLI) {
11634 list_for_each_entry(iocb, &pring->txq, list) {
11635 txq_cnt++;
11636 }
11637 list_for_each_entry(iocb, &pring->txcmplq, list) {
11638 txcmplq_cnt++;
11639 }
11640 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11641 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11642 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11643 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011644 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011645 } else {
11646 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011647 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011648 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011649 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011650 }
11651
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011652 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011653 if (lpfc_readl(phba->HCregaddr, &creg_val))
11654 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011655 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11656 writel(creg_val, phba->HCregaddr);
11657 readl(phba->HCregaddr); /* flush */
11658 }
11659
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011660 if (prspiocbq)
11661 piocb->context2 = NULL;
11662
11663 piocb->context_un.wait_queue = NULL;
11664 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011665 return retval;
11666}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011667
James Smarte59058c2008-08-24 21:49:00 -040011668/**
James Smart3621a712009-04-06 18:47:14 -040011669 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011670 * @phba: Pointer to HBA context object.
11671 * @pmboxq: Pointer to driver mailbox object.
11672 * @timeout: Timeout in number of seconds.
11673 *
11674 * This function issues the mailbox to firmware and waits for the
11675 * mailbox command to complete. If the mailbox command is not
11676 * completed within timeout seconds, it returns MBX_TIMEOUT.
11677 * The function waits for the mailbox completion using an
11678 * interruptible wait. If the thread is woken up due to a
11679 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11680 * should not free the mailbox resources, if this function returns
11681 * MBX_TIMEOUT.
11682 * This function will sleep while waiting for mailbox completion.
11683 * So, this function should not be called from any context which
11684 * does not allow sleeping. Due to the same reason, this function
11685 * cannot be called with interrupt disabled.
11686 * This function assumes that the mailbox completion occurs while
11687 * this function sleep. So, this function cannot be called from
11688 * the worker thread which processes mailbox completion.
11689 * This function is called in the context of HBA management
11690 * applications.
11691 * This function returns MBX_SUCCESS when successful.
11692 * This function is called with no lock held.
11693 **/
dea31012005-04-17 16:05:31 -050011694int
James Smart2e0fef82007-06-17 19:56:36 -050011695lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011696 uint32_t timeout)
11697{
James Smarte29d74f2018-03-05 12:04:07 -080011698 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050011699 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011700 unsigned long flag;
dea31012005-04-17 16:05:31 -050011701
James Smart495a7142008-06-14 22:52:59 -040011702 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011703 /* setup wake call as IOCB callback */
11704 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050011705
James Smarte29d74f2018-03-05 12:04:07 -080011706 /* setup context3 field to pass wait_queue pointer to wake function */
11707 init_completion(&mbox_done);
11708 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050011709 /* now issue the command */
11710 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011711 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080011712 wait_for_completion_timeout(&mbox_done,
11713 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011714
James Smart858c9f62007-06-17 19:56:39 -050011715 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080011716 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040011717 /*
11718 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11719 * else do not free the resources.
11720 */
James Smartd7c47992010-06-08 18:31:54 -040011721 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011722 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011723 } else {
James Smart7054a602007-04-25 09:52:34 -040011724 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011725 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11726 }
11727 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050011728 }
dea31012005-04-17 16:05:31 -050011729 return retval;
11730}
11731
James Smarte59058c2008-08-24 21:49:00 -040011732/**
James Smart3772a992009-05-22 14:50:54 -040011733 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011734 * @phba: Pointer to HBA context.
11735 *
James Smart3772a992009-05-22 14:50:54 -040011736 * This function is called to shutdown the driver's mailbox sub-system.
11737 * It first marks the mailbox sub-system is in a block state to prevent
11738 * the asynchronous mailbox command from issued off the pending mailbox
11739 * command queue. If the mailbox command sub-system shutdown is due to
11740 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11741 * the mailbox sub-system flush routine to forcefully bring down the
11742 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11743 * as with offline or HBA function reset), this routine will wait for the
11744 * outstanding mailbox command to complete before invoking the mailbox
11745 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011746 **/
James Smart3772a992009-05-22 14:50:54 -040011747void
James Smart618a5232012-06-12 13:54:36 -040011748lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011749{
James Smart3772a992009-05-22 14:50:54 -040011750 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011751 unsigned long timeout;
11752
James Smart618a5232012-06-12 13:54:36 -040011753 if (mbx_action == LPFC_MBX_NO_WAIT) {
11754 /* delay 100ms for port state */
11755 msleep(100);
11756 lpfc_sli_mbox_sys_flush(phba);
11757 return;
11758 }
James Smarta183a152011-10-10 21:32:43 -040011759 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011760
James Smart3772a992009-05-22 14:50:54 -040011761 spin_lock_irq(&phba->hbalock);
11762 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011763
11764 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011765 /* Determine how long we might wait for the active mailbox
11766 * command to be gracefully completed by firmware.
11767 */
James Smarta183a152011-10-10 21:32:43 -040011768 if (phba->sli.mbox_active)
11769 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11770 phba->sli.mbox_active) *
11771 1000) + jiffies;
11772 spin_unlock_irq(&phba->hbalock);
11773
James Smart3772a992009-05-22 14:50:54 -040011774 while (phba->sli.mbox_active) {
11775 /* Check active mailbox complete status every 2ms */
11776 msleep(2);
11777 if (time_after(jiffies, timeout))
11778 /* Timeout, let the mailbox flush routine to
11779 * forcefully release active mailbox command
11780 */
11781 break;
11782 }
James Smartd7069f02012-03-01 22:36:29 -050011783 } else
11784 spin_unlock_irq(&phba->hbalock);
11785
James Smart3772a992009-05-22 14:50:54 -040011786 lpfc_sli_mbox_sys_flush(phba);
11787}
11788
11789/**
11790 * lpfc_sli_eratt_read - read sli-3 error attention events
11791 * @phba: Pointer to HBA context.
11792 *
11793 * This function is called to read the SLI3 device error attention registers
11794 * for possible error attention events. The caller must hold the hostlock
11795 * with spin_lock_irq().
11796 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011797 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011798 * Register and returns 0 otherwise.
11799 **/
11800static int
11801lpfc_sli_eratt_read(struct lpfc_hba *phba)
11802{
James Smarted957682007-06-17 19:56:37 -050011803 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011804
James Smart3772a992009-05-22 14:50:54 -040011805 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011806 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11807 goto unplug_err;
11808
James Smart3772a992009-05-22 14:50:54 -040011809 if (ha_copy & HA_ERATT) {
11810 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011811 if (lpfc_sli_read_hs(phba))
11812 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011813
James Smart3772a992009-05-22 14:50:54 -040011814 /* Check if there is a deferred error condition is active */
11815 if ((HS_FFER1 & phba->work_hs) &&
11816 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011817 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011818 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011819 /* Clear all interrupt enable conditions */
11820 writel(0, phba->HCregaddr);
11821 readl(phba->HCregaddr);
11822 }
11823
11824 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011825 phba->work_ha |= HA_ERATT;
11826 /* Indicate polling handles this ERATT */
11827 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011828 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011829 }
James Smart3772a992009-05-22 14:50:54 -040011830 return 0;
James Smart9940b972011-03-11 16:06:12 -050011831
11832unplug_err:
11833 /* Set the driver HS work bitmap */
11834 phba->work_hs |= UNPLUG_ERR;
11835 /* Set the driver HA work bitmap */
11836 phba->work_ha |= HA_ERATT;
11837 /* Indicate polling handles this ERATT */
11838 phba->hba_flag |= HBA_ERATT_HANDLED;
11839 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011840}
11841
James Smarte59058c2008-08-24 21:49:00 -040011842/**
James Smartda0436e2009-05-22 14:51:39 -040011843 * lpfc_sli4_eratt_read - read sli-4 error attention events
11844 * @phba: Pointer to HBA context.
11845 *
11846 * This function is called to read the SLI4 device error attention registers
11847 * for possible error attention events. The caller must hold the hostlock
11848 * with spin_lock_irq().
11849 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011850 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011851 * Register and returns 0 otherwise.
11852 **/
11853static int
11854lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11855{
11856 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011857 uint32_t if_type, portsmphr;
11858 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011859
James Smart2fcee4b2010-12-15 17:57:46 -050011860 /*
11861 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011862 * registers for error attention. This can be changed later.
11863 */
James Smart2fcee4b2010-12-15 17:57:46 -050011864 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11865 switch (if_type) {
11866 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011867 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11868 &uerr_sta_lo) ||
11869 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11870 &uerr_sta_hi)) {
11871 phba->work_hs |= UNPLUG_ERR;
11872 phba->work_ha |= HA_ERATT;
11873 phba->hba_flag |= HBA_ERATT_HANDLED;
11874 return 1;
11875 }
James Smart2fcee4b2010-12-15 17:57:46 -050011876 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11877 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11878 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11879 "1423 HBA Unrecoverable error: "
11880 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11881 "ue_mask_lo_reg=0x%x, "
11882 "ue_mask_hi_reg=0x%x\n",
11883 uerr_sta_lo, uerr_sta_hi,
11884 phba->sli4_hba.ue_mask_lo,
11885 phba->sli4_hba.ue_mask_hi);
11886 phba->work_status[0] = uerr_sta_lo;
11887 phba->work_status[1] = uerr_sta_hi;
11888 phba->work_ha |= HA_ERATT;
11889 phba->hba_flag |= HBA_ERATT_HANDLED;
11890 return 1;
11891 }
11892 break;
11893 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080011894 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050011895 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11896 &portstat_reg.word0) ||
11897 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11898 &portsmphr)){
11899 phba->work_hs |= UNPLUG_ERR;
11900 phba->work_ha |= HA_ERATT;
11901 phba->hba_flag |= HBA_ERATT_HANDLED;
11902 return 1;
11903 }
James Smart2fcee4b2010-12-15 17:57:46 -050011904 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11905 phba->work_status[0] =
11906 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11907 phba->work_status[1] =
11908 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011910 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011911 "port status reg 0x%x, "
11912 "port smphr reg 0x%x, "
11913 "error 1=0x%x, error 2=0x%x\n",
11914 portstat_reg.word0,
11915 portsmphr,
11916 phba->work_status[0],
11917 phba->work_status[1]);
11918 phba->work_ha |= HA_ERATT;
11919 phba->hba_flag |= HBA_ERATT_HANDLED;
11920 return 1;
11921 }
11922 break;
11923 case LPFC_SLI_INTF_IF_TYPE_1:
11924 default:
James Smarta747c9c2009-11-18 15:41:10 -050011925 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011926 "2886 HBA Error Attention on unsupported "
11927 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011928 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011929 }
James Smart2fcee4b2010-12-15 17:57:46 -050011930
James Smartda0436e2009-05-22 14:51:39 -040011931 return 0;
11932}
11933
11934/**
James Smart3621a712009-04-06 18:47:14 -040011935 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011936 * @phba: Pointer to HBA context.
11937 *
James Smart3772a992009-05-22 14:50:54 -040011938 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011939 * error attention register bit for error attention events.
11940 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011941 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011942 * Register and returns 0 otherwise.
11943 **/
11944int
11945lpfc_sli_check_eratt(struct lpfc_hba *phba)
11946{
11947 uint32_t ha_copy;
11948
11949 /* If somebody is waiting to handle an eratt, don't process it
11950 * here. The brdkill function will do this.
11951 */
11952 if (phba->link_flag & LS_IGNORE_ERATT)
11953 return 0;
11954
11955 /* Check if interrupt handler handles this ERATT */
11956 spin_lock_irq(&phba->hbalock);
11957 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11958 /* Interrupt handler has handled ERATT */
11959 spin_unlock_irq(&phba->hbalock);
11960 return 0;
11961 }
11962
James Smarta257bf92009-04-06 18:48:10 -040011963 /*
11964 * If there is deferred error attention, do not check for error
11965 * attention
11966 */
11967 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11968 spin_unlock_irq(&phba->hbalock);
11969 return 0;
11970 }
11971
James Smart3772a992009-05-22 14:50:54 -040011972 /* If PCI channel is offline, don't process it */
11973 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011974 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011975 return 0;
11976 }
11977
11978 switch (phba->sli_rev) {
11979 case LPFC_SLI_REV2:
11980 case LPFC_SLI_REV3:
11981 /* Read chip Host Attention (HA) register */
11982 ha_copy = lpfc_sli_eratt_read(phba);
11983 break;
James Smartda0436e2009-05-22 14:51:39 -040011984 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011985 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011986 ha_copy = lpfc_sli4_eratt_read(phba);
11987 break;
James Smart3772a992009-05-22 14:50:54 -040011988 default:
11989 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11990 "0299 Invalid SLI revision (%d)\n",
11991 phba->sli_rev);
11992 ha_copy = 0;
11993 break;
James Smart93996272008-08-24 21:50:30 -040011994 }
11995 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011996
11997 return ha_copy;
11998}
11999
12000/**
12001 * lpfc_intr_state_check - Check device state for interrupt handling
12002 * @phba: Pointer to HBA context.
12003 *
12004 * This inline routine checks whether a device or its PCI slot is in a state
12005 * that the interrupt should be handled.
12006 *
12007 * This function returns 0 if the device or the PCI slot is in a state that
12008 * interrupt should be handled, otherwise -EIO.
12009 */
12010static inline int
12011lpfc_intr_state_check(struct lpfc_hba *phba)
12012{
12013 /* If the pci channel is offline, ignore all the interrupts */
12014 if (unlikely(pci_channel_offline(phba->pcidev)))
12015 return -EIO;
12016
12017 /* Update device level interrupt statistics */
12018 phba->sli.slistat.sli_intr++;
12019
12020 /* Ignore all interrupts during initialization. */
12021 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12022 return -EIO;
12023
James Smart93996272008-08-24 21:50:30 -040012024 return 0;
12025}
12026
12027/**
James Smart3772a992009-05-22 14:50:54 -040012028 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012029 * @irq: Interrupt number.
12030 * @dev_id: The device context pointer.
12031 *
James Smart93996272008-08-24 21:50:30 -040012032 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012033 * service routine when device with SLI-3 interface spec is enabled with
12034 * MSI-X multi-message interrupt mode and there are slow-path events in
12035 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12036 * interrupt mode, this function is called as part of the device-level
12037 * interrupt handler. When the PCI slot is in error recovery or the HBA
12038 * is undergoing initialization, the interrupt handler will not process
12039 * the interrupt. The link attention and ELS ring attention events are
12040 * handled by the worker thread. The interrupt handler signals the worker
12041 * thread and returns for these events. This function is called without
12042 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012043 * structures.
12044 *
12045 * This function returns IRQ_HANDLED when interrupt is handled else it
12046 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012047 **/
dea31012005-04-17 16:05:31 -050012048irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012049lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012050{
James Smart2e0fef82007-06-17 19:56:36 -050012051 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012052 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012053 uint32_t work_ha_copy;
12054 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012055 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012056 uint32_t control;
12057
James Smart92d7f7b2007-06-17 19:56:38 -050012058 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012059 struct lpfc_vport *vport;
12060 struct lpfc_nodelist *ndlp;
12061 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012062 LPFC_MBOXQ_t *pmb;
12063 int rc;
12064
dea31012005-04-17 16:05:31 -050012065 /*
12066 * Get the driver's phba structure from the dev_id and
12067 * assume the HBA is not interrupting.
12068 */
James Smart93996272008-08-24 21:50:30 -040012069 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012070
12071 if (unlikely(!phba))
12072 return IRQ_NONE;
12073
dea31012005-04-17 16:05:31 -050012074 /*
James Smart93996272008-08-24 21:50:30 -040012075 * Stuff needs to be attented to when this function is invoked as an
12076 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012077 */
James Smart93996272008-08-24 21:50:30 -040012078 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012079 /* Check device state for handling interrupt */
12080 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012081 return IRQ_NONE;
12082 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012083 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012084 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12085 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012086 /* If somebody is waiting to handle an eratt don't process it
12087 * here. The brdkill function will do this.
12088 */
12089 if (phba->link_flag & LS_IGNORE_ERATT)
12090 ha_copy &= ~HA_ERATT;
12091 /* Check the need for handling ERATT in interrupt handler */
12092 if (ha_copy & HA_ERATT) {
12093 if (phba->hba_flag & HBA_ERATT_HANDLED)
12094 /* ERATT polling has handled ERATT */
12095 ha_copy &= ~HA_ERATT;
12096 else
12097 /* Indicate interrupt handler handles ERATT */
12098 phba->hba_flag |= HBA_ERATT_HANDLED;
12099 }
James Smarta257bf92009-04-06 18:48:10 -040012100
12101 /*
12102 * If there is deferred error attention, do not check for any
12103 * interrupt.
12104 */
12105 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012106 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012107 return IRQ_NONE;
12108 }
12109
James Smart93996272008-08-24 21:50:30 -040012110 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012111 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12112 goto unplug_error;
12113
James Smarta747c9c2009-11-18 15:41:10 -050012114 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12115 HC_LAINT_ENA | HC_ERINT_ENA),
12116 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012117 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12118 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012119 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012120 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012121 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012122 } else
12123 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012124
dea31012005-04-17 16:05:31 -050012125 work_ha_copy = ha_copy & phba->work_ha_mask;
12126
James Smart93996272008-08-24 21:50:30 -040012127 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012128 if (work_ha_copy & HA_LATT) {
12129 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12130 /*
12131 * Turn off Link Attention interrupts
12132 * until CLEAR_LA done
12133 */
James Smart5b75da22008-12-04 22:39:35 -050012134 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012135 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012136 if (lpfc_readl(phba->HCregaddr, &control))
12137 goto unplug_error;
dea31012005-04-17 16:05:31 -050012138 control &= ~HC_LAINT_ENA;
12139 writel(control, phba->HCregaddr);
12140 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012141 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012142 }
12143 else
12144 work_ha_copy &= ~HA_LATT;
12145 }
12146
James Smart93996272008-08-24 21:50:30 -040012147 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012148 /*
12149 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12150 * the only slow ring.
12151 */
12152 status = (work_ha_copy &
12153 (HA_RXMASK << (4*LPFC_ELS_RING)));
12154 status >>= (4*LPFC_ELS_RING);
12155 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012156 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012157 if (lpfc_readl(phba->HCregaddr, &control))
12158 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012159
12160 lpfc_debugfs_slow_ring_trc(phba,
12161 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12162 control, status,
12163 (uint32_t)phba->sli.slistat.sli_intr);
12164
James Smart858c9f62007-06-17 19:56:39 -050012165 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012166 lpfc_debugfs_slow_ring_trc(phba,
12167 "ISR Disable ring:"
12168 "pwork:x%x hawork:x%x wait:x%x",
12169 phba->work_ha, work_ha_copy,
12170 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012171 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012172
James Smart858c9f62007-06-17 19:56:39 -050012173 control &=
12174 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012175 writel(control, phba->HCregaddr);
12176 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012177 }
James Smarta58cbd52007-08-02 11:09:43 -040012178 else {
12179 lpfc_debugfs_slow_ring_trc(phba,
12180 "ISR slow ring: pwork:"
12181 "x%x hawork:x%x wait:x%x",
12182 phba->work_ha, work_ha_copy,
12183 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012184 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012185 }
James Smart5b75da22008-12-04 22:39:35 -050012186 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012187 }
12188 }
James Smart5b75da22008-12-04 22:39:35 -050012189 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012190 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012191 if (lpfc_sli_read_hs(phba))
12192 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012193 /*
12194 * Check if there is a deferred error condition
12195 * is active
12196 */
12197 if ((HS_FFER1 & phba->work_hs) &&
12198 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012199 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12200 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012201 phba->hba_flag |= DEFER_ERATT;
12202 /* Clear all interrupt enable conditions */
12203 writel(0, phba->HCregaddr);
12204 readl(phba->HCregaddr);
12205 }
12206 }
12207
James Smart93996272008-08-24 21:50:30 -040012208 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012209 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012210 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012211 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012212 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012213
12214 /* First check out the status word */
12215 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12216 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012217 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012218 /*
12219 * Stray Mailbox Interrupt, mbxCommand <cmd>
12220 * mbxStatus <status>
12221 */
James Smart09372822008-01-11 01:52:54 -050012222 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012223 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012224 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012225 "Interrupt mbxCommand x%x "
12226 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012227 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012228 pmbox->mbxCommand,
12229 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012230 /* clear mailbox attention bit */
12231 work_ha_copy &= ~HA_MBATT;
12232 } else {
James Smart97eab632008-04-07 10:16:05 -040012233 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012234 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012235 phba->last_completion_time = jiffies;
12236 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012237 if (pmb->mbox_cmpl) {
12238 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12239 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012240 if (pmb->out_ext_byte_len &&
12241 pmb->context2)
12242 lpfc_sli_pcimem_bcopy(
12243 phba->mbox_ext,
12244 pmb->context2,
12245 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012246 }
James Smart09372822008-01-11 01:52:54 -050012247 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12248 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12249
12250 lpfc_debugfs_disc_trc(vport,
12251 LPFC_DISC_TRC_MBOX_VPORT,
12252 "MBOX dflt rpi: : "
12253 "status:x%x rpi:x%x",
12254 (uint32_t)pmbox->mbxStatus,
12255 pmbox->un.varWords[0], 0);
12256
12257 if (!pmbox->mbxStatus) {
12258 mp = (struct lpfc_dmabuf *)
12259 (pmb->context1);
12260 ndlp = (struct lpfc_nodelist *)
12261 pmb->context2;
12262
12263 /* Reg_LOGIN of dflt RPI was
12264 * successful. new lets get
12265 * rid of the RPI using the
12266 * same mbox buffer.
12267 */
12268 lpfc_unreg_login(phba,
12269 vport->vpi,
12270 pmbox->un.varWords[0],
12271 pmb);
12272 pmb->mbox_cmpl =
12273 lpfc_mbx_cmpl_dflt_rpi;
12274 pmb->context1 = mp;
12275 pmb->context2 = ndlp;
12276 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012277 rc = lpfc_sli_issue_mbox(phba,
12278 pmb,
12279 MBX_NOWAIT);
12280 if (rc != MBX_BUSY)
12281 lpfc_printf_log(phba,
12282 KERN_ERR,
12283 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012284 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012285 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012286 if (rc != MBX_NOT_FINISHED)
12287 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012288 }
12289 }
James Smart5b75da22008-12-04 22:39:35 -050012290 spin_lock_irqsave(
12291 &phba->pport->work_port_lock,
12292 iflag);
James Smart09372822008-01-11 01:52:54 -050012293 phba->pport->work_port_events &=
12294 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012295 spin_unlock_irqrestore(
12296 &phba->pport->work_port_lock,
12297 iflag);
James Smart09372822008-01-11 01:52:54 -050012298 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012299 }
James Smart97eab632008-04-07 10:16:05 -040012300 } else
James Smart5b75da22008-12-04 22:39:35 -050012301 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012302
James Smart92d7f7b2007-06-17 19:56:38 -050012303 if ((work_ha_copy & HA_MBATT) &&
12304 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012305send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012306 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012307 do {
12308 rc = lpfc_sli_issue_mbox(phba, NULL,
12309 MBX_NOWAIT);
12310 } while (rc == MBX_NOT_FINISHED);
12311 if (rc != MBX_SUCCESS)
12312 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12313 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012314 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012315 }
12316
James Smart5b75da22008-12-04 22:39:35 -050012317 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012318 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012319 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012320 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012321 }
James Smart93996272008-08-24 21:50:30 -040012322 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012323unplug_error:
12324 spin_unlock_irqrestore(&phba->hbalock, iflag);
12325 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012326
James Smart3772a992009-05-22 14:50:54 -040012327} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012328
12329/**
James Smart3772a992009-05-22 14:50:54 -040012330 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012331 * @irq: Interrupt number.
12332 * @dev_id: The device context pointer.
12333 *
12334 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012335 * service routine when device with SLI-3 interface spec is enabled with
12336 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12337 * ring event in the HBA. However, when the device is enabled with either
12338 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12339 * device-level interrupt handler. When the PCI slot is in error recovery
12340 * or the HBA is undergoing initialization, the interrupt handler will not
12341 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12342 * the intrrupt context. This function is called without any lock held.
12343 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012344 *
12345 * This function returns IRQ_HANDLED when interrupt is handled else it
12346 * returns IRQ_NONE.
12347 **/
12348irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012349lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012350{
12351 struct lpfc_hba *phba;
12352 uint32_t ha_copy;
12353 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012354 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012355 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012356
12357 /* Get the driver's phba structure from the dev_id and
12358 * assume the HBA is not interrupting.
12359 */
12360 phba = (struct lpfc_hba *) dev_id;
12361
12362 if (unlikely(!phba))
12363 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012364
12365 /*
James Smart93996272008-08-24 21:50:30 -040012366 * Stuff needs to be attented to when this function is invoked as an
12367 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012368 */
James Smart93996272008-08-24 21:50:30 -040012369 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012370 /* Check device state for handling interrupt */
12371 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012372 return IRQ_NONE;
12373 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012374 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12375 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012376 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012377 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012378 /*
12379 * If there is deferred error attention, do not check for
12380 * any interrupt.
12381 */
12382 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012383 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012384 return IRQ_NONE;
12385 }
James Smart93996272008-08-24 21:50:30 -040012386 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12387 phba->HAregaddr);
12388 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012389 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012390 } else
12391 ha_copy = phba->ha_copy;
12392
12393 /*
12394 * Process all events on FCP ring. Take the optimized path for FCP IO.
12395 */
12396 ha_copy &= ~(phba->work_ha_mask);
12397
12398 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012399 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012400 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012401 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012402 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012403
12404 if (phba->cfg_multi_ring_support == 2) {
12405 /*
James Smart93996272008-08-24 21:50:30 -040012406 * Process all events on extra ring. Take the optimized path
12407 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012408 */
James Smart93996272008-08-24 21:50:30 -040012409 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012410 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012411 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012412 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012413 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012414 status);
12415 }
12416 }
dea31012005-04-17 16:05:31 -050012417 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012418} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012419
James Smart93996272008-08-24 21:50:30 -040012420/**
James Smart3772a992009-05-22 14:50:54 -040012421 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012422 * @irq: Interrupt number.
12423 * @dev_id: The device context pointer.
12424 *
James Smart3772a992009-05-22 14:50:54 -040012425 * This function is the HBA device-level interrupt handler to device with
12426 * SLI-3 interface spec, called from the PCI layer when either MSI or
12427 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12428 * requires driver attention. This function invokes the slow-path interrupt
12429 * attention handling function and fast-path interrupt attention handling
12430 * function in turn to process the relevant HBA attention events. This
12431 * function is called without any lock held. It gets the hbalock to access
12432 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012433 *
12434 * This function returns IRQ_HANDLED when interrupt is handled, else it
12435 * returns IRQ_NONE.
12436 **/
12437irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012438lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012439{
12440 struct lpfc_hba *phba;
12441 irqreturn_t sp_irq_rc, fp_irq_rc;
12442 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012443 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012444
12445 /*
12446 * Get the driver's phba structure from the dev_id and
12447 * assume the HBA is not interrupting.
12448 */
12449 phba = (struct lpfc_hba *) dev_id;
12450
12451 if (unlikely(!phba))
12452 return IRQ_NONE;
12453
James Smart3772a992009-05-22 14:50:54 -040012454 /* Check device state for handling interrupt */
12455 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012456 return IRQ_NONE;
12457
12458 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012459 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12460 spin_unlock(&phba->hbalock);
12461 return IRQ_HANDLED;
12462 }
12463
James Smart93996272008-08-24 21:50:30 -040012464 if (unlikely(!phba->ha_copy)) {
12465 spin_unlock(&phba->hbalock);
12466 return IRQ_NONE;
12467 } else if (phba->ha_copy & HA_ERATT) {
12468 if (phba->hba_flag & HBA_ERATT_HANDLED)
12469 /* ERATT polling has handled ERATT */
12470 phba->ha_copy &= ~HA_ERATT;
12471 else
12472 /* Indicate interrupt handler handles ERATT */
12473 phba->hba_flag |= HBA_ERATT_HANDLED;
12474 }
12475
James Smarta257bf92009-04-06 18:48:10 -040012476 /*
12477 * If there is deferred error attention, do not check for any interrupt.
12478 */
12479 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012480 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012481 return IRQ_NONE;
12482 }
12483
James Smart93996272008-08-24 21:50:30 -040012484 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012485 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12486 spin_unlock(&phba->hbalock);
12487 return IRQ_HANDLED;
12488 }
James Smarta747c9c2009-11-18 15:41:10 -050012489 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12490 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12491 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012492 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012493 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012494 readl(phba->HAregaddr); /* flush */
12495 spin_unlock(&phba->hbalock);
12496
12497 /*
12498 * Invokes slow-path host attention interrupt handling as appropriate.
12499 */
12500
12501 /* status of events with mailbox and link attention */
12502 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12503
12504 /* status of events with ELS ring */
12505 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12506 status2 >>= (4*LPFC_ELS_RING);
12507
12508 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012509 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012510 else
12511 sp_irq_rc = IRQ_NONE;
12512
12513 /*
12514 * Invoke fast-path host attention interrupt handling as appropriate.
12515 */
12516
12517 /* status of events with FCP ring */
12518 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12519 status1 >>= (4*LPFC_FCP_RING);
12520
12521 /* status of events with extra ring */
12522 if (phba->cfg_multi_ring_support == 2) {
12523 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12524 status2 >>= (4*LPFC_EXTRA_RING);
12525 } else
12526 status2 = 0;
12527
12528 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012529 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012530 else
12531 fp_irq_rc = IRQ_NONE;
12532
12533 /* Return device-level interrupt handling status */
12534 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012535} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012536
12537/**
12538 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12539 * @phba: pointer to lpfc hba data structure.
12540 *
12541 * This routine is invoked by the worker thread to process all the pending
12542 * SLI4 FCP abort XRI events.
12543 **/
12544void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12545{
12546 struct lpfc_cq_event *cq_event;
12547
12548 /* First, declare the fcp xri abort event has been handled */
12549 spin_lock_irq(&phba->hbalock);
12550 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12551 spin_unlock_irq(&phba->hbalock);
12552 /* Now, handle all the fcp xri abort events */
12553 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12554 /* Get the first event from the head of the event queue */
12555 spin_lock_irq(&phba->hbalock);
12556 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12557 cq_event, struct lpfc_cq_event, list);
12558 spin_unlock_irq(&phba->hbalock);
12559 /* Notify aborted XRI for FCP work queue */
12560 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12561 /* Free the event processed back to the free pool */
12562 lpfc_sli4_cq_event_release(phba, cq_event);
12563 }
12564}
12565
12566/**
12567 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12568 * @phba: pointer to lpfc hba data structure.
12569 *
12570 * This routine is invoked by the worker thread to process all the pending
12571 * SLI4 els abort xri events.
12572 **/
12573void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12574{
12575 struct lpfc_cq_event *cq_event;
12576
12577 /* First, declare the els xri abort event has been handled */
12578 spin_lock_irq(&phba->hbalock);
12579 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12580 spin_unlock_irq(&phba->hbalock);
12581 /* Now, handle all the els xri abort events */
12582 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12583 /* Get the first event from the head of the event queue */
12584 spin_lock_irq(&phba->hbalock);
12585 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12586 cq_event, struct lpfc_cq_event, list);
12587 spin_unlock_irq(&phba->hbalock);
12588 /* Notify aborted XRI for ELS work queue */
12589 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12590 /* Free the event processed back to the free pool */
12591 lpfc_sli4_cq_event_release(phba, cq_event);
12592 }
12593}
12594
James Smart341af102010-01-26 23:07:37 -050012595/**
12596 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12597 * @phba: pointer to lpfc hba data structure
12598 * @pIocbIn: pointer to the rspiocbq
12599 * @pIocbOut: pointer to the cmdiocbq
12600 * @wcqe: pointer to the complete wcqe
12601 *
12602 * This routine transfers the fields of a command iocbq to a response iocbq
12603 * by copying all the IOCB fields from command iocbq and transferring the
12604 * completion status information from the complete wcqe.
12605 **/
James Smart4f774512009-05-22 14:52:35 -040012606static void
James Smart341af102010-01-26 23:07:37 -050012607lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12608 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012609 struct lpfc_iocbq *pIocbOut,
12610 struct lpfc_wcqe_complete *wcqe)
12611{
James Smartaf227412013-10-10 12:23:10 -040012612 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012613 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012614 uint32_t status, max_response;
12615 struct lpfc_dmabuf *dmabuf;
12616 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012617 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12618
12619 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12620 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012621 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012622 status = bf_get(lpfc_wcqe_c_status, wcqe);
12623 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012624 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12625 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12626 pIocbIn->iocb.un.fcpi.fcpi_parm =
12627 pIocbOut->iocb.un.fcpi.fcpi_parm -
12628 wcqe->total_data_placed;
12629 else
12630 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012631 else {
James Smart4f774512009-05-22 14:52:35 -040012632 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012633 switch (pIocbOut->iocb.ulpCommand) {
12634 case CMD_ELS_REQUEST64_CR:
12635 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12636 bpl = (struct ulp_bde64 *)dmabuf->virt;
12637 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12638 max_response = bde.tus.f.bdeSize;
12639 break;
12640 case CMD_GEN_REQUEST64_CR:
12641 max_response = 0;
12642 if (!pIocbOut->context3)
12643 break;
12644 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12645 sizeof(struct ulp_bde64);
12646 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12647 bpl = (struct ulp_bde64 *)dmabuf->virt;
12648 for (i = 0; i < numBdes; i++) {
12649 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12650 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12651 max_response += bde.tus.f.bdeSize;
12652 }
12653 break;
12654 default:
12655 max_response = wcqe->total_data_placed;
12656 break;
12657 }
12658 if (max_response < wcqe->total_data_placed)
12659 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12660 else
12661 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12662 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012663 }
James Smart341af102010-01-26 23:07:37 -050012664
James Smartacd68592012-01-18 16:25:09 -050012665 /* Convert BG errors for completion status */
12666 if (status == CQE_STATUS_DI_ERROR) {
12667 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12668
12669 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12670 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12671 else
12672 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12673
12674 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12675 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12676 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12677 BGS_GUARD_ERR_MASK;
12678 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12679 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12680 BGS_APPTAG_ERR_MASK;
12681 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12682 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12683 BGS_REFTAG_ERR_MASK;
12684
12685 /* Check to see if there was any good data before the error */
12686 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12687 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12688 BGS_HI_WATER_MARK_PRESENT_MASK;
12689 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12690 wcqe->total_data_placed;
12691 }
12692
12693 /*
12694 * Set ALL the error bits to indicate we don't know what
12695 * type of error it is.
12696 */
12697 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12698 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12699 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12700 BGS_GUARD_ERR_MASK);
12701 }
12702
James Smart341af102010-01-26 23:07:37 -050012703 /* Pick up HBA exchange busy condition */
12704 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12705 spin_lock_irqsave(&phba->hbalock, iflags);
12706 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12707 spin_unlock_irqrestore(&phba->hbalock, iflags);
12708 }
James Smart4f774512009-05-22 14:52:35 -040012709}
12710
12711/**
James Smart45ed1192009-10-02 15:17:02 -040012712 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12713 * @phba: Pointer to HBA context object.
12714 * @wcqe: Pointer to work-queue completion queue entry.
12715 *
12716 * This routine handles an ELS work-queue completion event and construct
12717 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12718 * discovery engine to handle.
12719 *
12720 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12721 **/
12722static struct lpfc_iocbq *
12723lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12724 struct lpfc_iocbq *irspiocbq)
12725{
James Smart895427b2017-02-12 13:52:30 -080012726 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012727 struct lpfc_iocbq *cmdiocbq;
12728 struct lpfc_wcqe_complete *wcqe;
12729 unsigned long iflags;
12730
James Smart895427b2017-02-12 13:52:30 -080012731 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070012732 if (unlikely(!pring))
12733 return NULL;
James Smart895427b2017-02-12 13:52:30 -080012734
James Smart45ed1192009-10-02 15:17:02 -040012735 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012736 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012737 pring->stats.iocb_event++;
12738 /* Look up the ELS command IOCB and create pseudo response IOCB */
12739 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12740 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040012741 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070012742 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012743 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12744 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070012745 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12746 wcqe->word0, wcqe->total_data_placed,
12747 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040012748 lpfc_sli_release_iocbq(phba, irspiocbq);
12749 return NULL;
12750 }
12751
Dick Kennedy401bb412017-09-29 17:34:28 -070012752 /* Put the iocb back on the txcmplq */
12753 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12754 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12755
James Smart45ed1192009-10-02 15:17:02 -040012756 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012757 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012758
12759 return irspiocbq;
12760}
12761
James Smart8a5ca102017-11-20 16:00:30 -080012762inline struct lpfc_cq_event *
12763lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12764{
12765 struct lpfc_cq_event *cq_event;
12766
12767 /* Allocate a new internal CQ_EVENT entry */
12768 cq_event = lpfc_sli4_cq_event_alloc(phba);
12769 if (!cq_event) {
12770 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12771 "0602 Failed to alloc CQ_EVENT entry\n");
12772 return NULL;
12773 }
12774
12775 /* Move the CQE into the event */
12776 memcpy(&cq_event->cqe, entry, size);
12777 return cq_event;
12778}
12779
James Smart45ed1192009-10-02 15:17:02 -040012780/**
James Smart04c68492009-05-22 14:52:52 -040012781 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12782 * @phba: Pointer to HBA context object.
12783 * @cqe: Pointer to mailbox completion queue entry.
12784 *
12785 * This routine process a mailbox completion queue entry with asynchrous
12786 * event.
12787 *
12788 * Return: true if work posted to worker thread, otherwise false.
12789 **/
12790static bool
12791lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12792{
12793 struct lpfc_cq_event *cq_event;
12794 unsigned long iflags;
12795
12796 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12797 "0392 Async Event: word0:x%x, word1:x%x, "
12798 "word2:x%x, word3:x%x\n", mcqe->word0,
12799 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12800
James Smart8a5ca102017-11-20 16:00:30 -080012801 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12802 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040012803 return false;
James Smart04c68492009-05-22 14:52:52 -040012804 spin_lock_irqsave(&phba->hbalock, iflags);
12805 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12806 /* Set the async event flag */
12807 phba->hba_flag |= ASYNC_EVENT;
12808 spin_unlock_irqrestore(&phba->hbalock, iflags);
12809
12810 return true;
12811}
12812
12813/**
12814 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12815 * @phba: Pointer to HBA context object.
12816 * @cqe: Pointer to mailbox completion queue entry.
12817 *
12818 * This routine process a mailbox completion queue entry with mailbox
12819 * completion event.
12820 *
12821 * Return: true if work posted to worker thread, otherwise false.
12822 **/
12823static bool
12824lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12825{
12826 uint32_t mcqe_status;
12827 MAILBOX_t *mbox, *pmbox;
12828 struct lpfc_mqe *mqe;
12829 struct lpfc_vport *vport;
12830 struct lpfc_nodelist *ndlp;
12831 struct lpfc_dmabuf *mp;
12832 unsigned long iflags;
12833 LPFC_MBOXQ_t *pmb;
12834 bool workposted = false;
12835 int rc;
12836
12837 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12838 if (!bf_get(lpfc_trailer_completed, mcqe))
12839 goto out_no_mqe_complete;
12840
12841 /* Get the reference to the active mbox command */
12842 spin_lock_irqsave(&phba->hbalock, iflags);
12843 pmb = phba->sli.mbox_active;
12844 if (unlikely(!pmb)) {
12845 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12846 "1832 No pending MBOX command to handle\n");
12847 spin_unlock_irqrestore(&phba->hbalock, iflags);
12848 goto out_no_mqe_complete;
12849 }
12850 spin_unlock_irqrestore(&phba->hbalock, iflags);
12851 mqe = &pmb->u.mqe;
12852 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12853 mbox = phba->mbox;
12854 vport = pmb->vport;
12855
12856 /* Reset heartbeat timer */
12857 phba->last_completion_time = jiffies;
12858 del_timer(&phba->sli.mbox_tmo);
12859
12860 /* Move mbox data to caller's mailbox region, do endian swapping */
12861 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070012862 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012863
James Smart73d91e52011-10-10 21:32:10 -040012864 /*
12865 * For mcqe errors, conditionally move a modified error code to
12866 * the mbox so that the error will not be missed.
12867 */
12868 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12869 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12870 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12871 bf_set(lpfc_mqe_status, mqe,
12872 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12873 }
James Smart04c68492009-05-22 14:52:52 -040012874 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12875 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12876 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12877 "MBOX dflt rpi: status:x%x rpi:x%x",
12878 mcqe_status,
12879 pmbox->un.varWords[0], 0);
12880 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12881 mp = (struct lpfc_dmabuf *)(pmb->context1);
12882 ndlp = (struct lpfc_nodelist *)pmb->context2;
12883 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12884 * RID of the PPI using the same mbox buffer.
12885 */
12886 lpfc_unreg_login(phba, vport->vpi,
12887 pmbox->un.varWords[0], pmb);
12888 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12889 pmb->context1 = mp;
12890 pmb->context2 = ndlp;
12891 pmb->vport = vport;
12892 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12893 if (rc != MBX_BUSY)
12894 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12895 LOG_SLI, "0385 rc should "
12896 "have been MBX_BUSY\n");
12897 if (rc != MBX_NOT_FINISHED)
12898 goto send_current_mbox;
12899 }
12900 }
12901 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12902 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12903 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12904
12905 /* There is mailbox completion work to do */
12906 spin_lock_irqsave(&phba->hbalock, iflags);
12907 __lpfc_mbox_cmpl_put(phba, pmb);
12908 phba->work_ha |= HA_MBATT;
12909 spin_unlock_irqrestore(&phba->hbalock, iflags);
12910 workposted = true;
12911
12912send_current_mbox:
12913 spin_lock_irqsave(&phba->hbalock, iflags);
12914 /* Release the mailbox command posting token */
12915 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12916 /* Setting active mailbox pointer need to be in sync to flag clear */
12917 phba->sli.mbox_active = NULL;
12918 spin_unlock_irqrestore(&phba->hbalock, iflags);
12919 /* Wake up worker thread to post the next pending mailbox command */
12920 lpfc_worker_wake_up(phba);
12921out_no_mqe_complete:
12922 if (bf_get(lpfc_trailer_consumed, mcqe))
12923 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12924 return workposted;
12925}
12926
12927/**
12928 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12929 * @phba: Pointer to HBA context object.
12930 * @cqe: Pointer to mailbox completion queue entry.
12931 *
12932 * This routine process a mailbox completion queue entry, it invokes the
12933 * proper mailbox complete handling or asynchrous event handling routine
12934 * according to the MCQE's async bit.
12935 *
12936 * Return: true if work posted to worker thread, otherwise false.
12937 **/
12938static bool
12939lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12940{
12941 struct lpfc_mcqe mcqe;
12942 bool workposted;
12943
12944 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070012945 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040012946
12947 /* Invoke the proper event handling routine */
12948 if (!bf_get(lpfc_trailer_async, &mcqe))
12949 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12950 else
12951 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12952 return workposted;
12953}
12954
12955/**
James Smart4f774512009-05-22 14:52:35 -040012956 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12957 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012958 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012959 * @wcqe: Pointer to work-queue completion queue entry.
12960 *
12961 * This routine handles an ELS work-queue completion event.
12962 *
12963 * Return: true if work posted to worker thread, otherwise false.
12964 **/
12965static bool
James Smart2a76a282012-08-03 12:35:54 -040012966lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012967 struct lpfc_wcqe_complete *wcqe)
12968{
James Smart4f774512009-05-22 14:52:35 -040012969 struct lpfc_iocbq *irspiocbq;
12970 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012971 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012972 int txq_cnt = 0;
12973 int txcmplq_cnt = 0;
12974 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012975
James Smart11f0e342018-05-04 20:37:57 -070012976 /* Check for response status */
12977 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12978 /* Log the error status */
12979 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12980 "0357 ELS CQE error: status=x%x: "
12981 "CQE: %08x %08x %08x %08x\n",
12982 bf_get(lpfc_wcqe_c_status, wcqe),
12983 wcqe->word0, wcqe->total_data_placed,
12984 wcqe->parameter, wcqe->word3);
12985 }
12986
James Smart45ed1192009-10-02 15:17:02 -040012987 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012988 irspiocbq = lpfc_sli_get_iocbq(phba);
12989 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012990 if (!list_empty(&pring->txq))
12991 txq_cnt++;
12992 if (!list_empty(&pring->txcmplq))
12993 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012994 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012995 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12996 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012997 txq_cnt, phba->iocb_cnt,
12998 fcp_txcmplq_cnt,
12999 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040013000 return false;
James Smart4f774512009-05-22 14:52:35 -040013001 }
James Smart4f774512009-05-22 14:52:35 -040013002
James Smart45ed1192009-10-02 15:17:02 -040013003 /* Save off the slow-path queue event for work thread to process */
13004 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040013005 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040013006 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013007 &phba->sli4_hba.sp_queue_event);
13008 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013009 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013010
James Smart45ed1192009-10-02 15:17:02 -040013011 return true;
James Smart4f774512009-05-22 14:52:35 -040013012}
13013
13014/**
13015 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13016 * @phba: Pointer to HBA context object.
13017 * @wcqe: Pointer to work-queue completion queue entry.
13018 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013019 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013020 * proper WQ release routine to the slow-path WQ.
13021 **/
13022static void
13023lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13024 struct lpfc_wcqe_release *wcqe)
13025{
James Smart2e90f4b2011-12-13 13:22:37 -050013026 /* sanity check on queue memory */
13027 if (unlikely(!phba->sli4_hba.els_wq))
13028 return;
James Smart4f774512009-05-22 14:52:35 -040013029 /* Check for the slow-path ELS work queue */
13030 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13031 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13032 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13033 else
13034 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13035 "2579 Slow-path wqe consume event carries "
13036 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13037 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13038 phba->sli4_hba.els_wq->queue_id);
13039}
13040
13041/**
13042 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13043 * @phba: Pointer to HBA context object.
13044 * @cq: Pointer to a WQ completion queue.
13045 * @wcqe: Pointer to work-queue completion queue entry.
13046 *
13047 * This routine handles an XRI abort event.
13048 *
13049 * Return: true if work posted to worker thread, otherwise false.
13050 **/
13051static bool
13052lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13053 struct lpfc_queue *cq,
13054 struct sli4_wcqe_xri_aborted *wcqe)
13055{
13056 bool workposted = false;
13057 struct lpfc_cq_event *cq_event;
13058 unsigned long iflags;
13059
James Smart4f774512009-05-22 14:52:35 -040013060 switch (cq->subtype) {
13061 case LPFC_FCP:
James Smart8a5ca102017-11-20 16:00:30 -080013062 cq_event = lpfc_cq_event_setup(
13063 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13064 if (!cq_event)
13065 return false;
James Smart4f774512009-05-22 14:52:35 -040013066 spin_lock_irqsave(&phba->hbalock, iflags);
13067 list_add_tail(&cq_event->list,
13068 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13069 /* Set the fcp xri abort event flag */
13070 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13071 spin_unlock_irqrestore(&phba->hbalock, iflags);
13072 workposted = true;
13073 break;
James Smart422c4cb2017-11-20 16:00:32 -080013074 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013075 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013076 cq_event = lpfc_cq_event_setup(
13077 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13078 if (!cq_event)
13079 return false;
James Smart4f774512009-05-22 14:52:35 -040013080 spin_lock_irqsave(&phba->hbalock, iflags);
13081 list_add_tail(&cq_event->list,
13082 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13083 /* Set the els xri abort event flag */
13084 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13085 spin_unlock_irqrestore(&phba->hbalock, iflags);
13086 workposted = true;
13087 break;
James Smart318083a2017-03-04 09:30:30 -080013088 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080013089 /* Notify aborted XRI for NVME work queue */
13090 if (phba->nvmet_support)
13091 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13092 else
13093 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13094
13095 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080013096 break;
James Smart4f774512009-05-22 14:52:35 -040013097 default:
13098 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013099 "0603 Invalid CQ subtype %d: "
13100 "%08x %08x %08x %08x\n",
13101 cq->subtype, wcqe->word0, wcqe->parameter,
13102 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013103 workposted = false;
13104 break;
13105 }
13106 return workposted;
13107}
13108
13109/**
James Smart4d9ab992009-10-02 15:16:39 -040013110 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013111 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013112 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013113 *
James Smart4d9ab992009-10-02 15:16:39 -040013114 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013115 *
13116 * Return: true if work posted to worker thread, otherwise false.
13117 **/
13118static bool
James Smart4d9ab992009-10-02 15:16:39 -040013119lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13120{
13121 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080013122 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013123 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13124 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013125 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013126 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013127 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013128 unsigned long iflags;
13129
James Smart2e90f4b2011-12-13 13:22:37 -050013130 /* sanity check on queue memory */
13131 if (unlikely(!hrq) || unlikely(!drq))
13132 return workposted;
13133
James Smart7851fe22011-07-22 18:36:52 -040013134 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13135 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13136 else
13137 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13138 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013139 goto out;
13140
13141 status = bf_get(lpfc_rcqe_status, rcqe);
13142 switch (status) {
13143 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13144 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13145 "2537 Receive Frame Truncated!!\n");
13146 case FC_STATUS_RQ_SUCCESS:
13147 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013148 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013149 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13150 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013151 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013152 spin_unlock_irqrestore(&phba->hbalock, iflags);
13153 goto out;
13154 }
James Smartb84daac2012-08-03 12:35:13 -040013155 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013156 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013157 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013158
13159 /* If a NVME LS event (type 0x28), treat it as Fast path */
13160 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13161
James Smart4d9ab992009-10-02 15:16:39 -040013162 /* save off the frame for the word thread to process */
13163 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013164 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013165 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013166 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013167 spin_unlock_irqrestore(&phba->hbalock, iflags);
13168 workposted = true;
13169 break;
James Smart4d9ab992009-10-02 15:16:39 -040013170 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013171 if (phba->nvmet_support) {
13172 tgtp = phba->targetport->private;
13173 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13174 "6402 RQE Error x%x, posted %d err_cnt "
13175 "%d: %x %x %x\n",
13176 status, hrq->RQ_buf_posted,
13177 hrq->RQ_no_posted_buf,
13178 atomic_read(&tgtp->rcv_fcp_cmd_in),
13179 atomic_read(&tgtp->rcv_fcp_cmd_out),
13180 atomic_read(&tgtp->xmt_fcp_release));
13181 }
13182 /* fallthrough */
13183
13184 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013185 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013186 /* Post more buffers if possible */
13187 spin_lock_irqsave(&phba->hbalock, iflags);
13188 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13189 spin_unlock_irqrestore(&phba->hbalock, iflags);
13190 workposted = true;
13191 break;
13192 }
13193out:
13194 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013195}
13196
13197/**
13198 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13199 * @phba: Pointer to HBA context object.
13200 * @cq: Pointer to the completion queue.
13201 * @wcqe: Pointer to a completion queue entry.
13202 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013203 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013204 * entry.
13205 *
13206 * Return: true if work posted to worker thread, otherwise false.
13207 **/
13208static bool
13209lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013210 struct lpfc_cqe *cqe)
13211{
James Smart45ed1192009-10-02 15:17:02 -040013212 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013213 bool workposted = false;
13214
13215 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013216 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013217
13218 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013219 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013220 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013221 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013222 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013223 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013224 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013225 break;
13226 case CQE_CODE_RELEASE_WQE:
13227 /* Process the WQ release event */
13228 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013229 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013230 break;
13231 case CQE_CODE_XRI_ABORTED:
13232 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013233 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013234 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013235 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013236 break;
James Smart4d9ab992009-10-02 15:16:39 -040013237 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013238 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013239 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013240 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013241 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013242 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013243 break;
James Smart4f774512009-05-22 14:52:35 -040013244 default:
13245 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13246 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013247 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013248 break;
13249 }
13250 return workposted;
13251}
13252
13253/**
James Smart4f774512009-05-22 14:52:35 -040013254 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13255 * @phba: Pointer to HBA context object.
13256 * @eqe: Pointer to fast-path event queue entry.
13257 *
13258 * This routine process a event queue entry from the slow-path event queue.
13259 * It will check the MajorCode and MinorCode to determine this is for a
13260 * completion event on a completion queue, if not, an error shall be logged
13261 * and just return. Otherwise, it will get to the corresponding completion
13262 * queue and process all the entries on that completion queue, rearm the
13263 * completion queue, and then return.
13264 *
13265 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013266static void
James Smart67d12732012-08-03 12:36:13 -040013267lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13268 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013269{
James Smart67d12732012-08-03 12:36:13 -040013270 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013271 uint16_t cqid;
13272
James Smart4f774512009-05-22 14:52:35 -040013273 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013274 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013275
James Smart4f774512009-05-22 14:52:35 -040013276 list_for_each_entry(childq, &speq->child_list, list) {
13277 if (childq->queue_id == cqid) {
13278 cq = childq;
13279 break;
13280 }
13281 }
13282 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013283 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13284 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13285 "0365 Slow-path CQ identifier "
13286 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013287 return;
James Smart4f774512009-05-22 14:52:35 -040013288 }
13289
James Smart895427b2017-02-12 13:52:30 -080013290 /* Save EQ associated with this CQ */
13291 cq->assoc_qp = speq;
13292
Dick Kennedyf485c182017-09-29 17:34:34 -070013293 if (!queue_work(phba->wq, &cq->spwork))
13294 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13295 "0390 Cannot schedule soft IRQ "
13296 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13297 cqid, cq->queue_id, smp_processor_id());
13298}
13299
13300/**
13301 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13302 * @phba: Pointer to HBA context object.
13303 *
13304 * This routine process a event queue entry from the slow-path event queue.
13305 * It will check the MajorCode and MinorCode to determine this is for a
13306 * completion event on a completion queue, if not, an error shall be logged
13307 * and just return. Otherwise, it will get to the corresponding completion
13308 * queue and process all the entries on that completion queue, rearm the
13309 * completion queue, and then return.
13310 *
13311 **/
13312static void
13313lpfc_sli4_sp_process_cq(struct work_struct *work)
13314{
13315 struct lpfc_queue *cq =
13316 container_of(work, struct lpfc_queue, spwork);
13317 struct lpfc_hba *phba = cq->phba;
13318 struct lpfc_cqe *cqe;
13319 bool workposted = false;
13320 int ccount = 0;
13321
James Smart4f774512009-05-22 14:52:35 -040013322 /* Process all the entries to the CQ */
13323 switch (cq->type) {
13324 case LPFC_MCQ:
13325 while ((cqe = lpfc_sli4_cq_get(cq))) {
13326 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013327 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013328 break;
James Smartb84daac2012-08-03 12:35:13 -040013329 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013330 }
13331 break;
13332 case LPFC_WCQ:
13333 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013334 if (cq->subtype == LPFC_FCP ||
13335 cq->subtype == LPFC_NVME) {
13336#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13337 if (phba->ktime_on)
13338 cq->isr_timestamp = ktime_get_ns();
13339 else
13340 cq->isr_timestamp = 0;
13341#endif
James Smart895427b2017-02-12 13:52:30 -080013342 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013343 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013344 } else {
James Smart05580562011-05-24 11:40:48 -040013345 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13346 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013347 }
Dick Kennedyf485c182017-09-29 17:34:34 -070013348 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013349 break;
James Smart4f774512009-05-22 14:52:35 -040013350 }
James Smartb84daac2012-08-03 12:35:13 -040013351
13352 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013353 if (ccount > cq->CQ_max_cqe)
13354 cq->CQ_max_cqe = ccount;
James Smart4f774512009-05-22 14:52:35 -040013355 break;
13356 default:
13357 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13358 "0370 Invalid completion queue type (%d)\n",
13359 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013360 return;
James Smart4f774512009-05-22 14:52:35 -040013361 }
13362
13363 /* Catch the no cq entry condition, log an error */
Dick Kennedyf485c182017-09-29 17:34:34 -070013364 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013365 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13366 "0371 No entry from the CQ: identifier "
13367 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13368
13369 /* In any case, flash and re-arm the RCQ */
James Smartb71413d2018-02-22 08:18:40 -080013370 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013371
13372 /* wake up worker thread if there are works to be done */
13373 if (workposted)
13374 lpfc_worker_wake_up(phba);
13375}
13376
13377/**
13378 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013379 * @phba: Pointer to HBA context object.
13380 * @cq: Pointer to associated CQ
13381 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013382 *
13383 * This routine process a fast-path work queue completion entry from fast-path
13384 * event queue for FCP command response completion.
13385 **/
13386static void
James Smart2a76a282012-08-03 12:35:54 -040013387lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013388 struct lpfc_wcqe_complete *wcqe)
13389{
James Smart2a76a282012-08-03 12:35:54 -040013390 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013391 struct lpfc_iocbq *cmdiocbq;
13392 struct lpfc_iocbq irspiocbq;
13393 unsigned long iflags;
13394
James Smart4f774512009-05-22 14:52:35 -040013395 /* Check for response status */
13396 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13397 /* If resource errors reported from HBA, reduce queue
13398 * depth of the SCSI device.
13399 */
James Smarte3d2b802012-08-14 14:25:43 -040013400 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13401 IOSTAT_LOCAL_REJECT)) &&
13402 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13403 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013404 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013405
James Smart4f774512009-05-22 14:52:35 -040013406 /* Log the error status */
James Smart11f0e342018-05-04 20:37:57 -070013407 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13408 "0373 FCP CQE error: status=x%x: "
13409 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013410 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070013411 wcqe->word0, wcqe->total_data_placed,
13412 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013413 }
13414
13415 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013416 spin_lock_irqsave(&pring->ring_lock, iflags);
13417 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013418 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13419 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013420 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013421 if (unlikely(!cmdiocbq)) {
13422 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13423 "0374 FCP complete with no corresponding "
13424 "cmdiocb: iotag (%d)\n",
13425 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13426 return;
13427 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013428#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13429 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13430#endif
James Smart895427b2017-02-12 13:52:30 -080013431 if (cmdiocbq->iocb_cmpl == NULL) {
13432 if (cmdiocbq->wqe_cmpl) {
13433 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13434 spin_lock_irqsave(&phba->hbalock, iflags);
13435 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13436 spin_unlock_irqrestore(&phba->hbalock, iflags);
13437 }
13438
13439 /* Pass the cmd_iocb and the wcqe to the upper layer */
13440 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13441 return;
13442 }
James Smart4f774512009-05-22 14:52:35 -040013443 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13444 "0375 FCP cmdiocb not callback function "
13445 "iotag: (%d)\n",
13446 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13447 return;
13448 }
13449
13450 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013451 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013452
James Smart0f65ff62010-02-26 14:14:23 -050013453 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13454 spin_lock_irqsave(&phba->hbalock, iflags);
13455 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13456 spin_unlock_irqrestore(&phba->hbalock, iflags);
13457 }
13458
James Smart4f774512009-05-22 14:52:35 -040013459 /* Pass the cmd_iocb and the rsp state to the upper layer */
13460 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13461}
13462
13463/**
13464 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13465 * @phba: Pointer to HBA context object.
13466 * @cq: Pointer to completion queue.
13467 * @wcqe: Pointer to work-queue completion queue entry.
13468 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013469 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013470 * proper WQ release routine to the slow-path WQ.
13471 **/
13472static void
13473lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13474 struct lpfc_wcqe_release *wcqe)
13475{
13476 struct lpfc_queue *childwq;
13477 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013478 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013479
13480 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013481 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013482 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013483 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013484 lpfc_sli4_wq_release(childwq,
13485 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013486 if (childwq->q_flag & HBA_NVMET_WQFULL)
13487 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013488 wqid_matched = true;
13489 break;
13490 }
13491 }
13492 /* Report warning log message if no match found */
13493 if (wqid_matched != true)
13494 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13495 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013496 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013497}
13498
13499/**
James Smart2d7dbc42017-02-12 13:52:35 -080013500 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13501 * @phba: Pointer to HBA context object.
13502 * @rcqe: Pointer to receive-queue completion queue entry.
13503 *
13504 * This routine process a receive-queue completion queue entry.
13505 *
13506 * Return: true if work posted to worker thread, otherwise false.
13507 **/
13508static bool
13509lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13510 struct lpfc_rcqe *rcqe)
13511{
13512 bool workposted = false;
13513 struct lpfc_queue *hrq;
13514 struct lpfc_queue *drq;
13515 struct rqb_dmabuf *dma_buf;
13516 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013517 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013518 uint32_t status, rq_id;
13519 unsigned long iflags;
13520 uint32_t fctl, idx;
13521
13522 if ((phba->nvmet_support == 0) ||
13523 (phba->sli4_hba.nvmet_cqset == NULL))
13524 return workposted;
13525
13526 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13527 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13528 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13529
13530 /* sanity check on queue memory */
13531 if (unlikely(!hrq) || unlikely(!drq))
13532 return workposted;
13533
13534 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13535 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13536 else
13537 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13538
13539 if ((phba->nvmet_support == 0) ||
13540 (rq_id != hrq->queue_id))
13541 return workposted;
13542
13543 status = bf_get(lpfc_rcqe_status, rcqe);
13544 switch (status) {
13545 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13547 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013548 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013549 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013550 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013551 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013552 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13553 if (!dma_buf) {
13554 hrq->RQ_no_buf_found++;
13555 spin_unlock_irqrestore(&phba->hbalock, iflags);
13556 goto out;
13557 }
13558 spin_unlock_irqrestore(&phba->hbalock, iflags);
13559 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013560 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013561 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13562
13563 /* Just some basic sanity checks on FCP Command frame */
13564 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13565 fc_hdr->fh_f_ctl[1] << 8 |
13566 fc_hdr->fh_f_ctl[2]);
13567 if (((fctl &
13568 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13569 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13570 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13571 goto drop;
13572
13573 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13574 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013575 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070013576 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013577 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013578 return false;
13579 }
13580drop:
13581 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13582 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013583 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013584 if (phba->nvmet_support) {
13585 tgtp = phba->targetport->private;
13586 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13587 "6401 RQE Error x%x, posted %d err_cnt "
13588 "%d: %x %x %x\n",
13589 status, hrq->RQ_buf_posted,
13590 hrq->RQ_no_posted_buf,
13591 atomic_read(&tgtp->rcv_fcp_cmd_in),
13592 atomic_read(&tgtp->rcv_fcp_cmd_out),
13593 atomic_read(&tgtp->xmt_fcp_release));
13594 }
13595 /* fallthrough */
13596
13597 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013598 hrq->RQ_no_posted_buf++;
13599 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013600 break;
13601 }
13602out:
13603 return workposted;
13604}
13605
13606/**
James Smart895427b2017-02-12 13:52:30 -080013607 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013608 * @cq: Pointer to the completion queue.
13609 * @eqe: Pointer to fast-path completion queue entry.
13610 *
13611 * This routine process a fast-path work queue completion entry from fast-path
13612 * event queue for FCP command response completion.
13613 **/
13614static int
James Smart895427b2017-02-12 13:52:30 -080013615lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013616 struct lpfc_cqe *cqe)
13617{
13618 struct lpfc_wcqe_release wcqe;
13619 bool workposted = false;
13620
13621 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013622 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013623
13624 /* Check and process for different type of WCQE and dispatch */
13625 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13626 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013627 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013628 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013629 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013630 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013631 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13632 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13633 (struct lpfc_wcqe_complete *)&wcqe);
13634 if (cq->subtype == LPFC_NVME_LS)
13635 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013636 (struct lpfc_wcqe_complete *)&wcqe);
13637 break;
13638 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013639 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013640 /* Process the WQ release event */
13641 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13642 (struct lpfc_wcqe_release *)&wcqe);
13643 break;
13644 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013645 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013646 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013647 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013648 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13649 (struct sli4_wcqe_xri_aborted *)&wcqe);
13650 break;
James Smart895427b2017-02-12 13:52:30 -080013651 case CQE_CODE_RECEIVE_V1:
13652 case CQE_CODE_RECEIVE:
13653 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013654 if (cq->subtype == LPFC_NVMET) {
13655 workposted = lpfc_sli4_nvmet_handle_rcqe(
13656 phba, cq, (struct lpfc_rcqe *)&wcqe);
13657 }
James Smart895427b2017-02-12 13:52:30 -080013658 break;
James Smart4f774512009-05-22 14:52:35 -040013659 default:
13660 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013661 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013662 bf_get(lpfc_wcqe_c_code, &wcqe));
13663 break;
13664 }
13665 return workposted;
13666}
13667
13668/**
James Smart67d12732012-08-03 12:36:13 -040013669 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013670 * @phba: Pointer to HBA context object.
13671 * @eqe: Pointer to fast-path event queue entry.
13672 *
13673 * This routine process a event queue entry from the fast-path event queue.
13674 * It will check the MajorCode and MinorCode to determine this is for a
13675 * completion event on a completion queue, if not, an error shall be logged
13676 * and just return. Otherwise, it will get to the corresponding completion
13677 * queue and process all the entries on the completion queue, rearm the
13678 * completion queue, and then return.
13679 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013680static void
James Smart67d12732012-08-03 12:36:13 -040013681lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13682 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013683{
James Smart895427b2017-02-12 13:52:30 -080013684 struct lpfc_queue *cq = NULL;
James Smart2d7dbc42017-02-12 13:52:35 -080013685 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013686
James Smartcb5172e2010-03-15 11:25:07 -040013687 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013688 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013689 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013690 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013691 bf_get_le32(lpfc_eqe_major_code, eqe),
13692 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070013693 return;
James Smart4f774512009-05-22 14:52:35 -040013694 }
13695
James Smart67d12732012-08-03 12:36:13 -040013696 /* Get the reference to the corresponding CQ */
13697 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13698
James Smart2d7dbc42017-02-12 13:52:35 -080013699 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13700 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13701 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13702 /* Process NVMET unsol rcv */
13703 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13704 goto process_cq;
13705 }
13706 }
13707
James Smart895427b2017-02-12 13:52:30 -080013708 if (phba->sli4_hba.nvme_cq_map &&
13709 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013710 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013711 cq = phba->sli4_hba.nvme_cq[qidx];
13712 goto process_cq;
13713 }
13714
13715 if (phba->sli4_hba.fcp_cq_map &&
13716 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13717 /* Process FCP command completion */
13718 cq = phba->sli4_hba.fcp_cq[qidx];
13719 goto process_cq;
13720 }
13721
13722 if (phba->sli4_hba.nvmels_cq &&
13723 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13724 /* Process NVME unsol rcv */
13725 cq = phba->sli4_hba.nvmels_cq;
13726 }
13727
13728 /* Otherwise this is a Slow path event */
13729 if (cq == NULL) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013730 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13731 return;
James Smart67d12732012-08-03 12:36:13 -040013732 }
13733
James Smart895427b2017-02-12 13:52:30 -080013734process_cq:
James Smart4f774512009-05-22 14:52:35 -040013735 if (unlikely(cqid != cq->queue_id)) {
13736 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13737 "0368 Miss-matched fast-path completion "
13738 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13739 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070013740 return;
James Smart4f774512009-05-22 14:52:35 -040013741 }
13742
James Smart895427b2017-02-12 13:52:30 -080013743 /* Save EQ associated with this CQ */
13744 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13745
Dick Kennedyf485c182017-09-29 17:34:34 -070013746 if (!queue_work(phba->wq, &cq->irqwork))
13747 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13748 "0363 Cannot schedule soft IRQ "
13749 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13750 cqid, cq->queue_id, smp_processor_id());
13751}
13752
13753/**
13754 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13755 * @phba: Pointer to HBA context object.
13756 * @eqe: Pointer to fast-path event queue entry.
13757 *
13758 * This routine process a event queue entry from the fast-path event queue.
13759 * It will check the MajorCode and MinorCode to determine this is for a
13760 * completion event on a completion queue, if not, an error shall be logged
13761 * and just return. Otherwise, it will get to the corresponding completion
13762 * queue and process all the entries on the completion queue, rearm the
13763 * completion queue, and then return.
13764 **/
13765static void
13766lpfc_sli4_hba_process_cq(struct work_struct *work)
13767{
13768 struct lpfc_queue *cq =
13769 container_of(work, struct lpfc_queue, irqwork);
13770 struct lpfc_hba *phba = cq->phba;
13771 struct lpfc_cqe *cqe;
13772 bool workposted = false;
13773 int ccount = 0;
13774
James Smart4f774512009-05-22 14:52:35 -040013775 /* Process all the entries to the CQ */
13776 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013777#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13778 if (phba->ktime_on)
13779 cq->isr_timestamp = ktime_get_ns();
13780 else
13781 cq->isr_timestamp = 0;
13782#endif
James Smart895427b2017-02-12 13:52:30 -080013783 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013784 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013785 break;
James Smart4f774512009-05-22 14:52:35 -040013786 }
13787
James Smartb84daac2012-08-03 12:35:13 -040013788 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013789 if (ccount > cq->CQ_max_cqe)
13790 cq->CQ_max_cqe = ccount;
13791 cq->assoc_qp->EQ_cqe_cnt += ccount;
James Smartb84daac2012-08-03 12:35:13 -040013792
James Smart4f774512009-05-22 14:52:35 -040013793 /* Catch the no cq entry condition */
Dick Kennedyf485c182017-09-29 17:34:34 -070013794 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013795 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13796 "0369 No entry from fast-path completion "
13797 "queue fcpcqid=%d\n", cq->queue_id);
13798
13799 /* In any case, flash and re-arm the CQ */
James Smartb71413d2018-02-22 08:18:40 -080013800 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013801
13802 /* wake up worker thread if there are works to be done */
13803 if (workposted)
13804 lpfc_worker_wake_up(phba);
13805}
13806
13807static void
13808lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13809{
13810 struct lpfc_eqe *eqe;
13811
13812 /* walk all the EQ entries and drop on the floor */
13813 while ((eqe = lpfc_sli4_eq_get(eq)))
13814 ;
13815
13816 /* Clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -080013817 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013818}
13819
James Smart1ba981f2014-02-20 09:56:45 -050013820
13821/**
13822 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13823 * entry
13824 * @phba: Pointer to HBA context object.
13825 * @eqe: Pointer to fast-path event queue entry.
13826 *
13827 * This routine process a event queue entry from the Flash Optimized Fabric
13828 * event queue. It will check the MajorCode and MinorCode to determine this
13829 * is for a completion event on a completion queue, if not, an error shall be
13830 * logged and just return. Otherwise, it will get to the corresponding
13831 * completion queue and process all the entries on the completion queue, rearm
13832 * the completion queue, and then return.
13833 **/
13834static void
13835lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13836{
13837 struct lpfc_queue *cq;
James Smart1ba981f2014-02-20 09:56:45 -050013838 uint16_t cqid;
James Smart1ba981f2014-02-20 09:56:45 -050013839
13840 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13841 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13842 "9147 Not a valid completion "
13843 "event: majorcode=x%x, minorcode=x%x\n",
13844 bf_get_le32(lpfc_eqe_major_code, eqe),
13845 bf_get_le32(lpfc_eqe_minor_code, eqe));
13846 return;
13847 }
13848
13849 /* Get the reference to the corresponding CQ */
13850 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13851
13852 /* Next check for OAS */
13853 cq = phba->sli4_hba.oas_cq;
13854 if (unlikely(!cq)) {
13855 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13856 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13857 "9148 OAS completion queue "
13858 "does not exist\n");
13859 return;
13860 }
13861
13862 if (unlikely(cqid != cq->queue_id)) {
13863 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13864 "9149 Miss-matched fast-path compl "
13865 "queue id: eqcqid=%d, fcpcqid=%d\n",
13866 cqid, cq->queue_id);
13867 return;
13868 }
13869
James Smartd41b65b2017-06-15 22:56:41 -070013870 /* Save EQ associated with this CQ */
13871 cq->assoc_qp = phba->sli4_hba.fof_eq;
13872
Dick Kennedyf485c182017-09-29 17:34:34 -070013873 /* CQ work will be processed on CPU affinitized to this IRQ */
13874 if (!queue_work(phba->wq, &cq->irqwork))
James Smart1ba981f2014-02-20 09:56:45 -050013875 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf485c182017-09-29 17:34:34 -070013876 "0367 Cannot schedule soft IRQ "
13877 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13878 cqid, cq->queue_id, smp_processor_id());
James Smart1ba981f2014-02-20 09:56:45 -050013879}
13880
13881/**
13882 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13883 * @irq: Interrupt number.
13884 * @dev_id: The device context pointer.
13885 *
13886 * This function is directly called from the PCI layer as an interrupt
13887 * service routine when device with SLI-4 interface spec is enabled with
13888 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13889 * IOCB ring event in the HBA. However, when the device is enabled with either
13890 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13891 * device-level interrupt handler. When the PCI slot is in error recovery
13892 * or the HBA is undergoing initialization, the interrupt handler will not
13893 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13894 * the intrrupt context. This function is called without any lock held.
13895 * It gets the hbalock to access and update SLI data structures. Note that,
13896 * the EQ to CQ are one-to-one map such that the EQ index is
13897 * equal to that of CQ index.
13898 *
13899 * This function returns IRQ_HANDLED when interrupt is handled else it
13900 * returns IRQ_NONE.
13901 **/
13902irqreturn_t
13903lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13904{
13905 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013906 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013907 struct lpfc_queue *eq;
13908 struct lpfc_eqe *eqe;
13909 unsigned long iflag;
13910 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013911
13912 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013913 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13914 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013915
13916 if (unlikely(!phba))
13917 return IRQ_NONE;
13918
13919 /* Get to the EQ struct associated with this vector */
13920 eq = phba->sli4_hba.fof_eq;
13921 if (unlikely(!eq))
13922 return IRQ_NONE;
13923
13924 /* Check device state for handling interrupt */
13925 if (unlikely(lpfc_intr_state_check(phba))) {
James Smart1ba981f2014-02-20 09:56:45 -050013926 /* Check again for link_state with lock held */
13927 spin_lock_irqsave(&phba->hbalock, iflag);
13928 if (phba->link_state < LPFC_LINK_DOWN)
13929 /* Flush, clear interrupt, and rearm the EQ */
13930 lpfc_sli4_eq_flush(phba, eq);
13931 spin_unlock_irqrestore(&phba->hbalock, iflag);
13932 return IRQ_NONE;
13933 }
13934
13935 /*
13936 * Process all the event on FCP fast-path EQ
13937 */
13938 while ((eqe = lpfc_sli4_eq_get(eq))) {
13939 lpfc_sli4_fof_handle_eqe(phba, eqe);
13940 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013941 break;
James Smart1ba981f2014-02-20 09:56:45 -050013942 eq->EQ_processed++;
13943 }
13944
13945 /* Track the max number of EQEs processed in 1 intr */
13946 if (ecount > eq->EQ_max_eqe)
13947 eq->EQ_max_eqe = ecount;
13948
13949
13950 if (unlikely(ecount == 0)) {
13951 eq->EQ_no_entry++;
13952
13953 if (phba->intr_type == MSIX)
13954 /* MSI-X treated interrupt served as no EQ share INT */
13955 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13956 "9145 MSI-X interrupt with no EQE\n");
13957 else {
13958 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13959 "9146 ISR interrupt with no EQE\n");
13960 /* Non MSI-X treated on interrupt as EQ share INT */
13961 return IRQ_NONE;
13962 }
13963 }
13964 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080013965 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -050013966 return IRQ_HANDLED;
13967}
13968
James Smart4f774512009-05-22 14:52:35 -040013969/**
James Smart67d12732012-08-03 12:36:13 -040013970 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013971 * @irq: Interrupt number.
13972 * @dev_id: The device context pointer.
13973 *
13974 * This function is directly called from the PCI layer as an interrupt
13975 * service routine when device with SLI-4 interface spec is enabled with
13976 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13977 * ring event in the HBA. However, when the device is enabled with either
13978 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13979 * device-level interrupt handler. When the PCI slot is in error recovery
13980 * or the HBA is undergoing initialization, the interrupt handler will not
13981 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13982 * the intrrupt context. This function is called without any lock held.
13983 * It gets the hbalock to access and update SLI data structures. Note that,
13984 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13985 * equal to that of FCP CQ index.
13986 *
James Smart67d12732012-08-03 12:36:13 -040013987 * The link attention and ELS ring attention events are handled
13988 * by the worker thread. The interrupt handler signals the worker thread
13989 * and returns for these events. This function is called without any lock
13990 * held. It gets the hbalock to access and update SLI data structures.
13991 *
James Smart4f774512009-05-22 14:52:35 -040013992 * This function returns IRQ_HANDLED when interrupt is handled else it
13993 * returns IRQ_NONE.
13994 **/
13995irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013996lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013997{
13998 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013999 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040014000 struct lpfc_queue *fpeq;
14001 struct lpfc_eqe *eqe;
14002 unsigned long iflag;
14003 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080014004 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040014005
14006 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014007 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14008 phba = hba_eq_hdl->phba;
14009 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014010
14011 if (unlikely(!phba))
14012 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040014013 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040014014 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014015
14016 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080014017 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050014018 if (unlikely(!fpeq))
14019 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014020
James Smartba20c852012-08-03 12:36:52 -040014021 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080014022 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartb71413d2018-02-22 08:18:40 -080014023 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -040014024 else {
James Smart895427b2017-02-12 13:52:30 -080014025 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040014026 return IRQ_NONE;
14027 }
14028 }
14029
James Smart4f774512009-05-22 14:52:35 -040014030 /* Check device state for handling interrupt */
14031 if (unlikely(lpfc_intr_state_check(phba))) {
14032 /* Check again for link_state with lock held */
14033 spin_lock_irqsave(&phba->hbalock, iflag);
14034 if (phba->link_state < LPFC_LINK_DOWN)
14035 /* Flush, clear interrupt, and rearm the EQ */
14036 lpfc_sli4_eq_flush(phba, fpeq);
14037 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040014038 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014039 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040014040 return IRQ_NONE;
14041 }
14042
14043 /*
14044 * Process all the event on FCP fast-path EQ
14045 */
14046 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
Dick Kennedyf485c182017-09-29 17:34:34 -070014047 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14048 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070014049 break;
James Smartb84daac2012-08-03 12:35:13 -040014050 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040014051 }
14052
James Smartb84daac2012-08-03 12:35:13 -040014053 /* Track the max number of EQEs processed in 1 intr */
14054 if (ecount > fpeq->EQ_max_eqe)
14055 fpeq->EQ_max_eqe = ecount;
14056
James Smart4f774512009-05-22 14:52:35 -040014057 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080014058 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014059
14060 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014061 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040014062
14063 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080014064 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040014065 return IRQ_NONE;
14066 }
14067
James Smart4f774512009-05-22 14:52:35 -040014068 if (phba->intr_type == MSIX)
14069 /* MSI-X treated interrupt served as no EQ share INT */
14070 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14071 "0358 MSI-X interrupt with no EQE\n");
14072 else
14073 /* Non MSI-X treated on interrupt as EQ share INT */
14074 return IRQ_NONE;
14075 }
14076
James Smartba20c852012-08-03 12:36:52 -040014077 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014078 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14079
James Smart4f774512009-05-22 14:52:35 -040014080 return IRQ_HANDLED;
14081} /* lpfc_sli4_fp_intr_handler */
14082
14083/**
14084 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14085 * @irq: Interrupt number.
14086 * @dev_id: The device context pointer.
14087 *
14088 * This function is the device-level interrupt handler to device with SLI-4
14089 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14090 * interrupt mode is enabled and there is an event in the HBA which requires
14091 * driver attention. This function invokes the slow-path interrupt attention
14092 * handling function and fast-path interrupt attention handling function in
14093 * turn to process the relevant HBA attention events. This function is called
14094 * without any lock held. It gets the hbalock to access and update SLI data
14095 * structures.
14096 *
14097 * This function returns IRQ_HANDLED when interrupt is handled, else it
14098 * returns IRQ_NONE.
14099 **/
14100irqreturn_t
14101lpfc_sli4_intr_handler(int irq, void *dev_id)
14102{
14103 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014104 irqreturn_t hba_irq_rc;
14105 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014106 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014107
14108 /* Get the driver's phba structure from the dev_id */
14109 phba = (struct lpfc_hba *)dev_id;
14110
14111 if (unlikely(!phba))
14112 return IRQ_NONE;
14113
14114 /*
James Smart4f774512009-05-22 14:52:35 -040014115 * Invoke fast-path host attention interrupt handling as appropriate.
14116 */
James Smart895427b2017-02-12 13:52:30 -080014117 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014118 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014119 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014120 if (hba_irq_rc == IRQ_HANDLED)
14121 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014122 }
14123
James Smart1ba981f2014-02-20 09:56:45 -050014124 if (phba->cfg_fof) {
14125 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014126 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050014127 if (hba_irq_rc == IRQ_HANDLED)
14128 hba_handled |= true;
14129 }
14130
James Smart67d12732012-08-03 12:36:13 -040014131 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014132} /* lpfc_sli4_intr_handler */
14133
14134/**
14135 * lpfc_sli4_queue_free - free a queue structure and associated memory
14136 * @queue: The queue structure to free.
14137 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014138 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014139 * the host resident queue. This function must be called after destroying the
14140 * queue on the HBA.
14141 **/
14142void
14143lpfc_sli4_queue_free(struct lpfc_queue *queue)
14144{
14145 struct lpfc_dmabuf *dmabuf;
14146
14147 if (!queue)
14148 return;
14149
14150 while (!list_empty(&queue->page_list)) {
14151 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14152 list);
James Smart81b96ed2017-11-20 16:00:29 -080014153 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014154 dmabuf->virt, dmabuf->phys);
14155 kfree(dmabuf);
14156 }
James Smart895427b2017-02-12 13:52:30 -080014157 if (queue->rqbp) {
14158 lpfc_free_rq_buffer(queue->phba, queue);
14159 kfree(queue->rqbp);
14160 }
James Smartd1f525a2017-04-21 16:04:55 -070014161
14162 if (!list_empty(&queue->wq_list))
14163 list_del(&queue->wq_list);
14164
James Smart4f774512009-05-22 14:52:35 -040014165 kfree(queue);
14166 return;
14167}
14168
14169/**
14170 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14171 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014172 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014173 * @entry_size: The size of each queue entry for this queue.
14174 * @entry count: The number of entries that this queue will handle.
14175 *
14176 * This function allocates a queue structure and the DMAable memory used for
14177 * the host resident queue. This function must be called before creating the
14178 * queue on the HBA.
14179 **/
14180struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014181lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14182 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040014183{
14184 struct lpfc_queue *queue;
14185 struct lpfc_dmabuf *dmabuf;
14186 int x, total_qe_count;
14187 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040014188 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040014189
James Smartcb5172e2010-03-15 11:25:07 -040014190 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014191 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014192
James Smart4f774512009-05-22 14:52:35 -040014193 queue = kzalloc(sizeof(struct lpfc_queue) +
14194 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14195 if (!queue)
14196 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040014197 queue->page_count = (ALIGN(entry_size * entry_count,
14198 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014199
14200 /* If needed, Adjust page count to match the max the adapter supports */
14201 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14202 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14203
James Smart4f774512009-05-22 14:52:35 -040014204 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014205 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014206 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014207 INIT_LIST_HEAD(&queue->page_list);
14208 INIT_LIST_HEAD(&queue->child_list);
James Smart81b96ed2017-11-20 16:00:29 -080014209
14210 /* Set queue parameters now. If the system cannot provide memory
14211 * resources, the free routine needs to know what was allocated.
14212 */
14213 queue->entry_size = entry_size;
14214 queue->entry_count = entry_count;
14215 queue->page_size = hw_page_size;
14216 queue->phba = phba;
14217
James Smart4f774512009-05-22 14:52:35 -040014218 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14219 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14220 if (!dmabuf)
14221 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040014222 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14223 hw_page_size, &dmabuf->phys,
14224 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014225 if (!dmabuf->virt) {
14226 kfree(dmabuf);
14227 goto out_fail;
14228 }
14229 dmabuf->buffer_tag = x;
14230 list_add_tail(&dmabuf->list, &queue->page_list);
14231 /* initialize queue's entry array */
14232 dma_pointer = dmabuf->virt;
14233 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040014234 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040014235 total_qe_count++, dma_pointer += entry_size) {
14236 queue->qe[total_qe_count].address = dma_pointer;
14237 }
14238 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014239 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14240 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014241
James Smart64eb4dc2017-05-15 15:20:49 -070014242 /* entry_repost will be set during q creation */
14243
James Smart4f774512009-05-22 14:52:35 -040014244 return queue;
14245out_fail:
14246 lpfc_sli4_queue_free(queue);
14247 return NULL;
14248}
14249
14250/**
James Smart962bc512013-01-03 15:44:00 -050014251 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14252 * @phba: HBA structure that indicates port to create a queue on.
14253 * @pci_barset: PCI BAR set flag.
14254 *
14255 * This function shall perform iomap of the specified PCI BAR address to host
14256 * memory address if not already done so and return it. The returned host
14257 * memory address can be NULL.
14258 */
14259static void __iomem *
14260lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14261{
James Smart962bc512013-01-03 15:44:00 -050014262 if (!phba->pcidev)
14263 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014264
14265 switch (pci_barset) {
14266 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014267 return phba->pci_bar0_memmap_p;
14268 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014269 return phba->pci_bar2_memmap_p;
14270 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014271 return phba->pci_bar4_memmap_p;
14272 default:
14273 break;
14274 }
14275 return NULL;
14276}
14277
14278/**
James Smart895427b2017-02-12 13:52:30 -080014279 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040014280 * @phba: HBA structure that indicates port to create a queue on.
14281 * @startq: The starting FCP EQ to modify
14282 *
14283 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080014284 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14285 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040014286 *
14287 * The @phba struct is used to send mailbox command to HBA. The @startq
14288 * is used to get the starting FCP EQ to change.
14289 * This function is asynchronous and will wait for the mailbox
14290 * command to finish before continuing.
14291 *
14292 * On success this function will return a zero. If unable to allocate enough
14293 * memory this function will return -ENOMEM. If the queue create mailbox command
14294 * fails this function will return -ENXIO.
14295 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014296int
James Smart0cf07f842017-06-01 21:07:10 -070014297lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14298 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014299{
14300 struct lpfc_mbx_modify_eq_delay *eq_delay;
14301 LPFC_MBOXQ_t *mbox;
14302 struct lpfc_queue *eq;
14303 int cnt, rc, length, status = 0;
14304 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014305 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014306 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014307 union lpfc_sli4_cfg_shdr *shdr;
14308 uint16_t dmult;
14309
James Smart895427b2017-02-12 13:52:30 -080014310 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040014311 return 0;
14312
14313 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14314 if (!mbox)
14315 return -ENOMEM;
14316 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14317 sizeof(struct lpfc_sli4_cfg_mhdr));
14318 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14319 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14320 length, LPFC_SLI4_MBX_EMBED);
14321 eq_delay = &mbox->u.mqe.un.eq_delay;
14322
14323 /* Calculate delay multiper from maximum interrupt per second */
James Smart0cf07f842017-06-01 21:07:10 -070014324 result = imax / phba->io_channel_irqs;
James Smart895427b2017-02-12 13:52:30 -080014325 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014326 dmult = 0;
14327 else
14328 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014329 if (dmult > LPFC_DMULT_MAX)
14330 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014331
14332 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014333 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14334 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014335 if (!eq)
14336 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014337 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014338 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14339 eq_delay->u.request.eq[cnt].phase = 0;
14340 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14341 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014342
14343 /* q_mode is only used for auto_imax */
14344 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14345 /* Use EQ Delay Register method for q_mode */
14346
14347 /* Convert for EQ Delay register */
14348 val = phba->cfg_fcp_imax;
14349 if (val) {
14350 /* First, interrupts per sec per EQ */
14351 val = phba->cfg_fcp_imax /
14352 phba->io_channel_irqs;
14353
14354 /* us delay between each interrupt */
14355 val = LPFC_SEC_TO_USEC / val;
14356 }
14357 eq->q_mode = val;
14358 } else {
14359 eq->q_mode = imax;
14360 }
14361
14362 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014363 break;
14364 }
14365 eq_delay->u.request.num_eq = cnt;
14366
14367 mbox->vport = phba->pport;
14368 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14369 mbox->context1 = NULL;
14370 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14371 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14372 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14373 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14374 if (shdr_status || shdr_add_status || rc) {
14375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14376 "2512 MODIFY_EQ_DELAY mailbox failed with "
14377 "status x%x add_status x%x, mbx status x%x\n",
14378 shdr_status, shdr_add_status, rc);
14379 status = -ENXIO;
14380 }
14381 mempool_free(mbox, phba->mbox_mem_pool);
14382 return status;
14383}
14384
14385/**
James Smart4f774512009-05-22 14:52:35 -040014386 * lpfc_eq_create - Create an Event Queue on the HBA
14387 * @phba: HBA structure that indicates port to create a queue on.
14388 * @eq: The queue structure to use to create the event queue.
14389 * @imax: The maximum interrupt per second limit.
14390 *
14391 * This function creates an event queue, as detailed in @eq, on a port,
14392 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14393 *
14394 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14395 * is used to get the entry count and entry size that are necessary to
14396 * determine the number of pages to allocate and use for this queue. This
14397 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14398 * event queue. This function is asynchronous and will wait for the mailbox
14399 * command to finish before continuing.
14400 *
14401 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014402 * memory this function will return -ENOMEM. If the queue create mailbox command
14403 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014404 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014405int
James Smartee020062012-09-29 11:28:52 -040014406lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014407{
14408 struct lpfc_mbx_eq_create *eq_create;
14409 LPFC_MBOXQ_t *mbox;
14410 int rc, length, status = 0;
14411 struct lpfc_dmabuf *dmabuf;
14412 uint32_t shdr_status, shdr_add_status;
14413 union lpfc_sli4_cfg_shdr *shdr;
14414 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014415 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14416
James Smart2e90f4b2011-12-13 13:22:37 -050014417 /* sanity check on queue memory */
14418 if (!eq)
14419 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014420 if (!phba->sli4_hba.pc_sli4_params.supported)
14421 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014422
14423 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14424 if (!mbox)
14425 return -ENOMEM;
14426 length = (sizeof(struct lpfc_mbx_eq_create) -
14427 sizeof(struct lpfc_sli4_cfg_mhdr));
14428 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14429 LPFC_MBOX_OPCODE_EQ_CREATE,
14430 length, LPFC_SLI4_MBX_EMBED);
14431 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014432 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014433 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14434 eq->page_count);
14435 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14436 LPFC_EQE_SIZE);
14437 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014438
14439 /* Use version 2 of CREATE_EQ if eqav is set */
14440 if (phba->sli4_hba.pc_sli4_params.eqav) {
14441 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14442 LPFC_Q_CREATE_VERSION_2);
14443 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14444 phba->sli4_hba.pc_sli4_params.eqav);
14445 }
14446
James Smart2c9c5a02015-04-07 15:07:15 -040014447 /* don't setup delay multiplier using EQ_CREATE */
14448 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014449 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14450 dmult);
14451 switch (eq->entry_count) {
14452 default:
14453 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14454 "0360 Unsupported EQ count. (%d)\n",
14455 eq->entry_count);
14456 if (eq->entry_count < 256)
14457 return -EINVAL;
14458 /* otherwise default to smallest count (drop through) */
14459 case 256:
14460 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14461 LPFC_EQ_CNT_256);
14462 break;
14463 case 512:
14464 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14465 LPFC_EQ_CNT_512);
14466 break;
14467 case 1024:
14468 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14469 LPFC_EQ_CNT_1024);
14470 break;
14471 case 2048:
14472 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14473 LPFC_EQ_CNT_2048);
14474 break;
14475 case 4096:
14476 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14477 LPFC_EQ_CNT_4096);
14478 break;
14479 }
14480 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014481 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014482 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14483 putPaddrLow(dmabuf->phys);
14484 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14485 putPaddrHigh(dmabuf->phys);
14486 }
14487 mbox->vport = phba->pport;
14488 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14489 mbox->context1 = NULL;
14490 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040014491 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14492 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14493 if (shdr_status || shdr_add_status || rc) {
14494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14495 "2500 EQ_CREATE mailbox failed with "
14496 "status x%x add_status x%x, mbx status x%x\n",
14497 shdr_status, shdr_add_status, rc);
14498 status = -ENXIO;
14499 }
14500 eq->type = LPFC_EQ;
14501 eq->subtype = LPFC_NONE;
14502 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14503 if (eq->queue_id == 0xFFFF)
14504 status = -ENXIO;
14505 eq->host_index = 0;
14506 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014507 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014508
James Smart8fa38512009-07-19 10:01:03 -040014509 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014510 return status;
14511}
14512
14513/**
14514 * lpfc_cq_create - Create a Completion Queue on the HBA
14515 * @phba: HBA structure that indicates port to create a queue on.
14516 * @cq: The queue structure to use to create the completion queue.
14517 * @eq: The event queue to bind this completion queue to.
14518 *
14519 * This function creates a completion queue, as detailed in @wq, on a port,
14520 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14521 *
14522 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14523 * is used to get the entry count and entry size that are necessary to
14524 * determine the number of pages to allocate and use for this queue. The @eq
14525 * is used to indicate which event queue to bind this completion queue to. This
14526 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14527 * completion queue. This function is asynchronous and will wait for the mailbox
14528 * command to finish before continuing.
14529 *
14530 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014531 * memory this function will return -ENOMEM. If the queue create mailbox command
14532 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014533 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014534int
James Smart4f774512009-05-22 14:52:35 -040014535lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14536 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14537{
14538 struct lpfc_mbx_cq_create *cq_create;
14539 struct lpfc_dmabuf *dmabuf;
14540 LPFC_MBOXQ_t *mbox;
14541 int rc, length, status = 0;
14542 uint32_t shdr_status, shdr_add_status;
14543 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014544 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14545
James Smart2e90f4b2011-12-13 13:22:37 -050014546 /* sanity check on queue memory */
14547 if (!cq || !eq)
14548 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014549 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014550 hw_page_size = cq->page_size;
James Smart49198b32010-04-06 15:04:33 -040014551
James Smart4f774512009-05-22 14:52:35 -040014552 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14553 if (!mbox)
14554 return -ENOMEM;
14555 length = (sizeof(struct lpfc_mbx_cq_create) -
14556 sizeof(struct lpfc_sli4_cfg_mhdr));
14557 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14558 LPFC_MBOX_OPCODE_CQ_CREATE,
14559 length, LPFC_SLI4_MBX_EMBED);
14560 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014561 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014562 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14563 cq->page_count);
14564 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14565 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014566 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14567 phba->sli4_hba.pc_sli4_params.cqv);
14568 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014569 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14570 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014571 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14572 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080014573 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14574 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050014575 } else {
14576 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14577 eq->queue_id);
14578 }
James Smart4f774512009-05-22 14:52:35 -040014579 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014580 case 2048:
14581 case 4096:
14582 if (phba->sli4_hba.pc_sli4_params.cqv ==
14583 LPFC_Q_CREATE_VERSION_2) {
14584 cq_create->u.request.context.lpfc_cq_context_count =
14585 cq->entry_count;
14586 bf_set(lpfc_cq_context_count,
14587 &cq_create->u.request.context,
14588 LPFC_CQ_CNT_WORD7);
14589 break;
14590 }
14591 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014592 default:
14593 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014594 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014595 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014596 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014597 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014598 if (cq->entry_count < 256) {
14599 status = -EINVAL;
14600 goto out;
14601 }
James Smart4f774512009-05-22 14:52:35 -040014602 /* otherwise default to smallest count (drop through) */
14603 case 256:
14604 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14605 LPFC_CQ_CNT_256);
14606 break;
14607 case 512:
14608 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14609 LPFC_CQ_CNT_512);
14610 break;
14611 case 1024:
14612 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14613 LPFC_CQ_CNT_1024);
14614 break;
14615 }
14616 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014617 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014618 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14619 putPaddrLow(dmabuf->phys);
14620 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14621 putPaddrHigh(dmabuf->phys);
14622 }
14623 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14624
14625 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014626 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14627 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14628 if (shdr_status || shdr_add_status || rc) {
14629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14630 "2501 CQ_CREATE mailbox failed with "
14631 "status x%x add_status x%x, mbx status x%x\n",
14632 shdr_status, shdr_add_status, rc);
14633 status = -ENXIO;
14634 goto out;
14635 }
14636 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14637 if (cq->queue_id == 0xFFFF) {
14638 status = -ENXIO;
14639 goto out;
14640 }
14641 /* link the cq onto the parent eq child list */
14642 list_add_tail(&cq->list, &eq->child_list);
14643 /* Set up completion queue's type and subtype */
14644 cq->type = type;
14645 cq->subtype = subtype;
14646 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014647 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014648 cq->host_index = 0;
14649 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014650 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014651
James Smart8fa38512009-07-19 10:01:03 -040014652out:
14653 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014654 return status;
14655}
14656
14657/**
James Smart2d7dbc42017-02-12 13:52:35 -080014658 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14659 * @phba: HBA structure that indicates port to create a queue on.
14660 * @cqp: The queue structure array to use to create the completion queues.
14661 * @eqp: The event queue array to bind these completion queues to.
14662 *
14663 * This function creates a set of completion queue, s to support MRQ
14664 * as detailed in @cqp, on a port,
14665 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14666 *
14667 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14668 * is used to get the entry count and entry size that are necessary to
14669 * determine the number of pages to allocate and use for this queue. The @eq
14670 * is used to indicate which event queue to bind this completion queue to. This
14671 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14672 * completion queue. This function is asynchronous and will wait for the mailbox
14673 * command to finish before continuing.
14674 *
14675 * On success this function will return a zero. If unable to allocate enough
14676 * memory this function will return -ENOMEM. If the queue create mailbox command
14677 * fails this function will return -ENXIO.
14678 **/
14679int
14680lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14681 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14682{
14683 struct lpfc_queue *cq;
14684 struct lpfc_queue *eq;
14685 struct lpfc_mbx_cq_create_set *cq_set;
14686 struct lpfc_dmabuf *dmabuf;
14687 LPFC_MBOXQ_t *mbox;
14688 int rc, length, alloclen, status = 0;
14689 int cnt, idx, numcq, page_idx = 0;
14690 uint32_t shdr_status, shdr_add_status;
14691 union lpfc_sli4_cfg_shdr *shdr;
14692 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14693
14694 /* sanity check on queue memory */
14695 numcq = phba->cfg_nvmet_mrq;
14696 if (!cqp || !eqp || !numcq)
14697 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014698
14699 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14700 if (!mbox)
14701 return -ENOMEM;
14702
14703 length = sizeof(struct lpfc_mbx_cq_create_set);
14704 length += ((numcq * cqp[0]->page_count) *
14705 sizeof(struct dma_address));
14706 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14707 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14708 LPFC_SLI4_MBX_NEMBED);
14709 if (alloclen < length) {
14710 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14711 "3098 Allocated DMA memory size (%d) is "
14712 "less than the requested DMA memory size "
14713 "(%d)\n", alloclen, length);
14714 status = -ENOMEM;
14715 goto out;
14716 }
14717 cq_set = mbox->sge_array->addr[0];
14718 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14719 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14720
14721 for (idx = 0; idx < numcq; idx++) {
14722 cq = cqp[idx];
14723 eq = eqp[idx];
14724 if (!cq || !eq) {
14725 status = -ENOMEM;
14726 goto out;
14727 }
James Smart81b96ed2017-11-20 16:00:29 -080014728 if (!phba->sli4_hba.pc_sli4_params.supported)
14729 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014730
14731 switch (idx) {
14732 case 0:
14733 bf_set(lpfc_mbx_cq_create_set_page_size,
14734 &cq_set->u.request,
14735 (hw_page_size / SLI4_PAGE_SIZE));
14736 bf_set(lpfc_mbx_cq_create_set_num_pages,
14737 &cq_set->u.request, cq->page_count);
14738 bf_set(lpfc_mbx_cq_create_set_evt,
14739 &cq_set->u.request, 1);
14740 bf_set(lpfc_mbx_cq_create_set_valid,
14741 &cq_set->u.request, 1);
14742 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14743 &cq_set->u.request, 0);
14744 bf_set(lpfc_mbx_cq_create_set_num_cq,
14745 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080014746 bf_set(lpfc_mbx_cq_create_set_autovalid,
14747 &cq_set->u.request,
14748 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080014749 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014750 case 2048:
14751 case 4096:
14752 if (phba->sli4_hba.pc_sli4_params.cqv ==
14753 LPFC_Q_CREATE_VERSION_2) {
14754 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14755 &cq_set->u.request,
14756 cq->entry_count);
14757 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14758 &cq_set->u.request,
14759 LPFC_CQ_CNT_WORD7);
14760 break;
14761 }
14762 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080014763 default:
14764 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14765 "3118 Bad CQ count. (%d)\n",
14766 cq->entry_count);
14767 if (cq->entry_count < 256) {
14768 status = -EINVAL;
14769 goto out;
14770 }
14771 /* otherwise default to smallest (drop thru) */
14772 case 256:
14773 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14774 &cq_set->u.request, LPFC_CQ_CNT_256);
14775 break;
14776 case 512:
14777 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14778 &cq_set->u.request, LPFC_CQ_CNT_512);
14779 break;
14780 case 1024:
14781 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14782 &cq_set->u.request, LPFC_CQ_CNT_1024);
14783 break;
14784 }
14785 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14786 &cq_set->u.request, eq->queue_id);
14787 break;
14788 case 1:
14789 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14790 &cq_set->u.request, eq->queue_id);
14791 break;
14792 case 2:
14793 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14794 &cq_set->u.request, eq->queue_id);
14795 break;
14796 case 3:
14797 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14798 &cq_set->u.request, eq->queue_id);
14799 break;
14800 case 4:
14801 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14802 &cq_set->u.request, eq->queue_id);
14803 break;
14804 case 5:
14805 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14806 &cq_set->u.request, eq->queue_id);
14807 break;
14808 case 6:
14809 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14810 &cq_set->u.request, eq->queue_id);
14811 break;
14812 case 7:
14813 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14814 &cq_set->u.request, eq->queue_id);
14815 break;
14816 case 8:
14817 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14818 &cq_set->u.request, eq->queue_id);
14819 break;
14820 case 9:
14821 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14822 &cq_set->u.request, eq->queue_id);
14823 break;
14824 case 10:
14825 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14826 &cq_set->u.request, eq->queue_id);
14827 break;
14828 case 11:
14829 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14830 &cq_set->u.request, eq->queue_id);
14831 break;
14832 case 12:
14833 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14834 &cq_set->u.request, eq->queue_id);
14835 break;
14836 case 13:
14837 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14838 &cq_set->u.request, eq->queue_id);
14839 break;
14840 case 14:
14841 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14842 &cq_set->u.request, eq->queue_id);
14843 break;
14844 case 15:
14845 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14846 &cq_set->u.request, eq->queue_id);
14847 break;
14848 }
14849
14850 /* link the cq onto the parent eq child list */
14851 list_add_tail(&cq->list, &eq->child_list);
14852 /* Set up completion queue's type and subtype */
14853 cq->type = type;
14854 cq->subtype = subtype;
14855 cq->assoc_qid = eq->queue_id;
14856 cq->host_index = 0;
14857 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014858 cq->entry_repost = LPFC_CQ_REPOST;
James Smart81b96ed2017-11-20 16:00:29 -080014859 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080014860
14861 rc = 0;
14862 list_for_each_entry(dmabuf, &cq->page_list, list) {
14863 memset(dmabuf->virt, 0, hw_page_size);
14864 cnt = page_idx + dmabuf->buffer_tag;
14865 cq_set->u.request.page[cnt].addr_lo =
14866 putPaddrLow(dmabuf->phys);
14867 cq_set->u.request.page[cnt].addr_hi =
14868 putPaddrHigh(dmabuf->phys);
14869 rc++;
14870 }
14871 page_idx += rc;
14872 }
14873
14874 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14875
14876 /* The IOCTL status is embedded in the mailbox subheader. */
14877 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14878 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14879 if (shdr_status || shdr_add_status || rc) {
14880 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14881 "3119 CQ_CREATE_SET mailbox failed with "
14882 "status x%x add_status x%x, mbx status x%x\n",
14883 shdr_status, shdr_add_status, rc);
14884 status = -ENXIO;
14885 goto out;
14886 }
14887 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14888 if (rc == 0xFFFF) {
14889 status = -ENXIO;
14890 goto out;
14891 }
14892
14893 for (idx = 0; idx < numcq; idx++) {
14894 cq = cqp[idx];
14895 cq->queue_id = rc + idx;
14896 }
14897
14898out:
14899 lpfc_sli4_mbox_cmd_free(phba, mbox);
14900 return status;
14901}
14902
14903/**
James Smartb19a0612010-04-06 14:48:51 -040014904 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014905 * @phba: HBA structure that indicates port to create a queue on.
14906 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014907 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14908 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014909 *
James Smartb19a0612010-04-06 14:48:51 -040014910 * This function provides failback (fb) functionality when the
14911 * mq_create_ext fails on older FW generations. It's purpose is identical
14912 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014913 *
James Smartb19a0612010-04-06 14:48:51 -040014914 * This routine cannot fail as all attributes were previously accessed and
14915 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014916 **/
James Smartb19a0612010-04-06 14:48:51 -040014917static void
14918lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14919 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014920{
14921 struct lpfc_mbx_mq_create *mq_create;
14922 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014923 int length;
James Smart04c68492009-05-22 14:52:52 -040014924
James Smart04c68492009-05-22 14:52:52 -040014925 length = (sizeof(struct lpfc_mbx_mq_create) -
14926 sizeof(struct lpfc_sli4_cfg_mhdr));
14927 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14928 LPFC_MBOX_OPCODE_MQ_CREATE,
14929 length, LPFC_SLI4_MBX_EMBED);
14930 mq_create = &mbox->u.mqe.un.mq_create;
14931 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014932 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014933 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014934 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014935 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14936 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014937 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014938 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14939 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014940 break;
14941 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014942 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14943 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014944 break;
14945 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014946 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14947 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014948 break;
14949 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014950 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14951 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014952 break;
14953 }
14954 list_for_each_entry(dmabuf, &mq->page_list, list) {
14955 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014956 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014957 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014958 putPaddrHigh(dmabuf->phys);
14959 }
14960}
14961
14962/**
14963 * lpfc_mq_create - Create a mailbox Queue on the HBA
14964 * @phba: HBA structure that indicates port to create a queue on.
14965 * @mq: The queue structure to use to create the mailbox queue.
14966 * @cq: The completion queue to associate with this cq.
14967 * @subtype: The queue's subtype.
14968 *
14969 * This function creates a mailbox queue, as detailed in @mq, on a port,
14970 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14971 *
14972 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14973 * is used to get the entry count and entry size that are necessary to
14974 * determine the number of pages to allocate and use for this queue. This
14975 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14976 * mailbox queue. This function is asynchronous and will wait for the mailbox
14977 * command to finish before continuing.
14978 *
14979 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014980 * memory this function will return -ENOMEM. If the queue create mailbox command
14981 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014982 **/
14983int32_t
14984lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14985 struct lpfc_queue *cq, uint32_t subtype)
14986{
14987 struct lpfc_mbx_mq_create *mq_create;
14988 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14989 struct lpfc_dmabuf *dmabuf;
14990 LPFC_MBOXQ_t *mbox;
14991 int rc, length, status = 0;
14992 uint32_t shdr_status, shdr_add_status;
14993 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014994 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014995
James Smart2e90f4b2011-12-13 13:22:37 -050014996 /* sanity check on queue memory */
14997 if (!mq || !cq)
14998 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014999 if (!phba->sli4_hba.pc_sli4_params.supported)
15000 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040015001
15002 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15003 if (!mbox)
15004 return -ENOMEM;
15005 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15006 sizeof(struct lpfc_sli4_cfg_mhdr));
15007 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15008 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15009 length, LPFC_SLI4_MBX_EMBED);
15010
15011 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015012 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015013 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15014 &mq_create_ext->u.request, mq->page_count);
15015 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15016 &mq_create_ext->u.request, 1);
15017 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015018 &mq_create_ext->u.request, 1);
15019 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15020 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015021 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15022 &mq_create_ext->u.request, 1);
15023 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15024 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015025 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015026 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15027 phba->sli4_hba.pc_sli4_params.mqv);
15028 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15029 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15030 cq->queue_id);
15031 else
15032 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15033 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015034 switch (mq->entry_count) {
15035 default:
15036 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15037 "0362 Unsupported MQ count. (%d)\n",
15038 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015039 if (mq->entry_count < 16) {
15040 status = -EINVAL;
15041 goto out;
15042 }
James Smartb19a0612010-04-06 14:48:51 -040015043 /* otherwise default to smallest count (drop through) */
15044 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015045 bf_set(lpfc_mq_context_ring_size,
15046 &mq_create_ext->u.request.context,
15047 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015048 break;
15049 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015050 bf_set(lpfc_mq_context_ring_size,
15051 &mq_create_ext->u.request.context,
15052 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015053 break;
15054 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015055 bf_set(lpfc_mq_context_ring_size,
15056 &mq_create_ext->u.request.context,
15057 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015058 break;
15059 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015060 bf_set(lpfc_mq_context_ring_size,
15061 &mq_create_ext->u.request.context,
15062 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015063 break;
15064 }
15065 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015066 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015067 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15068 putPaddrLow(dmabuf->phys);
15069 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015070 putPaddrHigh(dmabuf->phys);
15071 }
15072 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015073 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15074 &mq_create_ext->u.response);
15075 if (rc != MBX_SUCCESS) {
15076 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15077 "2795 MQ_CREATE_EXT failed with "
15078 "status x%x. Failback to MQ_CREATE.\n",
15079 rc);
15080 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15081 mq_create = &mbox->u.mqe.un.mq_create;
15082 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15083 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15084 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15085 &mq_create->u.response);
15086 }
15087
James Smart04c68492009-05-22 14:52:52 -040015088 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015089 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15090 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15091 if (shdr_status || shdr_add_status || rc) {
15092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15093 "2502 MQ_CREATE mailbox failed with "
15094 "status x%x add_status x%x, mbx status x%x\n",
15095 shdr_status, shdr_add_status, rc);
15096 status = -ENXIO;
15097 goto out;
15098 }
James Smart04c68492009-05-22 14:52:52 -040015099 if (mq->queue_id == 0xFFFF) {
15100 status = -ENXIO;
15101 goto out;
15102 }
15103 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015104 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015105 mq->subtype = subtype;
15106 mq->host_index = 0;
15107 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070015108 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040015109
15110 /* link the mq onto the parent cq child list */
15111 list_add_tail(&mq->list, &cq->child_list);
15112out:
James Smart8fa38512009-07-19 10:01:03 -040015113 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015114 return status;
15115}
15116
15117/**
James Smart4f774512009-05-22 14:52:35 -040015118 * lpfc_wq_create - Create a Work Queue on the HBA
15119 * @phba: HBA structure that indicates port to create a queue on.
15120 * @wq: The queue structure to use to create the work queue.
15121 * @cq: The completion queue to bind this work queue to.
15122 * @subtype: The subtype of the work queue indicating its functionality.
15123 *
15124 * This function creates a work queue, as detailed in @wq, on a port, described
15125 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15126 *
15127 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15128 * is used to get the entry count and entry size that are necessary to
15129 * determine the number of pages to allocate and use for this queue. The @cq
15130 * is used to indicate which completion queue to bind this work queue to. This
15131 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15132 * work queue. This function is asynchronous and will wait for the mailbox
15133 * command to finish before continuing.
15134 *
15135 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015136 * memory this function will return -ENOMEM. If the queue create mailbox command
15137 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015138 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015139int
James Smart4f774512009-05-22 14:52:35 -040015140lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15141 struct lpfc_queue *cq, uint32_t subtype)
15142{
15143 struct lpfc_mbx_wq_create *wq_create;
15144 struct lpfc_dmabuf *dmabuf;
15145 LPFC_MBOXQ_t *mbox;
15146 int rc, length, status = 0;
15147 uint32_t shdr_status, shdr_add_status;
15148 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015149 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015150 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015151 void __iomem *bar_memmap_p;
15152 uint32_t db_offset;
15153 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015154 uint8_t dpp_barset;
15155 uint32_t dpp_offset;
15156 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015157 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015158
James Smart2e90f4b2011-12-13 13:22:37 -050015159 /* sanity check on queue memory */
15160 if (!wq || !cq)
15161 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015162 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015163 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015164
15165 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15166 if (!mbox)
15167 return -ENOMEM;
15168 length = (sizeof(struct lpfc_mbx_wq_create) -
15169 sizeof(struct lpfc_sli4_cfg_mhdr));
15170 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15171 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15172 length, LPFC_SLI4_MBX_EMBED);
15173 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015174 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015175 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15176 wq->page_count);
15177 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15178 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015179
15180 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015181 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15182 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015183
James Smartc176ffa2018-01-30 15:58:46 -080015184 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15185 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015186 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15187 else
15188 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15189
James Smart0c651872013-07-15 18:33:23 -040015190
James Smart1351e692018-02-22 08:18:43 -080015191 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15192 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15193 else
15194 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15195
15196 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015197 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015198 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15199 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015200 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15201 LPFC_Q_CREATE_VERSION_1);
15202
James Smart5a6f1332011-03-11 16:05:35 -050015203 switch (wq->entry_size) {
15204 default:
15205 case 64:
15206 bf_set(lpfc_mbx_wq_create_wqe_size,
15207 &wq_create->u.request_1,
15208 LPFC_WQ_WQE_SIZE_64);
15209 break;
15210 case 128:
15211 bf_set(lpfc_mbx_wq_create_wqe_size,
15212 &wq_create->u.request_1,
15213 LPFC_WQ_WQE_SIZE_128);
15214 break;
15215 }
James Smart1351e692018-02-22 08:18:43 -080015216 /* Request DPP by default */
15217 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015218 bf_set(lpfc_mbx_wq_create_page_size,
15219 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015220 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015221 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015222 break;
15223 default:
James Smart1351e692018-02-22 08:18:43 -080015224 page = wq_create->u.request.page;
15225 break;
James Smart5a6f1332011-03-11 16:05:35 -050015226 }
James Smart0c651872013-07-15 18:33:23 -040015227
James Smart4f774512009-05-22 14:52:35 -040015228 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015229 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015230 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15231 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015232 }
James Smart962bc512013-01-03 15:44:00 -050015233
15234 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15235 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15236
James Smart4f774512009-05-22 14:52:35 -040015237 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15238 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015239 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15240 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15241 if (shdr_status || shdr_add_status || rc) {
15242 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15243 "2503 WQ_CREATE mailbox failed with "
15244 "status x%x add_status x%x, mbx status x%x\n",
15245 shdr_status, shdr_add_status, rc);
15246 status = -ENXIO;
15247 goto out;
15248 }
James Smart1351e692018-02-22 08:18:43 -080015249
15250 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15251 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15252 &wq_create->u.response);
15253 else
15254 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15255 &wq_create->u.response_1);
15256
James Smart4f774512009-05-22 14:52:35 -040015257 if (wq->queue_id == 0xFFFF) {
15258 status = -ENXIO;
15259 goto out;
15260 }
James Smart1351e692018-02-22 08:18:43 -080015261
15262 wq->db_format = LPFC_DB_LIST_FORMAT;
15263 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15264 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15265 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15266 &wq_create->u.response);
15267 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15268 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15269 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15270 "3265 WQ[%d] doorbell format "
15271 "not supported: x%x\n",
15272 wq->queue_id, wq->db_format);
15273 status = -EINVAL;
15274 goto out;
15275 }
15276 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15277 &wq_create->u.response);
15278 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15279 pci_barset);
15280 if (!bar_memmap_p) {
15281 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15282 "3263 WQ[%d] failed to memmap "
15283 "pci barset:x%x\n",
15284 wq->queue_id, pci_barset);
15285 status = -ENOMEM;
15286 goto out;
15287 }
15288 db_offset = wq_create->u.response.doorbell_offset;
15289 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15290 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15292 "3252 WQ[%d] doorbell offset "
15293 "not supported: x%x\n",
15294 wq->queue_id, db_offset);
15295 status = -EINVAL;
15296 goto out;
15297 }
15298 wq->db_regaddr = bar_memmap_p + db_offset;
15299 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15300 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15301 "format:x%x\n", wq->queue_id,
15302 pci_barset, db_offset, wq->db_format);
15303 } else
15304 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015305 } else {
James Smart1351e692018-02-22 08:18:43 -080015306 /* Check if DPP was honored by the firmware */
15307 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15308 &wq_create->u.response_1);
15309 if (wq->dpp_enable) {
15310 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15311 &wq_create->u.response_1);
15312 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15313 pci_barset);
15314 if (!bar_memmap_p) {
15315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15316 "3267 WQ[%d] failed to memmap "
15317 "pci barset:x%x\n",
15318 wq->queue_id, pci_barset);
15319 status = -ENOMEM;
15320 goto out;
15321 }
15322 db_offset = wq_create->u.response_1.doorbell_offset;
15323 wq->db_regaddr = bar_memmap_p + db_offset;
15324 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15325 &wq_create->u.response_1);
15326 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15327 &wq_create->u.response_1);
15328 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15329 dpp_barset);
15330 if (!bar_memmap_p) {
15331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15332 "3268 WQ[%d] failed to memmap "
15333 "pci barset:x%x\n",
15334 wq->queue_id, dpp_barset);
15335 status = -ENOMEM;
15336 goto out;
15337 }
15338 dpp_offset = wq_create->u.response_1.dpp_offset;
15339 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15340 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15341 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15342 "dpp_id:x%x dpp_barset:x%x "
15343 "dpp_offset:x%x\n",
15344 wq->queue_id, pci_barset, db_offset,
15345 wq->dpp_id, dpp_barset, dpp_offset);
15346
15347 /* Enable combined writes for DPP aperture */
15348 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15349#ifdef CONFIG_X86
15350 rc = set_memory_wc(pg_addr, 1);
15351 if (rc) {
15352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15353 "3272 Cannot setup Combined "
15354 "Write on WQ[%d] - disable DPP\n",
15355 wq->queue_id);
15356 phba->cfg_enable_dpp = 0;
15357 }
15358#else
15359 phba->cfg_enable_dpp = 0;
15360#endif
15361 } else
15362 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015363 }
James Smart895427b2017-02-12 13:52:30 -080015364 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15365 if (wq->pring == NULL) {
15366 status = -ENOMEM;
15367 goto out;
15368 }
James Smart4f774512009-05-22 14:52:35 -040015369 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015370 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015371 wq->subtype = subtype;
15372 wq->host_index = 0;
15373 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050015374 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040015375
15376 /* link the wq onto the parent cq child list */
15377 list_add_tail(&wq->list, &cq->child_list);
15378out:
James Smart8fa38512009-07-19 10:01:03 -040015379 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015380 return status;
15381}
15382
15383/**
15384 * lpfc_rq_create - Create a Receive Queue on the HBA
15385 * @phba: HBA structure that indicates port to create a queue on.
15386 * @hrq: The queue structure to use to create the header receive queue.
15387 * @drq: The queue structure to use to create the data receive queue.
15388 * @cq: The completion queue to bind this work queue to.
15389 *
15390 * This function creates a receive buffer queue pair , as detailed in @hrq and
15391 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15392 * to the HBA.
15393 *
15394 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15395 * struct is used to get the entry count that is necessary to determine the
15396 * number of pages to use for this queue. The @cq is used to indicate which
15397 * completion queue to bind received buffers that are posted to these queues to.
15398 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15399 * receive queue pair. This function is asynchronous and will wait for the
15400 * mailbox command to finish before continuing.
15401 *
15402 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015403 * memory this function will return -ENOMEM. If the queue create mailbox command
15404 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015405 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015406int
James Smart4f774512009-05-22 14:52:35 -040015407lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15408 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15409{
15410 struct lpfc_mbx_rq_create *rq_create;
15411 struct lpfc_dmabuf *dmabuf;
15412 LPFC_MBOXQ_t *mbox;
15413 int rc, length, status = 0;
15414 uint32_t shdr_status, shdr_add_status;
15415 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015416 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015417 void __iomem *bar_memmap_p;
15418 uint32_t db_offset;
15419 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015420
James Smart2e90f4b2011-12-13 13:22:37 -050015421 /* sanity check on queue memory */
15422 if (!hrq || !drq || !cq)
15423 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015424 if (!phba->sli4_hba.pc_sli4_params.supported)
15425 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015426
15427 if (hrq->entry_count != drq->entry_count)
15428 return -EINVAL;
15429 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15430 if (!mbox)
15431 return -ENOMEM;
15432 length = (sizeof(struct lpfc_mbx_rq_create) -
15433 sizeof(struct lpfc_sli4_cfg_mhdr));
15434 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15435 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15436 length, LPFC_SLI4_MBX_EMBED);
15437 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015438 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15439 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15440 phba->sli4_hba.pc_sli4_params.rqv);
15441 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15442 bf_set(lpfc_rq_context_rqe_count_1,
15443 &rq_create->u.request.context,
15444 hrq->entry_count);
15445 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015446 bf_set(lpfc_rq_context_rqe_size,
15447 &rq_create->u.request.context,
15448 LPFC_RQE_SIZE_8);
15449 bf_set(lpfc_rq_context_page_size,
15450 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015451 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015452 } else {
15453 switch (hrq->entry_count) {
15454 default:
15455 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15456 "2535 Unsupported RQ count. (%d)\n",
15457 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015458 if (hrq->entry_count < 512) {
15459 status = -EINVAL;
15460 goto out;
15461 }
James Smart5a6f1332011-03-11 16:05:35 -050015462 /* otherwise default to smallest count (drop through) */
15463 case 512:
15464 bf_set(lpfc_rq_context_rqe_count,
15465 &rq_create->u.request.context,
15466 LPFC_RQ_RING_SIZE_512);
15467 break;
15468 case 1024:
15469 bf_set(lpfc_rq_context_rqe_count,
15470 &rq_create->u.request.context,
15471 LPFC_RQ_RING_SIZE_1024);
15472 break;
15473 case 2048:
15474 bf_set(lpfc_rq_context_rqe_count,
15475 &rq_create->u.request.context,
15476 LPFC_RQ_RING_SIZE_2048);
15477 break;
15478 case 4096:
15479 bf_set(lpfc_rq_context_rqe_count,
15480 &rq_create->u.request.context,
15481 LPFC_RQ_RING_SIZE_4096);
15482 break;
15483 }
15484 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15485 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015486 }
15487 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15488 cq->queue_id);
15489 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15490 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015491 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015492 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015493 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15494 putPaddrLow(dmabuf->phys);
15495 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15496 putPaddrHigh(dmabuf->phys);
15497 }
James Smart962bc512013-01-03 15:44:00 -050015498 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15499 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15500
James Smart4f774512009-05-22 14:52:35 -040015501 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15502 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015503 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15504 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15505 if (shdr_status || shdr_add_status || rc) {
15506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15507 "2504 RQ_CREATE mailbox failed with "
15508 "status x%x add_status x%x, mbx status x%x\n",
15509 shdr_status, shdr_add_status, rc);
15510 status = -ENXIO;
15511 goto out;
15512 }
15513 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15514 if (hrq->queue_id == 0xFFFF) {
15515 status = -ENXIO;
15516 goto out;
15517 }
James Smart962bc512013-01-03 15:44:00 -050015518
15519 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15520 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15521 &rq_create->u.response);
15522 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15523 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15524 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15525 "3262 RQ [%d] doorbell format not "
15526 "supported: x%x\n", hrq->queue_id,
15527 hrq->db_format);
15528 status = -EINVAL;
15529 goto out;
15530 }
15531
15532 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15533 &rq_create->u.response);
15534 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15535 if (!bar_memmap_p) {
15536 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15537 "3269 RQ[%d] failed to memmap pci "
15538 "barset:x%x\n", hrq->queue_id,
15539 pci_barset);
15540 status = -ENOMEM;
15541 goto out;
15542 }
15543
15544 db_offset = rq_create->u.response.doorbell_offset;
15545 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15546 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15548 "3270 RQ[%d] doorbell offset not "
15549 "supported: x%x\n", hrq->queue_id,
15550 db_offset);
15551 status = -EINVAL;
15552 goto out;
15553 }
15554 hrq->db_regaddr = bar_memmap_p + db_offset;
15555 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015556 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15557 "format:x%x\n", hrq->queue_id, pci_barset,
15558 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015559 } else {
15560 hrq->db_format = LPFC_DB_RING_FORMAT;
15561 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15562 }
James Smart4f774512009-05-22 14:52:35 -040015563 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015564 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015565 hrq->subtype = subtype;
15566 hrq->host_index = 0;
15567 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015568 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015569
15570 /* now create the data queue */
15571 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15572 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15573 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015574 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15575 phba->sli4_hba.pc_sli4_params.rqv);
15576 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15577 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015578 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015579 if (subtype == LPFC_NVMET)
15580 rq_create->u.request.context.buffer_size =
15581 LPFC_NVMET_DATA_BUF_SIZE;
15582 else
15583 rq_create->u.request.context.buffer_size =
15584 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015585 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15586 LPFC_RQE_SIZE_8);
15587 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15588 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015589 } else {
15590 switch (drq->entry_count) {
15591 default:
15592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15593 "2536 Unsupported RQ count. (%d)\n",
15594 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015595 if (drq->entry_count < 512) {
15596 status = -EINVAL;
15597 goto out;
15598 }
James Smart5a6f1332011-03-11 16:05:35 -050015599 /* otherwise default to smallest count (drop through) */
15600 case 512:
15601 bf_set(lpfc_rq_context_rqe_count,
15602 &rq_create->u.request.context,
15603 LPFC_RQ_RING_SIZE_512);
15604 break;
15605 case 1024:
15606 bf_set(lpfc_rq_context_rqe_count,
15607 &rq_create->u.request.context,
15608 LPFC_RQ_RING_SIZE_1024);
15609 break;
15610 case 2048:
15611 bf_set(lpfc_rq_context_rqe_count,
15612 &rq_create->u.request.context,
15613 LPFC_RQ_RING_SIZE_2048);
15614 break;
15615 case 4096:
15616 bf_set(lpfc_rq_context_rqe_count,
15617 &rq_create->u.request.context,
15618 LPFC_RQ_RING_SIZE_4096);
15619 break;
15620 }
James Smart3c603be2017-05-15 15:20:44 -070015621 if (subtype == LPFC_NVMET)
15622 bf_set(lpfc_rq_context_buf_size,
15623 &rq_create->u.request.context,
15624 LPFC_NVMET_DATA_BUF_SIZE);
15625 else
15626 bf_set(lpfc_rq_context_buf_size,
15627 &rq_create->u.request.context,
15628 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015629 }
15630 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15631 cq->queue_id);
15632 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15633 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015634 list_for_each_entry(dmabuf, &drq->page_list, list) {
15635 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15636 putPaddrLow(dmabuf->phys);
15637 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15638 putPaddrHigh(dmabuf->phys);
15639 }
James Smart962bc512013-01-03 15:44:00 -050015640 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15641 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015642 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15643 /* The IOCTL status is embedded in the mailbox subheader. */
15644 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15645 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15646 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15647 if (shdr_status || shdr_add_status || rc) {
15648 status = -ENXIO;
15649 goto out;
15650 }
15651 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15652 if (drq->queue_id == 0xFFFF) {
15653 status = -ENXIO;
15654 goto out;
15655 }
15656 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015657 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015658 drq->subtype = subtype;
15659 drq->host_index = 0;
15660 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015661 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015662
15663 /* link the header and data RQs onto the parent cq child list */
15664 list_add_tail(&hrq->list, &cq->child_list);
15665 list_add_tail(&drq->list, &cq->child_list);
15666
15667out:
James Smart8fa38512009-07-19 10:01:03 -040015668 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015669 return status;
15670}
15671
15672/**
James Smart2d7dbc42017-02-12 13:52:35 -080015673 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15674 * @phba: HBA structure that indicates port to create a queue on.
15675 * @hrqp: The queue structure array to use to create the header receive queues.
15676 * @drqp: The queue structure array to use to create the data receive queues.
15677 * @cqp: The completion queue array to bind these receive queues to.
15678 *
15679 * This function creates a receive buffer queue pair , as detailed in @hrq and
15680 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15681 * to the HBA.
15682 *
15683 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15684 * struct is used to get the entry count that is necessary to determine the
15685 * number of pages to use for this queue. The @cq is used to indicate which
15686 * completion queue to bind received buffers that are posted to these queues to.
15687 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15688 * receive queue pair. This function is asynchronous and will wait for the
15689 * mailbox command to finish before continuing.
15690 *
15691 * On success this function will return a zero. If unable to allocate enough
15692 * memory this function will return -ENOMEM. If the queue create mailbox command
15693 * fails this function will return -ENXIO.
15694 **/
15695int
15696lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15697 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15698 uint32_t subtype)
15699{
15700 struct lpfc_queue *hrq, *drq, *cq;
15701 struct lpfc_mbx_rq_create_v2 *rq_create;
15702 struct lpfc_dmabuf *dmabuf;
15703 LPFC_MBOXQ_t *mbox;
15704 int rc, length, alloclen, status = 0;
15705 int cnt, idx, numrq, page_idx = 0;
15706 uint32_t shdr_status, shdr_add_status;
15707 union lpfc_sli4_cfg_shdr *shdr;
15708 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15709
15710 numrq = phba->cfg_nvmet_mrq;
15711 /* sanity check on array memory */
15712 if (!hrqp || !drqp || !cqp || !numrq)
15713 return -ENODEV;
15714 if (!phba->sli4_hba.pc_sli4_params.supported)
15715 hw_page_size = SLI4_PAGE_SIZE;
15716
15717 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15718 if (!mbox)
15719 return -ENOMEM;
15720
15721 length = sizeof(struct lpfc_mbx_rq_create_v2);
15722 length += ((2 * numrq * hrqp[0]->page_count) *
15723 sizeof(struct dma_address));
15724
15725 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15726 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15727 LPFC_SLI4_MBX_NEMBED);
15728 if (alloclen < length) {
15729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15730 "3099 Allocated DMA memory size (%d) is "
15731 "less than the requested DMA memory size "
15732 "(%d)\n", alloclen, length);
15733 status = -ENOMEM;
15734 goto out;
15735 }
15736
15737
15738
15739 rq_create = mbox->sge_array->addr[0];
15740 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15741
15742 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15743 cnt = 0;
15744
15745 for (idx = 0; idx < numrq; idx++) {
15746 hrq = hrqp[idx];
15747 drq = drqp[idx];
15748 cq = cqp[idx];
15749
James Smart2d7dbc42017-02-12 13:52:35 -080015750 /* sanity check on queue memory */
15751 if (!hrq || !drq || !cq) {
15752 status = -ENODEV;
15753 goto out;
15754 }
15755
James Smart7aabe842017-03-04 09:30:22 -080015756 if (hrq->entry_count != drq->entry_count) {
15757 status = -EINVAL;
15758 goto out;
15759 }
15760
James Smart2d7dbc42017-02-12 13:52:35 -080015761 if (idx == 0) {
15762 bf_set(lpfc_mbx_rq_create_num_pages,
15763 &rq_create->u.request,
15764 hrq->page_count);
15765 bf_set(lpfc_mbx_rq_create_rq_cnt,
15766 &rq_create->u.request, (numrq * 2));
15767 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15768 1);
15769 bf_set(lpfc_rq_context_base_cq,
15770 &rq_create->u.request.context,
15771 cq->queue_id);
15772 bf_set(lpfc_rq_context_data_size,
15773 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015774 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015775 bf_set(lpfc_rq_context_hdr_size,
15776 &rq_create->u.request.context,
15777 LPFC_HDR_BUF_SIZE);
15778 bf_set(lpfc_rq_context_rqe_count_1,
15779 &rq_create->u.request.context,
15780 hrq->entry_count);
15781 bf_set(lpfc_rq_context_rqe_size,
15782 &rq_create->u.request.context,
15783 LPFC_RQE_SIZE_8);
15784 bf_set(lpfc_rq_context_page_size,
15785 &rq_create->u.request.context,
15786 (PAGE_SIZE/SLI4_PAGE_SIZE));
15787 }
15788 rc = 0;
15789 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15790 memset(dmabuf->virt, 0, hw_page_size);
15791 cnt = page_idx + dmabuf->buffer_tag;
15792 rq_create->u.request.page[cnt].addr_lo =
15793 putPaddrLow(dmabuf->phys);
15794 rq_create->u.request.page[cnt].addr_hi =
15795 putPaddrHigh(dmabuf->phys);
15796 rc++;
15797 }
15798 page_idx += rc;
15799
15800 rc = 0;
15801 list_for_each_entry(dmabuf, &drq->page_list, list) {
15802 memset(dmabuf->virt, 0, hw_page_size);
15803 cnt = page_idx + dmabuf->buffer_tag;
15804 rq_create->u.request.page[cnt].addr_lo =
15805 putPaddrLow(dmabuf->phys);
15806 rq_create->u.request.page[cnt].addr_hi =
15807 putPaddrHigh(dmabuf->phys);
15808 rc++;
15809 }
15810 page_idx += rc;
15811
15812 hrq->db_format = LPFC_DB_RING_FORMAT;
15813 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15814 hrq->type = LPFC_HRQ;
15815 hrq->assoc_qid = cq->queue_id;
15816 hrq->subtype = subtype;
15817 hrq->host_index = 0;
15818 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015819 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015820
15821 drq->db_format = LPFC_DB_RING_FORMAT;
15822 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15823 drq->type = LPFC_DRQ;
15824 drq->assoc_qid = cq->queue_id;
15825 drq->subtype = subtype;
15826 drq->host_index = 0;
15827 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015828 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015829
15830 list_add_tail(&hrq->list, &cq->child_list);
15831 list_add_tail(&drq->list, &cq->child_list);
15832 }
15833
15834 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15835 /* The IOCTL status is embedded in the mailbox subheader. */
15836 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15837 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15838 if (shdr_status || shdr_add_status || rc) {
15839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15840 "3120 RQ_CREATE mailbox failed with "
15841 "status x%x add_status x%x, mbx status x%x\n",
15842 shdr_status, shdr_add_status, rc);
15843 status = -ENXIO;
15844 goto out;
15845 }
15846 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15847 if (rc == 0xFFFF) {
15848 status = -ENXIO;
15849 goto out;
15850 }
15851
15852 /* Initialize all RQs with associated queue id */
15853 for (idx = 0; idx < numrq; idx++) {
15854 hrq = hrqp[idx];
15855 hrq->queue_id = rc + (2 * idx);
15856 drq = drqp[idx];
15857 drq->queue_id = rc + (2 * idx) + 1;
15858 }
15859
15860out:
15861 lpfc_sli4_mbox_cmd_free(phba, mbox);
15862 return status;
15863}
15864
15865/**
James Smart4f774512009-05-22 14:52:35 -040015866 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15867 * @eq: The queue structure associated with the queue to destroy.
15868 *
15869 * This function destroys a queue, as detailed in @eq by sending an mailbox
15870 * command, specific to the type of queue, to the HBA.
15871 *
15872 * The @eq struct is used to get the queue ID of the queue to destroy.
15873 *
15874 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015875 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015876 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015877int
James Smart4f774512009-05-22 14:52:35 -040015878lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15879{
15880 LPFC_MBOXQ_t *mbox;
15881 int rc, length, status = 0;
15882 uint32_t shdr_status, shdr_add_status;
15883 union lpfc_sli4_cfg_shdr *shdr;
15884
James Smart2e90f4b2011-12-13 13:22:37 -050015885 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015886 if (!eq)
15887 return -ENODEV;
15888 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15889 if (!mbox)
15890 return -ENOMEM;
15891 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15892 sizeof(struct lpfc_sli4_cfg_mhdr));
15893 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15894 LPFC_MBOX_OPCODE_EQ_DESTROY,
15895 length, LPFC_SLI4_MBX_EMBED);
15896 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15897 eq->queue_id);
15898 mbox->vport = eq->phba->pport;
15899 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15900
15901 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15902 /* The IOCTL status is embedded in the mailbox subheader. */
15903 shdr = (union lpfc_sli4_cfg_shdr *)
15904 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15905 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15906 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15907 if (shdr_status || shdr_add_status || rc) {
15908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15909 "2505 EQ_DESTROY mailbox failed with "
15910 "status x%x add_status x%x, mbx status x%x\n",
15911 shdr_status, shdr_add_status, rc);
15912 status = -ENXIO;
15913 }
15914
15915 /* Remove eq from any list */
15916 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015917 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015918 return status;
15919}
15920
15921/**
15922 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15923 * @cq: The queue structure associated with the queue to destroy.
15924 *
15925 * This function destroys a queue, as detailed in @cq by sending an mailbox
15926 * command, specific to the type of queue, to the HBA.
15927 *
15928 * The @cq struct is used to get the queue ID of the queue to destroy.
15929 *
15930 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015931 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015932 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015933int
James Smart4f774512009-05-22 14:52:35 -040015934lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15935{
15936 LPFC_MBOXQ_t *mbox;
15937 int rc, length, status = 0;
15938 uint32_t shdr_status, shdr_add_status;
15939 union lpfc_sli4_cfg_shdr *shdr;
15940
James Smart2e90f4b2011-12-13 13:22:37 -050015941 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015942 if (!cq)
15943 return -ENODEV;
15944 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15945 if (!mbox)
15946 return -ENOMEM;
15947 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15948 sizeof(struct lpfc_sli4_cfg_mhdr));
15949 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15950 LPFC_MBOX_OPCODE_CQ_DESTROY,
15951 length, LPFC_SLI4_MBX_EMBED);
15952 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15953 cq->queue_id);
15954 mbox->vport = cq->phba->pport;
15955 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15956 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15957 /* The IOCTL status is embedded in the mailbox subheader. */
15958 shdr = (union lpfc_sli4_cfg_shdr *)
15959 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15960 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15961 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15962 if (shdr_status || shdr_add_status || rc) {
15963 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15964 "2506 CQ_DESTROY mailbox failed with "
15965 "status x%x add_status x%x, mbx status x%x\n",
15966 shdr_status, shdr_add_status, rc);
15967 status = -ENXIO;
15968 }
15969 /* Remove cq from any list */
15970 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015971 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015972 return status;
15973}
15974
15975/**
James Smart04c68492009-05-22 14:52:52 -040015976 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15977 * @qm: The queue structure associated with the queue to destroy.
15978 *
15979 * This function destroys a queue, as detailed in @mq by sending an mailbox
15980 * command, specific to the type of queue, to the HBA.
15981 *
15982 * The @mq struct is used to get the queue ID of the queue to destroy.
15983 *
15984 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015985 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015986 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015987int
James Smart04c68492009-05-22 14:52:52 -040015988lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15989{
15990 LPFC_MBOXQ_t *mbox;
15991 int rc, length, status = 0;
15992 uint32_t shdr_status, shdr_add_status;
15993 union lpfc_sli4_cfg_shdr *shdr;
15994
James Smart2e90f4b2011-12-13 13:22:37 -050015995 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015996 if (!mq)
15997 return -ENODEV;
15998 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15999 if (!mbox)
16000 return -ENOMEM;
16001 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16002 sizeof(struct lpfc_sli4_cfg_mhdr));
16003 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16004 LPFC_MBOX_OPCODE_MQ_DESTROY,
16005 length, LPFC_SLI4_MBX_EMBED);
16006 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16007 mq->queue_id);
16008 mbox->vport = mq->phba->pport;
16009 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16010 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16011 /* The IOCTL status is embedded in the mailbox subheader. */
16012 shdr = (union lpfc_sli4_cfg_shdr *)
16013 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16014 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16015 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16016 if (shdr_status || shdr_add_status || rc) {
16017 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16018 "2507 MQ_DESTROY mailbox failed with "
16019 "status x%x add_status x%x, mbx status x%x\n",
16020 shdr_status, shdr_add_status, rc);
16021 status = -ENXIO;
16022 }
16023 /* Remove mq from any list */
16024 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016025 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016026 return status;
16027}
16028
16029/**
James Smart4f774512009-05-22 14:52:35 -040016030 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16031 * @wq: The queue structure associated with the queue to destroy.
16032 *
16033 * This function destroys a queue, as detailed in @wq by sending an mailbox
16034 * command, specific to the type of queue, to the HBA.
16035 *
16036 * The @wq struct is used to get the queue ID of the queue to destroy.
16037 *
16038 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016039 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016040 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016041int
James Smart4f774512009-05-22 14:52:35 -040016042lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16043{
16044 LPFC_MBOXQ_t *mbox;
16045 int rc, length, status = 0;
16046 uint32_t shdr_status, shdr_add_status;
16047 union lpfc_sli4_cfg_shdr *shdr;
16048
James Smart2e90f4b2011-12-13 13:22:37 -050016049 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016050 if (!wq)
16051 return -ENODEV;
16052 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16053 if (!mbox)
16054 return -ENOMEM;
16055 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16056 sizeof(struct lpfc_sli4_cfg_mhdr));
16057 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16058 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16059 length, LPFC_SLI4_MBX_EMBED);
16060 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16061 wq->queue_id);
16062 mbox->vport = wq->phba->pport;
16063 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16064 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16065 shdr = (union lpfc_sli4_cfg_shdr *)
16066 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16067 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16068 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16069 if (shdr_status || shdr_add_status || rc) {
16070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16071 "2508 WQ_DESTROY mailbox failed with "
16072 "status x%x add_status x%x, mbx status x%x\n",
16073 shdr_status, shdr_add_status, rc);
16074 status = -ENXIO;
16075 }
16076 /* Remove wq from any list */
16077 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016078 kfree(wq->pring);
16079 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016080 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016081 return status;
16082}
16083
16084/**
16085 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16086 * @rq: The queue structure associated with the queue to destroy.
16087 *
16088 * This function destroys a queue, as detailed in @rq by sending an mailbox
16089 * command, specific to the type of queue, to the HBA.
16090 *
16091 * The @rq struct is used to get the queue ID of the queue to destroy.
16092 *
16093 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016094 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016095 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016096int
James Smart4f774512009-05-22 14:52:35 -040016097lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16098 struct lpfc_queue *drq)
16099{
16100 LPFC_MBOXQ_t *mbox;
16101 int rc, length, status = 0;
16102 uint32_t shdr_status, shdr_add_status;
16103 union lpfc_sli4_cfg_shdr *shdr;
16104
James Smart2e90f4b2011-12-13 13:22:37 -050016105 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016106 if (!hrq || !drq)
16107 return -ENODEV;
16108 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16109 if (!mbox)
16110 return -ENOMEM;
16111 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016112 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016113 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16114 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16115 length, LPFC_SLI4_MBX_EMBED);
16116 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16117 hrq->queue_id);
16118 mbox->vport = hrq->phba->pport;
16119 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16120 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16121 /* The IOCTL status is embedded in the mailbox subheader. */
16122 shdr = (union lpfc_sli4_cfg_shdr *)
16123 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16124 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16125 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16126 if (shdr_status || shdr_add_status || rc) {
16127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16128 "2509 RQ_DESTROY mailbox failed with "
16129 "status x%x add_status x%x, mbx status x%x\n",
16130 shdr_status, shdr_add_status, rc);
16131 if (rc != MBX_TIMEOUT)
16132 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16133 return -ENXIO;
16134 }
16135 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16136 drq->queue_id);
16137 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16138 shdr = (union lpfc_sli4_cfg_shdr *)
16139 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16140 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16141 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16142 if (shdr_status || shdr_add_status || rc) {
16143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16144 "2510 RQ_DESTROY mailbox failed with "
16145 "status x%x add_status x%x, mbx status x%x\n",
16146 shdr_status, shdr_add_status, rc);
16147 status = -ENXIO;
16148 }
16149 list_del_init(&hrq->list);
16150 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016151 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016152 return status;
16153}
16154
16155/**
16156 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16157 * @phba: The virtual port for which this call being executed.
16158 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16159 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16160 * @xritag: the xritag that ties this io to the SGL pages.
16161 *
16162 * This routine will post the sgl pages for the IO that has the xritag
16163 * that is in the iocbq structure. The xritag is assigned during iocbq
16164 * creation and persists for as long as the driver is loaded.
16165 * if the caller has fewer than 256 scatter gather segments to map then
16166 * pdma_phys_addr1 should be 0.
16167 * If the caller needs to map more than 256 scatter gather segment then
16168 * pdma_phys_addr1 should be a valid physical address.
16169 * physical address for SGLs must be 64 byte aligned.
16170 * If you are going to map 2 SGL's then the first one must have 256 entries
16171 * the second sgl can have between 1 and 256 entries.
16172 *
16173 * Return codes:
16174 * 0 - Success
16175 * -ENXIO, -ENOMEM - Failure
16176 **/
16177int
16178lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16179 dma_addr_t pdma_phys_addr0,
16180 dma_addr_t pdma_phys_addr1,
16181 uint16_t xritag)
16182{
16183 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16184 LPFC_MBOXQ_t *mbox;
16185 int rc;
16186 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016187 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016188 union lpfc_sli4_cfg_shdr *shdr;
16189
16190 if (xritag == NO_XRI) {
16191 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16192 "0364 Invalid param:\n");
16193 return -EINVAL;
16194 }
16195
16196 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16197 if (!mbox)
16198 return -ENOMEM;
16199
16200 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16201 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16202 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016203 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016204
16205 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16206 &mbox->u.mqe.un.post_sgl_pages;
16207 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16208 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16209
16210 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16211 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16212 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16213 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16214
16215 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16216 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16217 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16218 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16219 if (!phba->sli4_hba.intr_enable)
16220 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016221 else {
James Smarta183a152011-10-10 21:32:43 -040016222 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016223 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16224 }
James Smart4f774512009-05-22 14:52:35 -040016225 /* The IOCTL status is embedded in the mailbox subheader. */
16226 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16227 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16228 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16229 if (rc != MBX_TIMEOUT)
16230 mempool_free(mbox, phba->mbox_mem_pool);
16231 if (shdr_status || shdr_add_status || rc) {
16232 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16233 "2511 POST_SGL mailbox failed with "
16234 "status x%x add_status x%x, mbx status x%x\n",
16235 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016236 }
16237 return 0;
16238}
James Smart4f774512009-05-22 14:52:35 -040016239
16240/**
James Smart88a2cfb2011-07-22 18:36:33 -040016241 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016242 * @phba: pointer to lpfc hba data structure.
16243 *
16244 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016245 * HBA consistent with the SLI-4 interface spec. This routine
16246 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16247 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016248 *
James Smart88a2cfb2011-07-22 18:36:33 -040016249 * Returns
16250 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16251 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16252 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016253static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016254lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16255{
16256 unsigned long xri;
16257
16258 /*
16259 * Fetch the next logical xri. Because this index is logical,
16260 * the driver starts at 0 each time.
16261 */
16262 spin_lock_irq(&phba->hbalock);
16263 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16264 phba->sli4_hba.max_cfg_param.max_xri, 0);
16265 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16266 spin_unlock_irq(&phba->hbalock);
16267 return NO_XRI;
16268 } else {
16269 set_bit(xri, phba->sli4_hba.xri_bmask);
16270 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016271 }
James Smart6d368e52011-05-24 11:44:12 -040016272 spin_unlock_irq(&phba->hbalock);
16273 return xri;
16274}
16275
16276/**
16277 * lpfc_sli4_free_xri - Release an xri for reuse.
16278 * @phba: pointer to lpfc hba data structure.
16279 *
16280 * This routine is invoked to release an xri to the pool of
16281 * available rpis maintained by the driver.
16282 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016283static void
James Smart6d368e52011-05-24 11:44:12 -040016284__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16285{
16286 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016287 phba->sli4_hba.max_cfg_param.xri_used--;
16288 }
16289}
16290
16291/**
16292 * lpfc_sli4_free_xri - Release an xri for reuse.
16293 * @phba: pointer to lpfc hba data structure.
16294 *
16295 * This routine is invoked to release an xri to the pool of
16296 * available rpis maintained by the driver.
16297 **/
16298void
16299lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16300{
16301 spin_lock_irq(&phba->hbalock);
16302 __lpfc_sli4_free_xri(phba, xri);
16303 spin_unlock_irq(&phba->hbalock);
16304}
16305
16306/**
James Smart4f774512009-05-22 14:52:35 -040016307 * lpfc_sli4_next_xritag - Get an xritag for the io
16308 * @phba: Pointer to HBA context object.
16309 *
16310 * This function gets an xritag for the iocb. If there is no unused xritag
16311 * it will return 0xffff.
16312 * The function returns the allocated xritag if successful, else returns zero.
16313 * Zero is not a valid xritag.
16314 * The caller is not required to hold any lock.
16315 **/
16316uint16_t
16317lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16318{
James Smart6d368e52011-05-24 11:44:12 -040016319 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016320
James Smart6d368e52011-05-24 11:44:12 -040016321 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016322 if (xri_index == NO_XRI)
16323 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16324 "2004 Failed to allocate XRI.last XRITAG is %d"
16325 " Max XRI is %d, Used XRI is %d\n",
16326 xri_index,
16327 phba->sli4_hba.max_cfg_param.max_xri,
16328 phba->sli4_hba.max_cfg_param.xri_used);
16329 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016330}
16331
16332/**
James Smart895427b2017-02-12 13:52:30 -080016333 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016334 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016335 * @post_sgl_list: pointer to els sgl entry list.
16336 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016337 *
16338 * This routine is invoked to post a block of driver's sgl pages to the
16339 * HBA using non-embedded mailbox command. No Lock is held. This routine
16340 * is only called when the driver is loading and after all IO has been
16341 * stopped.
16342 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016343static int
James Smart895427b2017-02-12 13:52:30 -080016344lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016345 struct list_head *post_sgl_list,
16346 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016347{
James Smart8a9d2e82012-05-09 21:16:12 -040016348 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016349 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16350 struct sgl_page_pairs *sgl_pg_pairs;
16351 void *viraddr;
16352 LPFC_MBOXQ_t *mbox;
16353 uint32_t reqlen, alloclen, pg_pairs;
16354 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016355 uint16_t xritag_start = 0;
16356 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016357 uint32_t shdr_status, shdr_add_status;
16358 union lpfc_sli4_cfg_shdr *shdr;
16359
James Smart895427b2017-02-12 13:52:30 -080016360 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016361 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016362 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016364 "2559 Block sgl registration required DMA "
16365 "size (%d) great than a page\n", reqlen);
16366 return -ENOMEM;
16367 }
James Smart895427b2017-02-12 13:52:30 -080016368
James Smart4f774512009-05-22 14:52:35 -040016369 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016370 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016371 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016372
16373 /* Allocate DMA memory and set up the non-embedded mailbox command */
16374 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16375 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16376 LPFC_SLI4_MBX_NEMBED);
16377
16378 if (alloclen < reqlen) {
16379 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16380 "0285 Allocated DMA memory size (%d) is "
16381 "less than the requested DMA memory "
16382 "size (%d)\n", alloclen, reqlen);
16383 lpfc_sli4_mbox_cmd_free(phba, mbox);
16384 return -ENOMEM;
16385 }
James Smart4f774512009-05-22 14:52:35 -040016386 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016387 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016388 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16389 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16390
James Smart8a9d2e82012-05-09 21:16:12 -040016391 pg_pairs = 0;
16392 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016393 /* Set up the sge entry */
16394 sgl_pg_pairs->sgl_pg0_addr_lo =
16395 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16396 sgl_pg_pairs->sgl_pg0_addr_hi =
16397 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16398 sgl_pg_pairs->sgl_pg1_addr_lo =
16399 cpu_to_le32(putPaddrLow(0));
16400 sgl_pg_pairs->sgl_pg1_addr_hi =
16401 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016402
James Smart4f774512009-05-22 14:52:35 -040016403 /* Keep the first xritag on the list */
16404 if (pg_pairs == 0)
16405 xritag_start = sglq_entry->sli4_xritag;
16406 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016407 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016408 }
James Smart6d368e52011-05-24 11:44:12 -040016409
16410 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016411 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016412 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016413 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016414
James Smart4f774512009-05-22 14:52:35 -040016415 if (!phba->sli4_hba.intr_enable)
16416 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16417 else {
James Smarta183a152011-10-10 21:32:43 -040016418 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016419 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16420 }
16421 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16422 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16423 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16424 if (rc != MBX_TIMEOUT)
16425 lpfc_sli4_mbox_cmd_free(phba, mbox);
16426 if (shdr_status || shdr_add_status || rc) {
16427 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16428 "2513 POST_SGL_BLOCK mailbox command failed "
16429 "status x%x add_status x%x mbx status x%x\n",
16430 shdr_status, shdr_add_status, rc);
16431 rc = -ENXIO;
16432 }
16433 return rc;
16434}
16435
16436/**
16437 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16438 * @phba: pointer to lpfc hba data structure.
16439 * @sblist: pointer to scsi buffer list.
16440 * @count: number of scsi buffers on the list.
16441 *
16442 * This routine is invoked to post a block of @count scsi sgl pages from a
16443 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16444 * No Lock is held.
16445 *
16446 **/
16447int
James Smart8a9d2e82012-05-09 21:16:12 -040016448lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16449 struct list_head *sblist,
16450 int count)
James Smart4f774512009-05-22 14:52:35 -040016451{
16452 struct lpfc_scsi_buf *psb;
16453 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16454 struct sgl_page_pairs *sgl_pg_pairs;
16455 void *viraddr;
16456 LPFC_MBOXQ_t *mbox;
16457 uint32_t reqlen, alloclen, pg_pairs;
16458 uint32_t mbox_tmo;
16459 uint16_t xritag_start = 0;
16460 int rc = 0;
16461 uint32_t shdr_status, shdr_add_status;
16462 dma_addr_t pdma_phys_bpl1;
16463 union lpfc_sli4_cfg_shdr *shdr;
16464
16465 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016466 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016467 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016468 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016469 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16470 "0217 Block sgl registration required DMA "
16471 "size (%d) great than a page\n", reqlen);
16472 return -ENOMEM;
16473 }
16474 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16475 if (!mbox) {
16476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16477 "0283 Failed to allocate mbox cmd memory\n");
16478 return -ENOMEM;
16479 }
16480
16481 /* Allocate DMA memory and set up the non-embedded mailbox command */
16482 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16483 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16484 LPFC_SLI4_MBX_NEMBED);
16485
16486 if (alloclen < reqlen) {
16487 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16488 "2561 Allocated DMA memory size (%d) is "
16489 "less than the requested DMA memory "
16490 "size (%d)\n", alloclen, reqlen);
16491 lpfc_sli4_mbox_cmd_free(phba, mbox);
16492 return -ENOMEM;
16493 }
James Smart6d368e52011-05-24 11:44:12 -040016494
James Smart4f774512009-05-22 14:52:35 -040016495 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016496 viraddr = mbox->sge_array->addr[0];
16497
16498 /* Set up the SGL pages in the non-embedded DMA pages */
16499 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16500 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16501
16502 pg_pairs = 0;
16503 list_for_each_entry(psb, sblist, list) {
16504 /* Set up the sge entry */
16505 sgl_pg_pairs->sgl_pg0_addr_lo =
16506 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16507 sgl_pg_pairs->sgl_pg0_addr_hi =
16508 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16509 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16510 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16511 else
16512 pdma_phys_bpl1 = 0;
16513 sgl_pg_pairs->sgl_pg1_addr_lo =
16514 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16515 sgl_pg_pairs->sgl_pg1_addr_hi =
16516 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16517 /* Keep the first xritag on the list */
16518 if (pg_pairs == 0)
16519 xritag_start = psb->cur_iocbq.sli4_xritag;
16520 sgl_pg_pairs++;
16521 pg_pairs++;
16522 }
16523 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16524 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16525 /* Perform endian conversion if necessary */
16526 sgl->word0 = cpu_to_le32(sgl->word0);
16527
16528 if (!phba->sli4_hba.intr_enable)
16529 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16530 else {
James Smarta183a152011-10-10 21:32:43 -040016531 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016532 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16533 }
16534 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16535 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16536 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16537 if (rc != MBX_TIMEOUT)
16538 lpfc_sli4_mbox_cmd_free(phba, mbox);
16539 if (shdr_status || shdr_add_status || rc) {
16540 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16541 "2564 POST_SGL_BLOCK mailbox command failed "
16542 "status x%x add_status x%x mbx status x%x\n",
16543 shdr_status, shdr_add_status, rc);
16544 rc = -ENXIO;
16545 }
16546 return rc;
16547}
16548
16549/**
16550 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16551 * @phba: pointer to lpfc_hba struct that the frame was received on
16552 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16553 *
16554 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16555 * valid type of frame that the LPFC driver will handle. This function will
16556 * return a zero if the frame is a valid frame or a non zero value when the
16557 * frame does not pass the check.
16558 **/
16559static int
16560lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16561{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016562 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016563 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016564 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016565
James Smartae9e28f2017-05-15 15:20:51 -070016566#define FC_RCTL_MDS_DIAGS 0xF4
16567
James Smart4f774512009-05-22 14:52:35 -040016568 switch (fc_hdr->fh_r_ctl) {
16569 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16570 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16571 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16572 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16573 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16574 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16575 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16576 case FC_RCTL_DD_CMD_STATUS: /* command status */
16577 case FC_RCTL_ELS_REQ: /* extended link services request */
16578 case FC_RCTL_ELS_REP: /* extended link services reply */
16579 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16580 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16581 case FC_RCTL_BA_NOP: /* basic link service NOP */
16582 case FC_RCTL_BA_ABTS: /* basic link service abort */
16583 case FC_RCTL_BA_RMC: /* remove connection */
16584 case FC_RCTL_BA_ACC: /* basic accept */
16585 case FC_RCTL_BA_RJT: /* basic reject */
16586 case FC_RCTL_BA_PRMT:
16587 case FC_RCTL_ACK_1: /* acknowledge_1 */
16588 case FC_RCTL_ACK_0: /* acknowledge_0 */
16589 case FC_RCTL_P_RJT: /* port reject */
16590 case FC_RCTL_F_RJT: /* fabric reject */
16591 case FC_RCTL_P_BSY: /* port busy */
16592 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16593 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16594 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016595 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016596 case FC_RCTL_END: /* end */
16597 break;
16598 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16599 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16600 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16601 return lpfc_fc_frame_check(phba, fc_hdr);
16602 default:
16603 goto drop;
16604 }
James Smartae9e28f2017-05-15 15:20:51 -070016605
16606#define FC_TYPE_VENDOR_UNIQUE 0xFF
16607
James Smart4f774512009-05-22 14:52:35 -040016608 switch (fc_hdr->fh_type) {
16609 case FC_TYPE_BLS:
16610 case FC_TYPE_ELS:
16611 case FC_TYPE_FCP:
16612 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016613 case FC_TYPE_NVME:
James Smartae9e28f2017-05-15 15:20:51 -070016614 case FC_TYPE_VENDOR_UNIQUE:
James Smart4f774512009-05-22 14:52:35 -040016615 break;
16616 case FC_TYPE_IP:
16617 case FC_TYPE_ILS:
16618 default:
16619 goto drop;
16620 }
James Smart546fc852011-03-11 16:06:29 -050016621
James Smart4f774512009-05-22 14:52:35 -040016622 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016623 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016624 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016625 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16626 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16627 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16628 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16629 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016630 return 0;
16631drop:
16632 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016633 "2539 Dropped frame rctl:x%x type:x%x\n",
16634 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016635 return 1;
16636}
16637
16638/**
16639 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16640 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16641 *
16642 * This function processes the FC header to retrieve the VFI from the VF
16643 * header, if one exists. This function will return the VFI if one exists
16644 * or 0 if no VSAN Header exists.
16645 **/
16646static uint32_t
16647lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16648{
16649 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16650
16651 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16652 return 0;
16653 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16654}
16655
16656/**
16657 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16658 * @phba: Pointer to the HBA structure to search for the vport on
16659 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16660 * @fcfi: The FC Fabric ID that the frame came from
16661 *
16662 * This function searches the @phba for a vport that matches the content of the
16663 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16664 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16665 * returns the matching vport pointer or NULL if unable to match frame to a
16666 * vport.
16667 **/
16668static struct lpfc_vport *
16669lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016670 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016671{
16672 struct lpfc_vport **vports;
16673 struct lpfc_vport *vport = NULL;
16674 int i;
James Smart939723a2012-05-09 21:19:03 -040016675
James Smartbf086112011-08-21 21:48:13 -040016676 if (did == Fabric_DID)
16677 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016678 if ((phba->pport->fc_flag & FC_PT2PT) &&
16679 !(phba->link_state == LPFC_HBA_READY))
16680 return phba->pport;
16681
James Smart4f774512009-05-22 14:52:35 -040016682 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016683 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016684 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16685 if (phba->fcf.fcfi == fcfi &&
16686 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16687 vports[i]->fc_myDID == did) {
16688 vport = vports[i];
16689 break;
16690 }
16691 }
James Smart895427b2017-02-12 13:52:30 -080016692 }
James Smart4f774512009-05-22 14:52:35 -040016693 lpfc_destroy_vport_work_array(phba, vports);
16694 return vport;
16695}
16696
16697/**
James Smart45ed1192009-10-02 15:17:02 -040016698 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16699 * @vport: The vport to work on.
16700 *
16701 * This function updates the receive sequence time stamp for this vport. The
16702 * receive sequence time stamp indicates the time that the last frame of the
16703 * the sequence that has been idle for the longest amount of time was received.
16704 * the driver uses this time stamp to indicate if any received sequences have
16705 * timed out.
16706 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016707static void
James Smart45ed1192009-10-02 15:17:02 -040016708lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16709{
16710 struct lpfc_dmabuf *h_buf;
16711 struct hbq_dmabuf *dmabuf = NULL;
16712
16713 /* get the oldest sequence on the rcv list */
16714 h_buf = list_get_first(&vport->rcv_buffer_list,
16715 struct lpfc_dmabuf, list);
16716 if (!h_buf)
16717 return;
16718 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16719 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16720}
16721
16722/**
16723 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16724 * @vport: The vport that the received sequences were sent to.
16725 *
16726 * This function cleans up all outstanding received sequences. This is called
16727 * by the driver when a link event or user action invalidates all the received
16728 * sequences.
16729 **/
16730void
16731lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16732{
16733 struct lpfc_dmabuf *h_buf, *hnext;
16734 struct lpfc_dmabuf *d_buf, *dnext;
16735 struct hbq_dmabuf *dmabuf = NULL;
16736
16737 /* start with the oldest sequence on the rcv list */
16738 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16739 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16740 list_del_init(&dmabuf->hbuf.list);
16741 list_for_each_entry_safe(d_buf, dnext,
16742 &dmabuf->dbuf.list, list) {
16743 list_del_init(&d_buf->list);
16744 lpfc_in_buf_free(vport->phba, d_buf);
16745 }
16746 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16747 }
16748}
16749
16750/**
16751 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16752 * @vport: The vport that the received sequences were sent to.
16753 *
16754 * This function determines whether any received sequences have timed out by
16755 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16756 * indicates that there is at least one timed out sequence this routine will
16757 * go through the received sequences one at a time from most inactive to most
16758 * active to determine which ones need to be cleaned up. Once it has determined
16759 * that a sequence needs to be cleaned up it will simply free up the resources
16760 * without sending an abort.
16761 **/
16762void
16763lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16764{
16765 struct lpfc_dmabuf *h_buf, *hnext;
16766 struct lpfc_dmabuf *d_buf, *dnext;
16767 struct hbq_dmabuf *dmabuf = NULL;
16768 unsigned long timeout;
16769 int abort_count = 0;
16770
16771 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16772 vport->rcv_buffer_time_stamp);
16773 if (list_empty(&vport->rcv_buffer_list) ||
16774 time_before(jiffies, timeout))
16775 return;
16776 /* start with the oldest sequence on the rcv list */
16777 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16778 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16779 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16780 dmabuf->time_stamp);
16781 if (time_before(jiffies, timeout))
16782 break;
16783 abort_count++;
16784 list_del_init(&dmabuf->hbuf.list);
16785 list_for_each_entry_safe(d_buf, dnext,
16786 &dmabuf->dbuf.list, list) {
16787 list_del_init(&d_buf->list);
16788 lpfc_in_buf_free(vport->phba, d_buf);
16789 }
16790 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16791 }
16792 if (abort_count)
16793 lpfc_update_rcv_time_stamp(vport);
16794}
16795
16796/**
James Smart4f774512009-05-22 14:52:35 -040016797 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16798 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16799 *
16800 * This function searches through the existing incomplete sequences that have
16801 * been sent to this @vport. If the frame matches one of the incomplete
16802 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16803 * make up that sequence. If no sequence is found that matches this frame then
16804 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16805 * This function returns a pointer to the first dmabuf in the sequence list that
16806 * the frame was linked to.
16807 **/
16808static struct hbq_dmabuf *
16809lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16810{
16811 struct fc_frame_header *new_hdr;
16812 struct fc_frame_header *temp_hdr;
16813 struct lpfc_dmabuf *d_buf;
16814 struct lpfc_dmabuf *h_buf;
16815 struct hbq_dmabuf *seq_dmabuf = NULL;
16816 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016817 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016818
James Smart4d9ab992009-10-02 15:16:39 -040016819 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016820 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016821 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016822
James Smart4f774512009-05-22 14:52:35 -040016823 /* Use the hdr_buf to find the sequence that this frame belongs to */
16824 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16825 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16826 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16827 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16828 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16829 continue;
16830 /* found a pending sequence that matches this frame */
16831 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16832 break;
16833 }
16834 if (!seq_dmabuf) {
16835 /*
16836 * This indicates first frame received for this sequence.
16837 * Queue the buffer on the vport's rcv_buffer_list.
16838 */
16839 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016840 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016841 return dmabuf;
16842 }
16843 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016844 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16845 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016846 list_del_init(&seq_dmabuf->hbuf.list);
16847 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16848 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016849 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016850 return dmabuf;
16851 }
James Smart45ed1192009-10-02 15:17:02 -040016852 /* move this sequence to the tail to indicate a young sequence */
16853 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16854 seq_dmabuf->time_stamp = jiffies;
16855 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016856 if (list_empty(&seq_dmabuf->dbuf.list)) {
16857 temp_hdr = dmabuf->hbuf.virt;
16858 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16859 return seq_dmabuf;
16860 }
James Smart4f774512009-05-22 14:52:35 -040016861 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016862 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16863 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016864 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16865 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16866 /*
16867 * If the frame's sequence count is greater than the frame on
16868 * the list then insert the frame right after this frame
16869 */
James Smarteeead812009-12-21 17:01:23 -050016870 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16871 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016872 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016873 found = 1;
16874 break;
James Smart4f774512009-05-22 14:52:35 -040016875 }
James Smart4360ca92015-12-16 18:12:04 -050016876
16877 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16878 break;
16879 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016880 }
James Smart4360ca92015-12-16 18:12:04 -050016881
16882 if (found)
16883 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016884 return NULL;
16885}
16886
16887/**
James Smart6669f9b2009-10-02 15:16:45 -040016888 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16889 * @vport: pointer to a vitural port
16890 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16891 *
16892 * This function tries to abort from the partially assembed sequence, described
16893 * by the information from basic abbort @dmabuf. It checks to see whether such
16894 * partially assembled sequence held by the driver. If so, it shall free up all
16895 * the frames from the partially assembled sequence.
16896 *
16897 * Return
16898 * true -- if there is matching partially assembled sequence present and all
16899 * the frames freed with the sequence;
16900 * false -- if there is no matching partially assembled sequence present so
16901 * nothing got aborted in the lower layer driver
16902 **/
16903static bool
16904lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16905 struct hbq_dmabuf *dmabuf)
16906{
16907 struct fc_frame_header *new_hdr;
16908 struct fc_frame_header *temp_hdr;
16909 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16910 struct hbq_dmabuf *seq_dmabuf = NULL;
16911
16912 /* Use the hdr_buf to find the sequence that matches this frame */
16913 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16914 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16915 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16916 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16917 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16918 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16919 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16920 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16921 continue;
16922 /* found a pending sequence that matches this frame */
16923 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16924 break;
16925 }
16926
16927 /* Free up all the frames from the partially assembled sequence */
16928 if (seq_dmabuf) {
16929 list_for_each_entry_safe(d_buf, n_buf,
16930 &seq_dmabuf->dbuf.list, list) {
16931 list_del_init(&d_buf->list);
16932 lpfc_in_buf_free(vport->phba, d_buf);
16933 }
16934 return true;
16935 }
16936 return false;
16937}
16938
16939/**
James Smart6dd9e312013-01-03 15:43:37 -050016940 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16941 * @vport: pointer to a vitural port
16942 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16943 *
16944 * This function tries to abort from the assembed sequence from upper level
16945 * protocol, described by the information from basic abbort @dmabuf. It
16946 * checks to see whether such pending context exists at upper level protocol.
16947 * If so, it shall clean up the pending context.
16948 *
16949 * Return
16950 * true -- if there is matching pending context of the sequence cleaned
16951 * at ulp;
16952 * false -- if there is no matching pending context of the sequence present
16953 * at ulp.
16954 **/
16955static bool
16956lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16957{
16958 struct lpfc_hba *phba = vport->phba;
16959 int handled;
16960
16961 /* Accepting abort at ulp with SLI4 only */
16962 if (phba->sli_rev < LPFC_SLI_REV4)
16963 return false;
16964
16965 /* Register all caring upper level protocols to attend abort */
16966 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16967 if (handled)
16968 return true;
16969
16970 return false;
16971}
16972
16973/**
James Smart546fc852011-03-11 16:06:29 -050016974 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016975 * @phba: Pointer to HBA context object.
16976 * @cmd_iocbq: pointer to the command iocbq structure.
16977 * @rsp_iocbq: pointer to the response iocbq structure.
16978 *
James Smart546fc852011-03-11 16:06:29 -050016979 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016980 * event. It properly releases the memory allocated to the sequence abort
16981 * accept iocb.
16982 **/
16983static void
James Smart546fc852011-03-11 16:06:29 -050016984lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016985 struct lpfc_iocbq *cmd_iocbq,
16986 struct lpfc_iocbq *rsp_iocbq)
16987{
James Smart6dd9e312013-01-03 15:43:37 -050016988 struct lpfc_nodelist *ndlp;
16989
16990 if (cmd_iocbq) {
16991 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16992 lpfc_nlp_put(ndlp);
16993 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016994 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016995 }
James Smart6b5151f2012-01-18 16:24:06 -050016996
16997 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16998 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16999 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17000 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
17001 rsp_iocbq->iocb.ulpStatus,
17002 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040017003}
17004
17005/**
James Smart6d368e52011-05-24 11:44:12 -040017006 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17007 * @phba: Pointer to HBA context object.
17008 * @xri: xri id in transaction.
17009 *
17010 * This function validates the xri maps to the known range of XRIs allocated an
17011 * used by the driver.
17012 **/
James Smart7851fe22011-07-22 18:36:52 -040017013uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017014lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17015 uint16_t xri)
17016{
James Smarta2fc4aef2014-09-03 12:57:55 -040017017 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040017018
17019 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17020 if (xri == phba->sli4_hba.xri_ids[i])
17021 return i;
17022 }
17023 return NO_XRI;
17024}
17025
James Smart6d368e52011-05-24 11:44:12 -040017026/**
James Smart546fc852011-03-11 16:06:29 -050017027 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017028 * @phba: Pointer to HBA context object.
17029 * @fc_hdr: pointer to a FC frame header.
17030 *
James Smart546fc852011-03-11 16:06:29 -050017031 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017032 * event after aborting the sequence handling.
17033 **/
James Smart86c67372017-04-21 16:05:04 -070017034void
James Smart6dd9e312013-01-03 15:43:37 -050017035lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17036 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040017037{
James Smart6dd9e312013-01-03 15:43:37 -050017038 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017039 struct lpfc_iocbq *ctiocb = NULL;
17040 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017041 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017042 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017043 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017044 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017045
17046 if (!lpfc_is_link_up(phba))
17047 return;
17048
17049 sid = sli4_sid_from_fc_hdr(fc_hdr);
17050 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017051 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017052
James Smart6dd9e312013-01-03 15:43:37 -050017053 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017054 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017055 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017056 if (!ndlp) {
17057 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17058 "1268 Failed to allocate ndlp for "
17059 "oxid:x%x SID:x%x\n", oxid, sid);
17060 return;
17061 }
James Smart6dd9e312013-01-03 15:43:37 -050017062 /* Put ndlp onto pport node list */
17063 lpfc_enqueue_node(vport, ndlp);
17064 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17065 /* re-setup ndlp without removing from node list */
17066 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17067 if (!ndlp) {
17068 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17069 "3275 Failed to active ndlp found "
17070 "for oxid:x%x SID:x%x\n", oxid, sid);
17071 return;
17072 }
James Smart6669f9b2009-10-02 15:16:45 -040017073 }
17074
James Smart546fc852011-03-11 16:06:29 -050017075 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017076 ctiocb = lpfc_sli_get_iocbq(phba);
17077 if (!ctiocb)
17078 return;
17079
James Smart5ffc2662009-11-18 15:39:44 -050017080 /* Extract the F_CTL field from FC_HDR */
17081 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17082
James Smart6669f9b2009-10-02 15:16:45 -040017083 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017084 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017085 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017086 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17087 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17088 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17089
17090 /* Fill in the rest of iocb fields */
17091 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17092 icmd->ulpBdeCount = 0;
17093 icmd->ulpLe = 1;
17094 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017095 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017096 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017097
James Smart6669f9b2009-10-02 15:16:45 -040017098 ctiocb->iocb_cmpl = NULL;
17099 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017100 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017101 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017102 ctiocb->sli4_xritag = NO_XRI;
17103
James Smartee0f4fe2012-05-09 21:19:14 -040017104 if (fctl & FC_FC_EX_CTX)
17105 /* Exchange responder sent the abort so we
17106 * own the oxid.
17107 */
17108 xri = oxid;
17109 else
17110 xri = rxid;
17111 lxri = lpfc_sli4_xri_inrange(phba, xri);
17112 if (lxri != NO_XRI)
17113 lpfc_set_rrq_active(phba, ndlp, lxri,
17114 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017115 /* For BA_ABTS from exchange responder, if the logical xri with
17116 * the oxid maps to the FCP XRI range, the port no longer has
17117 * that exchange context, send a BLS_RJT. Override the IOCB for
17118 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017119 */
James Smart6dd9e312013-01-03 15:43:37 -050017120 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017121 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050017122 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17123 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17124 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17125 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17126 }
17127
17128 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17129 * the driver no longer has that exchange, send a BLS_RJT. Override
17130 * the IOCB for a BA_RJT.
17131 */
17132 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017133 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17134 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17135 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17136 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17137 }
James Smart6669f9b2009-10-02 15:16:45 -040017138
James Smart5ffc2662009-11-18 15:39:44 -050017139 if (fctl & FC_FC_EX_CTX) {
17140 /* ABTS sent by responder to CT exchange, construction
17141 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17142 * field and RX_ID from ABTS for RX_ID field.
17143 */
James Smart546fc852011-03-11 16:06:29 -050017144 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017145 } else {
17146 /* ABTS sent by initiator to CT exchange, construction
17147 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017148 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017149 */
James Smart546fc852011-03-11 16:06:29 -050017150 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017151 }
James Smartf09c3ac2012-03-01 22:33:29 -050017152 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017153 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017154
James Smart546fc852011-03-11 16:06:29 -050017155 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017156 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17157 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17158 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017159
17160 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17161 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017162 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17163 "2925 Failed to issue CT ABTS RSP x%x on "
17164 "xri x%x, Data x%x\n",
17165 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17166 phba->link_state);
17167 lpfc_nlp_put(ndlp);
17168 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017169 lpfc_sli_release_iocbq(phba, ctiocb);
17170 }
James Smart6669f9b2009-10-02 15:16:45 -040017171}
17172
17173/**
17174 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17175 * @vport: Pointer to the vport on which this sequence was received
17176 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17177 *
17178 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17179 * receive sequence is only partially assembed by the driver, it shall abort
17180 * the partially assembled frames for the sequence. Otherwise, if the
17181 * unsolicited receive sequence has been completely assembled and passed to
17182 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17183 * unsolicited sequence has been aborted. After that, it will issue a basic
17184 * accept to accept the abort.
17185 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017186static void
James Smart6669f9b2009-10-02 15:16:45 -040017187lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17188 struct hbq_dmabuf *dmabuf)
17189{
17190 struct lpfc_hba *phba = vport->phba;
17191 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017192 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017193 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017194
James Smart6669f9b2009-10-02 15:16:45 -040017195 /* Make a copy of fc_hdr before the dmabuf being released */
17196 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017197 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017198
James Smart5ffc2662009-11-18 15:39:44 -050017199 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017200 /* ABTS by responder to exchange, no cleanup needed */
17201 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017202 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017203 /* ABTS by initiator to exchange, need to do cleanup */
17204 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17205 if (aborted == false)
17206 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017207 }
James Smart6dd9e312013-01-03 15:43:37 -050017208 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17209
James Smart86c67372017-04-21 16:05:04 -070017210 if (phba->nvmet_support) {
17211 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17212 return;
17213 }
17214
James Smart6dd9e312013-01-03 15:43:37 -050017215 /* Respond with BA_ACC or BA_RJT accordingly */
17216 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017217}
17218
17219/**
James Smart4f774512009-05-22 14:52:35 -040017220 * lpfc_seq_complete - Indicates if a sequence is complete
17221 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17222 *
17223 * This function checks the sequence, starting with the frame described by
17224 * @dmabuf, to see if all the frames associated with this sequence are present.
17225 * the frames associated with this sequence are linked to the @dmabuf using the
17226 * dbuf list. This function looks for two major things. 1) That the first frame
17227 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17228 * set. 3) That there are no holes in the sequence count. The function will
17229 * return 1 when the sequence is complete, otherwise it will return 0.
17230 **/
17231static int
17232lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17233{
17234 struct fc_frame_header *hdr;
17235 struct lpfc_dmabuf *d_buf;
17236 struct hbq_dmabuf *seq_dmabuf;
17237 uint32_t fctl;
17238 int seq_count = 0;
17239
17240 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17241 /* make sure first fame of sequence has a sequence count of zero */
17242 if (hdr->fh_seq_cnt != seq_count)
17243 return 0;
17244 fctl = (hdr->fh_f_ctl[0] << 16 |
17245 hdr->fh_f_ctl[1] << 8 |
17246 hdr->fh_f_ctl[2]);
17247 /* If last frame of sequence we can return success. */
17248 if (fctl & FC_FC_END_SEQ)
17249 return 1;
17250 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17251 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17252 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17253 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017254 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017255 return 0;
17256 fctl = (hdr->fh_f_ctl[0] << 16 |
17257 hdr->fh_f_ctl[1] << 8 |
17258 hdr->fh_f_ctl[2]);
17259 /* If last frame of sequence we can return success. */
17260 if (fctl & FC_FC_END_SEQ)
17261 return 1;
17262 }
17263 return 0;
17264}
17265
17266/**
17267 * lpfc_prep_seq - Prep sequence for ULP processing
17268 * @vport: Pointer to the vport on which this sequence was received
17269 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17270 *
17271 * This function takes a sequence, described by a list of frames, and creates
17272 * a list of iocbq structures to describe the sequence. This iocbq list will be
17273 * used to issue to the generic unsolicited sequence handler. This routine
17274 * returns a pointer to the first iocbq in the list. If the function is unable
17275 * to allocate an iocbq then it throw out the received frames that were not
17276 * able to be described and return a pointer to the first iocbq. If unable to
17277 * allocate any iocbqs (including the first) this function will return NULL.
17278 **/
17279static struct lpfc_iocbq *
17280lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17281{
James Smart7851fe22011-07-22 18:36:52 -040017282 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017283 struct lpfc_dmabuf *d_buf, *n_buf;
17284 struct lpfc_iocbq *first_iocbq, *iocbq;
17285 struct fc_frame_header *fc_hdr;
17286 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017287 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017288 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017289
17290 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17291 /* remove from receive buffer list */
17292 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017293 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017294 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017295 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017296 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017297 /* Get an iocbq struct to fill in. */
17298 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17299 if (first_iocbq) {
17300 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017301 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017302 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017303 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017304
17305 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17306 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17307 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17308 first_iocbq->iocb.un.rcvels.parmRo =
17309 sli4_did_from_fc_hdr(fc_hdr);
17310 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17311 } else
17312 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017313 first_iocbq->iocb.ulpContext = NO_XRI;
17314 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17315 be16_to_cpu(fc_hdr->fh_ox_id);
17316 /* iocbq is prepped for internal consumption. Physical vpi. */
17317 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17318 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017319 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017320 tot_len = bf_get(lpfc_rcqe_length,
17321 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17322
James Smart4f774512009-05-22 14:52:35 -040017323 first_iocbq->context2 = &seq_dmabuf->dbuf;
17324 first_iocbq->context3 = NULL;
17325 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017326 if (tot_len > LPFC_DATA_BUF_SIZE)
17327 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017328 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017329 else
17330 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17331
James Smart4f774512009-05-22 14:52:35 -040017332 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017333
James Smart7851fe22011-07-22 18:36:52 -040017334 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017335 }
17336 iocbq = first_iocbq;
17337 /*
17338 * Each IOCBq can have two Buffers assigned, so go through the list
17339 * of buffers for this sequence and save two buffers in each IOCBq
17340 */
17341 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17342 if (!iocbq) {
17343 lpfc_in_buf_free(vport->phba, d_buf);
17344 continue;
17345 }
17346 if (!iocbq->context3) {
17347 iocbq->context3 = d_buf;
17348 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017349 /* We need to get the size out of the right CQE */
17350 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17351 len = bf_get(lpfc_rcqe_length,
17352 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017353 pbde = (struct ulp_bde64 *)
17354 &iocbq->iocb.unsli3.sli3Words[4];
17355 if (len > LPFC_DATA_BUF_SIZE)
17356 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17357 else
17358 pbde->tus.f.bdeSize = len;
17359
James Smart7851fe22011-07-22 18:36:52 -040017360 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17361 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017362 } else {
17363 iocbq = lpfc_sli_get_iocbq(vport->phba);
17364 if (!iocbq) {
17365 if (first_iocbq) {
17366 first_iocbq->iocb.ulpStatus =
17367 IOSTAT_FCP_RSP_ERROR;
17368 first_iocbq->iocb.un.ulpWord[4] =
17369 IOERR_NO_RESOURCES;
17370 }
17371 lpfc_in_buf_free(vport->phba, d_buf);
17372 continue;
17373 }
James Smart7851fe22011-07-22 18:36:52 -040017374 /* We need to get the size out of the right CQE */
17375 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17376 len = bf_get(lpfc_rcqe_length,
17377 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017378 iocbq->context2 = d_buf;
17379 iocbq->context3 = NULL;
17380 iocbq->iocb.ulpBdeCount = 1;
17381 if (len > LPFC_DATA_BUF_SIZE)
17382 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17383 LPFC_DATA_BUF_SIZE;
17384 else
17385 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17386
James Smart7851fe22011-07-22 18:36:52 -040017387 tot_len += len;
17388 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17389
James Smart4f774512009-05-22 14:52:35 -040017390 iocbq->iocb.un.rcvels.remoteID = sid;
17391 list_add_tail(&iocbq->list, &first_iocbq->list);
17392 }
17393 }
17394 return first_iocbq;
17395}
17396
James Smart6669f9b2009-10-02 15:16:45 -040017397static void
17398lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17399 struct hbq_dmabuf *seq_dmabuf)
17400{
17401 struct fc_frame_header *fc_hdr;
17402 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17403 struct lpfc_hba *phba = vport->phba;
17404
17405 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17406 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17407 if (!iocbq) {
17408 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17409 "2707 Ring %d handler: Failed to allocate "
17410 "iocb Rctl x%x Type x%x received\n",
17411 LPFC_ELS_RING,
17412 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17413 return;
17414 }
17415 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017416 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017417 iocbq, fc_hdr->fh_r_ctl,
17418 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017419 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017420 "2540 Ring %d handler: unexpected Rctl "
17421 "x%x Type x%x received\n",
17422 LPFC_ELS_RING,
17423 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17424
17425 /* Free iocb created in lpfc_prep_seq */
17426 list_for_each_entry_safe(curr_iocb, next_iocb,
17427 &iocbq->list, list) {
17428 list_del_init(&curr_iocb->list);
17429 lpfc_sli_release_iocbq(phba, curr_iocb);
17430 }
17431 lpfc_sli_release_iocbq(phba, iocbq);
17432}
17433
James Smartae9e28f2017-05-15 15:20:51 -070017434static void
17435lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17436 struct lpfc_iocbq *rspiocb)
17437{
17438 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17439
17440 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017441 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017442 kfree(pcmd);
17443 lpfc_sli_release_iocbq(phba, cmdiocb);
17444}
17445
17446static void
17447lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17448 struct hbq_dmabuf *dmabuf)
17449{
17450 struct fc_frame_header *fc_hdr;
17451 struct lpfc_hba *phba = vport->phba;
17452 struct lpfc_iocbq *iocbq = NULL;
17453 union lpfc_wqe *wqe;
17454 struct lpfc_dmabuf *pcmd = NULL;
17455 uint32_t frame_len;
17456 int rc;
17457
17458 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17459 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17460
17461 /* Send the received frame back */
17462 iocbq = lpfc_sli_get_iocbq(phba);
17463 if (!iocbq)
17464 goto exit;
17465
17466 /* Allocate buffer for command payload */
17467 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17468 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017469 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017470 &pcmd->phys);
17471 if (!pcmd || !pcmd->virt)
17472 goto exit;
17473
17474 INIT_LIST_HEAD(&pcmd->list);
17475
17476 /* copyin the payload */
17477 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17478
17479 /* fill in BDE's for command */
17480 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17481 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17482 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17483 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17484
17485 iocbq->context2 = pcmd;
17486 iocbq->vport = vport;
17487 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17488 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17489
17490 /*
17491 * Setup rest of the iocb as though it were a WQE
17492 * Build the SEND_FRAME WQE
17493 */
17494 wqe = (union lpfc_wqe *)&iocbq->iocb;
17495
17496 wqe->send_frame.frame_len = frame_len;
17497 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17498 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17499 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17500 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17501 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17502 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17503
17504 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17505 iocbq->iocb.ulpLe = 1;
17506 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17507 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17508 if (rc == IOCB_ERROR)
17509 goto exit;
17510
17511 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17512 return;
17513
17514exit:
17515 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17516 "2023 Unable to process MDS loopback frame\n");
17517 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017518 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017519 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017520 if (iocbq)
17521 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017522 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17523}
17524
James Smart4f774512009-05-22 14:52:35 -040017525/**
17526 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17527 * @phba: Pointer to HBA context object.
17528 *
17529 * This function is called with no lock held. This function processes all
17530 * the received buffers and gives it to upper layers when a received buffer
17531 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017532 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017533 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17534 * appropriate receive function when the final frame in a sequence is received.
17535 **/
James Smart4d9ab992009-10-02 15:16:39 -040017536void
17537lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17538 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017539{
James Smart4d9ab992009-10-02 15:16:39 -040017540 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017541 struct fc_frame_header *fc_hdr;
17542 struct lpfc_vport *vport;
17543 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017544 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017545
James Smart4f774512009-05-22 14:52:35 -040017546 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017547 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017548
James Smart4d9ab992009-10-02 15:16:39 -040017549 /* check to see if this a valid type of frame */
17550 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17551 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17552 return;
17553 }
James Smart2ea259e2017-02-12 13:52:27 -080017554
James Smart7851fe22011-07-22 18:36:52 -040017555 if ((bf_get(lpfc_cqe_code,
17556 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17557 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17558 &dmabuf->cq_event.cqe.rcqe_cmpl);
17559 else
17560 fcfi = bf_get(lpfc_rcqe_fcf_id,
17561 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017562
James Smartae9e28f2017-05-15 15:20:51 -070017563 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17564 vport = phba->pport;
17565 /* Handle MDS Loopback frames */
17566 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17567 return;
17568 }
17569
James Smart895427b2017-02-12 13:52:30 -080017570 /* d_id this frame is directed to */
17571 did = sli4_did_from_fc_hdr(fc_hdr);
17572
17573 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017574 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017575 /* throw out the frame */
17576 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17577 return;
17578 }
James Smart939723a2012-05-09 21:19:03 -040017579
James Smart939723a2012-05-09 21:19:03 -040017580 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17581 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17582 (did != Fabric_DID)) {
17583 /*
17584 * Throw out the frame if we are not pt2pt.
17585 * The pt2pt protocol allows for discovery frames
17586 * to be received without a registered VPI.
17587 */
17588 if (!(vport->fc_flag & FC_PT2PT) ||
17589 (phba->link_state == LPFC_HBA_READY)) {
17590 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17591 return;
17592 }
17593 }
17594
James Smart6669f9b2009-10-02 15:16:45 -040017595 /* Handle the basic abort sequence (BA_ABTS) event */
17596 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17597 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17598 return;
17599 }
17600
James Smart4d9ab992009-10-02 15:16:39 -040017601 /* Link this frame */
17602 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17603 if (!seq_dmabuf) {
17604 /* unable to add frame to vport - throw it out */
17605 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17606 return;
17607 }
17608 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017609 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017610 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017611
James Smart6669f9b2009-10-02 15:16:45 -040017612 /* Send the complete sequence to the upper layer protocol */
17613 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017614}
James Smart6fb120a2009-05-22 14:52:59 -040017615
17616/**
17617 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17618 * @phba: pointer to lpfc hba data structure.
17619 *
17620 * This routine is invoked to post rpi header templates to the
17621 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017622 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17623 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017624 *
17625 * This routine does not require any locks. It's usage is expected
17626 * to be driver load or reset recovery when the driver is
17627 * sequential.
17628 *
17629 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017630 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017631 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017632 * When this error occurs, the driver is not guaranteed
17633 * to have any rpi regions posted to the device and
17634 * must either attempt to repost the regions or take a
17635 * fatal error.
17636 **/
17637int
17638lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17639{
17640 struct lpfc_rpi_hdr *rpi_page;
17641 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017642 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017643
James Smart6d368e52011-05-24 11:44:12 -040017644 /* SLI4 ports that support extents do not require RPI headers. */
17645 if (!phba->sli4_hba.rpi_hdrs_in_use)
17646 goto exit;
17647 if (phba->sli4_hba.extents_in_use)
17648 return -EIO;
17649
James Smart6fb120a2009-05-22 14:52:59 -040017650 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017651 /*
17652 * Assign the rpi headers a physical rpi only if the driver
17653 * has not initialized those resources. A port reset only
17654 * needs the headers posted.
17655 */
17656 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17657 LPFC_RPI_RSRC_RDY)
17658 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17659
James Smart6fb120a2009-05-22 14:52:59 -040017660 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17661 if (rc != MBX_SUCCESS) {
17662 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17663 "2008 Error %d posting all rpi "
17664 "headers\n", rc);
17665 rc = -EIO;
17666 break;
17667 }
17668 }
17669
James Smart6d368e52011-05-24 11:44:12 -040017670 exit:
17671 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17672 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017673 return rc;
17674}
17675
17676/**
17677 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17678 * @phba: pointer to lpfc hba data structure.
17679 * @rpi_page: pointer to the rpi memory region.
17680 *
17681 * This routine is invoked to post a single rpi header to the
17682 * HBA consistent with the SLI-4 interface spec. This memory region
17683 * maps up to 64 rpi context regions.
17684 *
17685 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017686 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017687 * -ENOMEM - No available memory
17688 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017689 **/
17690int
17691lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17692{
17693 LPFC_MBOXQ_t *mboxq;
17694 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17695 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017696 uint32_t shdr_status, shdr_add_status;
17697 union lpfc_sli4_cfg_shdr *shdr;
17698
James Smart6d368e52011-05-24 11:44:12 -040017699 /* SLI4 ports that support extents do not require RPI headers. */
17700 if (!phba->sli4_hba.rpi_hdrs_in_use)
17701 return rc;
17702 if (phba->sli4_hba.extents_in_use)
17703 return -EIO;
17704
James Smart6fb120a2009-05-22 14:52:59 -040017705 /* The port is notified of the header region via a mailbox command. */
17706 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17707 if (!mboxq) {
17708 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17709 "2001 Unable to allocate memory for issuing "
17710 "SLI_CONFIG_SPECIAL mailbox command\n");
17711 return -ENOMEM;
17712 }
17713
17714 /* Post all rpi memory regions to the port. */
17715 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017716 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17717 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17718 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017719 sizeof(struct lpfc_sli4_cfg_mhdr),
17720 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017721
17722
17723 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017724 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17725 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017726 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17727 hdr_tmpl, rpi_page->page_count);
17728
James Smart6fb120a2009-05-22 14:52:59 -040017729 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17730 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017731 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017732 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17733 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17734 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17735 if (rc != MBX_TIMEOUT)
17736 mempool_free(mboxq, phba->mbox_mem_pool);
17737 if (shdr_status || shdr_add_status || rc) {
17738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17739 "2514 POST_RPI_HDR mailbox failed with "
17740 "status x%x add_status x%x, mbx status x%x\n",
17741 shdr_status, shdr_add_status, rc);
17742 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017743 } else {
17744 /*
17745 * The next_rpi stores the next logical module-64 rpi value used
17746 * to post physical rpis in subsequent rpi postings.
17747 */
17748 spin_lock_irq(&phba->hbalock);
17749 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17750 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017751 }
17752 return rc;
17753}
17754
17755/**
17756 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17757 * @phba: pointer to lpfc hba data structure.
17758 *
17759 * This routine is invoked to post rpi header templates to the
17760 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017761 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17762 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017763 *
17764 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017765 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017766 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17767 **/
17768int
17769lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17770{
James Smart6d368e52011-05-24 11:44:12 -040017771 unsigned long rpi;
17772 uint16_t max_rpi, rpi_limit;
17773 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017774 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017775 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017776
James Smart6fb120a2009-05-22 14:52:59 -040017777 /*
James Smart6d368e52011-05-24 11:44:12 -040017778 * Fetch the next logical rpi. Because this index is logical,
17779 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017780 */
James Smart4902b382013-10-10 12:20:35 -040017781 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017782 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17783 rpi_limit = phba->sli4_hba.next_rpi;
17784
James Smart6d368e52011-05-24 11:44:12 -040017785 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17786 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017787 rpi = LPFC_RPI_ALLOC_ERROR;
17788 else {
17789 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17790 phba->sli4_hba.max_cfg_param.rpi_used++;
17791 phba->sli4_hba.rpi_count++;
17792 }
James Smartbe6bb942015-04-07 15:07:22 -040017793 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17794 "0001 rpi:%x max:%x lim:%x\n",
17795 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017796
17797 /*
17798 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017799 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017800 */
17801 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17802 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017803 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017804 return rpi;
17805 }
17806
17807 /*
James Smart6d368e52011-05-24 11:44:12 -040017808 * RPI header postings are not required for SLI4 ports capable of
17809 * extents.
17810 */
17811 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017812 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017813 return rpi;
17814 }
17815
17816 /*
James Smart6fb120a2009-05-22 14:52:59 -040017817 * If the driver is running low on rpi resources, allocate another
17818 * page now. Note that the next_rpi value is used because
17819 * it represents how many are actually in use whereas max_rpi notes
17820 * how many are supported max by the device.
17821 */
James Smart6d368e52011-05-24 11:44:12 -040017822 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017823 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017824 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17825 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17826 if (!rpi_hdr) {
17827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17828 "2002 Error Could not grow rpi "
17829 "count\n");
17830 } else {
James Smart6d368e52011-05-24 11:44:12 -040017831 lrpi = rpi_hdr->start_rpi;
17832 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017833 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17834 }
17835 }
17836
17837 return rpi;
17838}
17839
17840/**
17841 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17842 * @phba: pointer to lpfc hba data structure.
17843 *
17844 * This routine is invoked to release an rpi to the pool of
17845 * available rpis maintained by the driver.
17846 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017847static void
James Smartd7c47992010-06-08 18:31:54 -040017848__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17849{
17850 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17851 phba->sli4_hba.rpi_count--;
17852 phba->sli4_hba.max_cfg_param.rpi_used--;
17853 }
17854}
17855
17856/**
17857 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17858 * @phba: pointer to lpfc hba data structure.
17859 *
17860 * This routine is invoked to release an rpi to the pool of
17861 * available rpis maintained by the driver.
17862 **/
17863void
James Smart6fb120a2009-05-22 14:52:59 -040017864lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17865{
17866 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017867 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017868 spin_unlock_irq(&phba->hbalock);
17869}
17870
17871/**
17872 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17873 * @phba: pointer to lpfc hba data structure.
17874 *
17875 * This routine is invoked to remove the memory region that
17876 * provided rpi via a bitmask.
17877 **/
17878void
17879lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17880{
17881 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017882 kfree(phba->sli4_hba.rpi_ids);
17883 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017884}
17885
17886/**
17887 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17888 * @phba: pointer to lpfc hba data structure.
17889 *
17890 * This routine is invoked to remove the memory region that
17891 * provided rpi via a bitmask.
17892 **/
17893int
James Smart6b5151f2012-01-18 16:24:06 -050017894lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17895 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017896{
17897 LPFC_MBOXQ_t *mboxq;
17898 struct lpfc_hba *phba = ndlp->phba;
17899 int rc;
17900
17901 /* The port is notified of the header region via a mailbox command. */
17902 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17903 if (!mboxq)
17904 return -ENOMEM;
17905
17906 /* Post all rpi memory regions to the port. */
17907 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017908 if (cmpl) {
17909 mboxq->mbox_cmpl = cmpl;
17910 mboxq->context1 = arg;
17911 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017912 } else
17913 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017914 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017915 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17916 if (rc == MBX_NOT_FINISHED) {
17917 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17918 "2010 Resume RPI Mailbox failed "
17919 "status %d, mbxStatus x%x\n", rc,
17920 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17921 mempool_free(mboxq, phba->mbox_mem_pool);
17922 return -EIO;
17923 }
17924 return 0;
17925}
17926
17927/**
17928 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017929 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017930 *
James Smart76a95d72010-11-20 23:11:48 -050017931 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017932 *
17933 * Returns:
17934 * 0 success
17935 * -Evalue otherwise
17936 **/
17937int
James Smart76a95d72010-11-20 23:11:48 -050017938lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017939{
17940 LPFC_MBOXQ_t *mboxq;
17941 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017942 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017943 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017944 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017945 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17946 if (!mboxq)
17947 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017948 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017949 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017950 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017951 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017952 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017953 "2022 INIT VPI Mailbox failed "
17954 "status %d, mbxStatus x%x\n", rc,
17955 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017956 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017957 }
James Smart6a9c52c2009-10-02 15:16:51 -040017958 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017959 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017960
17961 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017962}
17963
17964/**
17965 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17966 * @phba: pointer to lpfc hba data structure.
17967 * @mboxq: Pointer to mailbox object.
17968 *
17969 * This routine is invoked to manually add a single FCF record. The caller
17970 * must pass a completely initialized FCF_Record. This routine takes
17971 * care of the nonembedded mailbox operations.
17972 **/
17973static void
17974lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17975{
17976 void *virt_addr;
17977 union lpfc_sli4_cfg_shdr *shdr;
17978 uint32_t shdr_status, shdr_add_status;
17979
17980 virt_addr = mboxq->sge_array->addr[0];
17981 /* The IOCTL status is embedded in the mailbox subheader. */
17982 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17983 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17984 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17985
17986 if ((shdr_status || shdr_add_status) &&
17987 (shdr_status != STATUS_FCF_IN_USE))
17988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17989 "2558 ADD_FCF_RECORD mailbox failed with "
17990 "status x%x add_status x%x\n",
17991 shdr_status, shdr_add_status);
17992
17993 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17994}
17995
17996/**
17997 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17998 * @phba: pointer to lpfc hba data structure.
17999 * @fcf_record: pointer to the initialized fcf record to add.
18000 *
18001 * This routine is invoked to manually add a single FCF record. The caller
18002 * must pass a completely initialized FCF_Record. This routine takes
18003 * care of the nonembedded mailbox operations.
18004 **/
18005int
18006lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18007{
18008 int rc = 0;
18009 LPFC_MBOXQ_t *mboxq;
18010 uint8_t *bytep;
18011 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040018012 struct lpfc_mbx_sge sge;
18013 uint32_t alloc_len, req_len;
18014 uint32_t fcfindex;
18015
18016 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18017 if (!mboxq) {
18018 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18019 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18020 return -ENOMEM;
18021 }
18022
18023 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18024 sizeof(uint32_t);
18025
18026 /* Allocate DMA memory and set up the non-embedded mailbox command */
18027 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18028 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18029 req_len, LPFC_SLI4_MBX_NEMBED);
18030 if (alloc_len < req_len) {
18031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18032 "2523 Allocated DMA memory size (x%x) is "
18033 "less than the requested DMA memory "
18034 "size (x%x)\n", alloc_len, req_len);
18035 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18036 return -ENOMEM;
18037 }
18038
18039 /*
18040 * Get the first SGE entry from the non-embedded DMA memory. This
18041 * routine only uses a single SGE.
18042 */
18043 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018044 virt_addr = mboxq->sge_array->addr[0];
18045 /*
18046 * Configure the FCF record for FCFI 0. This is the driver's
18047 * hardcoded default and gets used in nonFIP mode.
18048 */
18049 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18050 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18051 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18052
18053 /*
18054 * Copy the fcf_index and the FCF Record Data. The data starts after
18055 * the FCoE header plus word10. The data copy needs to be endian
18056 * correct.
18057 */
18058 bytep += sizeof(uint32_t);
18059 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18060 mboxq->vport = phba->pport;
18061 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18062 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18063 if (rc == MBX_NOT_FINISHED) {
18064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18065 "2515 ADD_FCF_RECORD mailbox failed with "
18066 "status 0x%x\n", rc);
18067 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18068 rc = -EIO;
18069 } else
18070 rc = 0;
18071
18072 return rc;
18073}
18074
18075/**
18076 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18077 * @phba: pointer to lpfc hba data structure.
18078 * @fcf_record: pointer to the fcf record to write the default data.
18079 * @fcf_index: FCF table entry index.
18080 *
18081 * This routine is invoked to build the driver's default FCF record. The
18082 * values used are hardcoded. This routine handles memory initialization.
18083 *
18084 **/
18085void
18086lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18087 struct fcf_record *fcf_record,
18088 uint16_t fcf_index)
18089{
18090 memset(fcf_record, 0, sizeof(struct fcf_record));
18091 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18092 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18093 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18094 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18095 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18096 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18097 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18098 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18099 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18100 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18101 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18102 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18103 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018104 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018105 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18106 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18107 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18108 /* Set the VLAN bit map */
18109 if (phba->valid_vlan) {
18110 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18111 = 1 << (phba->vlan_id % 8);
18112 }
18113}
18114
18115/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018116 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018117 * @phba: pointer to lpfc hba data structure.
18118 * @fcf_index: FCF table entry offset.
18119 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018120 * This routine is invoked to scan the entire FCF table by reading FCF
18121 * record and processing it one at a time starting from the @fcf_index
18122 * for initial FCF discovery or fast FCF failover rediscovery.
18123 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018124 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018125 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018126 **/
18127int
James Smart0c9ab6f2010-02-26 14:15:57 -050018128lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018129{
18130 int rc = 0, error;
18131 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018132
James Smart32b97932009-07-19 10:01:21 -040018133 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018134 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018135 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18136 if (!mboxq) {
18137 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18138 "2000 Failed to allocate mbox for "
18139 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018140 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018141 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018142 }
James Smartecfd03c2010-02-12 14:41:27 -050018143 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018144 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018145 if (rc) {
18146 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018147 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018148 }
James Smartecfd03c2010-02-12 14:41:27 -050018149 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018150 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018151 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018152
18153 spin_lock_irq(&phba->hbalock);
18154 phba->hba_flag |= FCF_TS_INPROG;
18155 spin_unlock_irq(&phba->hbalock);
18156
James Smart6fb120a2009-05-22 14:52:59 -040018157 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018158 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018159 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018160 else {
James Smart38b92ef2010-08-04 16:11:39 -040018161 /* Reset eligible FCF count for new scan */
18162 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018163 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018164 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018165 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018166fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018167 if (error) {
18168 if (mboxq)
18169 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018170 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018171 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018172 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018173 spin_unlock_irq(&phba->hbalock);
18174 }
James Smart6fb120a2009-05-22 14:52:59 -040018175 return error;
18176}
James Smarta0c87cb2009-07-19 10:01:10 -040018177
18178/**
James Smarta93ff372010-10-22 11:06:08 -040018179 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018180 * @phba: pointer to lpfc hba data structure.
18181 * @fcf_index: FCF table entry offset.
18182 *
18183 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018184 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018185 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018186 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018187 * otherwise.
18188 **/
18189int
18190lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18191{
18192 int rc = 0, error;
18193 LPFC_MBOXQ_t *mboxq;
18194
18195 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18196 if (!mboxq) {
18197 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18198 "2763 Failed to allocate mbox for "
18199 "READ_FCF cmd\n");
18200 error = -ENOMEM;
18201 goto fail_fcf_read;
18202 }
18203 /* Construct the read FCF record mailbox command */
18204 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18205 if (rc) {
18206 error = -EINVAL;
18207 goto fail_fcf_read;
18208 }
18209 /* Issue the mailbox command asynchronously */
18210 mboxq->vport = phba->pport;
18211 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18212 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18213 if (rc == MBX_NOT_FINISHED)
18214 error = -EIO;
18215 else
18216 error = 0;
18217
18218fail_fcf_read:
18219 if (error && mboxq)
18220 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18221 return error;
18222}
18223
18224/**
18225 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18226 * @phba: pointer to lpfc hba data structure.
18227 * @fcf_index: FCF table entry offset.
18228 *
18229 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018230 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018231 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018232 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018233 * otherwise.
18234 **/
18235int
18236lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18237{
18238 int rc = 0, error;
18239 LPFC_MBOXQ_t *mboxq;
18240
18241 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18242 if (!mboxq) {
18243 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18244 "2758 Failed to allocate mbox for "
18245 "READ_FCF cmd\n");
18246 error = -ENOMEM;
18247 goto fail_fcf_read;
18248 }
18249 /* Construct the read FCF record mailbox command */
18250 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18251 if (rc) {
18252 error = -EINVAL;
18253 goto fail_fcf_read;
18254 }
18255 /* Issue the mailbox command asynchronously */
18256 mboxq->vport = phba->pport;
18257 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18258 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18259 if (rc == MBX_NOT_FINISHED)
18260 error = -EIO;
18261 else
18262 error = 0;
18263
18264fail_fcf_read:
18265 if (error && mboxq)
18266 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18267 return error;
18268}
18269
18270/**
James Smartf5cb5302015-12-16 18:11:52 -050018271 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018272 * phba pointer to the lpfc_hba struct for this port.
18273 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18274 * routine when the rr_bmask is empty. The FCF indecies are put into the
18275 * rr_bmask based on their priority level. Starting from the highest priority
18276 * to the lowest. The most likely FCF candidate will be in the highest
18277 * priority group. When this routine is called it searches the fcf_pri list for
18278 * next lowest priority group and repopulates the rr_bmask with only those
18279 * fcf_indexes.
18280 * returns:
18281 * 1=success 0=failure
18282 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018283static int
James Smart7d791df2011-07-22 18:37:52 -040018284lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18285{
18286 uint16_t next_fcf_pri;
18287 uint16_t last_index;
18288 struct lpfc_fcf_pri *fcf_pri;
18289 int rc;
18290 int ret = 0;
18291
18292 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18293 LPFC_SLI4_FCF_TBL_INDX_MAX);
18294 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18295 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018296
18297 /* Verify the priority list has 2 or more entries */
18298 spin_lock_irq(&phba->hbalock);
18299 if (list_empty(&phba->fcf.fcf_pri_list) ||
18300 list_is_singular(&phba->fcf.fcf_pri_list)) {
18301 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018302 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18303 "3061 Last IDX %d\n", last_index);
18304 return 0; /* Empty rr list */
18305 }
James Smart25626692013-03-01 16:36:54 -050018306 spin_unlock_irq(&phba->hbalock);
18307
James Smart7d791df2011-07-22 18:37:52 -040018308 next_fcf_pri = 0;
18309 /*
18310 * Clear the rr_bmask and set all of the bits that are at this
18311 * priority.
18312 */
18313 memset(phba->fcf.fcf_rr_bmask, 0,
18314 sizeof(*phba->fcf.fcf_rr_bmask));
18315 spin_lock_irq(&phba->hbalock);
18316 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18317 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18318 continue;
18319 /*
18320 * the 1st priority that has not FLOGI failed
18321 * will be the highest.
18322 */
18323 if (!next_fcf_pri)
18324 next_fcf_pri = fcf_pri->fcf_rec.priority;
18325 spin_unlock_irq(&phba->hbalock);
18326 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18327 rc = lpfc_sli4_fcf_rr_index_set(phba,
18328 fcf_pri->fcf_rec.fcf_index);
18329 if (rc)
18330 return 0;
18331 }
18332 spin_lock_irq(&phba->hbalock);
18333 }
18334 /*
18335 * if next_fcf_pri was not set above and the list is not empty then
18336 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018337 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018338 */
18339 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18340 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18341 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18342 /*
18343 * the 1st priority that has not FLOGI failed
18344 * will be the highest.
18345 */
18346 if (!next_fcf_pri)
18347 next_fcf_pri = fcf_pri->fcf_rec.priority;
18348 spin_unlock_irq(&phba->hbalock);
18349 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18350 rc = lpfc_sli4_fcf_rr_index_set(phba,
18351 fcf_pri->fcf_rec.fcf_index);
18352 if (rc)
18353 return 0;
18354 }
18355 spin_lock_irq(&phba->hbalock);
18356 }
18357 } else
18358 ret = 1;
18359 spin_unlock_irq(&phba->hbalock);
18360
18361 return ret;
18362}
18363/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018364 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18365 * @phba: pointer to lpfc hba data structure.
18366 *
18367 * This routine is to get the next eligible FCF record index in a round
18368 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018369 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018370 * shall be returned, otherwise, the next eligible FCF record's index
18371 * shall be returned.
18372 **/
18373uint16_t
18374lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18375{
18376 uint16_t next_fcf_index;
18377
James Smart421c6622013-01-03 15:44:16 -050018378initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018379 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018380 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18381
James Smart7d791df2011-07-22 18:37:52 -040018382next_priority:
James Smart421c6622013-01-03 15:44:16 -050018383 /* Determine the next fcf index to check */
18384 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018385 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18386 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018387 next_fcf_index);
18388
James Smart0c9ab6f2010-02-26 14:15:57 -050018389 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018390 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18391 /*
18392 * If we have wrapped then we need to clear the bits that
18393 * have been tested so that we can detect when we should
18394 * change the priority level.
18395 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018396 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18397 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018398 }
18399
James Smart0c9ab6f2010-02-26 14:15:57 -050018400
James Smart3804dc82010-07-14 15:31:37 -040018401 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018402 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18403 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18404 /*
18405 * If next fcf index is not found check if there are lower
18406 * Priority level fcf's in the fcf_priority list.
18407 * Set up the rr_bmask with all of the avaiable fcf bits
18408 * at that level and continue the selection process.
18409 */
18410 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018411 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018412 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18413 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040018414 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18415 return LPFC_FCOE_FCF_NEXT_NONE;
18416 else {
18417 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18418 "3063 Only FCF available idx %d, flag %x\n",
18419 next_fcf_index,
18420 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18421 return next_fcf_index;
18422 }
James Smart3804dc82010-07-14 15:31:37 -040018423 }
18424
James Smart7d791df2011-07-22 18:37:52 -040018425 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18426 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018427 LPFC_FCF_FLOGI_FAILED) {
18428 if (list_is_singular(&phba->fcf.fcf_pri_list))
18429 return LPFC_FCOE_FCF_NEXT_NONE;
18430
James Smart7d791df2011-07-22 18:37:52 -040018431 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018432 }
James Smart7d791df2011-07-22 18:37:52 -040018433
James Smart3804dc82010-07-14 15:31:37 -040018434 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018435 "2845 Get next roundrobin failover FCF (x%x)\n",
18436 next_fcf_index);
18437
James Smart0c9ab6f2010-02-26 14:15:57 -050018438 return next_fcf_index;
18439}
18440
18441/**
18442 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18443 * @phba: pointer to lpfc hba data structure.
18444 *
18445 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018446 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018447 * does not go beyond the range of the driver allocated bmask dimension
18448 * before setting the bit.
18449 *
18450 * Returns 0 if the index bit successfully set, otherwise, it returns
18451 * -EINVAL.
18452 **/
18453int
18454lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18455{
18456 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18457 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018458 "2610 FCF (x%x) reached driver's book "
18459 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018460 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18461 return -EINVAL;
18462 }
18463 /* Set the eligible FCF record index bmask */
18464 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18465
James Smart3804dc82010-07-14 15:31:37 -040018466 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018467 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018468 "bmask\n", fcf_index);
18469
James Smart0c9ab6f2010-02-26 14:15:57 -050018470 return 0;
18471}
18472
18473/**
James Smart3804dc82010-07-14 15:31:37 -040018474 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018475 * @phba: pointer to lpfc hba data structure.
18476 *
18477 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018478 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018479 * does not go beyond the range of the driver allocated bmask dimension
18480 * before clearing the bit.
18481 **/
18482void
18483lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18484{
James Smart9a803a72013-09-06 12:17:56 -040018485 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018486 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18487 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018488 "2762 FCF (x%x) reached driver's book "
18489 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018490 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18491 return;
18492 }
18493 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018494 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018495 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18496 list) {
James Smart7d791df2011-07-22 18:37:52 -040018497 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18498 list_del_init(&fcf_pri->list);
18499 break;
18500 }
18501 }
18502 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018503 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018504
18505 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018506 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018507 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018508}
18509
18510/**
James Smartecfd03c2010-02-12 14:41:27 -050018511 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18512 * @phba: pointer to lpfc hba data structure.
18513 *
18514 * This routine is the completion routine for the rediscover FCF table mailbox
18515 * command. If the mailbox command returned failure, it will try to stop the
18516 * FCF rediscover wait timer.
18517 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018518static void
James Smartecfd03c2010-02-12 14:41:27 -050018519lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18520{
18521 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18522 uint32_t shdr_status, shdr_add_status;
18523
18524 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18525
18526 shdr_status = bf_get(lpfc_mbox_hdr_status,
18527 &redisc_fcf->header.cfg_shdr.response);
18528 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18529 &redisc_fcf->header.cfg_shdr.response);
18530 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018531 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018532 "2746 Requesting for FCF rediscovery failed "
18533 "status x%x add_status x%x\n",
18534 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018535 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018536 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018537 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018538 spin_unlock_irq(&phba->hbalock);
18539 /*
18540 * CVL event triggered FCF rediscover request failed,
18541 * last resort to re-try current registered FCF entry.
18542 */
18543 lpfc_retry_pport_discovery(phba);
18544 } else {
18545 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018546 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018547 spin_unlock_irq(&phba->hbalock);
18548 /*
18549 * DEAD FCF event triggered FCF rediscover request
18550 * failed, last resort to fail over as a link down
18551 * to FCF registration.
18552 */
18553 lpfc_sli4_fcf_dead_failthrough(phba);
18554 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018555 } else {
18556 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018557 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018558 /*
18559 * Start FCF rediscovery wait timer for pending FCF
18560 * before rescan FCF record table.
18561 */
18562 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018563 }
James Smartecfd03c2010-02-12 14:41:27 -050018564
18565 mempool_free(mbox, phba->mbox_mem_pool);
18566}
18567
18568/**
James Smart3804dc82010-07-14 15:31:37 -040018569 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018570 * @phba: pointer to lpfc hba data structure.
18571 *
18572 * This routine is invoked to request for rediscovery of the entire FCF table
18573 * by the port.
18574 **/
18575int
18576lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18577{
18578 LPFC_MBOXQ_t *mbox;
18579 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18580 int rc, length;
18581
James Smart0c9ab6f2010-02-26 14:15:57 -050018582 /* Cancel retry delay timers to all vports before FCF rediscover */
18583 lpfc_cancel_all_vport_retry_delay_timer(phba);
18584
James Smartecfd03c2010-02-12 14:41:27 -050018585 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18586 if (!mbox) {
18587 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18588 "2745 Failed to allocate mbox for "
18589 "requesting FCF rediscover.\n");
18590 return -ENOMEM;
18591 }
18592
18593 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18594 sizeof(struct lpfc_sli4_cfg_mhdr));
18595 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18596 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18597 length, LPFC_SLI4_MBX_EMBED);
18598
18599 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18600 /* Set count to 0 for invalidating the entire FCF database */
18601 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18602
18603 /* Issue the mailbox command asynchronously */
18604 mbox->vport = phba->pport;
18605 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18606 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18607
18608 if (rc == MBX_NOT_FINISHED) {
18609 mempool_free(mbox, phba->mbox_mem_pool);
18610 return -EIO;
18611 }
18612 return 0;
18613}
18614
18615/**
James Smartfc2b9892010-02-26 14:15:29 -050018616 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18617 * @phba: pointer to lpfc hba data structure.
18618 *
18619 * This function is the failover routine as a last resort to the FCF DEAD
18620 * event when driver failed to perform fast FCF failover.
18621 **/
18622void
18623lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18624{
18625 uint32_t link_state;
18626
18627 /*
18628 * Last resort as FCF DEAD event failover will treat this as
18629 * a link down, but save the link state because we don't want
18630 * it to be changed to Link Down unless it is already down.
18631 */
18632 link_state = phba->link_state;
18633 lpfc_linkdown(phba);
18634 phba->link_state = link_state;
18635
18636 /* Unregister FCF if no devices connected to it */
18637 lpfc_unregister_unused_fcf(phba);
18638}
18639
18640/**
James Smart026abb82011-12-13 13:20:45 -050018641 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018642 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018643 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018644 *
James Smart026abb82011-12-13 13:20:45 -050018645 * This function gets SLI3 port configure region 23 data through memory dump
18646 * mailbox command. When it successfully retrieves data, the size of the data
18647 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018648 **/
James Smart026abb82011-12-13 13:20:45 -050018649static uint32_t
18650lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018651{
18652 LPFC_MBOXQ_t *pmb = NULL;
18653 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018654 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018655 int rc;
18656
James Smart026abb82011-12-13 13:20:45 -050018657 if (!rgn23_data)
18658 return 0;
18659
James Smarta0c87cb2009-07-19 10:01:10 -040018660 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18661 if (!pmb) {
18662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018663 "2600 failed to allocate mailbox memory\n");
18664 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018665 }
18666 mb = &pmb->u.mb;
18667
James Smarta0c87cb2009-07-19 10:01:10 -040018668 do {
18669 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18670 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18671
18672 if (rc != MBX_SUCCESS) {
18673 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018674 "2601 failed to read config "
18675 "region 23, rc 0x%x Status 0x%x\n",
18676 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018677 mb->un.varDmp.word_cnt = 0;
18678 }
18679 /*
18680 * dump mem may return a zero when finished or we got a
18681 * mailbox error, either way we are done.
18682 */
18683 if (mb->un.varDmp.word_cnt == 0)
18684 break;
18685 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18686 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18687
18688 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018689 rgn23_data + offset,
18690 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018691 offset += mb->un.varDmp.word_cnt;
18692 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18693
James Smart026abb82011-12-13 13:20:45 -050018694 mempool_free(pmb, phba->mbox_mem_pool);
18695 return offset;
18696}
18697
18698/**
18699 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18700 * @phba: pointer to lpfc hba data structure.
18701 * @rgn23_data: pointer to configure region 23 data.
18702 *
18703 * This function gets SLI4 port configure region 23 data through memory dump
18704 * mailbox command. When it successfully retrieves data, the size of the data
18705 * will be returned, otherwise, 0 will be returned.
18706 **/
18707static uint32_t
18708lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18709{
18710 LPFC_MBOXQ_t *mboxq = NULL;
18711 struct lpfc_dmabuf *mp = NULL;
18712 struct lpfc_mqe *mqe;
18713 uint32_t data_length = 0;
18714 int rc;
18715
18716 if (!rgn23_data)
18717 return 0;
18718
18719 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18720 if (!mboxq) {
18721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18722 "3105 failed to allocate mailbox memory\n");
18723 return 0;
18724 }
18725
18726 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18727 goto out;
18728 mqe = &mboxq->u.mqe;
18729 mp = (struct lpfc_dmabuf *) mboxq->context1;
18730 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18731 if (rc)
18732 goto out;
18733 data_length = mqe->un.mb_words[5];
18734 if (data_length == 0)
18735 goto out;
18736 if (data_length > DMP_RGN23_SIZE) {
18737 data_length = 0;
18738 goto out;
18739 }
18740 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18741out:
18742 mempool_free(mboxq, phba->mbox_mem_pool);
18743 if (mp) {
18744 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18745 kfree(mp);
18746 }
18747 return data_length;
18748}
18749
18750/**
18751 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18752 * @phba: pointer to lpfc hba data structure.
18753 *
18754 * This function read region 23 and parse TLV for port status to
18755 * decide if the user disaled the port. If the TLV indicates the
18756 * port is disabled, the hba_flag is set accordingly.
18757 **/
18758void
18759lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18760{
18761 uint8_t *rgn23_data = NULL;
18762 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18763 uint32_t offset = 0;
18764
18765 /* Get adapter Region 23 data */
18766 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18767 if (!rgn23_data)
18768 goto out;
18769
18770 if (phba->sli_rev < LPFC_SLI_REV4)
18771 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18772 else {
18773 if_type = bf_get(lpfc_sli_intf_if_type,
18774 &phba->sli4_hba.sli_intf);
18775 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18776 goto out;
18777 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18778 }
James Smarta0c87cb2009-07-19 10:01:10 -040018779
18780 if (!data_size)
18781 goto out;
18782
18783 /* Check the region signature first */
18784 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18786 "2619 Config region 23 has bad signature\n");
18787 goto out;
18788 }
18789 offset += 4;
18790
18791 /* Check the data structure version */
18792 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18794 "2620 Config region 23 has bad version\n");
18795 goto out;
18796 }
18797 offset += 4;
18798
18799 /* Parse TLV entries in the region */
18800 while (offset < data_size) {
18801 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18802 break;
18803 /*
18804 * If the TLV is not driver specific TLV or driver id is
18805 * not linux driver id, skip the record.
18806 */
18807 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18808 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18809 (rgn23_data[offset + 3] != 0)) {
18810 offset += rgn23_data[offset + 1] * 4 + 4;
18811 continue;
18812 }
18813
18814 /* Driver found a driver specific TLV in the config region */
18815 sub_tlv_len = rgn23_data[offset + 1] * 4;
18816 offset += 4;
18817 tlv_offset = 0;
18818
18819 /*
18820 * Search for configured port state sub-TLV.
18821 */
18822 while ((offset < data_size) &&
18823 (tlv_offset < sub_tlv_len)) {
18824 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18825 offset += 4;
18826 tlv_offset += 4;
18827 break;
18828 }
18829 if (rgn23_data[offset] != PORT_STE_TYPE) {
18830 offset += rgn23_data[offset + 1] * 4 + 4;
18831 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18832 continue;
18833 }
18834
18835 /* This HBA contains PORT_STE configured */
18836 if (!rgn23_data[offset + 2])
18837 phba->hba_flag |= LINK_DISABLED;
18838
18839 goto out;
18840 }
18841 }
James Smart026abb82011-12-13 13:20:45 -050018842
James Smarta0c87cb2009-07-19 10:01:10 -040018843out:
James Smarta0c87cb2009-07-19 10:01:10 -040018844 kfree(rgn23_data);
18845 return;
18846}
James Smart695a8142010-01-26 23:08:03 -050018847
18848/**
James Smart52d52442011-05-24 11:42:45 -040018849 * lpfc_wr_object - write an object to the firmware
18850 * @phba: HBA structure that indicates port to create a queue on.
18851 * @dmabuf_list: list of dmabufs to write to the port.
18852 * @size: the total byte value of the objects to write to the port.
18853 * @offset: the current offset to be used to start the transfer.
18854 *
18855 * This routine will create a wr_object mailbox command to send to the port.
18856 * the mailbox command will be constructed using the dma buffers described in
18857 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18858 * BDEs that the imbedded mailbox can support. The @offset variable will be
18859 * used to indicate the starting offset of the transfer and will also return
18860 * the offset after the write object mailbox has completed. @size is used to
18861 * determine the end of the object and whether the eof bit should be set.
18862 *
18863 * Return 0 is successful and offset will contain the the new offset to use
18864 * for the next write.
18865 * Return negative value for error cases.
18866 **/
18867int
18868lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18869 uint32_t size, uint32_t *offset)
18870{
18871 struct lpfc_mbx_wr_object *wr_object;
18872 LPFC_MBOXQ_t *mbox;
18873 int rc = 0, i = 0;
18874 uint32_t shdr_status, shdr_add_status;
18875 uint32_t mbox_tmo;
18876 union lpfc_sli4_cfg_shdr *shdr;
18877 struct lpfc_dmabuf *dmabuf;
18878 uint32_t written = 0;
18879
18880 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18881 if (!mbox)
18882 return -ENOMEM;
18883
18884 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18885 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18886 sizeof(struct lpfc_mbx_wr_object) -
18887 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18888
18889 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18890 wr_object->u.request.write_offset = *offset;
18891 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18892 wr_object->u.request.object_name[0] =
18893 cpu_to_le32(wr_object->u.request.object_name[0]);
18894 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18895 list_for_each_entry(dmabuf, dmabuf_list, list) {
18896 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18897 break;
18898 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18899 wr_object->u.request.bde[i].addrHigh =
18900 putPaddrHigh(dmabuf->phys);
18901 if (written + SLI4_PAGE_SIZE >= size) {
18902 wr_object->u.request.bde[i].tus.f.bdeSize =
18903 (size - written);
18904 written += (size - written);
18905 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18906 } else {
18907 wr_object->u.request.bde[i].tus.f.bdeSize =
18908 SLI4_PAGE_SIZE;
18909 written += SLI4_PAGE_SIZE;
18910 }
18911 i++;
18912 }
18913 wr_object->u.request.bde_count = i;
18914 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18915 if (!phba->sli4_hba.intr_enable)
18916 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18917 else {
James Smarta183a152011-10-10 21:32:43 -040018918 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018919 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18920 }
18921 /* The IOCTL status is embedded in the mailbox subheader. */
18922 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18923 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18924 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18925 if (rc != MBX_TIMEOUT)
18926 mempool_free(mbox, phba->mbox_mem_pool);
18927 if (shdr_status || shdr_add_status || rc) {
18928 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18929 "3025 Write Object mailbox failed with "
18930 "status x%x add_status x%x, mbx status x%x\n",
18931 shdr_status, shdr_add_status, rc);
18932 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080018933 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040018934 } else
18935 *offset += wr_object->u.response.actual_write_length;
18936 return rc;
18937}
18938
18939/**
James Smart695a8142010-01-26 23:08:03 -050018940 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18941 * @vport: pointer to vport data structure.
18942 *
18943 * This function iterate through the mailboxq and clean up all REG_LOGIN
18944 * and REG_VPI mailbox commands associated with the vport. This function
18945 * is called when driver want to restart discovery of the vport due to
18946 * a Clear Virtual Link event.
18947 **/
18948void
18949lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18950{
18951 struct lpfc_hba *phba = vport->phba;
18952 LPFC_MBOXQ_t *mb, *nextmb;
18953 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018954 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018955 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018956 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018957 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018958 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018959
James Smartd439d282010-09-29 11:18:45 -040018960 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018961 spin_lock_irq(&phba->hbalock);
18962 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18963 if (mb->vport != vport)
18964 continue;
18965
18966 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18967 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18968 continue;
18969
James Smartd439d282010-09-29 11:18:45 -040018970 list_del(&mb->list);
18971 list_add_tail(&mb->list, &mbox_cmd_list);
18972 }
18973 /* Clean up active mailbox command with the vport */
18974 mb = phba->sli.mbox_active;
18975 if (mb && (mb->vport == vport)) {
18976 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18977 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18978 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18979 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18980 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18981 /* Put reference count for delayed processing */
18982 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18983 /* Unregister the RPI when mailbox complete */
18984 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18985 }
18986 }
James Smart63e801c2010-11-20 23:14:19 -050018987 /* Cleanup any mailbox completions which are not yet processed */
18988 do {
18989 restart_loop = 0;
18990 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18991 /*
18992 * If this mailox is already processed or it is
18993 * for another vport ignore it.
18994 */
18995 if ((mb->vport != vport) ||
18996 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18997 continue;
18998
18999 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19000 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19001 continue;
19002
19003 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19004 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19005 ndlp = (struct lpfc_nodelist *)mb->context2;
19006 /* Unregister the RPI when mailbox complete */
19007 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19008 restart_loop = 1;
19009 spin_unlock_irq(&phba->hbalock);
19010 spin_lock(shost->host_lock);
19011 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19012 spin_unlock(shost->host_lock);
19013 spin_lock_irq(&phba->hbalock);
19014 break;
19015 }
19016 }
19017 } while (restart_loop);
19018
James Smartd439d282010-09-29 11:18:45 -040019019 spin_unlock_irq(&phba->hbalock);
19020
19021 /* Release the cleaned-up mailbox commands */
19022 while (!list_empty(&mbox_cmd_list)) {
19023 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050019024 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19025 mp = (struct lpfc_dmabuf *) (mb->context1);
19026 if (mp) {
19027 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19028 kfree(mp);
19029 }
James Smart78730cf2010-04-06 15:06:30 -040019030 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040019031 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019032 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019033 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019034 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019035 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019036 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019037 }
James Smart695a8142010-01-26 23:08:03 -050019038 }
James Smart695a8142010-01-26 23:08:03 -050019039 mempool_free(mb, phba->mbox_mem_pool);
19040 }
James Smartd439d282010-09-29 11:18:45 -040019041
19042 /* Release the ndlp with the cleaned-up active mailbox command */
19043 if (act_mbx_ndlp) {
19044 spin_lock(shost->host_lock);
19045 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19046 spin_unlock(shost->host_lock);
19047 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019048 }
James Smart695a8142010-01-26 23:08:03 -050019049}
19050
James Smart2a9bf3d2010-06-07 15:24:45 -040019051/**
19052 * lpfc_drain_txq - Drain the txq
19053 * @phba: Pointer to HBA context object.
19054 *
19055 * This function attempt to submit IOCBs on the txq
19056 * to the adapter. For SLI4 adapters, the txq contains
19057 * ELS IOCBs that have been deferred because the there
19058 * are no SGLs. This congestion can occur with large
19059 * vport counts during node discovery.
19060 **/
19061
19062uint32_t
19063lpfc_drain_txq(struct lpfc_hba *phba)
19064{
19065 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019066 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019067 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019068 unsigned long iflags = 0;
19069 char *fail_msg = NULL;
19070 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019071 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019072 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070019073 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040019074
James Smartdc19e3b2018-05-24 21:08:57 -070019075 if (phba->link_flag & LS_MDS_LOOPBACK) {
19076 /* MDS WQE are posted only to first WQ*/
19077 wq = phba->sli4_hba.fcp_wq[0];
19078 if (unlikely(!wq))
19079 return 0;
19080 pring = wq->pring;
19081 } else {
19082 wq = phba->sli4_hba.els_wq;
19083 if (unlikely(!wq))
19084 return 0;
19085 pring = lpfc_phba_elsring(phba);
19086 }
19087
19088 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019089 return 0;
James Smart895427b2017-02-12 13:52:30 -080019090
James Smart398d81c2013-05-31 17:04:19 -040019091 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019092 list_for_each_entry(piocbq, &pring->txq, list) {
19093 txq_cnt++;
19094 }
19095
19096 if (txq_cnt > pring->txq_max)
19097 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019098
James Smart398d81c2013-05-31 17:04:19 -040019099 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019100
James Smart0e9bb8d2013-03-01 16:35:12 -050019101 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019102 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019103
James Smart19ca7602010-11-20 23:11:55 -050019104 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019105 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019106 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019107 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19108 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019109 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019110 break;
19111 }
James Smart895427b2017-02-12 13:52:30 -080019112 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019113 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019114 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019115 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019116 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019117 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019118 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019119
19120 /* The xri and iocb resources secured,
19121 * attempt to issue request
19122 */
James Smart6d368e52011-05-24 11:44:12 -040019123 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019124 piocbq->sli4_xritag = sglq->sli4_xritag;
19125 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19126 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019127 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019128 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070019129 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019130 fail_msg = " - Wq is full";
19131 else
19132 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19133
19134 if (fail_msg) {
19135 /* Failed means we can't issue and need to cancel */
19136 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19137 "2822 IOCB failed %s iotag 0x%x "
19138 "xri 0x%x\n",
19139 fail_msg,
19140 piocbq->iotag, piocbq->sli4_xritag);
19141 list_add_tail(&piocbq->list, &completions);
19142 }
James Smart398d81c2013-05-31 17:04:19 -040019143 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019144 }
19145
James Smart2a9bf3d2010-06-07 15:24:45 -040019146 /* Cancel all the IOCBs that cannot be issued */
19147 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19148 IOERR_SLI_ABORTED);
19149
James Smart0e9bb8d2013-03-01 16:35:12 -050019150 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019151}
James Smart895427b2017-02-12 13:52:30 -080019152
19153/**
19154 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19155 * @phba: Pointer to HBA context object.
19156 * @pwqe: Pointer to command WQE.
19157 * @sglq: Pointer to the scatter gather queue object.
19158 *
19159 * This routine converts the bpl or bde that is in the WQE
19160 * to a sgl list for the sli4 hardware. The physical address
19161 * of the bpl/bde is converted back to a virtual address.
19162 * If the WQE contains a BPL then the list of BDE's is
19163 * converted to sli4_sge's. If the WQE contains a single
19164 * BDE then it is converted to a single sli_sge.
19165 * The WQE is still in cpu endianness so the contents of
19166 * the bpl can be used without byte swapping.
19167 *
19168 * Returns valid XRI = Success, NO_XRI = Failure.
19169 */
19170static uint16_t
19171lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19172 struct lpfc_sglq *sglq)
19173{
19174 uint16_t xritag = NO_XRI;
19175 struct ulp_bde64 *bpl = NULL;
19176 struct ulp_bde64 bde;
19177 struct sli4_sge *sgl = NULL;
19178 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080019179 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080019180 int numBdes = 0;
19181 int i = 0;
19182 uint32_t offset = 0; /* accumulated offset in the sg request list */
19183 int inbound = 0; /* number of sg reply entries inbound from firmware */
19184 uint32_t cmd;
19185
19186 if (!pwqeq || !sglq)
19187 return xritag;
19188
19189 sgl = (struct sli4_sge *)sglq->sgl;
19190 wqe = &pwqeq->wqe;
19191 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19192
19193 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19194 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19195 return sglq->sli4_xritag;
19196 numBdes = pwqeq->rsvd2;
19197 if (numBdes) {
19198 /* The addrHigh and addrLow fields within the WQE
19199 * have not been byteswapped yet so there is no
19200 * need to swap them back.
19201 */
19202 if (pwqeq->context3)
19203 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19204 else
19205 return xritag;
19206
19207 bpl = (struct ulp_bde64 *)dmabuf->virt;
19208 if (!bpl)
19209 return xritag;
19210
19211 for (i = 0; i < numBdes; i++) {
19212 /* Should already be byte swapped. */
19213 sgl->addr_hi = bpl->addrHigh;
19214 sgl->addr_lo = bpl->addrLow;
19215
19216 sgl->word2 = le32_to_cpu(sgl->word2);
19217 if ((i+1) == numBdes)
19218 bf_set(lpfc_sli4_sge_last, sgl, 1);
19219 else
19220 bf_set(lpfc_sli4_sge_last, sgl, 0);
19221 /* swap the size field back to the cpu so we
19222 * can assign it to the sgl.
19223 */
19224 bde.tus.w = le32_to_cpu(bpl->tus.w);
19225 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19226 /* The offsets in the sgl need to be accumulated
19227 * separately for the request and reply lists.
19228 * The request is always first, the reply follows.
19229 */
19230 switch (cmd) {
19231 case CMD_GEN_REQUEST64_WQE:
19232 /* add up the reply sg entries */
19233 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19234 inbound++;
19235 /* first inbound? reset the offset */
19236 if (inbound == 1)
19237 offset = 0;
19238 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19239 bf_set(lpfc_sli4_sge_type, sgl,
19240 LPFC_SGE_TYPE_DATA);
19241 offset += bde.tus.f.bdeSize;
19242 break;
19243 case CMD_FCP_TRSP64_WQE:
19244 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19245 bf_set(lpfc_sli4_sge_type, sgl,
19246 LPFC_SGE_TYPE_DATA);
19247 break;
19248 case CMD_FCP_TSEND64_WQE:
19249 case CMD_FCP_TRECEIVE64_WQE:
19250 bf_set(lpfc_sli4_sge_type, sgl,
19251 bpl->tus.f.bdeFlags);
19252 if (i < 3)
19253 offset = 0;
19254 else
19255 offset += bde.tus.f.bdeSize;
19256 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19257 break;
19258 }
19259 sgl->word2 = cpu_to_le32(sgl->word2);
19260 bpl++;
19261 sgl++;
19262 }
19263 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19264 /* The addrHigh and addrLow fields of the BDE have not
19265 * been byteswapped yet so they need to be swapped
19266 * before putting them in the sgl.
19267 */
19268 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19269 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19270 sgl->word2 = le32_to_cpu(sgl->word2);
19271 bf_set(lpfc_sli4_sge_last, sgl, 1);
19272 sgl->word2 = cpu_to_le32(sgl->word2);
19273 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19274 }
19275 return sglq->sli4_xritag;
19276}
19277
19278/**
19279 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19280 * @phba: Pointer to HBA context object.
19281 * @ring_number: Base sli ring number
19282 * @pwqe: Pointer to command WQE.
19283 **/
19284int
19285lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19286 struct lpfc_iocbq *pwqe)
19287{
James Smart205e8242018-03-05 12:04:03 -080019288 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019289 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019290 struct lpfc_queue *wq;
19291 struct lpfc_sglq *sglq;
19292 struct lpfc_sli_ring *pring;
19293 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019294 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019295
19296 /* NVME_LS and NVME_LS ABTS requests. */
19297 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19298 pring = phba->sli4_hba.nvmels_wq->pring;
19299 spin_lock_irqsave(&pring->ring_lock, iflags);
19300 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19301 if (!sglq) {
19302 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19303 return WQE_BUSY;
19304 }
19305 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19306 pwqe->sli4_xritag = sglq->sli4_xritag;
19307 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19308 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19309 return WQE_ERROR;
19310 }
19311 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19312 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019313 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19314 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019315 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019316 return ret;
James Smart895427b2017-02-12 13:52:30 -080019317 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019318
James Smart895427b2017-02-12 13:52:30 -080019319 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19320 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19321 return 0;
19322 }
19323
19324 /* NVME_FCREQ and NVME_ABTS requests */
19325 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19326 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19327 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19328
19329 spin_lock_irqsave(&pring->ring_lock, iflags);
19330 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19331 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19332 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019333 ret = lpfc_sli4_wq_put(wq, wqe);
19334 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019335 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019336 return ret;
James Smart895427b2017-02-12 13:52:30 -080019337 }
19338 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19339 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19340 return 0;
19341 }
19342
James Smartf358dd02017-02-12 13:52:34 -080019343 /* NVMET requests */
19344 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19345 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19346 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19347
19348 spin_lock_irqsave(&pring->ring_lock, iflags);
19349 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019350 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019351 if (pwqe->sli4_xritag == NO_XRI) {
19352 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19353 pwqe->sli4_xritag = sglq->sli4_xritag;
19354 }
19355 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19356 pwqe->sli4_xritag);
19357 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19358 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19359 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019360 ret = lpfc_sli4_wq_put(wq, wqe);
19361 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019362 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019363 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019364 }
19365 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19366 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19367 return 0;
19368 }
James Smart895427b2017-02-12 13:52:30 -080019369 return WQE_ERROR;
19370}