blob: 9830bdb6e072625cd75e46d8b4b99845e739305d [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 Smart414abe02018-06-26 08:24:26 -07009120 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9121 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009122 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 Smart414abe02018-06-26 08:24:26 -07009184 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9185 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009186 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) {
James Smartfaa832e2018-07-31 17:23:18 -070010706 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
10707 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
10708 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
10709 spin_unlock_irq(&phba->hbalock);
10710 goto release_iocb;
10711 }
James Smart45ed1192009-10-02 15:17:02 -040010712 if (abort_iotag != 0 &&
10713 abort_iotag <= phba->sli.last_iotag)
10714 abort_iocb =
10715 phba->sli.iocbq_lookup[abort_iotag];
10716 } else
10717 /* For sli4 the abort_tag is the XRI,
10718 * so the abort routine puts the iotag of the iocb
10719 * being aborted in the context field of the abort
10720 * IOCB.
10721 */
10722 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010723
James Smart2a9bf3d2010-06-07 15:24:45 -040010724 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10725 "0327 Cannot abort els iocb %p "
10726 "with tag %x context %x, abort status %x, "
10727 "abort code %x\n",
10728 abort_iocb, abort_iotag, abort_context,
10729 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010730
James Smartff78d8f2011-12-13 13:21:35 -050010731 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010732 }
James Smartfaa832e2018-07-31 17:23:18 -070010733release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050010734 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010735 return;
10736}
10737
James Smarte59058c2008-08-24 21:49:00 -040010738/**
James Smart3621a712009-04-06 18:47:14 -040010739 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010740 * @phba: Pointer to HBA context object.
10741 * @cmdiocb: Pointer to driver command iocb object.
10742 * @rspiocb: Pointer to driver response iocb object.
10743 *
10744 * The function is called from SLI ring event handler with no
10745 * lock held. This function is the completion handler for ELS commands
10746 * which are aborted. The function frees memory resources used for
10747 * the aborted ELS commands.
10748 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010749static void
10750lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10751 struct lpfc_iocbq *rspiocb)
10752{
10753 IOCB_t *irsp = &rspiocb->iocb;
10754
10755 /* ELS cmd tag <ulpIoTag> completes */
10756 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010757 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010758 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010759 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010760 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010761 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10762 lpfc_ct_free_iocb(phba, cmdiocb);
10763 else
10764 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010765 return;
10766}
10767
James Smarte59058c2008-08-24 21:49:00 -040010768/**
James Smart5af5eee2010-10-22 11:06:38 -040010769 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010770 * @phba: Pointer to HBA context object.
10771 * @pring: Pointer to driver SLI ring object.
10772 * @cmdiocb: Pointer to driver command iocb object.
10773 *
James Smart5af5eee2010-10-22 11:06:38 -040010774 * This function issues an abort iocb for the provided command iocb down to
10775 * the port. Other than the case the outstanding command iocb is an abort
10776 * request, this function issues abort out unconditionally. This function is
10777 * called with hbalock held. The function returns 0 when it fails due to
10778 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010779 **/
James Smart5af5eee2010-10-22 11:06:38 -040010780static int
10781lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010782 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010783{
James Smart2e0fef82007-06-17 19:56:36 -050010784 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010785 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010786 IOCB_t *icmd = NULL;
10787 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010788 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010789 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070010790 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040010791
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010792 lockdep_assert_held(&phba->hbalock);
10793
James Smart92d7f7b2007-06-17 19:56:38 -050010794 /*
10795 * There are certain command types we don't want to abort. And we
10796 * don't want to abort commands that are already in the process of
10797 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010798 */
10799 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010800 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010801 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10802 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010803 return 0;
10804
dea31012005-04-17 16:05:31 -050010805 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010806 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010807 if (abtsiocbp == NULL)
10808 return 0;
dea31012005-04-17 16:05:31 -050010809
James Smart07951072007-04-25 09:51:38 -040010810 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010811 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010812 */
10813 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10814
dea31012005-04-17 16:05:31 -050010815 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010816 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10817 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010818 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010819 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010820 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070010821 } else {
James Smartda0436e2009-05-22 14:51:39 -040010822 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070010823 if (pring->ringno == LPFC_ELS_RING) {
10824 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
10825 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
10826 }
10827 }
dea31012005-04-17 16:05:31 -050010828 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010829 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010830
James Smart5ffc2662009-11-18 15:39:44 -050010831 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010832 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010833 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10834 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010835 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10836 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010837
James Smart2e0fef82007-06-17 19:56:36 -050010838 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010839 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10840 else
10841 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10842
10843 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010844 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010845
James Smarte8b62012007-08-02 11:10:09 -040010846 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10847 "0339 Abort xri x%x, original iotag x%x, "
10848 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010849 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010850 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010851 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010852
10853 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010854 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10855 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010856 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010857 /* Note: both hbalock and ring_lock need to be set here */
10858 spin_lock_irqsave(&pring->ring_lock, iflags);
10859 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10860 abtsiocbp, 0);
10861 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10862 } else {
10863 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10864 abtsiocbp, 0);
10865 }
James Smart07951072007-04-25 09:51:38 -040010866
James Smartd7c255b2008-08-24 21:50:00 -040010867 if (retval)
10868 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010869
10870 /*
10871 * Caller to this routine should check for IOCB_ERROR
10872 * and handle it properly. This routine no longer removes
10873 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10874 */
10875 return retval;
10876}
10877
10878/**
10879 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10880 * @phba: Pointer to HBA context object.
10881 * @pring: Pointer to driver SLI ring object.
10882 * @cmdiocb: Pointer to driver command iocb object.
10883 *
10884 * This function issues an abort iocb for the provided command iocb. In case
10885 * of unloading, the abort iocb will not be issued to commands on the ELS
10886 * ring. Instead, the callback function shall be changed to those commands
10887 * so that nothing happens when them finishes. This function is called with
10888 * hbalock held. The function returns 0 when the command iocb is an abort
10889 * request.
10890 **/
10891int
10892lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10893 struct lpfc_iocbq *cmdiocb)
10894{
10895 struct lpfc_vport *vport = cmdiocb->vport;
10896 int retval = IOCB_ERROR;
10897 IOCB_t *icmd = NULL;
10898
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010899 lockdep_assert_held(&phba->hbalock);
10900
James Smart5af5eee2010-10-22 11:06:38 -040010901 /*
10902 * There are certain command types we don't want to abort. And we
10903 * don't want to abort commands that are already in the process of
10904 * being aborted.
10905 */
10906 icmd = &cmdiocb->iocb;
10907 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10908 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10909 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10910 return 0;
10911
Dick Kennedy1234a6d2017-09-29 17:34:29 -070010912 if (!pring) {
10913 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10914 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10915 else
10916 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10917 goto abort_iotag_exit;
10918 }
10919
James Smart5af5eee2010-10-22 11:06:38 -040010920 /*
10921 * If we're unloading, don't abort iocb on the ELS ring, but change
10922 * the callback so that nothing happens when it finishes.
10923 */
10924 if ((vport->load_flag & FC_UNLOADING) &&
10925 (pring->ringno == LPFC_ELS_RING)) {
10926 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10927 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10928 else
10929 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10930 goto abort_iotag_exit;
10931 }
10932
10933 /* Now, we try to issue the abort to the cmdiocb out */
10934 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10935
James Smart07951072007-04-25 09:51:38 -040010936abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010937 /*
10938 * Caller to this routine should check for IOCB_ERROR
10939 * and handle it properly. This routine no longer removes
10940 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010941 */
James Smart2e0fef82007-06-17 19:56:36 -050010942 return retval;
dea31012005-04-17 16:05:31 -050010943}
10944
James Smarte59058c2008-08-24 21:49:00 -040010945/**
James Smart895427b2017-02-12 13:52:30 -080010946 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10947 * @phba: Pointer to HBA context object.
10948 * @pring: Pointer to driver SLI ring object.
10949 * @cmdiocb: Pointer to driver command iocb object.
10950 *
10951 * This function issues an abort iocb for the provided command iocb down to
10952 * the port. Other than the case the outstanding command iocb is an abort
10953 * request, this function issues abort out unconditionally. This function is
10954 * called with hbalock held. The function returns 0 when it fails due to
10955 * memory allocation failure or when the command iocb is an abort request.
10956 **/
10957static int
10958lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10959 struct lpfc_iocbq *cmdiocb)
10960{
10961 struct lpfc_vport *vport = cmdiocb->vport;
10962 struct lpfc_iocbq *abtsiocbp;
James Smart205e8242018-03-05 12:04:03 -080010963 union lpfc_wqe128 *abts_wqe;
James Smart895427b2017-02-12 13:52:30 -080010964 int retval;
10965
10966 /*
10967 * There are certain command types we don't want to abort. And we
10968 * don't want to abort commands that are already in the process of
10969 * being aborted.
10970 */
10971 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10972 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10973 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10974 return 0;
10975
10976 /* issue ABTS for this io based on iotag */
10977 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10978 if (abtsiocbp == NULL)
10979 return 0;
10980
10981 /* This signals the response to set the correct status
10982 * before calling the completion handler
10983 */
10984 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10985
10986 /* Complete prepping the abort wqe and issue to the FW. */
10987 abts_wqe = &abtsiocbp->wqe;
10988 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10989 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10990
10991 /* Explicitly set reserved fields to zero.*/
10992 abts_wqe->abort_cmd.rsrvd4 = 0;
10993 abts_wqe->abort_cmd.rsrvd5 = 0;
10994
10995 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10996 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10997 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10998
10999 /* word 7 */
11000 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
11001 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
11002 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
11003 cmdiocb->iocb.ulpClass);
11004
11005 /* word 8 - tell the FW to abort the IO associated with this
11006 * outstanding exchange ID.
11007 */
11008 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
11009
11010 /* word 9 - this is the iotag for the abts_wqe completion. */
11011 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
11012 abtsiocbp->iotag);
11013
11014 /* word 10 */
11015 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
11016 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
11017 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
11018
11019 /* word 11 */
11020 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11021 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
11022 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
11023
11024 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11025 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
11026 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080011027 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080011028 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070011029 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080011030 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11031 "6147 Failed abts issue_wqe with status x%x "
11032 "for oxid x%x\n",
11033 retval, cmdiocb->sli4_xritag);
11034 lpfc_sli_release_iocbq(phba, abtsiocbp);
11035 return retval;
11036 }
11037
11038 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11039 "6148 Drv Abort NVME Request Issued for "
11040 "ox_id x%x on reqtag x%x\n",
11041 cmdiocb->sli4_xritag,
11042 abtsiocbp->iotag);
11043
11044 return retval;
11045}
11046
11047/**
James Smart5af5eee2010-10-22 11:06:38 -040011048 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11049 * @phba: pointer to lpfc HBA data structure.
11050 *
11051 * This routine will abort all pending and outstanding iocbs to an HBA.
11052 **/
11053void
11054lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11055{
11056 struct lpfc_sli *psli = &phba->sli;
11057 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011058 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011059 int i;
11060
James Smart895427b2017-02-12 13:52:30 -080011061 if (phba->sli_rev != LPFC_SLI_REV4) {
11062 for (i = 0; i < psli->num_rings; i++) {
11063 pring = &psli->sli3_ring[i];
11064 lpfc_sli_abort_iocb_ring(phba, pring);
11065 }
11066 return;
11067 }
11068 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11069 pring = qp->pring;
11070 if (!pring)
11071 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011072 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011073 }
11074}
11075
11076/**
James Smart3621a712009-04-06 18:47:14 -040011077 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011078 * @iocbq: Pointer to driver iocb object.
11079 * @vport: Pointer to driver virtual port object.
11080 * @tgt_id: SCSI ID of the target.
11081 * @lun_id: LUN ID of the scsi device.
11082 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11083 *
James Smart3621a712009-04-06 18:47:14 -040011084 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011085 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11086 * 0 if the filtering criteria is met for the given iocb and will return
11087 * 1 if the filtering criteria is not met.
11088 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11089 * given iocb is for the SCSI device specified by vport, tgt_id and
11090 * lun_id parameter.
11091 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11092 * given iocb is for the SCSI target specified by vport and tgt_id
11093 * parameters.
11094 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11095 * given iocb is for the SCSI host associated with the given vport.
11096 * This function is called with no locks held.
11097 **/
dea31012005-04-17 16:05:31 -050011098static int
James Smart51ef4c22007-08-02 11:10:31 -040011099lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11100 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011101 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011102{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011103 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011104 int rc = 1;
11105
James Smartb0e83012018-06-26 08:24:29 -070011106 if (iocbq->vport != vport)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011107 return rc;
11108
James Smartb0e83012018-06-26 08:24:29 -070011109 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
11110 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
James Smart51ef4c22007-08-02 11:10:31 -040011111 return rc;
11112
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011113 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011114
James Smart495a7142008-06-14 22:52:59 -040011115 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011116 return rc;
11117
11118 switch (ctx_cmd) {
11119 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070011120 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011121 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11122 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011123 rc = 0;
11124 break;
11125 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070011126 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011127 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011128 rc = 0;
11129 break;
dea31012005-04-17 16:05:31 -050011130 case LPFC_CTX_HOST:
11131 rc = 0;
11132 break;
11133 default:
11134 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011135 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011136 break;
11137 }
11138
11139 return rc;
11140}
11141
James Smarte59058c2008-08-24 21:49:00 -040011142/**
James Smart3621a712009-04-06 18:47:14 -040011143 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011144 * @vport: Pointer to virtual port.
11145 * @tgt_id: SCSI ID of the target.
11146 * @lun_id: LUN ID of the scsi device.
11147 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11148 *
11149 * This function returns number of FCP commands pending for the vport.
11150 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11151 * commands pending on the vport associated with SCSI device specified
11152 * by tgt_id and lun_id parameters.
11153 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11154 * commands pending on the vport associated with SCSI target specified
11155 * by tgt_id parameter.
11156 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11157 * commands pending on the vport.
11158 * This function returns the number of iocbs which satisfy the filter.
11159 * This function is called without any lock held.
11160 **/
dea31012005-04-17 16:05:31 -050011161int
James Smart51ef4c22007-08-02 11:10:31 -040011162lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11163 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011164{
James Smart51ef4c22007-08-02 11:10:31 -040011165 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011166 struct lpfc_iocbq *iocbq;
11167 int sum, i;
dea31012005-04-17 16:05:31 -050011168
Johannes Thumshirn31979002016-07-18 16:06:03 +020011169 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011170 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11171 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011172
James Smart51ef4c22007-08-02 11:10:31 -040011173 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11174 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011175 sum++;
dea31012005-04-17 16:05:31 -050011176 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011177 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011178
dea31012005-04-17 16:05:31 -050011179 return sum;
11180}
11181
James Smarte59058c2008-08-24 21:49:00 -040011182/**
James Smart3621a712009-04-06 18:47:14 -040011183 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011184 * @phba: Pointer to HBA context object
11185 * @cmdiocb: Pointer to command iocb object.
11186 * @rspiocb: Pointer to response iocb object.
11187 *
11188 * This function is called when an aborted FCP iocb completes. This
11189 * function is called by the ring event handler with no lock held.
11190 * This function frees the iocb.
11191 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011192void
James Smart2e0fef82007-06-17 19:56:36 -050011193lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11194 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011195{
James Smartcb69f7d2011-12-13 13:21:57 -050011196 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011197 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011198 "original iotag x%x, abort cmd iotag x%x "
11199 "status 0x%x, reason 0x%x\n",
11200 cmdiocb->iocb.un.acxri.abortContextTag,
11201 cmdiocb->iocb.un.acxri.abortIoTag,
11202 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11203 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011204 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011205 return;
11206}
11207
James Smarte59058c2008-08-24 21:49:00 -040011208/**
James Smart3621a712009-04-06 18:47:14 -040011209 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011210 * @vport: Pointer to virtual port.
11211 * @pring: Pointer to driver SLI ring object.
11212 * @tgt_id: SCSI ID of the target.
11213 * @lun_id: LUN ID of the scsi device.
11214 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11215 *
11216 * This function sends an abort command for every SCSI command
11217 * associated with the given virtual port pending on the ring
11218 * filtered by lpfc_sli_validate_fcp_iocb function.
11219 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11220 * FCP iocbs associated with lun specified by tgt_id and lun_id
11221 * parameters
11222 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11223 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11224 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11225 * FCP iocbs associated with virtual port.
11226 * This function returns number of iocbs it failed to abort.
11227 * This function is called with no locks held.
11228 **/
dea31012005-04-17 16:05:31 -050011229int
James Smart51ef4c22007-08-02 11:10:31 -040011230lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11231 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011232{
James Smart51ef4c22007-08-02 11:10:31 -040011233 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011234 struct lpfc_iocbq *iocbq;
11235 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011236 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011237 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011238 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011239 int i;
dea31012005-04-17 16:05:31 -050011240
James Smartb0e83012018-06-26 08:24:29 -070011241 /* all I/Os are in process of being flushed */
11242 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH)
11243 return errcnt;
11244
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011245 for (i = 1; i <= phba->sli.last_iotag; i++) {
11246 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011247
James Smart51ef4c22007-08-02 11:10:31 -040011248 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011249 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011250 continue;
11251
James Smartafbd8d82013-09-06 12:22:13 -040011252 /*
11253 * If the iocbq is already being aborted, don't take a second
11254 * action, but do count it.
11255 */
11256 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11257 continue;
11258
dea31012005-04-17 16:05:31 -050011259 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011260 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011261 if (abtsiocb == NULL) {
11262 errcnt++;
11263 continue;
11264 }
dea31012005-04-17 16:05:31 -050011265
James Smartafbd8d82013-09-06 12:22:13 -040011266 /* indicate the IO is being aborted by the driver. */
11267 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11268
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011269 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011270 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11271 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011272 if (phba->sli_rev == LPFC_SLI_REV4)
11273 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11274 else
11275 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011276 abtsiocb->iocb.ulpLe = 1;
11277 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011278 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011279
James Smart5ffc2662009-11-18 15:39:44 -050011280 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011281 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011282 if (iocbq->iocb_flag & LPFC_IO_FCP)
11283 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011284 if (iocbq->iocb_flag & LPFC_IO_FOF)
11285 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011286
James Smart2e0fef82007-06-17 19:56:36 -050011287 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011288 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11289 else
11290 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11291
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011292 /* Setup callback routine and issue the command. */
11293 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011294 if (phba->sli_rev == LPFC_SLI_REV4) {
11295 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11296 if (!pring_s4)
11297 continue;
11298 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11299 abtsiocb, 0);
11300 } else
11301 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11302 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011303 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011304 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011305 errcnt++;
11306 continue;
11307 }
11308 }
11309
11310 return errcnt;
11311}
11312
James Smarte59058c2008-08-24 21:49:00 -040011313/**
James Smart98912dda2014-04-04 13:52:31 -040011314 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11315 * @vport: Pointer to virtual port.
11316 * @pring: Pointer to driver SLI ring object.
11317 * @tgt_id: SCSI ID of the target.
11318 * @lun_id: LUN ID of the scsi device.
11319 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11320 *
11321 * This function sends an abort command for every SCSI command
11322 * associated with the given virtual port pending on the ring
11323 * filtered by lpfc_sli_validate_fcp_iocb function.
11324 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11325 * FCP iocbs associated with lun specified by tgt_id and lun_id
11326 * parameters
11327 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11328 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11329 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11330 * FCP iocbs associated with virtual port.
11331 * This function returns number of iocbs it aborted .
11332 * This function is called with no locks held right after a taskmgmt
11333 * command is sent.
11334 **/
11335int
11336lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11337 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11338{
11339 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011340 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011341 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011342 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011343 struct lpfc_iocbq *iocbq;
11344 IOCB_t *icmd;
11345 int sum, i, ret_val;
11346 unsigned long iflags;
11347 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011348
James Smart59c68ea2018-04-09 14:24:25 -070011349 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011350
11351 /* all I/Os are in process of being flushed */
11352 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070011353 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011354 return 0;
11355 }
11356 sum = 0;
11357
11358 for (i = 1; i <= phba->sli.last_iotag; i++) {
11359 iocbq = phba->sli.iocbq_lookup[i];
11360
11361 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11362 cmd) != 0)
11363 continue;
11364
11365 /*
11366 * If the iocbq is already being aborted, don't take a second
11367 * action, but do count it.
11368 */
11369 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11370 continue;
11371
11372 /* issue ABTS for this IOCB based on iotag */
11373 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11374 if (abtsiocbq == NULL)
11375 continue;
11376
11377 icmd = &iocbq->iocb;
11378 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11379 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11380 if (phba->sli_rev == LPFC_SLI_REV4)
11381 abtsiocbq->iocb.un.acxri.abortIoTag =
11382 iocbq->sli4_xritag;
11383 else
11384 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11385 abtsiocbq->iocb.ulpLe = 1;
11386 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11387 abtsiocbq->vport = vport;
11388
11389 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011390 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011391 if (iocbq->iocb_flag & LPFC_IO_FCP)
11392 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011393 if (iocbq->iocb_flag & LPFC_IO_FOF)
11394 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011395
James Smart8c50d252014-09-03 12:58:16 -040011396 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11397 ndlp = lpfc_cmd->rdata->pnode;
11398
11399 if (lpfc_is_link_up(phba) &&
11400 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011401 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11402 else
11403 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11404
11405 /* Setup callback routine and issue the command. */
11406 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11407
11408 /*
11409 * Indicate the IO is being aborted by the driver and set
11410 * the caller's flag into the aborted IO.
11411 */
11412 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11413
11414 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart59c68ea2018-04-09 14:24:25 -070011415 pring_s4 = lpfc_sli4_calc_ring(phba, abtsiocbq);
11416 if (!pring_s4)
James Smart895427b2017-02-12 13:52:30 -080011417 continue;
James Smart98912dda2014-04-04 13:52:31 -040011418 /* Note: both hbalock and ring_lock must be set here */
James Smart59c68ea2018-04-09 14:24:25 -070011419 spin_lock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011420 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11421 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070011422 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011423 } else {
11424 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11425 abtsiocbq, 0);
11426 }
11427
11428
11429 if (ret_val == IOCB_ERROR)
11430 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11431 else
11432 sum++;
11433 }
James Smart59c68ea2018-04-09 14:24:25 -070011434 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011435 return sum;
11436}
11437
11438/**
James Smart3621a712009-04-06 18:47:14 -040011439 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011440 * @phba: Pointer to HBA context object.
11441 * @cmdiocbq: Pointer to command iocb.
11442 * @rspiocbq: Pointer to response iocb.
11443 *
11444 * This function is the completion handler for iocbs issued using
11445 * lpfc_sli_issue_iocb_wait function. This function is called by the
11446 * ring event handler function without any lock held. This function
11447 * can be called from both worker thread context and interrupt
11448 * context. This function also can be called from other thread which
11449 * cleans up the SLI layer objects.
11450 * This function copy the contents of the response iocb to the
11451 * response iocb memory object provided by the caller of
11452 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11453 * sleeps for the iocb completion.
11454 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011455static void
11456lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11457 struct lpfc_iocbq *cmdiocbq,
11458 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011459{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011460 wait_queue_head_t *pdone_q;
11461 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011462 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011463
James Smart2e0fef82007-06-17 19:56:36 -050011464 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011465 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11466
11467 /*
11468 * A time out has occurred for the iocb. If a time out
11469 * completion handler has been supplied, call it. Otherwise,
11470 * just free the iocbq.
11471 */
11472
11473 spin_unlock_irqrestore(&phba->hbalock, iflags);
11474 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11475 cmdiocbq->wait_iocb_cmpl = NULL;
11476 if (cmdiocbq->iocb_cmpl)
11477 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11478 else
11479 lpfc_sli_release_iocbq(phba, cmdiocbq);
11480 return;
11481 }
11482
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011483 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11484 if (cmdiocbq->context2 && rspiocbq)
11485 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11486 &rspiocbq->iocb, sizeof(IOCB_t));
11487
James Smart0f65ff62010-02-26 14:14:23 -050011488 /* Set the exchange busy flag for task management commands */
11489 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11490 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11491 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11492 cur_iocbq);
11493 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11494 }
11495
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011496 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011497 if (pdone_q)
11498 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011499 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011500 return;
11501}
11502
James Smarte59058c2008-08-24 21:49:00 -040011503/**
James Smartd11e31d2009-06-10 17:23:06 -040011504 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11505 * @phba: Pointer to HBA context object..
11506 * @piocbq: Pointer to command iocb.
11507 * @flag: Flag to test.
11508 *
11509 * This routine grabs the hbalock and then test the iocb_flag to
11510 * see if the passed in flag is set.
11511 * Returns:
11512 * 1 if flag is set.
11513 * 0 if flag is not set.
11514 **/
11515static int
11516lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11517 struct lpfc_iocbq *piocbq, uint32_t flag)
11518{
11519 unsigned long iflags;
11520 int ret;
11521
11522 spin_lock_irqsave(&phba->hbalock, iflags);
11523 ret = piocbq->iocb_flag & flag;
11524 spin_unlock_irqrestore(&phba->hbalock, iflags);
11525 return ret;
11526
11527}
11528
11529/**
James Smart3621a712009-04-06 18:47:14 -040011530 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011531 * @phba: Pointer to HBA context object..
11532 * @pring: Pointer to sli ring.
11533 * @piocb: Pointer to command iocb.
11534 * @prspiocbq: Pointer to response iocb.
11535 * @timeout: Timeout in number of seconds.
11536 *
11537 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011538 * iocb to complete. The iocb_cmpl field of the shall be used
11539 * to handle iocbs which time out. If the field is NULL, the
11540 * function shall free the iocbq structure. If more clean up is
11541 * needed, the caller is expected to provide a completion function
11542 * that will provide the needed clean up. If the iocb command is
11543 * not completed within timeout seconds, the function will either
11544 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11545 * completion function set in the iocb_cmpl field and then return
11546 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11547 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011548 * The function waits for the iocb completion using an
11549 * non-interruptible wait.
11550 * This function will sleep while waiting for iocb completion.
11551 * So, this function should not be called from any context which
11552 * does not allow sleeping. Due to the same reason, this function
11553 * cannot be called with interrupt disabled.
11554 * This function assumes that the iocb completions occur while
11555 * this function sleep. So, this function cannot be called from
11556 * the thread which process iocb completion for this ring.
11557 * This function clears the iocb_flag of the iocb object before
11558 * issuing the iocb and the iocb completion handler sets this
11559 * flag and wakes this thread when the iocb completes.
11560 * The contents of the response iocb will be copied to prspiocbq
11561 * by the completion handler when the command completes.
11562 * This function returns IOCB_SUCCESS when success.
11563 * This function is called with no lock held.
11564 **/
dea31012005-04-17 16:05:31 -050011565int
James Smart2e0fef82007-06-17 19:56:36 -050011566lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011567 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011568 struct lpfc_iocbq *piocb,
11569 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011570 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011571{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011572 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011573 long timeleft, timeout_req = 0;
11574 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011575 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011576 struct lpfc_iocbq *iocb;
11577 int txq_cnt = 0;
11578 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011579 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011580 unsigned long iflags;
11581 bool iocb_completed = true;
11582
James Smart895427b2017-02-12 13:52:30 -080011583 if (phba->sli_rev >= LPFC_SLI_REV4)
11584 pring = lpfc_sli4_calc_ring(phba, piocb);
11585 else
11586 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011587 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011588 * If the caller has provided a response iocbq buffer, then context2
11589 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011590 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011591 if (prspiocbq) {
11592 if (piocb->context2)
11593 return IOCB_ERROR;
11594 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011595 }
11596
James Smart5a0916b2013-07-15 18:31:42 -040011597 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011598 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11599 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011600 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011601
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011602 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011603 if (lpfc_readl(phba->HCregaddr, &creg_val))
11604 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011605 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11606 writel(creg_val, phba->HCregaddr);
11607 readl(phba->HCregaddr); /* flush */
11608 }
11609
James Smart2a9bf3d2010-06-07 15:24:45 -040011610 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11611 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011612 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011613 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011614 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011615 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011616 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011617 spin_lock_irqsave(&phba->hbalock, iflags);
11618 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011619
James Smart5a0916b2013-07-15 18:31:42 -040011620 /*
11621 * IOCB timed out. Inform the wake iocb wait
11622 * completion function and set local status
11623 */
11624
11625 iocb_completed = false;
11626 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11627 }
11628 spin_unlock_irqrestore(&phba->hbalock, iflags);
11629 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011630 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011631 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011632 /* Note: we are not indicating if the IOCB has a success
11633 * status or not - that's for the caller to check.
11634 * IOCB_SUCCESS means just that the command was sent and
11635 * completed. Not that it completed successfully.
11636 * */
James Smart7054a602007-04-25 09:52:34 -040011637 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011638 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011639 "0338 IOCB wait timeout error - no "
11640 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011641 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011642 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011643 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011644 "0330 IOCB wake NOT set, "
11645 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011646 timeout, (timeleft / jiffies));
11647 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011648 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011649 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011650 if (phba->cfg_log_verbose & LOG_SLI) {
11651 list_for_each_entry(iocb, &pring->txq, list) {
11652 txq_cnt++;
11653 }
11654 list_for_each_entry(iocb, &pring->txcmplq, list) {
11655 txcmplq_cnt++;
11656 }
11657 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11658 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11659 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11660 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011661 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011662 } else {
11663 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011664 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011665 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011666 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011667 }
11668
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011669 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011670 if (lpfc_readl(phba->HCregaddr, &creg_val))
11671 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011672 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11673 writel(creg_val, phba->HCregaddr);
11674 readl(phba->HCregaddr); /* flush */
11675 }
11676
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011677 if (prspiocbq)
11678 piocb->context2 = NULL;
11679
11680 piocb->context_un.wait_queue = NULL;
11681 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011682 return retval;
11683}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011684
James Smarte59058c2008-08-24 21:49:00 -040011685/**
James Smart3621a712009-04-06 18:47:14 -040011686 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011687 * @phba: Pointer to HBA context object.
11688 * @pmboxq: Pointer to driver mailbox object.
11689 * @timeout: Timeout in number of seconds.
11690 *
11691 * This function issues the mailbox to firmware and waits for the
11692 * mailbox command to complete. If the mailbox command is not
11693 * completed within timeout seconds, it returns MBX_TIMEOUT.
11694 * The function waits for the mailbox completion using an
11695 * interruptible wait. If the thread is woken up due to a
11696 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11697 * should not free the mailbox resources, if this function returns
11698 * MBX_TIMEOUT.
11699 * This function will sleep while waiting for mailbox completion.
11700 * So, this function should not be called from any context which
11701 * does not allow sleeping. Due to the same reason, this function
11702 * cannot be called with interrupt disabled.
11703 * This function assumes that the mailbox completion occurs while
11704 * this function sleep. So, this function cannot be called from
11705 * the worker thread which processes mailbox completion.
11706 * This function is called in the context of HBA management
11707 * applications.
11708 * This function returns MBX_SUCCESS when successful.
11709 * This function is called with no lock held.
11710 **/
dea31012005-04-17 16:05:31 -050011711int
James Smart2e0fef82007-06-17 19:56:36 -050011712lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011713 uint32_t timeout)
11714{
James Smarte29d74f2018-03-05 12:04:07 -080011715 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050011716 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011717 unsigned long flag;
dea31012005-04-17 16:05:31 -050011718
James Smart495a7142008-06-14 22:52:59 -040011719 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011720 /* setup wake call as IOCB callback */
11721 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050011722
James Smarte29d74f2018-03-05 12:04:07 -080011723 /* setup context3 field to pass wait_queue pointer to wake function */
11724 init_completion(&mbox_done);
11725 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050011726 /* now issue the command */
11727 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011728 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080011729 wait_for_completion_timeout(&mbox_done,
11730 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011731
James Smart858c9f62007-06-17 19:56:39 -050011732 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080011733 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040011734 /*
11735 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11736 * else do not free the resources.
11737 */
James Smartd7c47992010-06-08 18:31:54 -040011738 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011739 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011740 } else {
James Smart7054a602007-04-25 09:52:34 -040011741 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011742 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11743 }
11744 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050011745 }
dea31012005-04-17 16:05:31 -050011746 return retval;
11747}
11748
James Smarte59058c2008-08-24 21:49:00 -040011749/**
James Smart3772a992009-05-22 14:50:54 -040011750 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011751 * @phba: Pointer to HBA context.
11752 *
James Smart3772a992009-05-22 14:50:54 -040011753 * This function is called to shutdown the driver's mailbox sub-system.
11754 * It first marks the mailbox sub-system is in a block state to prevent
11755 * the asynchronous mailbox command from issued off the pending mailbox
11756 * command queue. If the mailbox command sub-system shutdown is due to
11757 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11758 * the mailbox sub-system flush routine to forcefully bring down the
11759 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11760 * as with offline or HBA function reset), this routine will wait for the
11761 * outstanding mailbox command to complete before invoking the mailbox
11762 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011763 **/
James Smart3772a992009-05-22 14:50:54 -040011764void
James Smart618a5232012-06-12 13:54:36 -040011765lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011766{
James Smart3772a992009-05-22 14:50:54 -040011767 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011768 unsigned long timeout;
11769
James Smart618a5232012-06-12 13:54:36 -040011770 if (mbx_action == LPFC_MBX_NO_WAIT) {
11771 /* delay 100ms for port state */
11772 msleep(100);
11773 lpfc_sli_mbox_sys_flush(phba);
11774 return;
11775 }
James Smarta183a152011-10-10 21:32:43 -040011776 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011777
James Smart3772a992009-05-22 14:50:54 -040011778 spin_lock_irq(&phba->hbalock);
11779 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011780
11781 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011782 /* Determine how long we might wait for the active mailbox
11783 * command to be gracefully completed by firmware.
11784 */
James Smarta183a152011-10-10 21:32:43 -040011785 if (phba->sli.mbox_active)
11786 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11787 phba->sli.mbox_active) *
11788 1000) + jiffies;
11789 spin_unlock_irq(&phba->hbalock);
11790
James Smart3772a992009-05-22 14:50:54 -040011791 while (phba->sli.mbox_active) {
11792 /* Check active mailbox complete status every 2ms */
11793 msleep(2);
11794 if (time_after(jiffies, timeout))
11795 /* Timeout, let the mailbox flush routine to
11796 * forcefully release active mailbox command
11797 */
11798 break;
11799 }
James Smartd7069f02012-03-01 22:36:29 -050011800 } else
11801 spin_unlock_irq(&phba->hbalock);
11802
James Smart3772a992009-05-22 14:50:54 -040011803 lpfc_sli_mbox_sys_flush(phba);
11804}
11805
11806/**
11807 * lpfc_sli_eratt_read - read sli-3 error attention events
11808 * @phba: Pointer to HBA context.
11809 *
11810 * This function is called to read the SLI3 device error attention registers
11811 * for possible error attention events. The caller must hold the hostlock
11812 * with spin_lock_irq().
11813 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011814 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011815 * Register and returns 0 otherwise.
11816 **/
11817static int
11818lpfc_sli_eratt_read(struct lpfc_hba *phba)
11819{
James Smarted957682007-06-17 19:56:37 -050011820 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011821
James Smart3772a992009-05-22 14:50:54 -040011822 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011823 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11824 goto unplug_err;
11825
James Smart3772a992009-05-22 14:50:54 -040011826 if (ha_copy & HA_ERATT) {
11827 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011828 if (lpfc_sli_read_hs(phba))
11829 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011830
James Smart3772a992009-05-22 14:50:54 -040011831 /* Check if there is a deferred error condition is active */
11832 if ((HS_FFER1 & phba->work_hs) &&
11833 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011834 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011835 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011836 /* Clear all interrupt enable conditions */
11837 writel(0, phba->HCregaddr);
11838 readl(phba->HCregaddr);
11839 }
11840
11841 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011842 phba->work_ha |= HA_ERATT;
11843 /* Indicate polling handles this ERATT */
11844 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011845 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011846 }
James Smart3772a992009-05-22 14:50:54 -040011847 return 0;
James Smart9940b972011-03-11 16:06:12 -050011848
11849unplug_err:
11850 /* Set the driver HS work bitmap */
11851 phba->work_hs |= UNPLUG_ERR;
11852 /* Set the driver HA work bitmap */
11853 phba->work_ha |= HA_ERATT;
11854 /* Indicate polling handles this ERATT */
11855 phba->hba_flag |= HBA_ERATT_HANDLED;
11856 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011857}
11858
James Smarte59058c2008-08-24 21:49:00 -040011859/**
James Smartda0436e2009-05-22 14:51:39 -040011860 * lpfc_sli4_eratt_read - read sli-4 error attention events
11861 * @phba: Pointer to HBA context.
11862 *
11863 * This function is called to read the SLI4 device error attention registers
11864 * for possible error attention events. The caller must hold the hostlock
11865 * with spin_lock_irq().
11866 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011867 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011868 * Register and returns 0 otherwise.
11869 **/
11870static int
11871lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11872{
11873 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011874 uint32_t if_type, portsmphr;
11875 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011876
James Smart2fcee4b2010-12-15 17:57:46 -050011877 /*
11878 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011879 * registers for error attention. This can be changed later.
11880 */
James Smart2fcee4b2010-12-15 17:57:46 -050011881 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11882 switch (if_type) {
11883 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011884 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11885 &uerr_sta_lo) ||
11886 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11887 &uerr_sta_hi)) {
11888 phba->work_hs |= UNPLUG_ERR;
11889 phba->work_ha |= HA_ERATT;
11890 phba->hba_flag |= HBA_ERATT_HANDLED;
11891 return 1;
11892 }
James Smart2fcee4b2010-12-15 17:57:46 -050011893 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11894 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11895 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11896 "1423 HBA Unrecoverable error: "
11897 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11898 "ue_mask_lo_reg=0x%x, "
11899 "ue_mask_hi_reg=0x%x\n",
11900 uerr_sta_lo, uerr_sta_hi,
11901 phba->sli4_hba.ue_mask_lo,
11902 phba->sli4_hba.ue_mask_hi);
11903 phba->work_status[0] = uerr_sta_lo;
11904 phba->work_status[1] = uerr_sta_hi;
11905 phba->work_ha |= HA_ERATT;
11906 phba->hba_flag |= HBA_ERATT_HANDLED;
11907 return 1;
11908 }
11909 break;
11910 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080011911 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050011912 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11913 &portstat_reg.word0) ||
11914 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11915 &portsmphr)){
11916 phba->work_hs |= UNPLUG_ERR;
11917 phba->work_ha |= HA_ERATT;
11918 phba->hba_flag |= HBA_ERATT_HANDLED;
11919 return 1;
11920 }
James Smart2fcee4b2010-12-15 17:57:46 -050011921 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11922 phba->work_status[0] =
11923 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11924 phba->work_status[1] =
11925 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11926 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011927 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011928 "port status reg 0x%x, "
11929 "port smphr reg 0x%x, "
11930 "error 1=0x%x, error 2=0x%x\n",
11931 portstat_reg.word0,
11932 portsmphr,
11933 phba->work_status[0],
11934 phba->work_status[1]);
11935 phba->work_ha |= HA_ERATT;
11936 phba->hba_flag |= HBA_ERATT_HANDLED;
11937 return 1;
11938 }
11939 break;
11940 case LPFC_SLI_INTF_IF_TYPE_1:
11941 default:
James Smarta747c9c2009-11-18 15:41:10 -050011942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011943 "2886 HBA Error Attention on unsupported "
11944 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011945 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011946 }
James Smart2fcee4b2010-12-15 17:57:46 -050011947
James Smartda0436e2009-05-22 14:51:39 -040011948 return 0;
11949}
11950
11951/**
James Smart3621a712009-04-06 18:47:14 -040011952 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011953 * @phba: Pointer to HBA context.
11954 *
James Smart3772a992009-05-22 14:50:54 -040011955 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011956 * error attention register bit for error attention events.
11957 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011958 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011959 * Register and returns 0 otherwise.
11960 **/
11961int
11962lpfc_sli_check_eratt(struct lpfc_hba *phba)
11963{
11964 uint32_t ha_copy;
11965
11966 /* If somebody is waiting to handle an eratt, don't process it
11967 * here. The brdkill function will do this.
11968 */
11969 if (phba->link_flag & LS_IGNORE_ERATT)
11970 return 0;
11971
11972 /* Check if interrupt handler handles this ERATT */
11973 spin_lock_irq(&phba->hbalock);
11974 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11975 /* Interrupt handler has handled ERATT */
11976 spin_unlock_irq(&phba->hbalock);
11977 return 0;
11978 }
11979
James Smarta257bf92009-04-06 18:48:10 -040011980 /*
11981 * If there is deferred error attention, do not check for error
11982 * attention
11983 */
11984 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11985 spin_unlock_irq(&phba->hbalock);
11986 return 0;
11987 }
11988
James Smart3772a992009-05-22 14:50:54 -040011989 /* If PCI channel is offline, don't process it */
11990 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011991 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011992 return 0;
11993 }
11994
11995 switch (phba->sli_rev) {
11996 case LPFC_SLI_REV2:
11997 case LPFC_SLI_REV3:
11998 /* Read chip Host Attention (HA) register */
11999 ha_copy = lpfc_sli_eratt_read(phba);
12000 break;
James Smartda0436e2009-05-22 14:51:39 -040012001 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050012002 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040012003 ha_copy = lpfc_sli4_eratt_read(phba);
12004 break;
James Smart3772a992009-05-22 14:50:54 -040012005 default:
12006 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12007 "0299 Invalid SLI revision (%d)\n",
12008 phba->sli_rev);
12009 ha_copy = 0;
12010 break;
James Smart93996272008-08-24 21:50:30 -040012011 }
12012 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012013
12014 return ha_copy;
12015}
12016
12017/**
12018 * lpfc_intr_state_check - Check device state for interrupt handling
12019 * @phba: Pointer to HBA context.
12020 *
12021 * This inline routine checks whether a device or its PCI slot is in a state
12022 * that the interrupt should be handled.
12023 *
12024 * This function returns 0 if the device or the PCI slot is in a state that
12025 * interrupt should be handled, otherwise -EIO.
12026 */
12027static inline int
12028lpfc_intr_state_check(struct lpfc_hba *phba)
12029{
12030 /* If the pci channel is offline, ignore all the interrupts */
12031 if (unlikely(pci_channel_offline(phba->pcidev)))
12032 return -EIO;
12033
12034 /* Update device level interrupt statistics */
12035 phba->sli.slistat.sli_intr++;
12036
12037 /* Ignore all interrupts during initialization. */
12038 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12039 return -EIO;
12040
James Smart93996272008-08-24 21:50:30 -040012041 return 0;
12042}
12043
12044/**
James Smart3772a992009-05-22 14:50:54 -040012045 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012046 * @irq: Interrupt number.
12047 * @dev_id: The device context pointer.
12048 *
James Smart93996272008-08-24 21:50:30 -040012049 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012050 * service routine when device with SLI-3 interface spec is enabled with
12051 * MSI-X multi-message interrupt mode and there are slow-path events in
12052 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12053 * interrupt mode, this function is called as part of the device-level
12054 * interrupt handler. When the PCI slot is in error recovery or the HBA
12055 * is undergoing initialization, the interrupt handler will not process
12056 * the interrupt. The link attention and ELS ring attention events are
12057 * handled by the worker thread. The interrupt handler signals the worker
12058 * thread and returns for these events. This function is called without
12059 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012060 * structures.
12061 *
12062 * This function returns IRQ_HANDLED when interrupt is handled else it
12063 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012064 **/
dea31012005-04-17 16:05:31 -050012065irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012066lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012067{
James Smart2e0fef82007-06-17 19:56:36 -050012068 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012069 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012070 uint32_t work_ha_copy;
12071 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012072 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012073 uint32_t control;
12074
James Smart92d7f7b2007-06-17 19:56:38 -050012075 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012076 struct lpfc_vport *vport;
12077 struct lpfc_nodelist *ndlp;
12078 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012079 LPFC_MBOXQ_t *pmb;
12080 int rc;
12081
dea31012005-04-17 16:05:31 -050012082 /*
12083 * Get the driver's phba structure from the dev_id and
12084 * assume the HBA is not interrupting.
12085 */
James Smart93996272008-08-24 21:50:30 -040012086 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012087
12088 if (unlikely(!phba))
12089 return IRQ_NONE;
12090
dea31012005-04-17 16:05:31 -050012091 /*
James Smart93996272008-08-24 21:50:30 -040012092 * Stuff needs to be attented to when this function is invoked as an
12093 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012094 */
James Smart93996272008-08-24 21:50:30 -040012095 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012096 /* Check device state for handling interrupt */
12097 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012098 return IRQ_NONE;
12099 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012100 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012101 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12102 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012103 /* If somebody is waiting to handle an eratt don't process it
12104 * here. The brdkill function will do this.
12105 */
12106 if (phba->link_flag & LS_IGNORE_ERATT)
12107 ha_copy &= ~HA_ERATT;
12108 /* Check the need for handling ERATT in interrupt handler */
12109 if (ha_copy & HA_ERATT) {
12110 if (phba->hba_flag & HBA_ERATT_HANDLED)
12111 /* ERATT polling has handled ERATT */
12112 ha_copy &= ~HA_ERATT;
12113 else
12114 /* Indicate interrupt handler handles ERATT */
12115 phba->hba_flag |= HBA_ERATT_HANDLED;
12116 }
James Smarta257bf92009-04-06 18:48:10 -040012117
12118 /*
12119 * If there is deferred error attention, do not check for any
12120 * interrupt.
12121 */
12122 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012123 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012124 return IRQ_NONE;
12125 }
12126
James Smart93996272008-08-24 21:50:30 -040012127 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012128 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12129 goto unplug_error;
12130
James Smarta747c9c2009-11-18 15:41:10 -050012131 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12132 HC_LAINT_ENA | HC_ERINT_ENA),
12133 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012134 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12135 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012136 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012137 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012138 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012139 } else
12140 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012141
dea31012005-04-17 16:05:31 -050012142 work_ha_copy = ha_copy & phba->work_ha_mask;
12143
James Smart93996272008-08-24 21:50:30 -040012144 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012145 if (work_ha_copy & HA_LATT) {
12146 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12147 /*
12148 * Turn off Link Attention interrupts
12149 * until CLEAR_LA done
12150 */
James Smart5b75da22008-12-04 22:39:35 -050012151 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012152 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012153 if (lpfc_readl(phba->HCregaddr, &control))
12154 goto unplug_error;
dea31012005-04-17 16:05:31 -050012155 control &= ~HC_LAINT_ENA;
12156 writel(control, phba->HCregaddr);
12157 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012158 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012159 }
12160 else
12161 work_ha_copy &= ~HA_LATT;
12162 }
12163
James Smart93996272008-08-24 21:50:30 -040012164 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012165 /*
12166 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12167 * the only slow ring.
12168 */
12169 status = (work_ha_copy &
12170 (HA_RXMASK << (4*LPFC_ELS_RING)));
12171 status >>= (4*LPFC_ELS_RING);
12172 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012173 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012174 if (lpfc_readl(phba->HCregaddr, &control))
12175 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012176
12177 lpfc_debugfs_slow_ring_trc(phba,
12178 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12179 control, status,
12180 (uint32_t)phba->sli.slistat.sli_intr);
12181
James Smart858c9f62007-06-17 19:56:39 -050012182 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012183 lpfc_debugfs_slow_ring_trc(phba,
12184 "ISR Disable ring:"
12185 "pwork:x%x hawork:x%x wait:x%x",
12186 phba->work_ha, work_ha_copy,
12187 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012188 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012189
James Smart858c9f62007-06-17 19:56:39 -050012190 control &=
12191 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012192 writel(control, phba->HCregaddr);
12193 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012194 }
James Smarta58cbd52007-08-02 11:09:43 -040012195 else {
12196 lpfc_debugfs_slow_ring_trc(phba,
12197 "ISR slow ring: pwork:"
12198 "x%x hawork:x%x wait:x%x",
12199 phba->work_ha, work_ha_copy,
12200 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012201 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012202 }
James Smart5b75da22008-12-04 22:39:35 -050012203 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012204 }
12205 }
James Smart5b75da22008-12-04 22:39:35 -050012206 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012207 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012208 if (lpfc_sli_read_hs(phba))
12209 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012210 /*
12211 * Check if there is a deferred error condition
12212 * is active
12213 */
12214 if ((HS_FFER1 & phba->work_hs) &&
12215 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012216 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12217 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012218 phba->hba_flag |= DEFER_ERATT;
12219 /* Clear all interrupt enable conditions */
12220 writel(0, phba->HCregaddr);
12221 readl(phba->HCregaddr);
12222 }
12223 }
12224
James Smart93996272008-08-24 21:50:30 -040012225 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012226 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012227 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012228 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012229 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012230
12231 /* First check out the status word */
12232 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12233 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012234 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012235 /*
12236 * Stray Mailbox Interrupt, mbxCommand <cmd>
12237 * mbxStatus <status>
12238 */
James Smart09372822008-01-11 01:52:54 -050012239 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012240 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012241 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012242 "Interrupt mbxCommand x%x "
12243 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012244 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012245 pmbox->mbxCommand,
12246 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012247 /* clear mailbox attention bit */
12248 work_ha_copy &= ~HA_MBATT;
12249 } else {
James Smart97eab632008-04-07 10:16:05 -040012250 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012251 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012252 phba->last_completion_time = jiffies;
12253 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012254 if (pmb->mbox_cmpl) {
12255 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12256 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012257 if (pmb->out_ext_byte_len &&
12258 pmb->context2)
12259 lpfc_sli_pcimem_bcopy(
12260 phba->mbox_ext,
12261 pmb->context2,
12262 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012263 }
James Smart09372822008-01-11 01:52:54 -050012264 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12265 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12266
12267 lpfc_debugfs_disc_trc(vport,
12268 LPFC_DISC_TRC_MBOX_VPORT,
12269 "MBOX dflt rpi: : "
12270 "status:x%x rpi:x%x",
12271 (uint32_t)pmbox->mbxStatus,
12272 pmbox->un.varWords[0], 0);
12273
12274 if (!pmbox->mbxStatus) {
12275 mp = (struct lpfc_dmabuf *)
12276 (pmb->context1);
12277 ndlp = (struct lpfc_nodelist *)
12278 pmb->context2;
12279
12280 /* Reg_LOGIN of dflt RPI was
12281 * successful. new lets get
12282 * rid of the RPI using the
12283 * same mbox buffer.
12284 */
12285 lpfc_unreg_login(phba,
12286 vport->vpi,
12287 pmbox->un.varWords[0],
12288 pmb);
12289 pmb->mbox_cmpl =
12290 lpfc_mbx_cmpl_dflt_rpi;
12291 pmb->context1 = mp;
12292 pmb->context2 = ndlp;
12293 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012294 rc = lpfc_sli_issue_mbox(phba,
12295 pmb,
12296 MBX_NOWAIT);
12297 if (rc != MBX_BUSY)
12298 lpfc_printf_log(phba,
12299 KERN_ERR,
12300 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012301 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012302 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012303 if (rc != MBX_NOT_FINISHED)
12304 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012305 }
12306 }
James Smart5b75da22008-12-04 22:39:35 -050012307 spin_lock_irqsave(
12308 &phba->pport->work_port_lock,
12309 iflag);
James Smart09372822008-01-11 01:52:54 -050012310 phba->pport->work_port_events &=
12311 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012312 spin_unlock_irqrestore(
12313 &phba->pport->work_port_lock,
12314 iflag);
James Smart09372822008-01-11 01:52:54 -050012315 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012316 }
James Smart97eab632008-04-07 10:16:05 -040012317 } else
James Smart5b75da22008-12-04 22:39:35 -050012318 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012319
James Smart92d7f7b2007-06-17 19:56:38 -050012320 if ((work_ha_copy & HA_MBATT) &&
12321 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012322send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012323 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012324 do {
12325 rc = lpfc_sli_issue_mbox(phba, NULL,
12326 MBX_NOWAIT);
12327 } while (rc == MBX_NOT_FINISHED);
12328 if (rc != MBX_SUCCESS)
12329 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12330 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012331 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012332 }
12333
James Smart5b75da22008-12-04 22:39:35 -050012334 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012335 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012336 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012337 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012338 }
James Smart93996272008-08-24 21:50:30 -040012339 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012340unplug_error:
12341 spin_unlock_irqrestore(&phba->hbalock, iflag);
12342 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012343
James Smart3772a992009-05-22 14:50:54 -040012344} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012345
12346/**
James Smart3772a992009-05-22 14:50:54 -040012347 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012348 * @irq: Interrupt number.
12349 * @dev_id: The device context pointer.
12350 *
12351 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012352 * service routine when device with SLI-3 interface spec is enabled with
12353 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12354 * ring event in the HBA. However, when the device is enabled with either
12355 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12356 * device-level interrupt handler. When the PCI slot is in error recovery
12357 * or the HBA is undergoing initialization, the interrupt handler will not
12358 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12359 * the intrrupt context. This function is called without any lock held.
12360 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012361 *
12362 * This function returns IRQ_HANDLED when interrupt is handled else it
12363 * returns IRQ_NONE.
12364 **/
12365irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012366lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012367{
12368 struct lpfc_hba *phba;
12369 uint32_t ha_copy;
12370 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012371 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012372 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012373
12374 /* Get the driver's phba structure from the dev_id and
12375 * assume the HBA is not interrupting.
12376 */
12377 phba = (struct lpfc_hba *) dev_id;
12378
12379 if (unlikely(!phba))
12380 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012381
12382 /*
James Smart93996272008-08-24 21:50:30 -040012383 * Stuff needs to be attented to when this function is invoked as an
12384 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012385 */
James Smart93996272008-08-24 21:50:30 -040012386 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012387 /* Check device state for handling interrupt */
12388 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012389 return IRQ_NONE;
12390 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012391 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12392 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012393 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012394 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012395 /*
12396 * If there is deferred error attention, do not check for
12397 * any interrupt.
12398 */
12399 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012400 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012401 return IRQ_NONE;
12402 }
James Smart93996272008-08-24 21:50:30 -040012403 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12404 phba->HAregaddr);
12405 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012406 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012407 } else
12408 ha_copy = phba->ha_copy;
12409
12410 /*
12411 * Process all events on FCP ring. Take the optimized path for FCP IO.
12412 */
12413 ha_copy &= ~(phba->work_ha_mask);
12414
12415 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012416 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012417 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012418 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012419 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012420
12421 if (phba->cfg_multi_ring_support == 2) {
12422 /*
James Smart93996272008-08-24 21:50:30 -040012423 * Process all events on extra ring. Take the optimized path
12424 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012425 */
James Smart93996272008-08-24 21:50:30 -040012426 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012427 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012428 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012429 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012430 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012431 status);
12432 }
12433 }
dea31012005-04-17 16:05:31 -050012434 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012435} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012436
James Smart93996272008-08-24 21:50:30 -040012437/**
James Smart3772a992009-05-22 14:50:54 -040012438 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012439 * @irq: Interrupt number.
12440 * @dev_id: The device context pointer.
12441 *
James Smart3772a992009-05-22 14:50:54 -040012442 * This function is the HBA device-level interrupt handler to device with
12443 * SLI-3 interface spec, called from the PCI layer when either MSI or
12444 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12445 * requires driver attention. This function invokes the slow-path interrupt
12446 * attention handling function and fast-path interrupt attention handling
12447 * function in turn to process the relevant HBA attention events. This
12448 * function is called without any lock held. It gets the hbalock to access
12449 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012450 *
12451 * This function returns IRQ_HANDLED when interrupt is handled, else it
12452 * returns IRQ_NONE.
12453 **/
12454irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012455lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012456{
12457 struct lpfc_hba *phba;
12458 irqreturn_t sp_irq_rc, fp_irq_rc;
12459 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012460 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012461
12462 /*
12463 * Get the driver's phba structure from the dev_id and
12464 * assume the HBA is not interrupting.
12465 */
12466 phba = (struct lpfc_hba *) dev_id;
12467
12468 if (unlikely(!phba))
12469 return IRQ_NONE;
12470
James Smart3772a992009-05-22 14:50:54 -040012471 /* Check device state for handling interrupt */
12472 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012473 return IRQ_NONE;
12474
12475 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012476 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12477 spin_unlock(&phba->hbalock);
12478 return IRQ_HANDLED;
12479 }
12480
James Smart93996272008-08-24 21:50:30 -040012481 if (unlikely(!phba->ha_copy)) {
12482 spin_unlock(&phba->hbalock);
12483 return IRQ_NONE;
12484 } else if (phba->ha_copy & HA_ERATT) {
12485 if (phba->hba_flag & HBA_ERATT_HANDLED)
12486 /* ERATT polling has handled ERATT */
12487 phba->ha_copy &= ~HA_ERATT;
12488 else
12489 /* Indicate interrupt handler handles ERATT */
12490 phba->hba_flag |= HBA_ERATT_HANDLED;
12491 }
12492
James Smarta257bf92009-04-06 18:48:10 -040012493 /*
12494 * If there is deferred error attention, do not check for any interrupt.
12495 */
12496 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012497 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012498 return IRQ_NONE;
12499 }
12500
James Smart93996272008-08-24 21:50:30 -040012501 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012502 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12503 spin_unlock(&phba->hbalock);
12504 return IRQ_HANDLED;
12505 }
James Smarta747c9c2009-11-18 15:41:10 -050012506 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12507 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12508 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012509 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012510 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012511 readl(phba->HAregaddr); /* flush */
12512 spin_unlock(&phba->hbalock);
12513
12514 /*
12515 * Invokes slow-path host attention interrupt handling as appropriate.
12516 */
12517
12518 /* status of events with mailbox and link attention */
12519 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12520
12521 /* status of events with ELS ring */
12522 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12523 status2 >>= (4*LPFC_ELS_RING);
12524
12525 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012526 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012527 else
12528 sp_irq_rc = IRQ_NONE;
12529
12530 /*
12531 * Invoke fast-path host attention interrupt handling as appropriate.
12532 */
12533
12534 /* status of events with FCP ring */
12535 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12536 status1 >>= (4*LPFC_FCP_RING);
12537
12538 /* status of events with extra ring */
12539 if (phba->cfg_multi_ring_support == 2) {
12540 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12541 status2 >>= (4*LPFC_EXTRA_RING);
12542 } else
12543 status2 = 0;
12544
12545 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012546 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012547 else
12548 fp_irq_rc = IRQ_NONE;
12549
12550 /* Return device-level interrupt handling status */
12551 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012552} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012553
12554/**
12555 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12556 * @phba: pointer to lpfc hba data structure.
12557 *
12558 * This routine is invoked by the worker thread to process all the pending
12559 * SLI4 FCP abort XRI events.
12560 **/
12561void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12562{
12563 struct lpfc_cq_event *cq_event;
12564
12565 /* First, declare the fcp xri abort event has been handled */
12566 spin_lock_irq(&phba->hbalock);
12567 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12568 spin_unlock_irq(&phba->hbalock);
12569 /* Now, handle all the fcp xri abort events */
12570 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12571 /* Get the first event from the head of the event queue */
12572 spin_lock_irq(&phba->hbalock);
12573 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12574 cq_event, struct lpfc_cq_event, list);
12575 spin_unlock_irq(&phba->hbalock);
12576 /* Notify aborted XRI for FCP work queue */
12577 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12578 /* Free the event processed back to the free pool */
12579 lpfc_sli4_cq_event_release(phba, cq_event);
12580 }
12581}
12582
12583/**
12584 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12585 * @phba: pointer to lpfc hba data structure.
12586 *
12587 * This routine is invoked by the worker thread to process all the pending
12588 * SLI4 els abort xri events.
12589 **/
12590void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12591{
12592 struct lpfc_cq_event *cq_event;
12593
12594 /* First, declare the els xri abort event has been handled */
12595 spin_lock_irq(&phba->hbalock);
12596 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12597 spin_unlock_irq(&phba->hbalock);
12598 /* Now, handle all the els xri abort events */
12599 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12600 /* Get the first event from the head of the event queue */
12601 spin_lock_irq(&phba->hbalock);
12602 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12603 cq_event, struct lpfc_cq_event, list);
12604 spin_unlock_irq(&phba->hbalock);
12605 /* Notify aborted XRI for ELS work queue */
12606 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12607 /* Free the event processed back to the free pool */
12608 lpfc_sli4_cq_event_release(phba, cq_event);
12609 }
12610}
12611
James Smart341af102010-01-26 23:07:37 -050012612/**
12613 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12614 * @phba: pointer to lpfc hba data structure
12615 * @pIocbIn: pointer to the rspiocbq
12616 * @pIocbOut: pointer to the cmdiocbq
12617 * @wcqe: pointer to the complete wcqe
12618 *
12619 * This routine transfers the fields of a command iocbq to a response iocbq
12620 * by copying all the IOCB fields from command iocbq and transferring the
12621 * completion status information from the complete wcqe.
12622 **/
James Smart4f774512009-05-22 14:52:35 -040012623static void
James Smart341af102010-01-26 23:07:37 -050012624lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12625 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012626 struct lpfc_iocbq *pIocbOut,
12627 struct lpfc_wcqe_complete *wcqe)
12628{
James Smartaf227412013-10-10 12:23:10 -040012629 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012630 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012631 uint32_t status, max_response;
12632 struct lpfc_dmabuf *dmabuf;
12633 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012634 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12635
12636 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12637 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012638 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012639 status = bf_get(lpfc_wcqe_c_status, wcqe);
12640 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012641 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12642 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12643 pIocbIn->iocb.un.fcpi.fcpi_parm =
12644 pIocbOut->iocb.un.fcpi.fcpi_parm -
12645 wcqe->total_data_placed;
12646 else
12647 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012648 else {
James Smart4f774512009-05-22 14:52:35 -040012649 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012650 switch (pIocbOut->iocb.ulpCommand) {
12651 case CMD_ELS_REQUEST64_CR:
12652 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12653 bpl = (struct ulp_bde64 *)dmabuf->virt;
12654 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12655 max_response = bde.tus.f.bdeSize;
12656 break;
12657 case CMD_GEN_REQUEST64_CR:
12658 max_response = 0;
12659 if (!pIocbOut->context3)
12660 break;
12661 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12662 sizeof(struct ulp_bde64);
12663 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12664 bpl = (struct ulp_bde64 *)dmabuf->virt;
12665 for (i = 0; i < numBdes; i++) {
12666 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12667 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12668 max_response += bde.tus.f.bdeSize;
12669 }
12670 break;
12671 default:
12672 max_response = wcqe->total_data_placed;
12673 break;
12674 }
12675 if (max_response < wcqe->total_data_placed)
12676 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12677 else
12678 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12679 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012680 }
James Smart341af102010-01-26 23:07:37 -050012681
James Smartacd68592012-01-18 16:25:09 -050012682 /* Convert BG errors for completion status */
12683 if (status == CQE_STATUS_DI_ERROR) {
12684 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12685
12686 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12687 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12688 else
12689 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12690
12691 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12692 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12693 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12694 BGS_GUARD_ERR_MASK;
12695 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12696 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12697 BGS_APPTAG_ERR_MASK;
12698 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12699 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12700 BGS_REFTAG_ERR_MASK;
12701
12702 /* Check to see if there was any good data before the error */
12703 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12704 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12705 BGS_HI_WATER_MARK_PRESENT_MASK;
12706 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12707 wcqe->total_data_placed;
12708 }
12709
12710 /*
12711 * Set ALL the error bits to indicate we don't know what
12712 * type of error it is.
12713 */
12714 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12715 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12716 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12717 BGS_GUARD_ERR_MASK);
12718 }
12719
James Smart341af102010-01-26 23:07:37 -050012720 /* Pick up HBA exchange busy condition */
12721 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12722 spin_lock_irqsave(&phba->hbalock, iflags);
12723 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12724 spin_unlock_irqrestore(&phba->hbalock, iflags);
12725 }
James Smart4f774512009-05-22 14:52:35 -040012726}
12727
12728/**
James Smart45ed1192009-10-02 15:17:02 -040012729 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12730 * @phba: Pointer to HBA context object.
12731 * @wcqe: Pointer to work-queue completion queue entry.
12732 *
12733 * This routine handles an ELS work-queue completion event and construct
12734 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12735 * discovery engine to handle.
12736 *
12737 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12738 **/
12739static struct lpfc_iocbq *
12740lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12741 struct lpfc_iocbq *irspiocbq)
12742{
James Smart895427b2017-02-12 13:52:30 -080012743 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012744 struct lpfc_iocbq *cmdiocbq;
12745 struct lpfc_wcqe_complete *wcqe;
12746 unsigned long iflags;
12747
James Smart895427b2017-02-12 13:52:30 -080012748 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070012749 if (unlikely(!pring))
12750 return NULL;
James Smart895427b2017-02-12 13:52:30 -080012751
James Smart45ed1192009-10-02 15:17:02 -040012752 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012753 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012754 pring->stats.iocb_event++;
12755 /* Look up the ELS command IOCB and create pseudo response IOCB */
12756 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12757 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040012758 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070012759 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012760 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12761 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070012762 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12763 wcqe->word0, wcqe->total_data_placed,
12764 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040012765 lpfc_sli_release_iocbq(phba, irspiocbq);
12766 return NULL;
12767 }
12768
Dick Kennedy401bb412017-09-29 17:34:28 -070012769 /* Put the iocb back on the txcmplq */
12770 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12771 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12772
James Smart45ed1192009-10-02 15:17:02 -040012773 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012774 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012775
12776 return irspiocbq;
12777}
12778
James Smart8a5ca102017-11-20 16:00:30 -080012779inline struct lpfc_cq_event *
12780lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12781{
12782 struct lpfc_cq_event *cq_event;
12783
12784 /* Allocate a new internal CQ_EVENT entry */
12785 cq_event = lpfc_sli4_cq_event_alloc(phba);
12786 if (!cq_event) {
12787 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12788 "0602 Failed to alloc CQ_EVENT entry\n");
12789 return NULL;
12790 }
12791
12792 /* Move the CQE into the event */
12793 memcpy(&cq_event->cqe, entry, size);
12794 return cq_event;
12795}
12796
James Smart45ed1192009-10-02 15:17:02 -040012797/**
James Smart04c68492009-05-22 14:52:52 -040012798 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12799 * @phba: Pointer to HBA context object.
12800 * @cqe: Pointer to mailbox completion queue entry.
12801 *
12802 * This routine process a mailbox completion queue entry with asynchrous
12803 * event.
12804 *
12805 * Return: true if work posted to worker thread, otherwise false.
12806 **/
12807static bool
12808lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12809{
12810 struct lpfc_cq_event *cq_event;
12811 unsigned long iflags;
12812
12813 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12814 "0392 Async Event: word0:x%x, word1:x%x, "
12815 "word2:x%x, word3:x%x\n", mcqe->word0,
12816 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12817
James Smart8a5ca102017-11-20 16:00:30 -080012818 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12819 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040012820 return false;
James Smart04c68492009-05-22 14:52:52 -040012821 spin_lock_irqsave(&phba->hbalock, iflags);
12822 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12823 /* Set the async event flag */
12824 phba->hba_flag |= ASYNC_EVENT;
12825 spin_unlock_irqrestore(&phba->hbalock, iflags);
12826
12827 return true;
12828}
12829
12830/**
12831 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12832 * @phba: Pointer to HBA context object.
12833 * @cqe: Pointer to mailbox completion queue entry.
12834 *
12835 * This routine process a mailbox completion queue entry with mailbox
12836 * completion event.
12837 *
12838 * Return: true if work posted to worker thread, otherwise false.
12839 **/
12840static bool
12841lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12842{
12843 uint32_t mcqe_status;
12844 MAILBOX_t *mbox, *pmbox;
12845 struct lpfc_mqe *mqe;
12846 struct lpfc_vport *vport;
12847 struct lpfc_nodelist *ndlp;
12848 struct lpfc_dmabuf *mp;
12849 unsigned long iflags;
12850 LPFC_MBOXQ_t *pmb;
12851 bool workposted = false;
12852 int rc;
12853
12854 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12855 if (!bf_get(lpfc_trailer_completed, mcqe))
12856 goto out_no_mqe_complete;
12857
12858 /* Get the reference to the active mbox command */
12859 spin_lock_irqsave(&phba->hbalock, iflags);
12860 pmb = phba->sli.mbox_active;
12861 if (unlikely(!pmb)) {
12862 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12863 "1832 No pending MBOX command to handle\n");
12864 spin_unlock_irqrestore(&phba->hbalock, iflags);
12865 goto out_no_mqe_complete;
12866 }
12867 spin_unlock_irqrestore(&phba->hbalock, iflags);
12868 mqe = &pmb->u.mqe;
12869 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12870 mbox = phba->mbox;
12871 vport = pmb->vport;
12872
12873 /* Reset heartbeat timer */
12874 phba->last_completion_time = jiffies;
12875 del_timer(&phba->sli.mbox_tmo);
12876
12877 /* Move mbox data to caller's mailbox region, do endian swapping */
12878 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070012879 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012880
James Smart73d91e52011-10-10 21:32:10 -040012881 /*
12882 * For mcqe errors, conditionally move a modified error code to
12883 * the mbox so that the error will not be missed.
12884 */
12885 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12886 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12887 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12888 bf_set(lpfc_mqe_status, mqe,
12889 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12890 }
James Smart04c68492009-05-22 14:52:52 -040012891 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12892 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12893 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12894 "MBOX dflt rpi: status:x%x rpi:x%x",
12895 mcqe_status,
12896 pmbox->un.varWords[0], 0);
12897 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12898 mp = (struct lpfc_dmabuf *)(pmb->context1);
12899 ndlp = (struct lpfc_nodelist *)pmb->context2;
12900 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12901 * RID of the PPI using the same mbox buffer.
12902 */
12903 lpfc_unreg_login(phba, vport->vpi,
12904 pmbox->un.varWords[0], pmb);
12905 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12906 pmb->context1 = mp;
12907 pmb->context2 = ndlp;
12908 pmb->vport = vport;
12909 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12910 if (rc != MBX_BUSY)
12911 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12912 LOG_SLI, "0385 rc should "
12913 "have been MBX_BUSY\n");
12914 if (rc != MBX_NOT_FINISHED)
12915 goto send_current_mbox;
12916 }
12917 }
12918 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12919 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12920 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12921
12922 /* There is mailbox completion work to do */
12923 spin_lock_irqsave(&phba->hbalock, iflags);
12924 __lpfc_mbox_cmpl_put(phba, pmb);
12925 phba->work_ha |= HA_MBATT;
12926 spin_unlock_irqrestore(&phba->hbalock, iflags);
12927 workposted = true;
12928
12929send_current_mbox:
12930 spin_lock_irqsave(&phba->hbalock, iflags);
12931 /* Release the mailbox command posting token */
12932 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12933 /* Setting active mailbox pointer need to be in sync to flag clear */
12934 phba->sli.mbox_active = NULL;
12935 spin_unlock_irqrestore(&phba->hbalock, iflags);
12936 /* Wake up worker thread to post the next pending mailbox command */
12937 lpfc_worker_wake_up(phba);
12938out_no_mqe_complete:
12939 if (bf_get(lpfc_trailer_consumed, mcqe))
12940 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12941 return workposted;
12942}
12943
12944/**
12945 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12946 * @phba: Pointer to HBA context object.
12947 * @cqe: Pointer to mailbox completion queue entry.
12948 *
12949 * This routine process a mailbox completion queue entry, it invokes the
12950 * proper mailbox complete handling or asynchrous event handling routine
12951 * according to the MCQE's async bit.
12952 *
12953 * Return: true if work posted to worker thread, otherwise false.
12954 **/
12955static bool
12956lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12957{
12958 struct lpfc_mcqe mcqe;
12959 bool workposted;
12960
12961 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070012962 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040012963
12964 /* Invoke the proper event handling routine */
12965 if (!bf_get(lpfc_trailer_async, &mcqe))
12966 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12967 else
12968 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12969 return workposted;
12970}
12971
12972/**
James Smart4f774512009-05-22 14:52:35 -040012973 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12974 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012975 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012976 * @wcqe: Pointer to work-queue completion queue entry.
12977 *
12978 * This routine handles an ELS work-queue completion event.
12979 *
12980 * Return: true if work posted to worker thread, otherwise false.
12981 **/
12982static bool
James Smart2a76a282012-08-03 12:35:54 -040012983lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012984 struct lpfc_wcqe_complete *wcqe)
12985{
James Smart4f774512009-05-22 14:52:35 -040012986 struct lpfc_iocbq *irspiocbq;
12987 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012988 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012989 int txq_cnt = 0;
12990 int txcmplq_cnt = 0;
12991 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012992
James Smart11f0e342018-05-04 20:37:57 -070012993 /* Check for response status */
12994 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12995 /* Log the error status */
12996 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12997 "0357 ELS CQE error: status=x%x: "
12998 "CQE: %08x %08x %08x %08x\n",
12999 bf_get(lpfc_wcqe_c_status, wcqe),
13000 wcqe->word0, wcqe->total_data_placed,
13001 wcqe->parameter, wcqe->word3);
13002 }
13003
James Smart45ed1192009-10-02 15:17:02 -040013004 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040013005 irspiocbq = lpfc_sli_get_iocbq(phba);
13006 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013007 if (!list_empty(&pring->txq))
13008 txq_cnt++;
13009 if (!list_empty(&pring->txcmplq))
13010 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040013011 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040013012 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13013 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050013014 txq_cnt, phba->iocb_cnt,
13015 fcp_txcmplq_cnt,
13016 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040013017 return false;
James Smart4f774512009-05-22 14:52:35 -040013018 }
James Smart4f774512009-05-22 14:52:35 -040013019
James Smart45ed1192009-10-02 15:17:02 -040013020 /* Save off the slow-path queue event for work thread to process */
13021 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040013022 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040013023 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013024 &phba->sli4_hba.sp_queue_event);
13025 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013026 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013027
James Smart45ed1192009-10-02 15:17:02 -040013028 return true;
James Smart4f774512009-05-22 14:52:35 -040013029}
13030
13031/**
13032 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13033 * @phba: Pointer to HBA context object.
13034 * @wcqe: Pointer to work-queue completion queue entry.
13035 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013036 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013037 * proper WQ release routine to the slow-path WQ.
13038 **/
13039static void
13040lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13041 struct lpfc_wcqe_release *wcqe)
13042{
James Smart2e90f4b2011-12-13 13:22:37 -050013043 /* sanity check on queue memory */
13044 if (unlikely(!phba->sli4_hba.els_wq))
13045 return;
James Smart4f774512009-05-22 14:52:35 -040013046 /* Check for the slow-path ELS work queue */
13047 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13048 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13049 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13050 else
13051 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13052 "2579 Slow-path wqe consume event carries "
13053 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13054 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13055 phba->sli4_hba.els_wq->queue_id);
13056}
13057
13058/**
13059 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13060 * @phba: Pointer to HBA context object.
13061 * @cq: Pointer to a WQ completion queue.
13062 * @wcqe: Pointer to work-queue completion queue entry.
13063 *
13064 * This routine handles an XRI abort event.
13065 *
13066 * Return: true if work posted to worker thread, otherwise false.
13067 **/
13068static bool
13069lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13070 struct lpfc_queue *cq,
13071 struct sli4_wcqe_xri_aborted *wcqe)
13072{
13073 bool workposted = false;
13074 struct lpfc_cq_event *cq_event;
13075 unsigned long iflags;
13076
James Smart4f774512009-05-22 14:52:35 -040013077 switch (cq->subtype) {
13078 case LPFC_FCP:
James Smart8a5ca102017-11-20 16:00:30 -080013079 cq_event = lpfc_cq_event_setup(
13080 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13081 if (!cq_event)
13082 return false;
James Smart4f774512009-05-22 14:52:35 -040013083 spin_lock_irqsave(&phba->hbalock, iflags);
13084 list_add_tail(&cq_event->list,
13085 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13086 /* Set the fcp xri abort event flag */
13087 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13088 spin_unlock_irqrestore(&phba->hbalock, iflags);
13089 workposted = true;
13090 break;
James Smart422c4cb2017-11-20 16:00:32 -080013091 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013092 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013093 cq_event = lpfc_cq_event_setup(
13094 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13095 if (!cq_event)
13096 return false;
James Smart4f774512009-05-22 14:52:35 -040013097 spin_lock_irqsave(&phba->hbalock, iflags);
13098 list_add_tail(&cq_event->list,
13099 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13100 /* Set the els xri abort event flag */
13101 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13102 spin_unlock_irqrestore(&phba->hbalock, iflags);
13103 workposted = true;
13104 break;
James Smart318083a2017-03-04 09:30:30 -080013105 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080013106 /* Notify aborted XRI for NVME work queue */
13107 if (phba->nvmet_support)
13108 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13109 else
13110 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13111
13112 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080013113 break;
James Smart4f774512009-05-22 14:52:35 -040013114 default:
13115 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013116 "0603 Invalid CQ subtype %d: "
13117 "%08x %08x %08x %08x\n",
13118 cq->subtype, wcqe->word0, wcqe->parameter,
13119 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013120 workposted = false;
13121 break;
13122 }
13123 return workposted;
13124}
13125
13126/**
James Smart4d9ab992009-10-02 15:16:39 -040013127 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013128 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013129 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013130 *
James Smart4d9ab992009-10-02 15:16:39 -040013131 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013132 *
13133 * Return: true if work posted to worker thread, otherwise false.
13134 **/
13135static bool
James Smart4d9ab992009-10-02 15:16:39 -040013136lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13137{
13138 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080013139 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013140 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13141 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013142 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013143 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013144 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013145 unsigned long iflags;
13146
James Smart2e90f4b2011-12-13 13:22:37 -050013147 /* sanity check on queue memory */
13148 if (unlikely(!hrq) || unlikely(!drq))
13149 return workposted;
13150
James Smart7851fe22011-07-22 18:36:52 -040013151 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13152 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13153 else
13154 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13155 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013156 goto out;
13157
13158 status = bf_get(lpfc_rcqe_status, rcqe);
13159 switch (status) {
13160 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13161 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13162 "2537 Receive Frame Truncated!!\n");
13163 case FC_STATUS_RQ_SUCCESS:
13164 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013165 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013166 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13167 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013168 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013169 spin_unlock_irqrestore(&phba->hbalock, iflags);
13170 goto out;
13171 }
James Smartb84daac2012-08-03 12:35:13 -040013172 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013173 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013174 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013175
13176 /* If a NVME LS event (type 0x28), treat it as Fast path */
13177 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13178
James Smart4d9ab992009-10-02 15:16:39 -040013179 /* save off the frame for the word thread to process */
13180 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013181 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013182 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013183 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013184 spin_unlock_irqrestore(&phba->hbalock, iflags);
13185 workposted = true;
13186 break;
James Smart4d9ab992009-10-02 15:16:39 -040013187 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013188 if (phba->nvmet_support) {
13189 tgtp = phba->targetport->private;
13190 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13191 "6402 RQE Error x%x, posted %d err_cnt "
13192 "%d: %x %x %x\n",
13193 status, hrq->RQ_buf_posted,
13194 hrq->RQ_no_posted_buf,
13195 atomic_read(&tgtp->rcv_fcp_cmd_in),
13196 atomic_read(&tgtp->rcv_fcp_cmd_out),
13197 atomic_read(&tgtp->xmt_fcp_release));
13198 }
13199 /* fallthrough */
13200
13201 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013202 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013203 /* Post more buffers if possible */
13204 spin_lock_irqsave(&phba->hbalock, iflags);
13205 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13206 spin_unlock_irqrestore(&phba->hbalock, iflags);
13207 workposted = true;
13208 break;
13209 }
13210out:
13211 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013212}
13213
13214/**
13215 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13216 * @phba: Pointer to HBA context object.
13217 * @cq: Pointer to the completion queue.
13218 * @wcqe: Pointer to a completion queue entry.
13219 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013220 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013221 * entry.
13222 *
13223 * Return: true if work posted to worker thread, otherwise false.
13224 **/
13225static bool
13226lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013227 struct lpfc_cqe *cqe)
13228{
James Smart45ed1192009-10-02 15:17:02 -040013229 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013230 bool workposted = false;
13231
13232 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013233 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013234
13235 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013236 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013237 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013238 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013239 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013240 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013241 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013242 break;
13243 case CQE_CODE_RELEASE_WQE:
13244 /* Process the WQ release event */
13245 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013246 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013247 break;
13248 case CQE_CODE_XRI_ABORTED:
13249 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013250 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013251 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013252 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013253 break;
James Smart4d9ab992009-10-02 15:16:39 -040013254 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013255 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013256 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013257 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013258 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013259 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013260 break;
James Smart4f774512009-05-22 14:52:35 -040013261 default:
13262 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13263 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013264 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013265 break;
13266 }
13267 return workposted;
13268}
13269
13270/**
James Smart4f774512009-05-22 14:52:35 -040013271 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13272 * @phba: Pointer to HBA context object.
13273 * @eqe: Pointer to fast-path event queue entry.
13274 *
13275 * This routine process a event queue entry from the slow-path event queue.
13276 * It will check the MajorCode and MinorCode to determine this is for a
13277 * completion event on a completion queue, if not, an error shall be logged
13278 * and just return. Otherwise, it will get to the corresponding completion
13279 * queue and process all the entries on that completion queue, rearm the
13280 * completion queue, and then return.
13281 *
13282 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013283static void
James Smart67d12732012-08-03 12:36:13 -040013284lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13285 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013286{
James Smart67d12732012-08-03 12:36:13 -040013287 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013288 uint16_t cqid;
13289
James Smart4f774512009-05-22 14:52:35 -040013290 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013291 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013292
James Smart4f774512009-05-22 14:52:35 -040013293 list_for_each_entry(childq, &speq->child_list, list) {
13294 if (childq->queue_id == cqid) {
13295 cq = childq;
13296 break;
13297 }
13298 }
13299 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013300 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13301 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13302 "0365 Slow-path CQ identifier "
13303 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013304 return;
James Smart4f774512009-05-22 14:52:35 -040013305 }
13306
James Smart895427b2017-02-12 13:52:30 -080013307 /* Save EQ associated with this CQ */
13308 cq->assoc_qp = speq;
13309
Dick Kennedyf485c182017-09-29 17:34:34 -070013310 if (!queue_work(phba->wq, &cq->spwork))
13311 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13312 "0390 Cannot schedule soft IRQ "
13313 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13314 cqid, cq->queue_id, smp_processor_id());
13315}
13316
13317/**
13318 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13319 * @phba: Pointer to HBA context object.
13320 *
13321 * This routine process a event queue entry from the slow-path event queue.
13322 * It will check the MajorCode and MinorCode to determine this is for a
13323 * completion event on a completion queue, if not, an error shall be logged
13324 * and just return. Otherwise, it will get to the corresponding completion
13325 * queue and process all the entries on that completion queue, rearm the
13326 * completion queue, and then return.
13327 *
13328 **/
13329static void
13330lpfc_sli4_sp_process_cq(struct work_struct *work)
13331{
13332 struct lpfc_queue *cq =
13333 container_of(work, struct lpfc_queue, spwork);
13334 struct lpfc_hba *phba = cq->phba;
13335 struct lpfc_cqe *cqe;
13336 bool workposted = false;
13337 int ccount = 0;
13338
James Smart4f774512009-05-22 14:52:35 -040013339 /* Process all the entries to the CQ */
13340 switch (cq->type) {
13341 case LPFC_MCQ:
13342 while ((cqe = lpfc_sli4_cq_get(cq))) {
13343 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013344 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013345 break;
James Smartb84daac2012-08-03 12:35:13 -040013346 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013347 }
13348 break;
13349 case LPFC_WCQ:
13350 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013351 if (cq->subtype == LPFC_FCP ||
13352 cq->subtype == LPFC_NVME) {
13353#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13354 if (phba->ktime_on)
13355 cq->isr_timestamp = ktime_get_ns();
13356 else
13357 cq->isr_timestamp = 0;
13358#endif
James Smart895427b2017-02-12 13:52:30 -080013359 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013360 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013361 } else {
James Smart05580562011-05-24 11:40:48 -040013362 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13363 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013364 }
Dick Kennedyf485c182017-09-29 17:34:34 -070013365 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013366 break;
James Smart4f774512009-05-22 14:52:35 -040013367 }
James Smartb84daac2012-08-03 12:35:13 -040013368
13369 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013370 if (ccount > cq->CQ_max_cqe)
13371 cq->CQ_max_cqe = ccount;
James Smart4f774512009-05-22 14:52:35 -040013372 break;
13373 default:
13374 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13375 "0370 Invalid completion queue type (%d)\n",
13376 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013377 return;
James Smart4f774512009-05-22 14:52:35 -040013378 }
13379
13380 /* Catch the no cq entry condition, log an error */
Dick Kennedyf485c182017-09-29 17:34:34 -070013381 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13383 "0371 No entry from the CQ: identifier "
13384 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13385
13386 /* In any case, flash and re-arm the RCQ */
James Smartb71413d2018-02-22 08:18:40 -080013387 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013388
13389 /* wake up worker thread if there are works to be done */
13390 if (workposted)
13391 lpfc_worker_wake_up(phba);
13392}
13393
13394/**
13395 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013396 * @phba: Pointer to HBA context object.
13397 * @cq: Pointer to associated CQ
13398 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013399 *
13400 * This routine process a fast-path work queue completion entry from fast-path
13401 * event queue for FCP command response completion.
13402 **/
13403static void
James Smart2a76a282012-08-03 12:35:54 -040013404lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013405 struct lpfc_wcqe_complete *wcqe)
13406{
James Smart2a76a282012-08-03 12:35:54 -040013407 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013408 struct lpfc_iocbq *cmdiocbq;
13409 struct lpfc_iocbq irspiocbq;
13410 unsigned long iflags;
13411
James Smart4f774512009-05-22 14:52:35 -040013412 /* Check for response status */
13413 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13414 /* If resource errors reported from HBA, reduce queue
13415 * depth of the SCSI device.
13416 */
James Smarte3d2b802012-08-14 14:25:43 -040013417 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13418 IOSTAT_LOCAL_REJECT)) &&
13419 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13420 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013421 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013422
James Smart4f774512009-05-22 14:52:35 -040013423 /* Log the error status */
James Smart11f0e342018-05-04 20:37:57 -070013424 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13425 "0373 FCP CQE error: status=x%x: "
13426 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013427 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070013428 wcqe->word0, wcqe->total_data_placed,
13429 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013430 }
13431
13432 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013433 spin_lock_irqsave(&pring->ring_lock, iflags);
13434 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013435 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13436 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013437 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013438 if (unlikely(!cmdiocbq)) {
13439 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13440 "0374 FCP complete with no corresponding "
13441 "cmdiocb: iotag (%d)\n",
13442 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13443 return;
13444 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013445#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13446 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13447#endif
James Smart895427b2017-02-12 13:52:30 -080013448 if (cmdiocbq->iocb_cmpl == NULL) {
13449 if (cmdiocbq->wqe_cmpl) {
13450 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13451 spin_lock_irqsave(&phba->hbalock, iflags);
13452 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13453 spin_unlock_irqrestore(&phba->hbalock, iflags);
13454 }
13455
13456 /* Pass the cmd_iocb and the wcqe to the upper layer */
13457 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13458 return;
13459 }
James Smart4f774512009-05-22 14:52:35 -040013460 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13461 "0375 FCP cmdiocb not callback function "
13462 "iotag: (%d)\n",
13463 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13464 return;
13465 }
13466
13467 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013468 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013469
James Smart0f65ff62010-02-26 14:14:23 -050013470 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13471 spin_lock_irqsave(&phba->hbalock, iflags);
13472 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13473 spin_unlock_irqrestore(&phba->hbalock, iflags);
13474 }
13475
James Smart4f774512009-05-22 14:52:35 -040013476 /* Pass the cmd_iocb and the rsp state to the upper layer */
13477 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13478}
13479
13480/**
13481 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13482 * @phba: Pointer to HBA context object.
13483 * @cq: Pointer to completion queue.
13484 * @wcqe: Pointer to work-queue completion queue entry.
13485 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013486 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013487 * proper WQ release routine to the slow-path WQ.
13488 **/
13489static void
13490lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13491 struct lpfc_wcqe_release *wcqe)
13492{
13493 struct lpfc_queue *childwq;
13494 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013495 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013496
13497 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013498 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013499 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013500 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013501 lpfc_sli4_wq_release(childwq,
13502 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013503 if (childwq->q_flag & HBA_NVMET_WQFULL)
13504 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013505 wqid_matched = true;
13506 break;
13507 }
13508 }
13509 /* Report warning log message if no match found */
13510 if (wqid_matched != true)
13511 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13512 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013513 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013514}
13515
13516/**
James Smart2d7dbc42017-02-12 13:52:35 -080013517 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13518 * @phba: Pointer to HBA context object.
13519 * @rcqe: Pointer to receive-queue completion queue entry.
13520 *
13521 * This routine process a receive-queue completion queue entry.
13522 *
13523 * Return: true if work posted to worker thread, otherwise false.
13524 **/
13525static bool
13526lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13527 struct lpfc_rcqe *rcqe)
13528{
13529 bool workposted = false;
13530 struct lpfc_queue *hrq;
13531 struct lpfc_queue *drq;
13532 struct rqb_dmabuf *dma_buf;
13533 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013534 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013535 uint32_t status, rq_id;
13536 unsigned long iflags;
13537 uint32_t fctl, idx;
13538
13539 if ((phba->nvmet_support == 0) ||
13540 (phba->sli4_hba.nvmet_cqset == NULL))
13541 return workposted;
13542
13543 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13544 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13545 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13546
13547 /* sanity check on queue memory */
13548 if (unlikely(!hrq) || unlikely(!drq))
13549 return workposted;
13550
13551 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13552 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13553 else
13554 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13555
13556 if ((phba->nvmet_support == 0) ||
13557 (rq_id != hrq->queue_id))
13558 return workposted;
13559
13560 status = bf_get(lpfc_rcqe_status, rcqe);
13561 switch (status) {
13562 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13563 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13564 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013565 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013566 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013567 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013568 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013569 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13570 if (!dma_buf) {
13571 hrq->RQ_no_buf_found++;
13572 spin_unlock_irqrestore(&phba->hbalock, iflags);
13573 goto out;
13574 }
13575 spin_unlock_irqrestore(&phba->hbalock, iflags);
13576 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013577 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013578 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13579
13580 /* Just some basic sanity checks on FCP Command frame */
13581 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13582 fc_hdr->fh_f_ctl[1] << 8 |
13583 fc_hdr->fh_f_ctl[2]);
13584 if (((fctl &
13585 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13586 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13587 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13588 goto drop;
13589
13590 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13591 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013592 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070013593 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013594 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013595 return false;
13596 }
13597drop:
13598 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13599 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013600 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013601 if (phba->nvmet_support) {
13602 tgtp = phba->targetport->private;
13603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13604 "6401 RQE Error x%x, posted %d err_cnt "
13605 "%d: %x %x %x\n",
13606 status, hrq->RQ_buf_posted,
13607 hrq->RQ_no_posted_buf,
13608 atomic_read(&tgtp->rcv_fcp_cmd_in),
13609 atomic_read(&tgtp->rcv_fcp_cmd_out),
13610 atomic_read(&tgtp->xmt_fcp_release));
13611 }
13612 /* fallthrough */
13613
13614 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013615 hrq->RQ_no_posted_buf++;
13616 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013617 break;
13618 }
13619out:
13620 return workposted;
13621}
13622
13623/**
James Smart895427b2017-02-12 13:52:30 -080013624 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013625 * @cq: Pointer to the completion queue.
13626 * @eqe: Pointer to fast-path completion queue entry.
13627 *
13628 * This routine process a fast-path work queue completion entry from fast-path
13629 * event queue for FCP command response completion.
13630 **/
13631static int
James Smart895427b2017-02-12 13:52:30 -080013632lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013633 struct lpfc_cqe *cqe)
13634{
13635 struct lpfc_wcqe_release wcqe;
13636 bool workposted = false;
13637
13638 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013639 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013640
13641 /* Check and process for different type of WCQE and dispatch */
13642 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13643 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013644 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013645 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013646 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013647 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013648 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13649 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13650 (struct lpfc_wcqe_complete *)&wcqe);
13651 if (cq->subtype == LPFC_NVME_LS)
13652 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013653 (struct lpfc_wcqe_complete *)&wcqe);
13654 break;
13655 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013656 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013657 /* Process the WQ release event */
13658 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13659 (struct lpfc_wcqe_release *)&wcqe);
13660 break;
13661 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013662 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013663 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013664 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013665 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13666 (struct sli4_wcqe_xri_aborted *)&wcqe);
13667 break;
James Smart895427b2017-02-12 13:52:30 -080013668 case CQE_CODE_RECEIVE_V1:
13669 case CQE_CODE_RECEIVE:
13670 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013671 if (cq->subtype == LPFC_NVMET) {
13672 workposted = lpfc_sli4_nvmet_handle_rcqe(
13673 phba, cq, (struct lpfc_rcqe *)&wcqe);
13674 }
James Smart895427b2017-02-12 13:52:30 -080013675 break;
James Smart4f774512009-05-22 14:52:35 -040013676 default:
13677 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013678 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013679 bf_get(lpfc_wcqe_c_code, &wcqe));
13680 break;
13681 }
13682 return workposted;
13683}
13684
13685/**
James Smart67d12732012-08-03 12:36:13 -040013686 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013687 * @phba: Pointer to HBA context object.
13688 * @eqe: Pointer to fast-path event queue entry.
13689 *
13690 * This routine process a event queue entry from the fast-path event queue.
13691 * It will check the MajorCode and MinorCode to determine this is for a
13692 * completion event on a completion queue, if not, an error shall be logged
13693 * and just return. Otherwise, it will get to the corresponding completion
13694 * queue and process all the entries on the completion queue, rearm the
13695 * completion queue, and then return.
13696 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013697static void
James Smart67d12732012-08-03 12:36:13 -040013698lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13699 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013700{
James Smart895427b2017-02-12 13:52:30 -080013701 struct lpfc_queue *cq = NULL;
James Smart2d7dbc42017-02-12 13:52:35 -080013702 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013703
James Smartcb5172e2010-03-15 11:25:07 -040013704 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013705 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013706 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013707 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013708 bf_get_le32(lpfc_eqe_major_code, eqe),
13709 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070013710 return;
James Smart4f774512009-05-22 14:52:35 -040013711 }
13712
James Smart67d12732012-08-03 12:36:13 -040013713 /* Get the reference to the corresponding CQ */
13714 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13715
James Smart2d7dbc42017-02-12 13:52:35 -080013716 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13717 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13718 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13719 /* Process NVMET unsol rcv */
13720 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13721 goto process_cq;
13722 }
13723 }
13724
James Smart895427b2017-02-12 13:52:30 -080013725 if (phba->sli4_hba.nvme_cq_map &&
13726 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013727 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013728 cq = phba->sli4_hba.nvme_cq[qidx];
13729 goto process_cq;
13730 }
13731
13732 if (phba->sli4_hba.fcp_cq_map &&
13733 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13734 /* Process FCP command completion */
13735 cq = phba->sli4_hba.fcp_cq[qidx];
13736 goto process_cq;
13737 }
13738
13739 if (phba->sli4_hba.nvmels_cq &&
13740 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13741 /* Process NVME unsol rcv */
13742 cq = phba->sli4_hba.nvmels_cq;
13743 }
13744
13745 /* Otherwise this is a Slow path event */
13746 if (cq == NULL) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013747 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13748 return;
James Smart67d12732012-08-03 12:36:13 -040013749 }
13750
James Smart895427b2017-02-12 13:52:30 -080013751process_cq:
James Smart4f774512009-05-22 14:52:35 -040013752 if (unlikely(cqid != cq->queue_id)) {
13753 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13754 "0368 Miss-matched fast-path completion "
13755 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13756 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070013757 return;
James Smart4f774512009-05-22 14:52:35 -040013758 }
13759
James Smart895427b2017-02-12 13:52:30 -080013760 /* Save EQ associated with this CQ */
13761 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13762
Dick Kennedyf485c182017-09-29 17:34:34 -070013763 if (!queue_work(phba->wq, &cq->irqwork))
13764 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13765 "0363 Cannot schedule soft IRQ "
13766 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13767 cqid, cq->queue_id, smp_processor_id());
13768}
13769
13770/**
13771 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13772 * @phba: Pointer to HBA context object.
13773 * @eqe: Pointer to fast-path event queue entry.
13774 *
13775 * This routine process a event queue entry from the fast-path event queue.
13776 * It will check the MajorCode and MinorCode to determine this is for a
13777 * completion event on a completion queue, if not, an error shall be logged
13778 * and just return. Otherwise, it will get to the corresponding completion
13779 * queue and process all the entries on the completion queue, rearm the
13780 * completion queue, and then return.
13781 **/
13782static void
13783lpfc_sli4_hba_process_cq(struct work_struct *work)
13784{
13785 struct lpfc_queue *cq =
13786 container_of(work, struct lpfc_queue, irqwork);
13787 struct lpfc_hba *phba = cq->phba;
13788 struct lpfc_cqe *cqe;
13789 bool workposted = false;
13790 int ccount = 0;
13791
James Smart4f774512009-05-22 14:52:35 -040013792 /* Process all the entries to the CQ */
13793 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013794#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13795 if (phba->ktime_on)
13796 cq->isr_timestamp = ktime_get_ns();
13797 else
13798 cq->isr_timestamp = 0;
13799#endif
James Smart895427b2017-02-12 13:52:30 -080013800 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013801 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013802 break;
James Smart4f774512009-05-22 14:52:35 -040013803 }
13804
James Smartb84daac2012-08-03 12:35:13 -040013805 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013806 if (ccount > cq->CQ_max_cqe)
13807 cq->CQ_max_cqe = ccount;
13808 cq->assoc_qp->EQ_cqe_cnt += ccount;
James Smartb84daac2012-08-03 12:35:13 -040013809
James Smart4f774512009-05-22 14:52:35 -040013810 /* Catch the no cq entry condition */
Dick Kennedyf485c182017-09-29 17:34:34 -070013811 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013812 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13813 "0369 No entry from fast-path completion "
13814 "queue fcpcqid=%d\n", cq->queue_id);
13815
13816 /* In any case, flash and re-arm the CQ */
James Smartb71413d2018-02-22 08:18:40 -080013817 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013818
13819 /* wake up worker thread if there are works to be done */
13820 if (workposted)
13821 lpfc_worker_wake_up(phba);
13822}
13823
13824static void
13825lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13826{
13827 struct lpfc_eqe *eqe;
13828
13829 /* walk all the EQ entries and drop on the floor */
13830 while ((eqe = lpfc_sli4_eq_get(eq)))
13831 ;
13832
13833 /* Clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -080013834 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013835}
13836
James Smart1ba981f2014-02-20 09:56:45 -050013837
13838/**
13839 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13840 * entry
13841 * @phba: Pointer to HBA context object.
13842 * @eqe: Pointer to fast-path event queue entry.
13843 *
13844 * This routine process a event queue entry from the Flash Optimized Fabric
13845 * event queue. It will check the MajorCode and MinorCode to determine this
13846 * is for a completion event on a completion queue, if not, an error shall be
13847 * logged and just return. Otherwise, it will get to the corresponding
13848 * completion queue and process all the entries on the completion queue, rearm
13849 * the completion queue, and then return.
13850 **/
13851static void
13852lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13853{
13854 struct lpfc_queue *cq;
James Smart1ba981f2014-02-20 09:56:45 -050013855 uint16_t cqid;
James Smart1ba981f2014-02-20 09:56:45 -050013856
13857 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13858 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13859 "9147 Not a valid completion "
13860 "event: majorcode=x%x, minorcode=x%x\n",
13861 bf_get_le32(lpfc_eqe_major_code, eqe),
13862 bf_get_le32(lpfc_eqe_minor_code, eqe));
13863 return;
13864 }
13865
13866 /* Get the reference to the corresponding CQ */
13867 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13868
13869 /* Next check for OAS */
13870 cq = phba->sli4_hba.oas_cq;
13871 if (unlikely(!cq)) {
13872 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13873 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13874 "9148 OAS completion queue "
13875 "does not exist\n");
13876 return;
13877 }
13878
13879 if (unlikely(cqid != cq->queue_id)) {
13880 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13881 "9149 Miss-matched fast-path compl "
13882 "queue id: eqcqid=%d, fcpcqid=%d\n",
13883 cqid, cq->queue_id);
13884 return;
13885 }
13886
James Smartd41b65b2017-06-15 22:56:41 -070013887 /* Save EQ associated with this CQ */
13888 cq->assoc_qp = phba->sli4_hba.fof_eq;
13889
Dick Kennedyf485c182017-09-29 17:34:34 -070013890 /* CQ work will be processed on CPU affinitized to this IRQ */
13891 if (!queue_work(phba->wq, &cq->irqwork))
James Smart1ba981f2014-02-20 09:56:45 -050013892 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf485c182017-09-29 17:34:34 -070013893 "0367 Cannot schedule soft IRQ "
13894 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13895 cqid, cq->queue_id, smp_processor_id());
James Smart1ba981f2014-02-20 09:56:45 -050013896}
13897
13898/**
13899 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13900 * @irq: Interrupt number.
13901 * @dev_id: The device context pointer.
13902 *
13903 * This function is directly called from the PCI layer as an interrupt
13904 * service routine when device with SLI-4 interface spec is enabled with
13905 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13906 * IOCB ring event in the HBA. However, when the device is enabled with either
13907 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13908 * device-level interrupt handler. When the PCI slot is in error recovery
13909 * or the HBA is undergoing initialization, the interrupt handler will not
13910 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13911 * the intrrupt context. This function is called without any lock held.
13912 * It gets the hbalock to access and update SLI data structures. Note that,
13913 * the EQ to CQ are one-to-one map such that the EQ index is
13914 * equal to that of CQ index.
13915 *
13916 * This function returns IRQ_HANDLED when interrupt is handled else it
13917 * returns IRQ_NONE.
13918 **/
13919irqreturn_t
13920lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13921{
13922 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013923 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013924 struct lpfc_queue *eq;
13925 struct lpfc_eqe *eqe;
13926 unsigned long iflag;
13927 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013928
13929 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013930 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13931 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013932
13933 if (unlikely(!phba))
13934 return IRQ_NONE;
13935
13936 /* Get to the EQ struct associated with this vector */
13937 eq = phba->sli4_hba.fof_eq;
13938 if (unlikely(!eq))
13939 return IRQ_NONE;
13940
13941 /* Check device state for handling interrupt */
13942 if (unlikely(lpfc_intr_state_check(phba))) {
James Smart1ba981f2014-02-20 09:56:45 -050013943 /* Check again for link_state with lock held */
13944 spin_lock_irqsave(&phba->hbalock, iflag);
13945 if (phba->link_state < LPFC_LINK_DOWN)
13946 /* Flush, clear interrupt, and rearm the EQ */
13947 lpfc_sli4_eq_flush(phba, eq);
13948 spin_unlock_irqrestore(&phba->hbalock, iflag);
13949 return IRQ_NONE;
13950 }
13951
13952 /*
13953 * Process all the event on FCP fast-path EQ
13954 */
13955 while ((eqe = lpfc_sli4_eq_get(eq))) {
13956 lpfc_sli4_fof_handle_eqe(phba, eqe);
13957 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013958 break;
James Smart1ba981f2014-02-20 09:56:45 -050013959 eq->EQ_processed++;
13960 }
13961
13962 /* Track the max number of EQEs processed in 1 intr */
13963 if (ecount > eq->EQ_max_eqe)
13964 eq->EQ_max_eqe = ecount;
13965
13966
13967 if (unlikely(ecount == 0)) {
13968 eq->EQ_no_entry++;
13969
13970 if (phba->intr_type == MSIX)
13971 /* MSI-X treated interrupt served as no EQ share INT */
13972 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13973 "9145 MSI-X interrupt with no EQE\n");
13974 else {
13975 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13976 "9146 ISR interrupt with no EQE\n");
13977 /* Non MSI-X treated on interrupt as EQ share INT */
13978 return IRQ_NONE;
13979 }
13980 }
13981 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080013982 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -050013983 return IRQ_HANDLED;
13984}
13985
James Smart4f774512009-05-22 14:52:35 -040013986/**
James Smart67d12732012-08-03 12:36:13 -040013987 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013988 * @irq: Interrupt number.
13989 * @dev_id: The device context pointer.
13990 *
13991 * This function is directly called from the PCI layer as an interrupt
13992 * service routine when device with SLI-4 interface spec is enabled with
13993 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13994 * ring event in the HBA. However, when the device is enabled with either
13995 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13996 * device-level interrupt handler. When the PCI slot is in error recovery
13997 * or the HBA is undergoing initialization, the interrupt handler will not
13998 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13999 * the intrrupt context. This function is called without any lock held.
14000 * It gets the hbalock to access and update SLI data structures. Note that,
14001 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14002 * equal to that of FCP CQ index.
14003 *
James Smart67d12732012-08-03 12:36:13 -040014004 * The link attention and ELS ring attention events are handled
14005 * by the worker thread. The interrupt handler signals the worker thread
14006 * and returns for these events. This function is called without any lock
14007 * held. It gets the hbalock to access and update SLI data structures.
14008 *
James Smart4f774512009-05-22 14:52:35 -040014009 * This function returns IRQ_HANDLED when interrupt is handled else it
14010 * returns IRQ_NONE.
14011 **/
14012irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040014013lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040014014{
14015 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080014016 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040014017 struct lpfc_queue *fpeq;
14018 struct lpfc_eqe *eqe;
14019 unsigned long iflag;
14020 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080014021 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040014022
14023 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014024 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14025 phba = hba_eq_hdl->phba;
14026 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014027
14028 if (unlikely(!phba))
14029 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040014030 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040014031 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014032
14033 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080014034 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050014035 if (unlikely(!fpeq))
14036 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014037
James Smartba20c852012-08-03 12:36:52 -040014038 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080014039 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartb71413d2018-02-22 08:18:40 -080014040 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -040014041 else {
James Smart895427b2017-02-12 13:52:30 -080014042 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040014043 return IRQ_NONE;
14044 }
14045 }
14046
James Smart4f774512009-05-22 14:52:35 -040014047 /* Check device state for handling interrupt */
14048 if (unlikely(lpfc_intr_state_check(phba))) {
14049 /* Check again for link_state with lock held */
14050 spin_lock_irqsave(&phba->hbalock, iflag);
14051 if (phba->link_state < LPFC_LINK_DOWN)
14052 /* Flush, clear interrupt, and rearm the EQ */
14053 lpfc_sli4_eq_flush(phba, fpeq);
14054 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040014055 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014056 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040014057 return IRQ_NONE;
14058 }
14059
14060 /*
14061 * Process all the event on FCP fast-path EQ
14062 */
14063 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
Dick Kennedyf485c182017-09-29 17:34:34 -070014064 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14065 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070014066 break;
James Smartb84daac2012-08-03 12:35:13 -040014067 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040014068 }
14069
James Smartb84daac2012-08-03 12:35:13 -040014070 /* Track the max number of EQEs processed in 1 intr */
14071 if (ecount > fpeq->EQ_max_eqe)
14072 fpeq->EQ_max_eqe = ecount;
14073
James Smart4f774512009-05-22 14:52:35 -040014074 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080014075 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014076
14077 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014078 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040014079
14080 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080014081 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040014082 return IRQ_NONE;
14083 }
14084
James Smart4f774512009-05-22 14:52:35 -040014085 if (phba->intr_type == MSIX)
14086 /* MSI-X treated interrupt served as no EQ share INT */
14087 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14088 "0358 MSI-X interrupt with no EQE\n");
14089 else
14090 /* Non MSI-X treated on interrupt as EQ share INT */
14091 return IRQ_NONE;
14092 }
14093
James Smartba20c852012-08-03 12:36:52 -040014094 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080014095 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14096
James Smart4f774512009-05-22 14:52:35 -040014097 return IRQ_HANDLED;
14098} /* lpfc_sli4_fp_intr_handler */
14099
14100/**
14101 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14102 * @irq: Interrupt number.
14103 * @dev_id: The device context pointer.
14104 *
14105 * This function is the device-level interrupt handler to device with SLI-4
14106 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14107 * interrupt mode is enabled and there is an event in the HBA which requires
14108 * driver attention. This function invokes the slow-path interrupt attention
14109 * handling function and fast-path interrupt attention handling function in
14110 * turn to process the relevant HBA attention events. This function is called
14111 * without any lock held. It gets the hbalock to access and update SLI data
14112 * structures.
14113 *
14114 * This function returns IRQ_HANDLED when interrupt is handled, else it
14115 * returns IRQ_NONE.
14116 **/
14117irqreturn_t
14118lpfc_sli4_intr_handler(int irq, void *dev_id)
14119{
14120 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014121 irqreturn_t hba_irq_rc;
14122 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014123 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014124
14125 /* Get the driver's phba structure from the dev_id */
14126 phba = (struct lpfc_hba *)dev_id;
14127
14128 if (unlikely(!phba))
14129 return IRQ_NONE;
14130
14131 /*
James Smart4f774512009-05-22 14:52:35 -040014132 * Invoke fast-path host attention interrupt handling as appropriate.
14133 */
James Smart895427b2017-02-12 13:52:30 -080014134 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014135 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014136 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014137 if (hba_irq_rc == IRQ_HANDLED)
14138 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014139 }
14140
James Smart1ba981f2014-02-20 09:56:45 -050014141 if (phba->cfg_fof) {
14142 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014143 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050014144 if (hba_irq_rc == IRQ_HANDLED)
14145 hba_handled |= true;
14146 }
14147
James Smart67d12732012-08-03 12:36:13 -040014148 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014149} /* lpfc_sli4_intr_handler */
14150
14151/**
14152 * lpfc_sli4_queue_free - free a queue structure and associated memory
14153 * @queue: The queue structure to free.
14154 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014155 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014156 * the host resident queue. This function must be called after destroying the
14157 * queue on the HBA.
14158 **/
14159void
14160lpfc_sli4_queue_free(struct lpfc_queue *queue)
14161{
14162 struct lpfc_dmabuf *dmabuf;
14163
14164 if (!queue)
14165 return;
14166
14167 while (!list_empty(&queue->page_list)) {
14168 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14169 list);
James Smart81b96ed2017-11-20 16:00:29 -080014170 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014171 dmabuf->virt, dmabuf->phys);
14172 kfree(dmabuf);
14173 }
James Smart895427b2017-02-12 13:52:30 -080014174 if (queue->rqbp) {
14175 lpfc_free_rq_buffer(queue->phba, queue);
14176 kfree(queue->rqbp);
14177 }
James Smartd1f525a2017-04-21 16:04:55 -070014178
14179 if (!list_empty(&queue->wq_list))
14180 list_del(&queue->wq_list);
14181
James Smart4f774512009-05-22 14:52:35 -040014182 kfree(queue);
14183 return;
14184}
14185
14186/**
14187 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14188 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014189 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014190 * @entry_size: The size of each queue entry for this queue.
14191 * @entry count: The number of entries that this queue will handle.
14192 *
14193 * This function allocates a queue structure and the DMAable memory used for
14194 * the host resident queue. This function must be called before creating the
14195 * queue on the HBA.
14196 **/
14197struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014198lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14199 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040014200{
14201 struct lpfc_queue *queue;
14202 struct lpfc_dmabuf *dmabuf;
14203 int x, total_qe_count;
14204 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040014205 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040014206
James Smartcb5172e2010-03-15 11:25:07 -040014207 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014208 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014209
James Smart4f774512009-05-22 14:52:35 -040014210 queue = kzalloc(sizeof(struct lpfc_queue) +
14211 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14212 if (!queue)
14213 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040014214 queue->page_count = (ALIGN(entry_size * entry_count,
14215 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014216
14217 /* If needed, Adjust page count to match the max the adapter supports */
14218 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14219 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14220
James Smart4f774512009-05-22 14:52:35 -040014221 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014222 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014223 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014224 INIT_LIST_HEAD(&queue->page_list);
14225 INIT_LIST_HEAD(&queue->child_list);
James Smart81b96ed2017-11-20 16:00:29 -080014226
14227 /* Set queue parameters now. If the system cannot provide memory
14228 * resources, the free routine needs to know what was allocated.
14229 */
14230 queue->entry_size = entry_size;
14231 queue->entry_count = entry_count;
14232 queue->page_size = hw_page_size;
14233 queue->phba = phba;
14234
James Smart4f774512009-05-22 14:52:35 -040014235 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14236 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14237 if (!dmabuf)
14238 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040014239 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14240 hw_page_size, &dmabuf->phys,
14241 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014242 if (!dmabuf->virt) {
14243 kfree(dmabuf);
14244 goto out_fail;
14245 }
14246 dmabuf->buffer_tag = x;
14247 list_add_tail(&dmabuf->list, &queue->page_list);
14248 /* initialize queue's entry array */
14249 dma_pointer = dmabuf->virt;
14250 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040014251 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040014252 total_qe_count++, dma_pointer += entry_size) {
14253 queue->qe[total_qe_count].address = dma_pointer;
14254 }
14255 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014256 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14257 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014258
James Smart64eb4dc2017-05-15 15:20:49 -070014259 /* entry_repost will be set during q creation */
14260
James Smart4f774512009-05-22 14:52:35 -040014261 return queue;
14262out_fail:
14263 lpfc_sli4_queue_free(queue);
14264 return NULL;
14265}
14266
14267/**
James Smart962bc512013-01-03 15:44:00 -050014268 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14269 * @phba: HBA structure that indicates port to create a queue on.
14270 * @pci_barset: PCI BAR set flag.
14271 *
14272 * This function shall perform iomap of the specified PCI BAR address to host
14273 * memory address if not already done so and return it. The returned host
14274 * memory address can be NULL.
14275 */
14276static void __iomem *
14277lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14278{
James Smart962bc512013-01-03 15:44:00 -050014279 if (!phba->pcidev)
14280 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014281
14282 switch (pci_barset) {
14283 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014284 return phba->pci_bar0_memmap_p;
14285 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014286 return phba->pci_bar2_memmap_p;
14287 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014288 return phba->pci_bar4_memmap_p;
14289 default:
14290 break;
14291 }
14292 return NULL;
14293}
14294
14295/**
James Smart895427b2017-02-12 13:52:30 -080014296 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040014297 * @phba: HBA structure that indicates port to create a queue on.
14298 * @startq: The starting FCP EQ to modify
14299 *
14300 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080014301 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14302 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040014303 *
14304 * The @phba struct is used to send mailbox command to HBA. The @startq
14305 * is used to get the starting FCP EQ to change.
14306 * This function is asynchronous and will wait for the mailbox
14307 * command to finish before continuing.
14308 *
14309 * On success this function will return a zero. If unable to allocate enough
14310 * memory this function will return -ENOMEM. If the queue create mailbox command
14311 * fails this function will return -ENXIO.
14312 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014313int
James Smart0cf07f842017-06-01 21:07:10 -070014314lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14315 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014316{
14317 struct lpfc_mbx_modify_eq_delay *eq_delay;
14318 LPFC_MBOXQ_t *mbox;
14319 struct lpfc_queue *eq;
14320 int cnt, rc, length, status = 0;
14321 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014322 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014323 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014324 union lpfc_sli4_cfg_shdr *shdr;
14325 uint16_t dmult;
14326
James Smart895427b2017-02-12 13:52:30 -080014327 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040014328 return 0;
14329
14330 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14331 if (!mbox)
14332 return -ENOMEM;
14333 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14334 sizeof(struct lpfc_sli4_cfg_mhdr));
14335 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14336 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14337 length, LPFC_SLI4_MBX_EMBED);
14338 eq_delay = &mbox->u.mqe.un.eq_delay;
14339
14340 /* Calculate delay multiper from maximum interrupt per second */
James Smart0cf07f842017-06-01 21:07:10 -070014341 result = imax / phba->io_channel_irqs;
James Smart895427b2017-02-12 13:52:30 -080014342 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014343 dmult = 0;
14344 else
14345 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014346 if (dmult > LPFC_DMULT_MAX)
14347 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014348
14349 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014350 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14351 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014352 if (!eq)
14353 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014354 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014355 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14356 eq_delay->u.request.eq[cnt].phase = 0;
14357 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14358 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014359
14360 /* q_mode is only used for auto_imax */
14361 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14362 /* Use EQ Delay Register method for q_mode */
14363
14364 /* Convert for EQ Delay register */
14365 val = phba->cfg_fcp_imax;
14366 if (val) {
14367 /* First, interrupts per sec per EQ */
14368 val = phba->cfg_fcp_imax /
14369 phba->io_channel_irqs;
14370
14371 /* us delay between each interrupt */
14372 val = LPFC_SEC_TO_USEC / val;
14373 }
14374 eq->q_mode = val;
14375 } else {
14376 eq->q_mode = imax;
14377 }
14378
14379 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014380 break;
14381 }
14382 eq_delay->u.request.num_eq = cnt;
14383
14384 mbox->vport = phba->pport;
14385 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14386 mbox->context1 = NULL;
14387 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14388 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14389 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14390 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14391 if (shdr_status || shdr_add_status || rc) {
14392 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14393 "2512 MODIFY_EQ_DELAY mailbox failed with "
14394 "status x%x add_status x%x, mbx status x%x\n",
14395 shdr_status, shdr_add_status, rc);
14396 status = -ENXIO;
14397 }
14398 mempool_free(mbox, phba->mbox_mem_pool);
14399 return status;
14400}
14401
14402/**
James Smart4f774512009-05-22 14:52:35 -040014403 * lpfc_eq_create - Create an Event Queue on the HBA
14404 * @phba: HBA structure that indicates port to create a queue on.
14405 * @eq: The queue structure to use to create the event queue.
14406 * @imax: The maximum interrupt per second limit.
14407 *
14408 * This function creates an event queue, as detailed in @eq, on a port,
14409 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14410 *
14411 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14412 * is used to get the entry count and entry size that are necessary to
14413 * determine the number of pages to allocate and use for this queue. This
14414 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14415 * event queue. This function is asynchronous and will wait for the mailbox
14416 * command to finish before continuing.
14417 *
14418 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014419 * memory this function will return -ENOMEM. If the queue create mailbox command
14420 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014421 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014422int
James Smartee020062012-09-29 11:28:52 -040014423lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014424{
14425 struct lpfc_mbx_eq_create *eq_create;
14426 LPFC_MBOXQ_t *mbox;
14427 int rc, length, status = 0;
14428 struct lpfc_dmabuf *dmabuf;
14429 uint32_t shdr_status, shdr_add_status;
14430 union lpfc_sli4_cfg_shdr *shdr;
14431 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014432 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14433
James Smart2e90f4b2011-12-13 13:22:37 -050014434 /* sanity check on queue memory */
14435 if (!eq)
14436 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014437 if (!phba->sli4_hba.pc_sli4_params.supported)
14438 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014439
14440 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14441 if (!mbox)
14442 return -ENOMEM;
14443 length = (sizeof(struct lpfc_mbx_eq_create) -
14444 sizeof(struct lpfc_sli4_cfg_mhdr));
14445 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14446 LPFC_MBOX_OPCODE_EQ_CREATE,
14447 length, LPFC_SLI4_MBX_EMBED);
14448 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080014449 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014450 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14451 eq->page_count);
14452 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14453 LPFC_EQE_SIZE);
14454 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080014455
14456 /* Use version 2 of CREATE_EQ if eqav is set */
14457 if (phba->sli4_hba.pc_sli4_params.eqav) {
14458 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14459 LPFC_Q_CREATE_VERSION_2);
14460 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14461 phba->sli4_hba.pc_sli4_params.eqav);
14462 }
14463
James Smart2c9c5a02015-04-07 15:07:15 -040014464 /* don't setup delay multiplier using EQ_CREATE */
14465 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014466 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14467 dmult);
14468 switch (eq->entry_count) {
14469 default:
14470 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14471 "0360 Unsupported EQ count. (%d)\n",
14472 eq->entry_count);
14473 if (eq->entry_count < 256)
14474 return -EINVAL;
14475 /* otherwise default to smallest count (drop through) */
14476 case 256:
14477 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14478 LPFC_EQ_CNT_256);
14479 break;
14480 case 512:
14481 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14482 LPFC_EQ_CNT_512);
14483 break;
14484 case 1024:
14485 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14486 LPFC_EQ_CNT_1024);
14487 break;
14488 case 2048:
14489 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14490 LPFC_EQ_CNT_2048);
14491 break;
14492 case 4096:
14493 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14494 LPFC_EQ_CNT_4096);
14495 break;
14496 }
14497 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014498 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014499 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14500 putPaddrLow(dmabuf->phys);
14501 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14502 putPaddrHigh(dmabuf->phys);
14503 }
14504 mbox->vport = phba->pport;
14505 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14506 mbox->context1 = NULL;
14507 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040014508 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14509 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14510 if (shdr_status || shdr_add_status || rc) {
14511 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14512 "2500 EQ_CREATE mailbox failed with "
14513 "status x%x add_status x%x, mbx status x%x\n",
14514 shdr_status, shdr_add_status, rc);
14515 status = -ENXIO;
14516 }
14517 eq->type = LPFC_EQ;
14518 eq->subtype = LPFC_NONE;
14519 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14520 if (eq->queue_id == 0xFFFF)
14521 status = -ENXIO;
14522 eq->host_index = 0;
14523 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014524 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014525
James Smart8fa38512009-07-19 10:01:03 -040014526 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014527 return status;
14528}
14529
14530/**
14531 * lpfc_cq_create - Create a Completion Queue on the HBA
14532 * @phba: HBA structure that indicates port to create a queue on.
14533 * @cq: The queue structure to use to create the completion queue.
14534 * @eq: The event queue to bind this completion queue to.
14535 *
14536 * This function creates a completion queue, as detailed in @wq, on a port,
14537 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14538 *
14539 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14540 * is used to get the entry count and entry size that are necessary to
14541 * determine the number of pages to allocate and use for this queue. The @eq
14542 * is used to indicate which event queue to bind this completion queue to. This
14543 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14544 * completion queue. This function is asynchronous and will wait for the mailbox
14545 * command to finish before continuing.
14546 *
14547 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014548 * memory this function will return -ENOMEM. If the queue create mailbox command
14549 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014550 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014551int
James Smart4f774512009-05-22 14:52:35 -040014552lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14553 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14554{
14555 struct lpfc_mbx_cq_create *cq_create;
14556 struct lpfc_dmabuf *dmabuf;
14557 LPFC_MBOXQ_t *mbox;
14558 int rc, length, status = 0;
14559 uint32_t shdr_status, shdr_add_status;
14560 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014561 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14562
James Smart2e90f4b2011-12-13 13:22:37 -050014563 /* sanity check on queue memory */
14564 if (!cq || !eq)
14565 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014566 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014567 hw_page_size = cq->page_size;
James Smart49198b32010-04-06 15:04:33 -040014568
James Smart4f774512009-05-22 14:52:35 -040014569 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14570 if (!mbox)
14571 return -ENOMEM;
14572 length = (sizeof(struct lpfc_mbx_cq_create) -
14573 sizeof(struct lpfc_sli4_cfg_mhdr));
14574 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14575 LPFC_MBOX_OPCODE_CQ_CREATE,
14576 length, LPFC_SLI4_MBX_EMBED);
14577 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014578 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014579 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14580 cq->page_count);
14581 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14582 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014583 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14584 phba->sli4_hba.pc_sli4_params.cqv);
14585 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014586 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14587 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014588 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14589 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080014590 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14591 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050014592 } else {
14593 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14594 eq->queue_id);
14595 }
James Smart4f774512009-05-22 14:52:35 -040014596 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014597 case 2048:
14598 case 4096:
14599 if (phba->sli4_hba.pc_sli4_params.cqv ==
14600 LPFC_Q_CREATE_VERSION_2) {
14601 cq_create->u.request.context.lpfc_cq_context_count =
14602 cq->entry_count;
14603 bf_set(lpfc_cq_context_count,
14604 &cq_create->u.request.context,
14605 LPFC_CQ_CNT_WORD7);
14606 break;
14607 }
14608 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014609 default:
14610 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014611 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014612 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014613 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014614 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014615 if (cq->entry_count < 256) {
14616 status = -EINVAL;
14617 goto out;
14618 }
James Smart4f774512009-05-22 14:52:35 -040014619 /* otherwise default to smallest count (drop through) */
14620 case 256:
14621 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14622 LPFC_CQ_CNT_256);
14623 break;
14624 case 512:
14625 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14626 LPFC_CQ_CNT_512);
14627 break;
14628 case 1024:
14629 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14630 LPFC_CQ_CNT_1024);
14631 break;
14632 }
14633 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014634 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014635 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14636 putPaddrLow(dmabuf->phys);
14637 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14638 putPaddrHigh(dmabuf->phys);
14639 }
14640 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14641
14642 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014643 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14644 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14645 if (shdr_status || shdr_add_status || rc) {
14646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14647 "2501 CQ_CREATE mailbox failed with "
14648 "status x%x add_status x%x, mbx status x%x\n",
14649 shdr_status, shdr_add_status, rc);
14650 status = -ENXIO;
14651 goto out;
14652 }
14653 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14654 if (cq->queue_id == 0xFFFF) {
14655 status = -ENXIO;
14656 goto out;
14657 }
14658 /* link the cq onto the parent eq child list */
14659 list_add_tail(&cq->list, &eq->child_list);
14660 /* Set up completion queue's type and subtype */
14661 cq->type = type;
14662 cq->subtype = subtype;
14663 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014664 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014665 cq->host_index = 0;
14666 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014667 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014668
James Smart8fa38512009-07-19 10:01:03 -040014669out:
14670 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014671 return status;
14672}
14673
14674/**
James Smart2d7dbc42017-02-12 13:52:35 -080014675 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14676 * @phba: HBA structure that indicates port to create a queue on.
14677 * @cqp: The queue structure array to use to create the completion queues.
14678 * @eqp: The event queue array to bind these completion queues to.
14679 *
14680 * This function creates a set of completion queue, s to support MRQ
14681 * as detailed in @cqp, on a port,
14682 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14683 *
14684 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14685 * is used to get the entry count and entry size that are necessary to
14686 * determine the number of pages to allocate and use for this queue. The @eq
14687 * is used to indicate which event queue to bind this completion queue to. This
14688 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14689 * completion queue. This function is asynchronous and will wait for the mailbox
14690 * command to finish before continuing.
14691 *
14692 * On success this function will return a zero. If unable to allocate enough
14693 * memory this function will return -ENOMEM. If the queue create mailbox command
14694 * fails this function will return -ENXIO.
14695 **/
14696int
14697lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14698 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14699{
14700 struct lpfc_queue *cq;
14701 struct lpfc_queue *eq;
14702 struct lpfc_mbx_cq_create_set *cq_set;
14703 struct lpfc_dmabuf *dmabuf;
14704 LPFC_MBOXQ_t *mbox;
14705 int rc, length, alloclen, status = 0;
14706 int cnt, idx, numcq, page_idx = 0;
14707 uint32_t shdr_status, shdr_add_status;
14708 union lpfc_sli4_cfg_shdr *shdr;
14709 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14710
14711 /* sanity check on queue memory */
14712 numcq = phba->cfg_nvmet_mrq;
14713 if (!cqp || !eqp || !numcq)
14714 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014715
14716 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14717 if (!mbox)
14718 return -ENOMEM;
14719
14720 length = sizeof(struct lpfc_mbx_cq_create_set);
14721 length += ((numcq * cqp[0]->page_count) *
14722 sizeof(struct dma_address));
14723 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14724 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14725 LPFC_SLI4_MBX_NEMBED);
14726 if (alloclen < length) {
14727 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14728 "3098 Allocated DMA memory size (%d) is "
14729 "less than the requested DMA memory size "
14730 "(%d)\n", alloclen, length);
14731 status = -ENOMEM;
14732 goto out;
14733 }
14734 cq_set = mbox->sge_array->addr[0];
14735 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14736 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14737
14738 for (idx = 0; idx < numcq; idx++) {
14739 cq = cqp[idx];
14740 eq = eqp[idx];
14741 if (!cq || !eq) {
14742 status = -ENOMEM;
14743 goto out;
14744 }
James Smart81b96ed2017-11-20 16:00:29 -080014745 if (!phba->sli4_hba.pc_sli4_params.supported)
14746 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014747
14748 switch (idx) {
14749 case 0:
14750 bf_set(lpfc_mbx_cq_create_set_page_size,
14751 &cq_set->u.request,
14752 (hw_page_size / SLI4_PAGE_SIZE));
14753 bf_set(lpfc_mbx_cq_create_set_num_pages,
14754 &cq_set->u.request, cq->page_count);
14755 bf_set(lpfc_mbx_cq_create_set_evt,
14756 &cq_set->u.request, 1);
14757 bf_set(lpfc_mbx_cq_create_set_valid,
14758 &cq_set->u.request, 1);
14759 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14760 &cq_set->u.request, 0);
14761 bf_set(lpfc_mbx_cq_create_set_num_cq,
14762 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080014763 bf_set(lpfc_mbx_cq_create_set_autovalid,
14764 &cq_set->u.request,
14765 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080014766 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014767 case 2048:
14768 case 4096:
14769 if (phba->sli4_hba.pc_sli4_params.cqv ==
14770 LPFC_Q_CREATE_VERSION_2) {
14771 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14772 &cq_set->u.request,
14773 cq->entry_count);
14774 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14775 &cq_set->u.request,
14776 LPFC_CQ_CNT_WORD7);
14777 break;
14778 }
14779 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080014780 default:
14781 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14782 "3118 Bad CQ count. (%d)\n",
14783 cq->entry_count);
14784 if (cq->entry_count < 256) {
14785 status = -EINVAL;
14786 goto out;
14787 }
14788 /* otherwise default to smallest (drop thru) */
14789 case 256:
14790 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14791 &cq_set->u.request, LPFC_CQ_CNT_256);
14792 break;
14793 case 512:
14794 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14795 &cq_set->u.request, LPFC_CQ_CNT_512);
14796 break;
14797 case 1024:
14798 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14799 &cq_set->u.request, LPFC_CQ_CNT_1024);
14800 break;
14801 }
14802 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14803 &cq_set->u.request, eq->queue_id);
14804 break;
14805 case 1:
14806 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14807 &cq_set->u.request, eq->queue_id);
14808 break;
14809 case 2:
14810 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14811 &cq_set->u.request, eq->queue_id);
14812 break;
14813 case 3:
14814 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14815 &cq_set->u.request, eq->queue_id);
14816 break;
14817 case 4:
14818 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14819 &cq_set->u.request, eq->queue_id);
14820 break;
14821 case 5:
14822 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14823 &cq_set->u.request, eq->queue_id);
14824 break;
14825 case 6:
14826 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14827 &cq_set->u.request, eq->queue_id);
14828 break;
14829 case 7:
14830 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14831 &cq_set->u.request, eq->queue_id);
14832 break;
14833 case 8:
14834 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14835 &cq_set->u.request, eq->queue_id);
14836 break;
14837 case 9:
14838 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14839 &cq_set->u.request, eq->queue_id);
14840 break;
14841 case 10:
14842 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14843 &cq_set->u.request, eq->queue_id);
14844 break;
14845 case 11:
14846 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14847 &cq_set->u.request, eq->queue_id);
14848 break;
14849 case 12:
14850 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14851 &cq_set->u.request, eq->queue_id);
14852 break;
14853 case 13:
14854 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14855 &cq_set->u.request, eq->queue_id);
14856 break;
14857 case 14:
14858 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14859 &cq_set->u.request, eq->queue_id);
14860 break;
14861 case 15:
14862 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14863 &cq_set->u.request, eq->queue_id);
14864 break;
14865 }
14866
14867 /* link the cq onto the parent eq child list */
14868 list_add_tail(&cq->list, &eq->child_list);
14869 /* Set up completion queue's type and subtype */
14870 cq->type = type;
14871 cq->subtype = subtype;
14872 cq->assoc_qid = eq->queue_id;
14873 cq->host_index = 0;
14874 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014875 cq->entry_repost = LPFC_CQ_REPOST;
James Smart81b96ed2017-11-20 16:00:29 -080014876 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080014877
14878 rc = 0;
14879 list_for_each_entry(dmabuf, &cq->page_list, list) {
14880 memset(dmabuf->virt, 0, hw_page_size);
14881 cnt = page_idx + dmabuf->buffer_tag;
14882 cq_set->u.request.page[cnt].addr_lo =
14883 putPaddrLow(dmabuf->phys);
14884 cq_set->u.request.page[cnt].addr_hi =
14885 putPaddrHigh(dmabuf->phys);
14886 rc++;
14887 }
14888 page_idx += rc;
14889 }
14890
14891 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14892
14893 /* The IOCTL status is embedded in the mailbox subheader. */
14894 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14895 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14896 if (shdr_status || shdr_add_status || rc) {
14897 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14898 "3119 CQ_CREATE_SET mailbox failed with "
14899 "status x%x add_status x%x, mbx status x%x\n",
14900 shdr_status, shdr_add_status, rc);
14901 status = -ENXIO;
14902 goto out;
14903 }
14904 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14905 if (rc == 0xFFFF) {
14906 status = -ENXIO;
14907 goto out;
14908 }
14909
14910 for (idx = 0; idx < numcq; idx++) {
14911 cq = cqp[idx];
14912 cq->queue_id = rc + idx;
14913 }
14914
14915out:
14916 lpfc_sli4_mbox_cmd_free(phba, mbox);
14917 return status;
14918}
14919
14920/**
James Smartb19a0612010-04-06 14:48:51 -040014921 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014922 * @phba: HBA structure that indicates port to create a queue on.
14923 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014924 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14925 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014926 *
James Smartb19a0612010-04-06 14:48:51 -040014927 * This function provides failback (fb) functionality when the
14928 * mq_create_ext fails on older FW generations. It's purpose is identical
14929 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014930 *
James Smartb19a0612010-04-06 14:48:51 -040014931 * This routine cannot fail as all attributes were previously accessed and
14932 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014933 **/
James Smartb19a0612010-04-06 14:48:51 -040014934static void
14935lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14936 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014937{
14938 struct lpfc_mbx_mq_create *mq_create;
14939 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014940 int length;
James Smart04c68492009-05-22 14:52:52 -040014941
James Smart04c68492009-05-22 14:52:52 -040014942 length = (sizeof(struct lpfc_mbx_mq_create) -
14943 sizeof(struct lpfc_sli4_cfg_mhdr));
14944 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14945 LPFC_MBOX_OPCODE_MQ_CREATE,
14946 length, LPFC_SLI4_MBX_EMBED);
14947 mq_create = &mbox->u.mqe.un.mq_create;
14948 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014949 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014950 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014951 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014952 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14953 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014954 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014955 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14956 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014957 break;
14958 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014959 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14960 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014961 break;
14962 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014963 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14964 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014965 break;
14966 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014967 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14968 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014969 break;
14970 }
14971 list_for_each_entry(dmabuf, &mq->page_list, list) {
14972 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014973 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014974 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014975 putPaddrHigh(dmabuf->phys);
14976 }
14977}
14978
14979/**
14980 * lpfc_mq_create - Create a mailbox Queue on the HBA
14981 * @phba: HBA structure that indicates port to create a queue on.
14982 * @mq: The queue structure to use to create the mailbox queue.
14983 * @cq: The completion queue to associate with this cq.
14984 * @subtype: The queue's subtype.
14985 *
14986 * This function creates a mailbox queue, as detailed in @mq, on a port,
14987 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14988 *
14989 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14990 * is used to get the entry count and entry size that are necessary to
14991 * determine the number of pages to allocate and use for this queue. This
14992 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14993 * mailbox queue. This function is asynchronous and will wait for the mailbox
14994 * command to finish before continuing.
14995 *
14996 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014997 * memory this function will return -ENOMEM. If the queue create mailbox command
14998 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014999 **/
15000int32_t
15001lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15002 struct lpfc_queue *cq, uint32_t subtype)
15003{
15004 struct lpfc_mbx_mq_create *mq_create;
15005 struct lpfc_mbx_mq_create_ext *mq_create_ext;
15006 struct lpfc_dmabuf *dmabuf;
15007 LPFC_MBOXQ_t *mbox;
15008 int rc, length, status = 0;
15009 uint32_t shdr_status, shdr_add_status;
15010 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015011 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040015012
James Smart2e90f4b2011-12-13 13:22:37 -050015013 /* sanity check on queue memory */
15014 if (!mq || !cq)
15015 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015016 if (!phba->sli4_hba.pc_sli4_params.supported)
15017 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040015018
15019 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15020 if (!mbox)
15021 return -ENOMEM;
15022 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15023 sizeof(struct lpfc_sli4_cfg_mhdr));
15024 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15025 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15026 length, LPFC_SLI4_MBX_EMBED);
15027
15028 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015029 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015030 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15031 &mq_create_ext->u.request, mq->page_count);
15032 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15033 &mq_create_ext->u.request, 1);
15034 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015035 &mq_create_ext->u.request, 1);
15036 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15037 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015038 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15039 &mq_create_ext->u.request, 1);
15040 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15041 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015042 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015043 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15044 phba->sli4_hba.pc_sli4_params.mqv);
15045 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15046 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15047 cq->queue_id);
15048 else
15049 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15050 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015051 switch (mq->entry_count) {
15052 default:
15053 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15054 "0362 Unsupported MQ count. (%d)\n",
15055 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015056 if (mq->entry_count < 16) {
15057 status = -EINVAL;
15058 goto out;
15059 }
James Smartb19a0612010-04-06 14:48:51 -040015060 /* otherwise default to smallest count (drop through) */
15061 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015062 bf_set(lpfc_mq_context_ring_size,
15063 &mq_create_ext->u.request.context,
15064 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015065 break;
15066 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015067 bf_set(lpfc_mq_context_ring_size,
15068 &mq_create_ext->u.request.context,
15069 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015070 break;
15071 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015072 bf_set(lpfc_mq_context_ring_size,
15073 &mq_create_ext->u.request.context,
15074 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015075 break;
15076 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015077 bf_set(lpfc_mq_context_ring_size,
15078 &mq_create_ext->u.request.context,
15079 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015080 break;
15081 }
15082 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015083 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015084 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15085 putPaddrLow(dmabuf->phys);
15086 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015087 putPaddrHigh(dmabuf->phys);
15088 }
15089 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015090 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15091 &mq_create_ext->u.response);
15092 if (rc != MBX_SUCCESS) {
15093 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15094 "2795 MQ_CREATE_EXT failed with "
15095 "status x%x. Failback to MQ_CREATE.\n",
15096 rc);
15097 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15098 mq_create = &mbox->u.mqe.un.mq_create;
15099 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15100 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15101 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15102 &mq_create->u.response);
15103 }
15104
James Smart04c68492009-05-22 14:52:52 -040015105 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015106 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15107 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15108 if (shdr_status || shdr_add_status || rc) {
15109 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15110 "2502 MQ_CREATE mailbox failed with "
15111 "status x%x add_status x%x, mbx status x%x\n",
15112 shdr_status, shdr_add_status, rc);
15113 status = -ENXIO;
15114 goto out;
15115 }
James Smart04c68492009-05-22 14:52:52 -040015116 if (mq->queue_id == 0xFFFF) {
15117 status = -ENXIO;
15118 goto out;
15119 }
15120 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015121 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015122 mq->subtype = subtype;
15123 mq->host_index = 0;
15124 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070015125 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040015126
15127 /* link the mq onto the parent cq child list */
15128 list_add_tail(&mq->list, &cq->child_list);
15129out:
James Smart8fa38512009-07-19 10:01:03 -040015130 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015131 return status;
15132}
15133
15134/**
James Smart4f774512009-05-22 14:52:35 -040015135 * lpfc_wq_create - Create a Work Queue on the HBA
15136 * @phba: HBA structure that indicates port to create a queue on.
15137 * @wq: The queue structure to use to create the work queue.
15138 * @cq: The completion queue to bind this work queue to.
15139 * @subtype: The subtype of the work queue indicating its functionality.
15140 *
15141 * This function creates a work queue, as detailed in @wq, on a port, described
15142 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15143 *
15144 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15145 * is used to get the entry count and entry size that are necessary to
15146 * determine the number of pages to allocate and use for this queue. The @cq
15147 * is used to indicate which completion queue to bind this work queue to. This
15148 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15149 * work queue. This function is asynchronous and will wait for the mailbox
15150 * command to finish before continuing.
15151 *
15152 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015153 * memory this function will return -ENOMEM. If the queue create mailbox command
15154 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015155 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015156int
James Smart4f774512009-05-22 14:52:35 -040015157lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15158 struct lpfc_queue *cq, uint32_t subtype)
15159{
15160 struct lpfc_mbx_wq_create *wq_create;
15161 struct lpfc_dmabuf *dmabuf;
15162 LPFC_MBOXQ_t *mbox;
15163 int rc, length, status = 0;
15164 uint32_t shdr_status, shdr_add_status;
15165 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015166 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015167 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015168 void __iomem *bar_memmap_p;
15169 uint32_t db_offset;
15170 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015171 uint8_t dpp_barset;
15172 uint32_t dpp_offset;
15173 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015174 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015175
James Smart2e90f4b2011-12-13 13:22:37 -050015176 /* sanity check on queue memory */
15177 if (!wq || !cq)
15178 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015179 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015180 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015181
15182 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15183 if (!mbox)
15184 return -ENOMEM;
15185 length = (sizeof(struct lpfc_mbx_wq_create) -
15186 sizeof(struct lpfc_sli4_cfg_mhdr));
15187 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15188 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15189 length, LPFC_SLI4_MBX_EMBED);
15190 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015191 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015192 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15193 wq->page_count);
15194 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15195 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015196
15197 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015198 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15199 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015200
James Smartc176ffa2018-01-30 15:58:46 -080015201 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15202 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015203 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15204 else
15205 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15206
James Smart0c651872013-07-15 18:33:23 -040015207
James Smart1351e692018-02-22 08:18:43 -080015208 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15209 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15210 else
15211 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15212
15213 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015214 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015215 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15216 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015217 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15218 LPFC_Q_CREATE_VERSION_1);
15219
James Smart5a6f1332011-03-11 16:05:35 -050015220 switch (wq->entry_size) {
15221 default:
15222 case 64:
15223 bf_set(lpfc_mbx_wq_create_wqe_size,
15224 &wq_create->u.request_1,
15225 LPFC_WQ_WQE_SIZE_64);
15226 break;
15227 case 128:
15228 bf_set(lpfc_mbx_wq_create_wqe_size,
15229 &wq_create->u.request_1,
15230 LPFC_WQ_WQE_SIZE_128);
15231 break;
15232 }
James Smart1351e692018-02-22 08:18:43 -080015233 /* Request DPP by default */
15234 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015235 bf_set(lpfc_mbx_wq_create_page_size,
15236 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015237 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015238 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015239 break;
15240 default:
James Smart1351e692018-02-22 08:18:43 -080015241 page = wq_create->u.request.page;
15242 break;
James Smart5a6f1332011-03-11 16:05:35 -050015243 }
James Smart0c651872013-07-15 18:33:23 -040015244
James Smart4f774512009-05-22 14:52:35 -040015245 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015246 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015247 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15248 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015249 }
James Smart962bc512013-01-03 15:44:00 -050015250
15251 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15252 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15253
James Smart4f774512009-05-22 14:52:35 -040015254 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15255 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015256 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15257 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15258 if (shdr_status || shdr_add_status || rc) {
15259 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15260 "2503 WQ_CREATE mailbox failed with "
15261 "status x%x add_status x%x, mbx status x%x\n",
15262 shdr_status, shdr_add_status, rc);
15263 status = -ENXIO;
15264 goto out;
15265 }
James Smart1351e692018-02-22 08:18:43 -080015266
15267 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15268 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15269 &wq_create->u.response);
15270 else
15271 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15272 &wq_create->u.response_1);
15273
James Smart4f774512009-05-22 14:52:35 -040015274 if (wq->queue_id == 0xFFFF) {
15275 status = -ENXIO;
15276 goto out;
15277 }
James Smart1351e692018-02-22 08:18:43 -080015278
15279 wq->db_format = LPFC_DB_LIST_FORMAT;
15280 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15281 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15282 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15283 &wq_create->u.response);
15284 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15285 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15287 "3265 WQ[%d] doorbell format "
15288 "not supported: x%x\n",
15289 wq->queue_id, wq->db_format);
15290 status = -EINVAL;
15291 goto out;
15292 }
15293 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15294 &wq_create->u.response);
15295 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15296 pci_barset);
15297 if (!bar_memmap_p) {
15298 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15299 "3263 WQ[%d] failed to memmap "
15300 "pci barset:x%x\n",
15301 wq->queue_id, pci_barset);
15302 status = -ENOMEM;
15303 goto out;
15304 }
15305 db_offset = wq_create->u.response.doorbell_offset;
15306 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15307 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15308 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15309 "3252 WQ[%d] doorbell offset "
15310 "not supported: x%x\n",
15311 wq->queue_id, db_offset);
15312 status = -EINVAL;
15313 goto out;
15314 }
15315 wq->db_regaddr = bar_memmap_p + db_offset;
15316 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15317 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15318 "format:x%x\n", wq->queue_id,
15319 pci_barset, db_offset, wq->db_format);
15320 } else
15321 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015322 } else {
James Smart1351e692018-02-22 08:18:43 -080015323 /* Check if DPP was honored by the firmware */
15324 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15325 &wq_create->u.response_1);
15326 if (wq->dpp_enable) {
15327 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15328 &wq_create->u.response_1);
15329 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15330 pci_barset);
15331 if (!bar_memmap_p) {
15332 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15333 "3267 WQ[%d] failed to memmap "
15334 "pci barset:x%x\n",
15335 wq->queue_id, pci_barset);
15336 status = -ENOMEM;
15337 goto out;
15338 }
15339 db_offset = wq_create->u.response_1.doorbell_offset;
15340 wq->db_regaddr = bar_memmap_p + db_offset;
15341 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15342 &wq_create->u.response_1);
15343 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15344 &wq_create->u.response_1);
15345 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15346 dpp_barset);
15347 if (!bar_memmap_p) {
15348 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15349 "3268 WQ[%d] failed to memmap "
15350 "pci barset:x%x\n",
15351 wq->queue_id, dpp_barset);
15352 status = -ENOMEM;
15353 goto out;
15354 }
15355 dpp_offset = wq_create->u.response_1.dpp_offset;
15356 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15357 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15358 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15359 "dpp_id:x%x dpp_barset:x%x "
15360 "dpp_offset:x%x\n",
15361 wq->queue_id, pci_barset, db_offset,
15362 wq->dpp_id, dpp_barset, dpp_offset);
15363
15364 /* Enable combined writes for DPP aperture */
15365 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15366#ifdef CONFIG_X86
15367 rc = set_memory_wc(pg_addr, 1);
15368 if (rc) {
15369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15370 "3272 Cannot setup Combined "
15371 "Write on WQ[%d] - disable DPP\n",
15372 wq->queue_id);
15373 phba->cfg_enable_dpp = 0;
15374 }
15375#else
15376 phba->cfg_enable_dpp = 0;
15377#endif
15378 } else
15379 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015380 }
James Smart895427b2017-02-12 13:52:30 -080015381 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15382 if (wq->pring == NULL) {
15383 status = -ENOMEM;
15384 goto out;
15385 }
James Smart4f774512009-05-22 14:52:35 -040015386 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015387 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015388 wq->subtype = subtype;
15389 wq->host_index = 0;
15390 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050015391 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040015392
15393 /* link the wq onto the parent cq child list */
15394 list_add_tail(&wq->list, &cq->child_list);
15395out:
James Smart8fa38512009-07-19 10:01:03 -040015396 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015397 return status;
15398}
15399
15400/**
15401 * lpfc_rq_create - Create a Receive Queue on the HBA
15402 * @phba: HBA structure that indicates port to create a queue on.
15403 * @hrq: The queue structure to use to create the header receive queue.
15404 * @drq: The queue structure to use to create the data receive queue.
15405 * @cq: The completion queue to bind this work queue to.
15406 *
15407 * This function creates a receive buffer queue pair , as detailed in @hrq and
15408 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15409 * to the HBA.
15410 *
15411 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15412 * struct is used to get the entry count that is necessary to determine the
15413 * number of pages to use for this queue. The @cq is used to indicate which
15414 * completion queue to bind received buffers that are posted to these queues to.
15415 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15416 * receive queue pair. This function is asynchronous and will wait for the
15417 * mailbox command to finish before continuing.
15418 *
15419 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015420 * memory this function will return -ENOMEM. If the queue create mailbox command
15421 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015422 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015423int
James Smart4f774512009-05-22 14:52:35 -040015424lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15425 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15426{
15427 struct lpfc_mbx_rq_create *rq_create;
15428 struct lpfc_dmabuf *dmabuf;
15429 LPFC_MBOXQ_t *mbox;
15430 int rc, length, status = 0;
15431 uint32_t shdr_status, shdr_add_status;
15432 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015433 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015434 void __iomem *bar_memmap_p;
15435 uint32_t db_offset;
15436 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015437
James Smart2e90f4b2011-12-13 13:22:37 -050015438 /* sanity check on queue memory */
15439 if (!hrq || !drq || !cq)
15440 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015441 if (!phba->sli4_hba.pc_sli4_params.supported)
15442 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015443
15444 if (hrq->entry_count != drq->entry_count)
15445 return -EINVAL;
15446 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15447 if (!mbox)
15448 return -ENOMEM;
15449 length = (sizeof(struct lpfc_mbx_rq_create) -
15450 sizeof(struct lpfc_sli4_cfg_mhdr));
15451 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15452 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15453 length, LPFC_SLI4_MBX_EMBED);
15454 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015455 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15456 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15457 phba->sli4_hba.pc_sli4_params.rqv);
15458 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15459 bf_set(lpfc_rq_context_rqe_count_1,
15460 &rq_create->u.request.context,
15461 hrq->entry_count);
15462 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015463 bf_set(lpfc_rq_context_rqe_size,
15464 &rq_create->u.request.context,
15465 LPFC_RQE_SIZE_8);
15466 bf_set(lpfc_rq_context_page_size,
15467 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015468 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015469 } else {
15470 switch (hrq->entry_count) {
15471 default:
15472 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15473 "2535 Unsupported RQ count. (%d)\n",
15474 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015475 if (hrq->entry_count < 512) {
15476 status = -EINVAL;
15477 goto out;
15478 }
James Smart5a6f1332011-03-11 16:05:35 -050015479 /* otherwise default to smallest count (drop through) */
15480 case 512:
15481 bf_set(lpfc_rq_context_rqe_count,
15482 &rq_create->u.request.context,
15483 LPFC_RQ_RING_SIZE_512);
15484 break;
15485 case 1024:
15486 bf_set(lpfc_rq_context_rqe_count,
15487 &rq_create->u.request.context,
15488 LPFC_RQ_RING_SIZE_1024);
15489 break;
15490 case 2048:
15491 bf_set(lpfc_rq_context_rqe_count,
15492 &rq_create->u.request.context,
15493 LPFC_RQ_RING_SIZE_2048);
15494 break;
15495 case 4096:
15496 bf_set(lpfc_rq_context_rqe_count,
15497 &rq_create->u.request.context,
15498 LPFC_RQ_RING_SIZE_4096);
15499 break;
15500 }
15501 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15502 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015503 }
15504 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15505 cq->queue_id);
15506 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15507 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015508 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015509 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015510 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15511 putPaddrLow(dmabuf->phys);
15512 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15513 putPaddrHigh(dmabuf->phys);
15514 }
James Smart962bc512013-01-03 15:44:00 -050015515 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15516 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15517
James Smart4f774512009-05-22 14:52:35 -040015518 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15519 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015520 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15521 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15522 if (shdr_status || shdr_add_status || rc) {
15523 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15524 "2504 RQ_CREATE mailbox failed with "
15525 "status x%x add_status x%x, mbx status x%x\n",
15526 shdr_status, shdr_add_status, rc);
15527 status = -ENXIO;
15528 goto out;
15529 }
15530 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15531 if (hrq->queue_id == 0xFFFF) {
15532 status = -ENXIO;
15533 goto out;
15534 }
James Smart962bc512013-01-03 15:44:00 -050015535
15536 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15537 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15538 &rq_create->u.response);
15539 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15540 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15542 "3262 RQ [%d] doorbell format not "
15543 "supported: x%x\n", hrq->queue_id,
15544 hrq->db_format);
15545 status = -EINVAL;
15546 goto out;
15547 }
15548
15549 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15550 &rq_create->u.response);
15551 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15552 if (!bar_memmap_p) {
15553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15554 "3269 RQ[%d] failed to memmap pci "
15555 "barset:x%x\n", hrq->queue_id,
15556 pci_barset);
15557 status = -ENOMEM;
15558 goto out;
15559 }
15560
15561 db_offset = rq_create->u.response.doorbell_offset;
15562 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15563 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15564 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15565 "3270 RQ[%d] doorbell offset not "
15566 "supported: x%x\n", hrq->queue_id,
15567 db_offset);
15568 status = -EINVAL;
15569 goto out;
15570 }
15571 hrq->db_regaddr = bar_memmap_p + db_offset;
15572 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015573 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15574 "format:x%x\n", hrq->queue_id, pci_barset,
15575 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015576 } else {
15577 hrq->db_format = LPFC_DB_RING_FORMAT;
15578 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15579 }
James Smart4f774512009-05-22 14:52:35 -040015580 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015581 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015582 hrq->subtype = subtype;
15583 hrq->host_index = 0;
15584 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015585 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015586
15587 /* now create the data queue */
15588 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15589 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15590 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015591 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15592 phba->sli4_hba.pc_sli4_params.rqv);
15593 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15594 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015595 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015596 if (subtype == LPFC_NVMET)
15597 rq_create->u.request.context.buffer_size =
15598 LPFC_NVMET_DATA_BUF_SIZE;
15599 else
15600 rq_create->u.request.context.buffer_size =
15601 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015602 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15603 LPFC_RQE_SIZE_8);
15604 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15605 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015606 } else {
15607 switch (drq->entry_count) {
15608 default:
15609 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15610 "2536 Unsupported RQ count. (%d)\n",
15611 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015612 if (drq->entry_count < 512) {
15613 status = -EINVAL;
15614 goto out;
15615 }
James Smart5a6f1332011-03-11 16:05:35 -050015616 /* otherwise default to smallest count (drop through) */
15617 case 512:
15618 bf_set(lpfc_rq_context_rqe_count,
15619 &rq_create->u.request.context,
15620 LPFC_RQ_RING_SIZE_512);
15621 break;
15622 case 1024:
15623 bf_set(lpfc_rq_context_rqe_count,
15624 &rq_create->u.request.context,
15625 LPFC_RQ_RING_SIZE_1024);
15626 break;
15627 case 2048:
15628 bf_set(lpfc_rq_context_rqe_count,
15629 &rq_create->u.request.context,
15630 LPFC_RQ_RING_SIZE_2048);
15631 break;
15632 case 4096:
15633 bf_set(lpfc_rq_context_rqe_count,
15634 &rq_create->u.request.context,
15635 LPFC_RQ_RING_SIZE_4096);
15636 break;
15637 }
James Smart3c603be2017-05-15 15:20:44 -070015638 if (subtype == LPFC_NVMET)
15639 bf_set(lpfc_rq_context_buf_size,
15640 &rq_create->u.request.context,
15641 LPFC_NVMET_DATA_BUF_SIZE);
15642 else
15643 bf_set(lpfc_rq_context_buf_size,
15644 &rq_create->u.request.context,
15645 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015646 }
15647 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15648 cq->queue_id);
15649 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15650 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015651 list_for_each_entry(dmabuf, &drq->page_list, list) {
15652 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15653 putPaddrLow(dmabuf->phys);
15654 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15655 putPaddrHigh(dmabuf->phys);
15656 }
James Smart962bc512013-01-03 15:44:00 -050015657 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15658 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015659 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15660 /* The IOCTL status is embedded in the mailbox subheader. */
15661 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15662 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15663 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15664 if (shdr_status || shdr_add_status || rc) {
15665 status = -ENXIO;
15666 goto out;
15667 }
15668 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15669 if (drq->queue_id == 0xFFFF) {
15670 status = -ENXIO;
15671 goto out;
15672 }
15673 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015674 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015675 drq->subtype = subtype;
15676 drq->host_index = 0;
15677 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015678 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015679
15680 /* link the header and data RQs onto the parent cq child list */
15681 list_add_tail(&hrq->list, &cq->child_list);
15682 list_add_tail(&drq->list, &cq->child_list);
15683
15684out:
James Smart8fa38512009-07-19 10:01:03 -040015685 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015686 return status;
15687}
15688
15689/**
James Smart2d7dbc42017-02-12 13:52:35 -080015690 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15691 * @phba: HBA structure that indicates port to create a queue on.
15692 * @hrqp: The queue structure array to use to create the header receive queues.
15693 * @drqp: The queue structure array to use to create the data receive queues.
15694 * @cqp: The completion queue array to bind these receive queues to.
15695 *
15696 * This function creates a receive buffer queue pair , as detailed in @hrq and
15697 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15698 * to the HBA.
15699 *
15700 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15701 * struct is used to get the entry count that is necessary to determine the
15702 * number of pages to use for this queue. The @cq is used to indicate which
15703 * completion queue to bind received buffers that are posted to these queues to.
15704 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15705 * receive queue pair. This function is asynchronous and will wait for the
15706 * mailbox command to finish before continuing.
15707 *
15708 * On success this function will return a zero. If unable to allocate enough
15709 * memory this function will return -ENOMEM. If the queue create mailbox command
15710 * fails this function will return -ENXIO.
15711 **/
15712int
15713lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15714 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15715 uint32_t subtype)
15716{
15717 struct lpfc_queue *hrq, *drq, *cq;
15718 struct lpfc_mbx_rq_create_v2 *rq_create;
15719 struct lpfc_dmabuf *dmabuf;
15720 LPFC_MBOXQ_t *mbox;
15721 int rc, length, alloclen, status = 0;
15722 int cnt, idx, numrq, page_idx = 0;
15723 uint32_t shdr_status, shdr_add_status;
15724 union lpfc_sli4_cfg_shdr *shdr;
15725 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15726
15727 numrq = phba->cfg_nvmet_mrq;
15728 /* sanity check on array memory */
15729 if (!hrqp || !drqp || !cqp || !numrq)
15730 return -ENODEV;
15731 if (!phba->sli4_hba.pc_sli4_params.supported)
15732 hw_page_size = SLI4_PAGE_SIZE;
15733
15734 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15735 if (!mbox)
15736 return -ENOMEM;
15737
15738 length = sizeof(struct lpfc_mbx_rq_create_v2);
15739 length += ((2 * numrq * hrqp[0]->page_count) *
15740 sizeof(struct dma_address));
15741
15742 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15743 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15744 LPFC_SLI4_MBX_NEMBED);
15745 if (alloclen < length) {
15746 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15747 "3099 Allocated DMA memory size (%d) is "
15748 "less than the requested DMA memory size "
15749 "(%d)\n", alloclen, length);
15750 status = -ENOMEM;
15751 goto out;
15752 }
15753
15754
15755
15756 rq_create = mbox->sge_array->addr[0];
15757 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15758
15759 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15760 cnt = 0;
15761
15762 for (idx = 0; idx < numrq; idx++) {
15763 hrq = hrqp[idx];
15764 drq = drqp[idx];
15765 cq = cqp[idx];
15766
James Smart2d7dbc42017-02-12 13:52:35 -080015767 /* sanity check on queue memory */
15768 if (!hrq || !drq || !cq) {
15769 status = -ENODEV;
15770 goto out;
15771 }
15772
James Smart7aabe842017-03-04 09:30:22 -080015773 if (hrq->entry_count != drq->entry_count) {
15774 status = -EINVAL;
15775 goto out;
15776 }
15777
James Smart2d7dbc42017-02-12 13:52:35 -080015778 if (idx == 0) {
15779 bf_set(lpfc_mbx_rq_create_num_pages,
15780 &rq_create->u.request,
15781 hrq->page_count);
15782 bf_set(lpfc_mbx_rq_create_rq_cnt,
15783 &rq_create->u.request, (numrq * 2));
15784 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15785 1);
15786 bf_set(lpfc_rq_context_base_cq,
15787 &rq_create->u.request.context,
15788 cq->queue_id);
15789 bf_set(lpfc_rq_context_data_size,
15790 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015791 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015792 bf_set(lpfc_rq_context_hdr_size,
15793 &rq_create->u.request.context,
15794 LPFC_HDR_BUF_SIZE);
15795 bf_set(lpfc_rq_context_rqe_count_1,
15796 &rq_create->u.request.context,
15797 hrq->entry_count);
15798 bf_set(lpfc_rq_context_rqe_size,
15799 &rq_create->u.request.context,
15800 LPFC_RQE_SIZE_8);
15801 bf_set(lpfc_rq_context_page_size,
15802 &rq_create->u.request.context,
15803 (PAGE_SIZE/SLI4_PAGE_SIZE));
15804 }
15805 rc = 0;
15806 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15807 memset(dmabuf->virt, 0, hw_page_size);
15808 cnt = page_idx + dmabuf->buffer_tag;
15809 rq_create->u.request.page[cnt].addr_lo =
15810 putPaddrLow(dmabuf->phys);
15811 rq_create->u.request.page[cnt].addr_hi =
15812 putPaddrHigh(dmabuf->phys);
15813 rc++;
15814 }
15815 page_idx += rc;
15816
15817 rc = 0;
15818 list_for_each_entry(dmabuf, &drq->page_list, list) {
15819 memset(dmabuf->virt, 0, hw_page_size);
15820 cnt = page_idx + dmabuf->buffer_tag;
15821 rq_create->u.request.page[cnt].addr_lo =
15822 putPaddrLow(dmabuf->phys);
15823 rq_create->u.request.page[cnt].addr_hi =
15824 putPaddrHigh(dmabuf->phys);
15825 rc++;
15826 }
15827 page_idx += rc;
15828
15829 hrq->db_format = LPFC_DB_RING_FORMAT;
15830 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15831 hrq->type = LPFC_HRQ;
15832 hrq->assoc_qid = cq->queue_id;
15833 hrq->subtype = subtype;
15834 hrq->host_index = 0;
15835 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015836 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015837
15838 drq->db_format = LPFC_DB_RING_FORMAT;
15839 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15840 drq->type = LPFC_DRQ;
15841 drq->assoc_qid = cq->queue_id;
15842 drq->subtype = subtype;
15843 drq->host_index = 0;
15844 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015845 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015846
15847 list_add_tail(&hrq->list, &cq->child_list);
15848 list_add_tail(&drq->list, &cq->child_list);
15849 }
15850
15851 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15852 /* The IOCTL status is embedded in the mailbox subheader. */
15853 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15854 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15855 if (shdr_status || shdr_add_status || rc) {
15856 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15857 "3120 RQ_CREATE mailbox failed with "
15858 "status x%x add_status x%x, mbx status x%x\n",
15859 shdr_status, shdr_add_status, rc);
15860 status = -ENXIO;
15861 goto out;
15862 }
15863 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15864 if (rc == 0xFFFF) {
15865 status = -ENXIO;
15866 goto out;
15867 }
15868
15869 /* Initialize all RQs with associated queue id */
15870 for (idx = 0; idx < numrq; idx++) {
15871 hrq = hrqp[idx];
15872 hrq->queue_id = rc + (2 * idx);
15873 drq = drqp[idx];
15874 drq->queue_id = rc + (2 * idx) + 1;
15875 }
15876
15877out:
15878 lpfc_sli4_mbox_cmd_free(phba, mbox);
15879 return status;
15880}
15881
15882/**
James Smart4f774512009-05-22 14:52:35 -040015883 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15884 * @eq: The queue structure associated with the queue to destroy.
15885 *
15886 * This function destroys a queue, as detailed in @eq by sending an mailbox
15887 * command, specific to the type of queue, to the HBA.
15888 *
15889 * The @eq struct is used to get the queue ID of the queue to destroy.
15890 *
15891 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015892 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015893 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015894int
James Smart4f774512009-05-22 14:52:35 -040015895lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15896{
15897 LPFC_MBOXQ_t *mbox;
15898 int rc, length, status = 0;
15899 uint32_t shdr_status, shdr_add_status;
15900 union lpfc_sli4_cfg_shdr *shdr;
15901
James Smart2e90f4b2011-12-13 13:22:37 -050015902 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015903 if (!eq)
15904 return -ENODEV;
15905 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15906 if (!mbox)
15907 return -ENOMEM;
15908 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15909 sizeof(struct lpfc_sli4_cfg_mhdr));
15910 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15911 LPFC_MBOX_OPCODE_EQ_DESTROY,
15912 length, LPFC_SLI4_MBX_EMBED);
15913 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15914 eq->queue_id);
15915 mbox->vport = eq->phba->pport;
15916 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15917
15918 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15919 /* The IOCTL status is embedded in the mailbox subheader. */
15920 shdr = (union lpfc_sli4_cfg_shdr *)
15921 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15922 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15923 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15924 if (shdr_status || shdr_add_status || rc) {
15925 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15926 "2505 EQ_DESTROY mailbox failed with "
15927 "status x%x add_status x%x, mbx status x%x\n",
15928 shdr_status, shdr_add_status, rc);
15929 status = -ENXIO;
15930 }
15931
15932 /* Remove eq from any list */
15933 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015934 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015935 return status;
15936}
15937
15938/**
15939 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15940 * @cq: The queue structure associated with the queue to destroy.
15941 *
15942 * This function destroys a queue, as detailed in @cq by sending an mailbox
15943 * command, specific to the type of queue, to the HBA.
15944 *
15945 * The @cq struct is used to get the queue ID of the queue to destroy.
15946 *
15947 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015948 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015949 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015950int
James Smart4f774512009-05-22 14:52:35 -040015951lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15952{
15953 LPFC_MBOXQ_t *mbox;
15954 int rc, length, status = 0;
15955 uint32_t shdr_status, shdr_add_status;
15956 union lpfc_sli4_cfg_shdr *shdr;
15957
James Smart2e90f4b2011-12-13 13:22:37 -050015958 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015959 if (!cq)
15960 return -ENODEV;
15961 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15962 if (!mbox)
15963 return -ENOMEM;
15964 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15965 sizeof(struct lpfc_sli4_cfg_mhdr));
15966 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15967 LPFC_MBOX_OPCODE_CQ_DESTROY,
15968 length, LPFC_SLI4_MBX_EMBED);
15969 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15970 cq->queue_id);
15971 mbox->vport = cq->phba->pport;
15972 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15973 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15974 /* The IOCTL status is embedded in the mailbox subheader. */
15975 shdr = (union lpfc_sli4_cfg_shdr *)
15976 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15977 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15978 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15979 if (shdr_status || shdr_add_status || rc) {
15980 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15981 "2506 CQ_DESTROY mailbox failed with "
15982 "status x%x add_status x%x, mbx status x%x\n",
15983 shdr_status, shdr_add_status, rc);
15984 status = -ENXIO;
15985 }
15986 /* Remove cq from any list */
15987 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015988 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015989 return status;
15990}
15991
15992/**
James Smart04c68492009-05-22 14:52:52 -040015993 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15994 * @qm: The queue structure associated with the queue to destroy.
15995 *
15996 * This function destroys a queue, as detailed in @mq by sending an mailbox
15997 * command, specific to the type of queue, to the HBA.
15998 *
15999 * The @mq struct is used to get the queue ID of the queue to destroy.
16000 *
16001 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016002 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040016003 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016004int
James Smart04c68492009-05-22 14:52:52 -040016005lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16006{
16007 LPFC_MBOXQ_t *mbox;
16008 int rc, length, status = 0;
16009 uint32_t shdr_status, shdr_add_status;
16010 union lpfc_sli4_cfg_shdr *shdr;
16011
James Smart2e90f4b2011-12-13 13:22:37 -050016012 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040016013 if (!mq)
16014 return -ENODEV;
16015 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16016 if (!mbox)
16017 return -ENOMEM;
16018 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16019 sizeof(struct lpfc_sli4_cfg_mhdr));
16020 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16021 LPFC_MBOX_OPCODE_MQ_DESTROY,
16022 length, LPFC_SLI4_MBX_EMBED);
16023 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16024 mq->queue_id);
16025 mbox->vport = mq->phba->pport;
16026 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16027 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16028 /* The IOCTL status is embedded in the mailbox subheader. */
16029 shdr = (union lpfc_sli4_cfg_shdr *)
16030 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16031 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16032 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16033 if (shdr_status || shdr_add_status || rc) {
16034 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16035 "2507 MQ_DESTROY mailbox failed with "
16036 "status x%x add_status x%x, mbx status x%x\n",
16037 shdr_status, shdr_add_status, rc);
16038 status = -ENXIO;
16039 }
16040 /* Remove mq from any list */
16041 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016042 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016043 return status;
16044}
16045
16046/**
James Smart4f774512009-05-22 14:52:35 -040016047 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16048 * @wq: The queue structure associated with the queue to destroy.
16049 *
16050 * This function destroys a queue, as detailed in @wq by sending an mailbox
16051 * command, specific to the type of queue, to the HBA.
16052 *
16053 * The @wq struct is used to get the queue ID of the queue to destroy.
16054 *
16055 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016056 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016057 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016058int
James Smart4f774512009-05-22 14:52:35 -040016059lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16060{
16061 LPFC_MBOXQ_t *mbox;
16062 int rc, length, status = 0;
16063 uint32_t shdr_status, shdr_add_status;
16064 union lpfc_sli4_cfg_shdr *shdr;
16065
James Smart2e90f4b2011-12-13 13:22:37 -050016066 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016067 if (!wq)
16068 return -ENODEV;
16069 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16070 if (!mbox)
16071 return -ENOMEM;
16072 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16073 sizeof(struct lpfc_sli4_cfg_mhdr));
16074 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16075 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16076 length, LPFC_SLI4_MBX_EMBED);
16077 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16078 wq->queue_id);
16079 mbox->vport = wq->phba->pport;
16080 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16081 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16082 shdr = (union lpfc_sli4_cfg_shdr *)
16083 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16084 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16085 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16086 if (shdr_status || shdr_add_status || rc) {
16087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16088 "2508 WQ_DESTROY mailbox failed with "
16089 "status x%x add_status x%x, mbx status x%x\n",
16090 shdr_status, shdr_add_status, rc);
16091 status = -ENXIO;
16092 }
16093 /* Remove wq from any list */
16094 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016095 kfree(wq->pring);
16096 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016097 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016098 return status;
16099}
16100
16101/**
16102 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16103 * @rq: The queue structure associated with the queue to destroy.
16104 *
16105 * This function destroys a queue, as detailed in @rq by sending an mailbox
16106 * command, specific to the type of queue, to the HBA.
16107 *
16108 * The @rq struct is used to get the queue ID of the queue to destroy.
16109 *
16110 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016111 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016112 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016113int
James Smart4f774512009-05-22 14:52:35 -040016114lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16115 struct lpfc_queue *drq)
16116{
16117 LPFC_MBOXQ_t *mbox;
16118 int rc, length, status = 0;
16119 uint32_t shdr_status, shdr_add_status;
16120 union lpfc_sli4_cfg_shdr *shdr;
16121
James Smart2e90f4b2011-12-13 13:22:37 -050016122 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016123 if (!hrq || !drq)
16124 return -ENODEV;
16125 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16126 if (!mbox)
16127 return -ENOMEM;
16128 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016129 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016130 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16131 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16132 length, LPFC_SLI4_MBX_EMBED);
16133 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16134 hrq->queue_id);
16135 mbox->vport = hrq->phba->pport;
16136 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16137 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16138 /* The IOCTL status is embedded in the mailbox subheader. */
16139 shdr = (union lpfc_sli4_cfg_shdr *)
16140 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16141 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16142 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16143 if (shdr_status || shdr_add_status || rc) {
16144 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16145 "2509 RQ_DESTROY mailbox failed with "
16146 "status x%x add_status x%x, mbx status x%x\n",
16147 shdr_status, shdr_add_status, rc);
16148 if (rc != MBX_TIMEOUT)
16149 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16150 return -ENXIO;
16151 }
16152 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16153 drq->queue_id);
16154 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16155 shdr = (union lpfc_sli4_cfg_shdr *)
16156 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16157 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16158 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16159 if (shdr_status || shdr_add_status || rc) {
16160 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16161 "2510 RQ_DESTROY mailbox failed with "
16162 "status x%x add_status x%x, mbx status x%x\n",
16163 shdr_status, shdr_add_status, rc);
16164 status = -ENXIO;
16165 }
16166 list_del_init(&hrq->list);
16167 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016168 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016169 return status;
16170}
16171
16172/**
16173 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16174 * @phba: The virtual port for which this call being executed.
16175 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16176 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16177 * @xritag: the xritag that ties this io to the SGL pages.
16178 *
16179 * This routine will post the sgl pages for the IO that has the xritag
16180 * that is in the iocbq structure. The xritag is assigned during iocbq
16181 * creation and persists for as long as the driver is loaded.
16182 * if the caller has fewer than 256 scatter gather segments to map then
16183 * pdma_phys_addr1 should be 0.
16184 * If the caller needs to map more than 256 scatter gather segment then
16185 * pdma_phys_addr1 should be a valid physical address.
16186 * physical address for SGLs must be 64 byte aligned.
16187 * If you are going to map 2 SGL's then the first one must have 256 entries
16188 * the second sgl can have between 1 and 256 entries.
16189 *
16190 * Return codes:
16191 * 0 - Success
16192 * -ENXIO, -ENOMEM - Failure
16193 **/
16194int
16195lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16196 dma_addr_t pdma_phys_addr0,
16197 dma_addr_t pdma_phys_addr1,
16198 uint16_t xritag)
16199{
16200 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16201 LPFC_MBOXQ_t *mbox;
16202 int rc;
16203 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016204 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016205 union lpfc_sli4_cfg_shdr *shdr;
16206
16207 if (xritag == NO_XRI) {
16208 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16209 "0364 Invalid param:\n");
16210 return -EINVAL;
16211 }
16212
16213 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16214 if (!mbox)
16215 return -ENOMEM;
16216
16217 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16218 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16219 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016220 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016221
16222 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16223 &mbox->u.mqe.un.post_sgl_pages;
16224 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16225 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16226
16227 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16228 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16229 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16230 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16231
16232 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16233 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16234 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16235 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16236 if (!phba->sli4_hba.intr_enable)
16237 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016238 else {
James Smarta183a152011-10-10 21:32:43 -040016239 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016240 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16241 }
James Smart4f774512009-05-22 14:52:35 -040016242 /* The IOCTL status is embedded in the mailbox subheader. */
16243 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16244 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16245 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16246 if (rc != MBX_TIMEOUT)
16247 mempool_free(mbox, phba->mbox_mem_pool);
16248 if (shdr_status || shdr_add_status || rc) {
16249 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16250 "2511 POST_SGL mailbox failed with "
16251 "status x%x add_status x%x, mbx status x%x\n",
16252 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016253 }
16254 return 0;
16255}
James Smart4f774512009-05-22 14:52:35 -040016256
16257/**
James Smart88a2cfb2011-07-22 18:36:33 -040016258 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016259 * @phba: pointer to lpfc hba data structure.
16260 *
16261 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016262 * HBA consistent with the SLI-4 interface spec. This routine
16263 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16264 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016265 *
James Smart88a2cfb2011-07-22 18:36:33 -040016266 * Returns
16267 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16268 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16269 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016270static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016271lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16272{
16273 unsigned long xri;
16274
16275 /*
16276 * Fetch the next logical xri. Because this index is logical,
16277 * the driver starts at 0 each time.
16278 */
16279 spin_lock_irq(&phba->hbalock);
16280 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16281 phba->sli4_hba.max_cfg_param.max_xri, 0);
16282 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16283 spin_unlock_irq(&phba->hbalock);
16284 return NO_XRI;
16285 } else {
16286 set_bit(xri, phba->sli4_hba.xri_bmask);
16287 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016288 }
James Smart6d368e52011-05-24 11:44:12 -040016289 spin_unlock_irq(&phba->hbalock);
16290 return xri;
16291}
16292
16293/**
16294 * lpfc_sli4_free_xri - Release an xri for reuse.
16295 * @phba: pointer to lpfc hba data structure.
16296 *
16297 * This routine is invoked to release an xri to the pool of
16298 * available rpis maintained by the driver.
16299 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016300static void
James Smart6d368e52011-05-24 11:44:12 -040016301__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16302{
16303 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016304 phba->sli4_hba.max_cfg_param.xri_used--;
16305 }
16306}
16307
16308/**
16309 * lpfc_sli4_free_xri - Release an xri for reuse.
16310 * @phba: pointer to lpfc hba data structure.
16311 *
16312 * This routine is invoked to release an xri to the pool of
16313 * available rpis maintained by the driver.
16314 **/
16315void
16316lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16317{
16318 spin_lock_irq(&phba->hbalock);
16319 __lpfc_sli4_free_xri(phba, xri);
16320 spin_unlock_irq(&phba->hbalock);
16321}
16322
16323/**
James Smart4f774512009-05-22 14:52:35 -040016324 * lpfc_sli4_next_xritag - Get an xritag for the io
16325 * @phba: Pointer to HBA context object.
16326 *
16327 * This function gets an xritag for the iocb. If there is no unused xritag
16328 * it will return 0xffff.
16329 * The function returns the allocated xritag if successful, else returns zero.
16330 * Zero is not a valid xritag.
16331 * The caller is not required to hold any lock.
16332 **/
16333uint16_t
16334lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16335{
James Smart6d368e52011-05-24 11:44:12 -040016336 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016337
James Smart6d368e52011-05-24 11:44:12 -040016338 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016339 if (xri_index == NO_XRI)
16340 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16341 "2004 Failed to allocate XRI.last XRITAG is %d"
16342 " Max XRI is %d, Used XRI is %d\n",
16343 xri_index,
16344 phba->sli4_hba.max_cfg_param.max_xri,
16345 phba->sli4_hba.max_cfg_param.xri_used);
16346 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016347}
16348
16349/**
James Smart895427b2017-02-12 13:52:30 -080016350 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016351 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016352 * @post_sgl_list: pointer to els sgl entry list.
16353 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016354 *
16355 * This routine is invoked to post a block of driver's sgl pages to the
16356 * HBA using non-embedded mailbox command. No Lock is held. This routine
16357 * is only called when the driver is loading and after all IO has been
16358 * stopped.
16359 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016360static int
James Smart895427b2017-02-12 13:52:30 -080016361lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016362 struct list_head *post_sgl_list,
16363 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016364{
James Smart8a9d2e82012-05-09 21:16:12 -040016365 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016366 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16367 struct sgl_page_pairs *sgl_pg_pairs;
16368 void *viraddr;
16369 LPFC_MBOXQ_t *mbox;
16370 uint32_t reqlen, alloclen, pg_pairs;
16371 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016372 uint16_t xritag_start = 0;
16373 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016374 uint32_t shdr_status, shdr_add_status;
16375 union lpfc_sli4_cfg_shdr *shdr;
16376
James Smart895427b2017-02-12 13:52:30 -080016377 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016378 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016379 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016381 "2559 Block sgl registration required DMA "
16382 "size (%d) great than a page\n", reqlen);
16383 return -ENOMEM;
16384 }
James Smart895427b2017-02-12 13:52:30 -080016385
James Smart4f774512009-05-22 14:52:35 -040016386 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016387 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016388 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016389
16390 /* Allocate DMA memory and set up the non-embedded mailbox command */
16391 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16392 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16393 LPFC_SLI4_MBX_NEMBED);
16394
16395 if (alloclen < reqlen) {
16396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16397 "0285 Allocated DMA memory size (%d) is "
16398 "less than the requested DMA memory "
16399 "size (%d)\n", alloclen, reqlen);
16400 lpfc_sli4_mbox_cmd_free(phba, mbox);
16401 return -ENOMEM;
16402 }
James Smart4f774512009-05-22 14:52:35 -040016403 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016404 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016405 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16406 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16407
James Smart8a9d2e82012-05-09 21:16:12 -040016408 pg_pairs = 0;
16409 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016410 /* Set up the sge entry */
16411 sgl_pg_pairs->sgl_pg0_addr_lo =
16412 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16413 sgl_pg_pairs->sgl_pg0_addr_hi =
16414 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16415 sgl_pg_pairs->sgl_pg1_addr_lo =
16416 cpu_to_le32(putPaddrLow(0));
16417 sgl_pg_pairs->sgl_pg1_addr_hi =
16418 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016419
James Smart4f774512009-05-22 14:52:35 -040016420 /* Keep the first xritag on the list */
16421 if (pg_pairs == 0)
16422 xritag_start = sglq_entry->sli4_xritag;
16423 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016424 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016425 }
James Smart6d368e52011-05-24 11:44:12 -040016426
16427 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016428 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016429 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016430 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016431
James Smart4f774512009-05-22 14:52:35 -040016432 if (!phba->sli4_hba.intr_enable)
16433 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16434 else {
James Smarta183a152011-10-10 21:32:43 -040016435 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016436 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16437 }
16438 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16439 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16440 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16441 if (rc != MBX_TIMEOUT)
16442 lpfc_sli4_mbox_cmd_free(phba, mbox);
16443 if (shdr_status || shdr_add_status || rc) {
16444 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16445 "2513 POST_SGL_BLOCK mailbox command failed "
16446 "status x%x add_status x%x mbx status x%x\n",
16447 shdr_status, shdr_add_status, rc);
16448 rc = -ENXIO;
16449 }
16450 return rc;
16451}
16452
16453/**
16454 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16455 * @phba: pointer to lpfc hba data structure.
16456 * @sblist: pointer to scsi buffer list.
16457 * @count: number of scsi buffers on the list.
16458 *
16459 * This routine is invoked to post a block of @count scsi sgl pages from a
16460 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16461 * No Lock is held.
16462 *
16463 **/
16464int
James Smart8a9d2e82012-05-09 21:16:12 -040016465lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16466 struct list_head *sblist,
16467 int count)
James Smart4f774512009-05-22 14:52:35 -040016468{
16469 struct lpfc_scsi_buf *psb;
16470 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16471 struct sgl_page_pairs *sgl_pg_pairs;
16472 void *viraddr;
16473 LPFC_MBOXQ_t *mbox;
16474 uint32_t reqlen, alloclen, pg_pairs;
16475 uint32_t mbox_tmo;
16476 uint16_t xritag_start = 0;
16477 int rc = 0;
16478 uint32_t shdr_status, shdr_add_status;
16479 dma_addr_t pdma_phys_bpl1;
16480 union lpfc_sli4_cfg_shdr *shdr;
16481
16482 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016483 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016484 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016485 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016486 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16487 "0217 Block sgl registration required DMA "
16488 "size (%d) great than a page\n", reqlen);
16489 return -ENOMEM;
16490 }
16491 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16492 if (!mbox) {
16493 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16494 "0283 Failed to allocate mbox cmd memory\n");
16495 return -ENOMEM;
16496 }
16497
16498 /* Allocate DMA memory and set up the non-embedded mailbox command */
16499 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16500 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16501 LPFC_SLI4_MBX_NEMBED);
16502
16503 if (alloclen < reqlen) {
16504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16505 "2561 Allocated DMA memory size (%d) is "
16506 "less than the requested DMA memory "
16507 "size (%d)\n", alloclen, reqlen);
16508 lpfc_sli4_mbox_cmd_free(phba, mbox);
16509 return -ENOMEM;
16510 }
James Smart6d368e52011-05-24 11:44:12 -040016511
James Smart4f774512009-05-22 14:52:35 -040016512 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016513 viraddr = mbox->sge_array->addr[0];
16514
16515 /* Set up the SGL pages in the non-embedded DMA pages */
16516 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16517 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16518
16519 pg_pairs = 0;
16520 list_for_each_entry(psb, sblist, list) {
16521 /* Set up the sge entry */
16522 sgl_pg_pairs->sgl_pg0_addr_lo =
16523 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16524 sgl_pg_pairs->sgl_pg0_addr_hi =
16525 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16526 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16527 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16528 else
16529 pdma_phys_bpl1 = 0;
16530 sgl_pg_pairs->sgl_pg1_addr_lo =
16531 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16532 sgl_pg_pairs->sgl_pg1_addr_hi =
16533 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16534 /* Keep the first xritag on the list */
16535 if (pg_pairs == 0)
16536 xritag_start = psb->cur_iocbq.sli4_xritag;
16537 sgl_pg_pairs++;
16538 pg_pairs++;
16539 }
16540 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16541 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16542 /* Perform endian conversion if necessary */
16543 sgl->word0 = cpu_to_le32(sgl->word0);
16544
16545 if (!phba->sli4_hba.intr_enable)
16546 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16547 else {
James Smarta183a152011-10-10 21:32:43 -040016548 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016549 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16550 }
16551 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16552 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16553 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16554 if (rc != MBX_TIMEOUT)
16555 lpfc_sli4_mbox_cmd_free(phba, mbox);
16556 if (shdr_status || shdr_add_status || rc) {
16557 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16558 "2564 POST_SGL_BLOCK mailbox command failed "
16559 "status x%x add_status x%x mbx status x%x\n",
16560 shdr_status, shdr_add_status, rc);
16561 rc = -ENXIO;
16562 }
16563 return rc;
16564}
16565
16566/**
16567 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16568 * @phba: pointer to lpfc_hba struct that the frame was received on
16569 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16570 *
16571 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16572 * valid type of frame that the LPFC driver will handle. This function will
16573 * return a zero if the frame is a valid frame or a non zero value when the
16574 * frame does not pass the check.
16575 **/
16576static int
16577lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16578{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016579 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016580 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016581 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016582
James Smartae9e28f2017-05-15 15:20:51 -070016583#define FC_RCTL_MDS_DIAGS 0xF4
16584
James Smart4f774512009-05-22 14:52:35 -040016585 switch (fc_hdr->fh_r_ctl) {
16586 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16587 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16588 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16589 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16590 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16591 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16592 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16593 case FC_RCTL_DD_CMD_STATUS: /* command status */
16594 case FC_RCTL_ELS_REQ: /* extended link services request */
16595 case FC_RCTL_ELS_REP: /* extended link services reply */
16596 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16597 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16598 case FC_RCTL_BA_NOP: /* basic link service NOP */
16599 case FC_RCTL_BA_ABTS: /* basic link service abort */
16600 case FC_RCTL_BA_RMC: /* remove connection */
16601 case FC_RCTL_BA_ACC: /* basic accept */
16602 case FC_RCTL_BA_RJT: /* basic reject */
16603 case FC_RCTL_BA_PRMT:
16604 case FC_RCTL_ACK_1: /* acknowledge_1 */
16605 case FC_RCTL_ACK_0: /* acknowledge_0 */
16606 case FC_RCTL_P_RJT: /* port reject */
16607 case FC_RCTL_F_RJT: /* fabric reject */
16608 case FC_RCTL_P_BSY: /* port busy */
16609 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16610 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16611 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016612 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016613 case FC_RCTL_END: /* end */
16614 break;
16615 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16616 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16617 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16618 return lpfc_fc_frame_check(phba, fc_hdr);
16619 default:
16620 goto drop;
16621 }
James Smartae9e28f2017-05-15 15:20:51 -070016622
16623#define FC_TYPE_VENDOR_UNIQUE 0xFF
16624
James Smart4f774512009-05-22 14:52:35 -040016625 switch (fc_hdr->fh_type) {
16626 case FC_TYPE_BLS:
16627 case FC_TYPE_ELS:
16628 case FC_TYPE_FCP:
16629 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016630 case FC_TYPE_NVME:
James Smartae9e28f2017-05-15 15:20:51 -070016631 case FC_TYPE_VENDOR_UNIQUE:
James Smart4f774512009-05-22 14:52:35 -040016632 break;
16633 case FC_TYPE_IP:
16634 case FC_TYPE_ILS:
16635 default:
16636 goto drop;
16637 }
James Smart546fc852011-03-11 16:06:29 -050016638
James Smart4f774512009-05-22 14:52:35 -040016639 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016640 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016641 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016642 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16643 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16644 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16645 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16646 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016647 return 0;
16648drop:
16649 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016650 "2539 Dropped frame rctl:x%x type:x%x\n",
16651 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016652 return 1;
16653}
16654
16655/**
16656 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16657 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16658 *
16659 * This function processes the FC header to retrieve the VFI from the VF
16660 * header, if one exists. This function will return the VFI if one exists
16661 * or 0 if no VSAN Header exists.
16662 **/
16663static uint32_t
16664lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16665{
16666 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16667
16668 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16669 return 0;
16670 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16671}
16672
16673/**
16674 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16675 * @phba: Pointer to the HBA structure to search for the vport on
16676 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16677 * @fcfi: The FC Fabric ID that the frame came from
16678 *
16679 * This function searches the @phba for a vport that matches the content of the
16680 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16681 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16682 * returns the matching vport pointer or NULL if unable to match frame to a
16683 * vport.
16684 **/
16685static struct lpfc_vport *
16686lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016687 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016688{
16689 struct lpfc_vport **vports;
16690 struct lpfc_vport *vport = NULL;
16691 int i;
James Smart939723a2012-05-09 21:19:03 -040016692
James Smartbf086112011-08-21 21:48:13 -040016693 if (did == Fabric_DID)
16694 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016695 if ((phba->pport->fc_flag & FC_PT2PT) &&
16696 !(phba->link_state == LPFC_HBA_READY))
16697 return phba->pport;
16698
James Smart4f774512009-05-22 14:52:35 -040016699 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016700 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016701 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16702 if (phba->fcf.fcfi == fcfi &&
16703 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16704 vports[i]->fc_myDID == did) {
16705 vport = vports[i];
16706 break;
16707 }
16708 }
James Smart895427b2017-02-12 13:52:30 -080016709 }
James Smart4f774512009-05-22 14:52:35 -040016710 lpfc_destroy_vport_work_array(phba, vports);
16711 return vport;
16712}
16713
16714/**
James Smart45ed1192009-10-02 15:17:02 -040016715 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16716 * @vport: The vport to work on.
16717 *
16718 * This function updates the receive sequence time stamp for this vport. The
16719 * receive sequence time stamp indicates the time that the last frame of the
16720 * the sequence that has been idle for the longest amount of time was received.
16721 * the driver uses this time stamp to indicate if any received sequences have
16722 * timed out.
16723 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016724static void
James Smart45ed1192009-10-02 15:17:02 -040016725lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16726{
16727 struct lpfc_dmabuf *h_buf;
16728 struct hbq_dmabuf *dmabuf = NULL;
16729
16730 /* get the oldest sequence on the rcv list */
16731 h_buf = list_get_first(&vport->rcv_buffer_list,
16732 struct lpfc_dmabuf, list);
16733 if (!h_buf)
16734 return;
16735 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16736 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16737}
16738
16739/**
16740 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16741 * @vport: The vport that the received sequences were sent to.
16742 *
16743 * This function cleans up all outstanding received sequences. This is called
16744 * by the driver when a link event or user action invalidates all the received
16745 * sequences.
16746 **/
16747void
16748lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16749{
16750 struct lpfc_dmabuf *h_buf, *hnext;
16751 struct lpfc_dmabuf *d_buf, *dnext;
16752 struct hbq_dmabuf *dmabuf = NULL;
16753
16754 /* start with the oldest sequence on the rcv list */
16755 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16756 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16757 list_del_init(&dmabuf->hbuf.list);
16758 list_for_each_entry_safe(d_buf, dnext,
16759 &dmabuf->dbuf.list, list) {
16760 list_del_init(&d_buf->list);
16761 lpfc_in_buf_free(vport->phba, d_buf);
16762 }
16763 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16764 }
16765}
16766
16767/**
16768 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16769 * @vport: The vport that the received sequences were sent to.
16770 *
16771 * This function determines whether any received sequences have timed out by
16772 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16773 * indicates that there is at least one timed out sequence this routine will
16774 * go through the received sequences one at a time from most inactive to most
16775 * active to determine which ones need to be cleaned up. Once it has determined
16776 * that a sequence needs to be cleaned up it will simply free up the resources
16777 * without sending an abort.
16778 **/
16779void
16780lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16781{
16782 struct lpfc_dmabuf *h_buf, *hnext;
16783 struct lpfc_dmabuf *d_buf, *dnext;
16784 struct hbq_dmabuf *dmabuf = NULL;
16785 unsigned long timeout;
16786 int abort_count = 0;
16787
16788 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16789 vport->rcv_buffer_time_stamp);
16790 if (list_empty(&vport->rcv_buffer_list) ||
16791 time_before(jiffies, timeout))
16792 return;
16793 /* start with the oldest sequence on the rcv list */
16794 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16795 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16796 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16797 dmabuf->time_stamp);
16798 if (time_before(jiffies, timeout))
16799 break;
16800 abort_count++;
16801 list_del_init(&dmabuf->hbuf.list);
16802 list_for_each_entry_safe(d_buf, dnext,
16803 &dmabuf->dbuf.list, list) {
16804 list_del_init(&d_buf->list);
16805 lpfc_in_buf_free(vport->phba, d_buf);
16806 }
16807 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16808 }
16809 if (abort_count)
16810 lpfc_update_rcv_time_stamp(vport);
16811}
16812
16813/**
James Smart4f774512009-05-22 14:52:35 -040016814 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16815 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16816 *
16817 * This function searches through the existing incomplete sequences that have
16818 * been sent to this @vport. If the frame matches one of the incomplete
16819 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16820 * make up that sequence. If no sequence is found that matches this frame then
16821 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16822 * This function returns a pointer to the first dmabuf in the sequence list that
16823 * the frame was linked to.
16824 **/
16825static struct hbq_dmabuf *
16826lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16827{
16828 struct fc_frame_header *new_hdr;
16829 struct fc_frame_header *temp_hdr;
16830 struct lpfc_dmabuf *d_buf;
16831 struct lpfc_dmabuf *h_buf;
16832 struct hbq_dmabuf *seq_dmabuf = NULL;
16833 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016834 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016835
James Smart4d9ab992009-10-02 15:16:39 -040016836 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016837 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016838 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016839
James Smart4f774512009-05-22 14:52:35 -040016840 /* Use the hdr_buf to find the sequence that this frame belongs to */
16841 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16842 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16843 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16844 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16845 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16846 continue;
16847 /* found a pending sequence that matches this frame */
16848 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16849 break;
16850 }
16851 if (!seq_dmabuf) {
16852 /*
16853 * This indicates first frame received for this sequence.
16854 * Queue the buffer on the vport's rcv_buffer_list.
16855 */
16856 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016857 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016858 return dmabuf;
16859 }
16860 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016861 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16862 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016863 list_del_init(&seq_dmabuf->hbuf.list);
16864 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16865 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016866 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016867 return dmabuf;
16868 }
James Smart45ed1192009-10-02 15:17:02 -040016869 /* move this sequence to the tail to indicate a young sequence */
16870 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16871 seq_dmabuf->time_stamp = jiffies;
16872 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016873 if (list_empty(&seq_dmabuf->dbuf.list)) {
16874 temp_hdr = dmabuf->hbuf.virt;
16875 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16876 return seq_dmabuf;
16877 }
James Smart4f774512009-05-22 14:52:35 -040016878 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016879 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16880 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016881 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16882 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16883 /*
16884 * If the frame's sequence count is greater than the frame on
16885 * the list then insert the frame right after this frame
16886 */
James Smarteeead812009-12-21 17:01:23 -050016887 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16888 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016889 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016890 found = 1;
16891 break;
James Smart4f774512009-05-22 14:52:35 -040016892 }
James Smart4360ca92015-12-16 18:12:04 -050016893
16894 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16895 break;
16896 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016897 }
James Smart4360ca92015-12-16 18:12:04 -050016898
16899 if (found)
16900 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016901 return NULL;
16902}
16903
16904/**
James Smart6669f9b2009-10-02 15:16:45 -040016905 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16906 * @vport: pointer to a vitural port
16907 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16908 *
16909 * This function tries to abort from the partially assembed sequence, described
16910 * by the information from basic abbort @dmabuf. It checks to see whether such
16911 * partially assembled sequence held by the driver. If so, it shall free up all
16912 * the frames from the partially assembled sequence.
16913 *
16914 * Return
16915 * true -- if there is matching partially assembled sequence present and all
16916 * the frames freed with the sequence;
16917 * false -- if there is no matching partially assembled sequence present so
16918 * nothing got aborted in the lower layer driver
16919 **/
16920static bool
16921lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16922 struct hbq_dmabuf *dmabuf)
16923{
16924 struct fc_frame_header *new_hdr;
16925 struct fc_frame_header *temp_hdr;
16926 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16927 struct hbq_dmabuf *seq_dmabuf = NULL;
16928
16929 /* Use the hdr_buf to find the sequence that matches this frame */
16930 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16931 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16932 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16933 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16934 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16935 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16936 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16937 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16938 continue;
16939 /* found a pending sequence that matches this frame */
16940 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16941 break;
16942 }
16943
16944 /* Free up all the frames from the partially assembled sequence */
16945 if (seq_dmabuf) {
16946 list_for_each_entry_safe(d_buf, n_buf,
16947 &seq_dmabuf->dbuf.list, list) {
16948 list_del_init(&d_buf->list);
16949 lpfc_in_buf_free(vport->phba, d_buf);
16950 }
16951 return true;
16952 }
16953 return false;
16954}
16955
16956/**
James Smart6dd9e312013-01-03 15:43:37 -050016957 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16958 * @vport: pointer to a vitural port
16959 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16960 *
16961 * This function tries to abort from the assembed sequence from upper level
16962 * protocol, described by the information from basic abbort @dmabuf. It
16963 * checks to see whether such pending context exists at upper level protocol.
16964 * If so, it shall clean up the pending context.
16965 *
16966 * Return
16967 * true -- if there is matching pending context of the sequence cleaned
16968 * at ulp;
16969 * false -- if there is no matching pending context of the sequence present
16970 * at ulp.
16971 **/
16972static bool
16973lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16974{
16975 struct lpfc_hba *phba = vport->phba;
16976 int handled;
16977
16978 /* Accepting abort at ulp with SLI4 only */
16979 if (phba->sli_rev < LPFC_SLI_REV4)
16980 return false;
16981
16982 /* Register all caring upper level protocols to attend abort */
16983 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16984 if (handled)
16985 return true;
16986
16987 return false;
16988}
16989
16990/**
James Smart546fc852011-03-11 16:06:29 -050016991 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016992 * @phba: Pointer to HBA context object.
16993 * @cmd_iocbq: pointer to the command iocbq structure.
16994 * @rsp_iocbq: pointer to the response iocbq structure.
16995 *
James Smart546fc852011-03-11 16:06:29 -050016996 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016997 * event. It properly releases the memory allocated to the sequence abort
16998 * accept iocb.
16999 **/
17000static void
James Smart546fc852011-03-11 16:06:29 -050017001lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040017002 struct lpfc_iocbq *cmd_iocbq,
17003 struct lpfc_iocbq *rsp_iocbq)
17004{
James Smart6dd9e312013-01-03 15:43:37 -050017005 struct lpfc_nodelist *ndlp;
17006
17007 if (cmd_iocbq) {
17008 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17009 lpfc_nlp_put(ndlp);
17010 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017011 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050017012 }
James Smart6b5151f2012-01-18 16:24:06 -050017013
17014 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17015 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17016 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17017 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
17018 rsp_iocbq->iocb.ulpStatus,
17019 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040017020}
17021
17022/**
James Smart6d368e52011-05-24 11:44:12 -040017023 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17024 * @phba: Pointer to HBA context object.
17025 * @xri: xri id in transaction.
17026 *
17027 * This function validates the xri maps to the known range of XRIs allocated an
17028 * used by the driver.
17029 **/
James Smart7851fe22011-07-22 18:36:52 -040017030uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017031lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17032 uint16_t xri)
17033{
James Smarta2fc4aef2014-09-03 12:57:55 -040017034 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040017035
17036 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17037 if (xri == phba->sli4_hba.xri_ids[i])
17038 return i;
17039 }
17040 return NO_XRI;
17041}
17042
James Smart6d368e52011-05-24 11:44:12 -040017043/**
James Smart546fc852011-03-11 16:06:29 -050017044 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017045 * @phba: Pointer to HBA context object.
17046 * @fc_hdr: pointer to a FC frame header.
17047 *
James Smart546fc852011-03-11 16:06:29 -050017048 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017049 * event after aborting the sequence handling.
17050 **/
James Smart86c67372017-04-21 16:05:04 -070017051void
James Smart6dd9e312013-01-03 15:43:37 -050017052lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17053 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040017054{
James Smart6dd9e312013-01-03 15:43:37 -050017055 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017056 struct lpfc_iocbq *ctiocb = NULL;
17057 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017058 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017059 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017060 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017061 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017062
17063 if (!lpfc_is_link_up(phba))
17064 return;
17065
17066 sid = sli4_sid_from_fc_hdr(fc_hdr);
17067 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017068 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017069
James Smart6dd9e312013-01-03 15:43:37 -050017070 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017071 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017072 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017073 if (!ndlp) {
17074 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17075 "1268 Failed to allocate ndlp for "
17076 "oxid:x%x SID:x%x\n", oxid, sid);
17077 return;
17078 }
James Smart6dd9e312013-01-03 15:43:37 -050017079 /* Put ndlp onto pport node list */
17080 lpfc_enqueue_node(vport, ndlp);
17081 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17082 /* re-setup ndlp without removing from node list */
17083 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17084 if (!ndlp) {
17085 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17086 "3275 Failed to active ndlp found "
17087 "for oxid:x%x SID:x%x\n", oxid, sid);
17088 return;
17089 }
James Smart6669f9b2009-10-02 15:16:45 -040017090 }
17091
James Smart546fc852011-03-11 16:06:29 -050017092 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017093 ctiocb = lpfc_sli_get_iocbq(phba);
17094 if (!ctiocb)
17095 return;
17096
James Smart5ffc2662009-11-18 15:39:44 -050017097 /* Extract the F_CTL field from FC_HDR */
17098 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17099
James Smart6669f9b2009-10-02 15:16:45 -040017100 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017101 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017102 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017103 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17104 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17105 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17106
17107 /* Fill in the rest of iocb fields */
17108 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17109 icmd->ulpBdeCount = 0;
17110 icmd->ulpLe = 1;
17111 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017112 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017113 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017114
James Smart6669f9b2009-10-02 15:16:45 -040017115 ctiocb->iocb_cmpl = NULL;
17116 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017117 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017118 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017119 ctiocb->sli4_xritag = NO_XRI;
17120
James Smartee0f4fe2012-05-09 21:19:14 -040017121 if (fctl & FC_FC_EX_CTX)
17122 /* Exchange responder sent the abort so we
17123 * own the oxid.
17124 */
17125 xri = oxid;
17126 else
17127 xri = rxid;
17128 lxri = lpfc_sli4_xri_inrange(phba, xri);
17129 if (lxri != NO_XRI)
17130 lpfc_set_rrq_active(phba, ndlp, lxri,
17131 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017132 /* For BA_ABTS from exchange responder, if the logical xri with
17133 * the oxid maps to the FCP XRI range, the port no longer has
17134 * that exchange context, send a BLS_RJT. Override the IOCB for
17135 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017136 */
James Smart6dd9e312013-01-03 15:43:37 -050017137 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017138 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050017139 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17140 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17141 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17142 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17143 }
17144
17145 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17146 * the driver no longer has that exchange, send a BLS_RJT. Override
17147 * the IOCB for a BA_RJT.
17148 */
17149 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017150 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17151 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17152 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17153 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17154 }
James Smart6669f9b2009-10-02 15:16:45 -040017155
James Smart5ffc2662009-11-18 15:39:44 -050017156 if (fctl & FC_FC_EX_CTX) {
17157 /* ABTS sent by responder to CT exchange, construction
17158 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17159 * field and RX_ID from ABTS for RX_ID field.
17160 */
James Smart546fc852011-03-11 16:06:29 -050017161 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017162 } else {
17163 /* ABTS sent by initiator to CT exchange, construction
17164 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017165 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017166 */
James Smart546fc852011-03-11 16:06:29 -050017167 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017168 }
James Smartf09c3ac2012-03-01 22:33:29 -050017169 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017170 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017171
James Smart546fc852011-03-11 16:06:29 -050017172 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017173 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17174 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17175 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017176
17177 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17178 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017179 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17180 "2925 Failed to issue CT ABTS RSP x%x on "
17181 "xri x%x, Data x%x\n",
17182 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17183 phba->link_state);
17184 lpfc_nlp_put(ndlp);
17185 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017186 lpfc_sli_release_iocbq(phba, ctiocb);
17187 }
James Smart6669f9b2009-10-02 15:16:45 -040017188}
17189
17190/**
17191 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17192 * @vport: Pointer to the vport on which this sequence was received
17193 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17194 *
17195 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17196 * receive sequence is only partially assembed by the driver, it shall abort
17197 * the partially assembled frames for the sequence. Otherwise, if the
17198 * unsolicited receive sequence has been completely assembled and passed to
17199 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17200 * unsolicited sequence has been aborted. After that, it will issue a basic
17201 * accept to accept the abort.
17202 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017203static void
James Smart6669f9b2009-10-02 15:16:45 -040017204lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17205 struct hbq_dmabuf *dmabuf)
17206{
17207 struct lpfc_hba *phba = vport->phba;
17208 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017209 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017210 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017211
James Smart6669f9b2009-10-02 15:16:45 -040017212 /* Make a copy of fc_hdr before the dmabuf being released */
17213 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017214 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017215
James Smart5ffc2662009-11-18 15:39:44 -050017216 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017217 /* ABTS by responder to exchange, no cleanup needed */
17218 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017219 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017220 /* ABTS by initiator to exchange, need to do cleanup */
17221 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17222 if (aborted == false)
17223 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017224 }
James Smart6dd9e312013-01-03 15:43:37 -050017225 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17226
James Smart86c67372017-04-21 16:05:04 -070017227 if (phba->nvmet_support) {
17228 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17229 return;
17230 }
17231
James Smart6dd9e312013-01-03 15:43:37 -050017232 /* Respond with BA_ACC or BA_RJT accordingly */
17233 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017234}
17235
17236/**
James Smart4f774512009-05-22 14:52:35 -040017237 * lpfc_seq_complete - Indicates if a sequence is complete
17238 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17239 *
17240 * This function checks the sequence, starting with the frame described by
17241 * @dmabuf, to see if all the frames associated with this sequence are present.
17242 * the frames associated with this sequence are linked to the @dmabuf using the
17243 * dbuf list. This function looks for two major things. 1) That the first frame
17244 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17245 * set. 3) That there are no holes in the sequence count. The function will
17246 * return 1 when the sequence is complete, otherwise it will return 0.
17247 **/
17248static int
17249lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17250{
17251 struct fc_frame_header *hdr;
17252 struct lpfc_dmabuf *d_buf;
17253 struct hbq_dmabuf *seq_dmabuf;
17254 uint32_t fctl;
17255 int seq_count = 0;
17256
17257 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17258 /* make sure first fame of sequence has a sequence count of zero */
17259 if (hdr->fh_seq_cnt != seq_count)
17260 return 0;
17261 fctl = (hdr->fh_f_ctl[0] << 16 |
17262 hdr->fh_f_ctl[1] << 8 |
17263 hdr->fh_f_ctl[2]);
17264 /* If last frame of sequence we can return success. */
17265 if (fctl & FC_FC_END_SEQ)
17266 return 1;
17267 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17268 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17269 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17270 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017271 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017272 return 0;
17273 fctl = (hdr->fh_f_ctl[0] << 16 |
17274 hdr->fh_f_ctl[1] << 8 |
17275 hdr->fh_f_ctl[2]);
17276 /* If last frame of sequence we can return success. */
17277 if (fctl & FC_FC_END_SEQ)
17278 return 1;
17279 }
17280 return 0;
17281}
17282
17283/**
17284 * lpfc_prep_seq - Prep sequence for ULP processing
17285 * @vport: Pointer to the vport on which this sequence was received
17286 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17287 *
17288 * This function takes a sequence, described by a list of frames, and creates
17289 * a list of iocbq structures to describe the sequence. This iocbq list will be
17290 * used to issue to the generic unsolicited sequence handler. This routine
17291 * returns a pointer to the first iocbq in the list. If the function is unable
17292 * to allocate an iocbq then it throw out the received frames that were not
17293 * able to be described and return a pointer to the first iocbq. If unable to
17294 * allocate any iocbqs (including the first) this function will return NULL.
17295 **/
17296static struct lpfc_iocbq *
17297lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17298{
James Smart7851fe22011-07-22 18:36:52 -040017299 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017300 struct lpfc_dmabuf *d_buf, *n_buf;
17301 struct lpfc_iocbq *first_iocbq, *iocbq;
17302 struct fc_frame_header *fc_hdr;
17303 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017304 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017305 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017306
17307 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17308 /* remove from receive buffer list */
17309 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017310 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017311 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017312 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017313 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017314 /* Get an iocbq struct to fill in. */
17315 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17316 if (first_iocbq) {
17317 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017318 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017319 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017320 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017321
17322 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17323 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17324 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17325 first_iocbq->iocb.un.rcvels.parmRo =
17326 sli4_did_from_fc_hdr(fc_hdr);
17327 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17328 } else
17329 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017330 first_iocbq->iocb.ulpContext = NO_XRI;
17331 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17332 be16_to_cpu(fc_hdr->fh_ox_id);
17333 /* iocbq is prepped for internal consumption. Physical vpi. */
17334 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17335 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017336 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017337 tot_len = bf_get(lpfc_rcqe_length,
17338 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17339
James Smart4f774512009-05-22 14:52:35 -040017340 first_iocbq->context2 = &seq_dmabuf->dbuf;
17341 first_iocbq->context3 = NULL;
17342 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017343 if (tot_len > LPFC_DATA_BUF_SIZE)
17344 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017345 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017346 else
17347 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17348
James Smart4f774512009-05-22 14:52:35 -040017349 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017350
James Smart7851fe22011-07-22 18:36:52 -040017351 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017352 }
17353 iocbq = first_iocbq;
17354 /*
17355 * Each IOCBq can have two Buffers assigned, so go through the list
17356 * of buffers for this sequence and save two buffers in each IOCBq
17357 */
17358 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17359 if (!iocbq) {
17360 lpfc_in_buf_free(vport->phba, d_buf);
17361 continue;
17362 }
17363 if (!iocbq->context3) {
17364 iocbq->context3 = d_buf;
17365 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017366 /* We need to get the size out of the right CQE */
17367 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17368 len = bf_get(lpfc_rcqe_length,
17369 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017370 pbde = (struct ulp_bde64 *)
17371 &iocbq->iocb.unsli3.sli3Words[4];
17372 if (len > LPFC_DATA_BUF_SIZE)
17373 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17374 else
17375 pbde->tus.f.bdeSize = len;
17376
James Smart7851fe22011-07-22 18:36:52 -040017377 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17378 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017379 } else {
17380 iocbq = lpfc_sli_get_iocbq(vport->phba);
17381 if (!iocbq) {
17382 if (first_iocbq) {
17383 first_iocbq->iocb.ulpStatus =
17384 IOSTAT_FCP_RSP_ERROR;
17385 first_iocbq->iocb.un.ulpWord[4] =
17386 IOERR_NO_RESOURCES;
17387 }
17388 lpfc_in_buf_free(vport->phba, d_buf);
17389 continue;
17390 }
James Smart7851fe22011-07-22 18:36:52 -040017391 /* We need to get the size out of the right CQE */
17392 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17393 len = bf_get(lpfc_rcqe_length,
17394 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017395 iocbq->context2 = d_buf;
17396 iocbq->context3 = NULL;
17397 iocbq->iocb.ulpBdeCount = 1;
17398 if (len > LPFC_DATA_BUF_SIZE)
17399 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17400 LPFC_DATA_BUF_SIZE;
17401 else
17402 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17403
James Smart7851fe22011-07-22 18:36:52 -040017404 tot_len += len;
17405 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17406
James Smart4f774512009-05-22 14:52:35 -040017407 iocbq->iocb.un.rcvels.remoteID = sid;
17408 list_add_tail(&iocbq->list, &first_iocbq->list);
17409 }
17410 }
17411 return first_iocbq;
17412}
17413
James Smart6669f9b2009-10-02 15:16:45 -040017414static void
17415lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17416 struct hbq_dmabuf *seq_dmabuf)
17417{
17418 struct fc_frame_header *fc_hdr;
17419 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17420 struct lpfc_hba *phba = vport->phba;
17421
17422 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17423 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17424 if (!iocbq) {
17425 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17426 "2707 Ring %d handler: Failed to allocate "
17427 "iocb Rctl x%x Type x%x received\n",
17428 LPFC_ELS_RING,
17429 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17430 return;
17431 }
17432 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017433 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017434 iocbq, fc_hdr->fh_r_ctl,
17435 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017436 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017437 "2540 Ring %d handler: unexpected Rctl "
17438 "x%x Type x%x received\n",
17439 LPFC_ELS_RING,
17440 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17441
17442 /* Free iocb created in lpfc_prep_seq */
17443 list_for_each_entry_safe(curr_iocb, next_iocb,
17444 &iocbq->list, list) {
17445 list_del_init(&curr_iocb->list);
17446 lpfc_sli_release_iocbq(phba, curr_iocb);
17447 }
17448 lpfc_sli_release_iocbq(phba, iocbq);
17449}
17450
James Smartae9e28f2017-05-15 15:20:51 -070017451static void
17452lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17453 struct lpfc_iocbq *rspiocb)
17454{
17455 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17456
17457 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017458 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017459 kfree(pcmd);
17460 lpfc_sli_release_iocbq(phba, cmdiocb);
17461}
17462
17463static void
17464lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17465 struct hbq_dmabuf *dmabuf)
17466{
17467 struct fc_frame_header *fc_hdr;
17468 struct lpfc_hba *phba = vport->phba;
17469 struct lpfc_iocbq *iocbq = NULL;
17470 union lpfc_wqe *wqe;
17471 struct lpfc_dmabuf *pcmd = NULL;
17472 uint32_t frame_len;
17473 int rc;
17474
17475 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17476 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17477
17478 /* Send the received frame back */
17479 iocbq = lpfc_sli_get_iocbq(phba);
17480 if (!iocbq)
17481 goto exit;
17482
17483 /* Allocate buffer for command payload */
17484 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17485 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017486 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017487 &pcmd->phys);
17488 if (!pcmd || !pcmd->virt)
17489 goto exit;
17490
17491 INIT_LIST_HEAD(&pcmd->list);
17492
17493 /* copyin the payload */
17494 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17495
17496 /* fill in BDE's for command */
17497 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17498 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17499 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17500 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17501
17502 iocbq->context2 = pcmd;
17503 iocbq->vport = vport;
17504 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17505 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17506
17507 /*
17508 * Setup rest of the iocb as though it were a WQE
17509 * Build the SEND_FRAME WQE
17510 */
17511 wqe = (union lpfc_wqe *)&iocbq->iocb;
17512
17513 wqe->send_frame.frame_len = frame_len;
17514 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17515 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17516 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17517 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17518 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17519 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17520
17521 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17522 iocbq->iocb.ulpLe = 1;
17523 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17524 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17525 if (rc == IOCB_ERROR)
17526 goto exit;
17527
17528 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17529 return;
17530
17531exit:
17532 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17533 "2023 Unable to process MDS loopback frame\n");
17534 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017535 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017536 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017537 if (iocbq)
17538 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017539 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17540}
17541
James Smart4f774512009-05-22 14:52:35 -040017542/**
17543 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17544 * @phba: Pointer to HBA context object.
17545 *
17546 * This function is called with no lock held. This function processes all
17547 * the received buffers and gives it to upper layers when a received buffer
17548 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017549 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017550 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17551 * appropriate receive function when the final frame in a sequence is received.
17552 **/
James Smart4d9ab992009-10-02 15:16:39 -040017553void
17554lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17555 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017556{
James Smart4d9ab992009-10-02 15:16:39 -040017557 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017558 struct fc_frame_header *fc_hdr;
17559 struct lpfc_vport *vport;
17560 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017561 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017562
James Smart4f774512009-05-22 14:52:35 -040017563 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017564 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017565
James Smart4d9ab992009-10-02 15:16:39 -040017566 /* check to see if this a valid type of frame */
17567 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17568 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17569 return;
17570 }
James Smart2ea259e2017-02-12 13:52:27 -080017571
James Smart7851fe22011-07-22 18:36:52 -040017572 if ((bf_get(lpfc_cqe_code,
17573 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17574 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17575 &dmabuf->cq_event.cqe.rcqe_cmpl);
17576 else
17577 fcfi = bf_get(lpfc_rcqe_fcf_id,
17578 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017579
James Smartae9e28f2017-05-15 15:20:51 -070017580 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17581 vport = phba->pport;
17582 /* Handle MDS Loopback frames */
17583 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17584 return;
17585 }
17586
James Smart895427b2017-02-12 13:52:30 -080017587 /* d_id this frame is directed to */
17588 did = sli4_did_from_fc_hdr(fc_hdr);
17589
17590 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017591 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017592 /* throw out the frame */
17593 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17594 return;
17595 }
James Smart939723a2012-05-09 21:19:03 -040017596
James Smart939723a2012-05-09 21:19:03 -040017597 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17598 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17599 (did != Fabric_DID)) {
17600 /*
17601 * Throw out the frame if we are not pt2pt.
17602 * The pt2pt protocol allows for discovery frames
17603 * to be received without a registered VPI.
17604 */
17605 if (!(vport->fc_flag & FC_PT2PT) ||
17606 (phba->link_state == LPFC_HBA_READY)) {
17607 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17608 return;
17609 }
17610 }
17611
James Smart6669f9b2009-10-02 15:16:45 -040017612 /* Handle the basic abort sequence (BA_ABTS) event */
17613 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17614 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17615 return;
17616 }
17617
James Smart4d9ab992009-10-02 15:16:39 -040017618 /* Link this frame */
17619 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17620 if (!seq_dmabuf) {
17621 /* unable to add frame to vport - throw it out */
17622 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17623 return;
17624 }
17625 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017626 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017627 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017628
James Smart6669f9b2009-10-02 15:16:45 -040017629 /* Send the complete sequence to the upper layer protocol */
17630 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017631}
James Smart6fb120a2009-05-22 14:52:59 -040017632
17633/**
17634 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17635 * @phba: pointer to lpfc hba data structure.
17636 *
17637 * This routine is invoked to post rpi header templates to the
17638 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017639 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17640 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017641 *
17642 * This routine does not require any locks. It's usage is expected
17643 * to be driver load or reset recovery when the driver is
17644 * sequential.
17645 *
17646 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017647 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017648 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017649 * When this error occurs, the driver is not guaranteed
17650 * to have any rpi regions posted to the device and
17651 * must either attempt to repost the regions or take a
17652 * fatal error.
17653 **/
17654int
17655lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17656{
17657 struct lpfc_rpi_hdr *rpi_page;
17658 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017659 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017660
James Smart6d368e52011-05-24 11:44:12 -040017661 /* SLI4 ports that support extents do not require RPI headers. */
17662 if (!phba->sli4_hba.rpi_hdrs_in_use)
17663 goto exit;
17664 if (phba->sli4_hba.extents_in_use)
17665 return -EIO;
17666
James Smart6fb120a2009-05-22 14:52:59 -040017667 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017668 /*
17669 * Assign the rpi headers a physical rpi only if the driver
17670 * has not initialized those resources. A port reset only
17671 * needs the headers posted.
17672 */
17673 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17674 LPFC_RPI_RSRC_RDY)
17675 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17676
James Smart6fb120a2009-05-22 14:52:59 -040017677 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17678 if (rc != MBX_SUCCESS) {
17679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17680 "2008 Error %d posting all rpi "
17681 "headers\n", rc);
17682 rc = -EIO;
17683 break;
17684 }
17685 }
17686
James Smart6d368e52011-05-24 11:44:12 -040017687 exit:
17688 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17689 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017690 return rc;
17691}
17692
17693/**
17694 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17695 * @phba: pointer to lpfc hba data structure.
17696 * @rpi_page: pointer to the rpi memory region.
17697 *
17698 * This routine is invoked to post a single rpi header to the
17699 * HBA consistent with the SLI-4 interface spec. This memory region
17700 * maps up to 64 rpi context regions.
17701 *
17702 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017703 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017704 * -ENOMEM - No available memory
17705 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017706 **/
17707int
17708lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17709{
17710 LPFC_MBOXQ_t *mboxq;
17711 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17712 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017713 uint32_t shdr_status, shdr_add_status;
17714 union lpfc_sli4_cfg_shdr *shdr;
17715
James Smart6d368e52011-05-24 11:44:12 -040017716 /* SLI4 ports that support extents do not require RPI headers. */
17717 if (!phba->sli4_hba.rpi_hdrs_in_use)
17718 return rc;
17719 if (phba->sli4_hba.extents_in_use)
17720 return -EIO;
17721
James Smart6fb120a2009-05-22 14:52:59 -040017722 /* The port is notified of the header region via a mailbox command. */
17723 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17724 if (!mboxq) {
17725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17726 "2001 Unable to allocate memory for issuing "
17727 "SLI_CONFIG_SPECIAL mailbox command\n");
17728 return -ENOMEM;
17729 }
17730
17731 /* Post all rpi memory regions to the port. */
17732 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017733 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17734 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17735 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017736 sizeof(struct lpfc_sli4_cfg_mhdr),
17737 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017738
17739
17740 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017741 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17742 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017743 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17744 hdr_tmpl, rpi_page->page_count);
17745
James Smart6fb120a2009-05-22 14:52:59 -040017746 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17747 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017748 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017749 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17750 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17751 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17752 if (rc != MBX_TIMEOUT)
17753 mempool_free(mboxq, phba->mbox_mem_pool);
17754 if (shdr_status || shdr_add_status || rc) {
17755 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17756 "2514 POST_RPI_HDR mailbox failed with "
17757 "status x%x add_status x%x, mbx status x%x\n",
17758 shdr_status, shdr_add_status, rc);
17759 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017760 } else {
17761 /*
17762 * The next_rpi stores the next logical module-64 rpi value used
17763 * to post physical rpis in subsequent rpi postings.
17764 */
17765 spin_lock_irq(&phba->hbalock);
17766 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17767 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017768 }
17769 return rc;
17770}
17771
17772/**
17773 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17774 * @phba: pointer to lpfc hba data structure.
17775 *
17776 * This routine is invoked to post rpi header templates to the
17777 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017778 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17779 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017780 *
17781 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017782 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017783 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17784 **/
17785int
17786lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17787{
James Smart6d368e52011-05-24 11:44:12 -040017788 unsigned long rpi;
17789 uint16_t max_rpi, rpi_limit;
17790 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017791 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017792 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017793
James Smart6fb120a2009-05-22 14:52:59 -040017794 /*
James Smart6d368e52011-05-24 11:44:12 -040017795 * Fetch the next logical rpi. Because this index is logical,
17796 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017797 */
James Smart4902b382013-10-10 12:20:35 -040017798 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017799 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17800 rpi_limit = phba->sli4_hba.next_rpi;
17801
James Smart6d368e52011-05-24 11:44:12 -040017802 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17803 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017804 rpi = LPFC_RPI_ALLOC_ERROR;
17805 else {
17806 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17807 phba->sli4_hba.max_cfg_param.rpi_used++;
17808 phba->sli4_hba.rpi_count++;
17809 }
James Smartbe6bb942015-04-07 15:07:22 -040017810 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17811 "0001 rpi:%x max:%x lim:%x\n",
17812 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017813
17814 /*
17815 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017816 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017817 */
17818 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17819 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017820 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017821 return rpi;
17822 }
17823
17824 /*
James Smart6d368e52011-05-24 11:44:12 -040017825 * RPI header postings are not required for SLI4 ports capable of
17826 * extents.
17827 */
17828 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017829 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017830 return rpi;
17831 }
17832
17833 /*
James Smart6fb120a2009-05-22 14:52:59 -040017834 * If the driver is running low on rpi resources, allocate another
17835 * page now. Note that the next_rpi value is used because
17836 * it represents how many are actually in use whereas max_rpi notes
17837 * how many are supported max by the device.
17838 */
James Smart6d368e52011-05-24 11:44:12 -040017839 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017840 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017841 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17842 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17843 if (!rpi_hdr) {
17844 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17845 "2002 Error Could not grow rpi "
17846 "count\n");
17847 } else {
James Smart6d368e52011-05-24 11:44:12 -040017848 lrpi = rpi_hdr->start_rpi;
17849 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017850 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17851 }
17852 }
17853
17854 return rpi;
17855}
17856
17857/**
17858 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17859 * @phba: pointer to lpfc hba data structure.
17860 *
17861 * This routine is invoked to release an rpi to the pool of
17862 * available rpis maintained by the driver.
17863 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017864static void
James Smartd7c47992010-06-08 18:31:54 -040017865__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17866{
17867 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17868 phba->sli4_hba.rpi_count--;
17869 phba->sli4_hba.max_cfg_param.rpi_used--;
17870 }
17871}
17872
17873/**
17874 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17875 * @phba: pointer to lpfc hba data structure.
17876 *
17877 * This routine is invoked to release an rpi to the pool of
17878 * available rpis maintained by the driver.
17879 **/
17880void
James Smart6fb120a2009-05-22 14:52:59 -040017881lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17882{
17883 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017884 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017885 spin_unlock_irq(&phba->hbalock);
17886}
17887
17888/**
17889 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17890 * @phba: pointer to lpfc hba data structure.
17891 *
17892 * This routine is invoked to remove the memory region that
17893 * provided rpi via a bitmask.
17894 **/
17895void
17896lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17897{
17898 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017899 kfree(phba->sli4_hba.rpi_ids);
17900 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017901}
17902
17903/**
17904 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17905 * @phba: pointer to lpfc hba data structure.
17906 *
17907 * This routine is invoked to remove the memory region that
17908 * provided rpi via a bitmask.
17909 **/
17910int
James Smart6b5151f2012-01-18 16:24:06 -050017911lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17912 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017913{
17914 LPFC_MBOXQ_t *mboxq;
17915 struct lpfc_hba *phba = ndlp->phba;
17916 int rc;
17917
17918 /* The port is notified of the header region via a mailbox command. */
17919 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17920 if (!mboxq)
17921 return -ENOMEM;
17922
17923 /* Post all rpi memory regions to the port. */
17924 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017925 if (cmpl) {
17926 mboxq->mbox_cmpl = cmpl;
17927 mboxq->context1 = arg;
17928 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017929 } else
17930 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017931 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017932 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17933 if (rc == MBX_NOT_FINISHED) {
17934 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17935 "2010 Resume RPI Mailbox failed "
17936 "status %d, mbxStatus x%x\n", rc,
17937 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17938 mempool_free(mboxq, phba->mbox_mem_pool);
17939 return -EIO;
17940 }
17941 return 0;
17942}
17943
17944/**
17945 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017946 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017947 *
James Smart76a95d72010-11-20 23:11:48 -050017948 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017949 *
17950 * Returns:
17951 * 0 success
17952 * -Evalue otherwise
17953 **/
17954int
James Smart76a95d72010-11-20 23:11:48 -050017955lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017956{
17957 LPFC_MBOXQ_t *mboxq;
17958 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017959 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017960 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017961 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017962 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17963 if (!mboxq)
17964 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017965 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017966 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017967 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017968 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017969 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017970 "2022 INIT VPI Mailbox failed "
17971 "status %d, mbxStatus x%x\n", rc,
17972 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017973 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017974 }
James Smart6a9c52c2009-10-02 15:16:51 -040017975 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017976 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017977
17978 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017979}
17980
17981/**
17982 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17983 * @phba: pointer to lpfc hba data structure.
17984 * @mboxq: Pointer to mailbox object.
17985 *
17986 * This routine is invoked to manually add a single FCF record. The caller
17987 * must pass a completely initialized FCF_Record. This routine takes
17988 * care of the nonembedded mailbox operations.
17989 **/
17990static void
17991lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17992{
17993 void *virt_addr;
17994 union lpfc_sli4_cfg_shdr *shdr;
17995 uint32_t shdr_status, shdr_add_status;
17996
17997 virt_addr = mboxq->sge_array->addr[0];
17998 /* The IOCTL status is embedded in the mailbox subheader. */
17999 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18000 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18001 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18002
18003 if ((shdr_status || shdr_add_status) &&
18004 (shdr_status != STATUS_FCF_IN_USE))
18005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18006 "2558 ADD_FCF_RECORD mailbox failed with "
18007 "status x%x add_status x%x\n",
18008 shdr_status, shdr_add_status);
18009
18010 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18011}
18012
18013/**
18014 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18015 * @phba: pointer to lpfc hba data structure.
18016 * @fcf_record: pointer to the initialized fcf record to add.
18017 *
18018 * This routine is invoked to manually add a single FCF record. The caller
18019 * must pass a completely initialized FCF_Record. This routine takes
18020 * care of the nonembedded mailbox operations.
18021 **/
18022int
18023lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18024{
18025 int rc = 0;
18026 LPFC_MBOXQ_t *mboxq;
18027 uint8_t *bytep;
18028 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040018029 struct lpfc_mbx_sge sge;
18030 uint32_t alloc_len, req_len;
18031 uint32_t fcfindex;
18032
18033 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18034 if (!mboxq) {
18035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18036 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18037 return -ENOMEM;
18038 }
18039
18040 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18041 sizeof(uint32_t);
18042
18043 /* Allocate DMA memory and set up the non-embedded mailbox command */
18044 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18045 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18046 req_len, LPFC_SLI4_MBX_NEMBED);
18047 if (alloc_len < req_len) {
18048 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18049 "2523 Allocated DMA memory size (x%x) is "
18050 "less than the requested DMA memory "
18051 "size (x%x)\n", alloc_len, req_len);
18052 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18053 return -ENOMEM;
18054 }
18055
18056 /*
18057 * Get the first SGE entry from the non-embedded DMA memory. This
18058 * routine only uses a single SGE.
18059 */
18060 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018061 virt_addr = mboxq->sge_array->addr[0];
18062 /*
18063 * Configure the FCF record for FCFI 0. This is the driver's
18064 * hardcoded default and gets used in nonFIP mode.
18065 */
18066 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18067 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18068 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18069
18070 /*
18071 * Copy the fcf_index and the FCF Record Data. The data starts after
18072 * the FCoE header plus word10. The data copy needs to be endian
18073 * correct.
18074 */
18075 bytep += sizeof(uint32_t);
18076 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18077 mboxq->vport = phba->pport;
18078 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18079 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18080 if (rc == MBX_NOT_FINISHED) {
18081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18082 "2515 ADD_FCF_RECORD mailbox failed with "
18083 "status 0x%x\n", rc);
18084 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18085 rc = -EIO;
18086 } else
18087 rc = 0;
18088
18089 return rc;
18090}
18091
18092/**
18093 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18094 * @phba: pointer to lpfc hba data structure.
18095 * @fcf_record: pointer to the fcf record to write the default data.
18096 * @fcf_index: FCF table entry index.
18097 *
18098 * This routine is invoked to build the driver's default FCF record. The
18099 * values used are hardcoded. This routine handles memory initialization.
18100 *
18101 **/
18102void
18103lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18104 struct fcf_record *fcf_record,
18105 uint16_t fcf_index)
18106{
18107 memset(fcf_record, 0, sizeof(struct fcf_record));
18108 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18109 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18110 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18111 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18112 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18113 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18114 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18115 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18116 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18117 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18118 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18119 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18120 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018121 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018122 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18123 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18124 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18125 /* Set the VLAN bit map */
18126 if (phba->valid_vlan) {
18127 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18128 = 1 << (phba->vlan_id % 8);
18129 }
18130}
18131
18132/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018133 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018134 * @phba: pointer to lpfc hba data structure.
18135 * @fcf_index: FCF table entry offset.
18136 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018137 * This routine is invoked to scan the entire FCF table by reading FCF
18138 * record and processing it one at a time starting from the @fcf_index
18139 * for initial FCF discovery or fast FCF failover rediscovery.
18140 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018141 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018142 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018143 **/
18144int
James Smart0c9ab6f2010-02-26 14:15:57 -050018145lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018146{
18147 int rc = 0, error;
18148 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018149
James Smart32b97932009-07-19 10:01:21 -040018150 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018151 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018152 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18153 if (!mboxq) {
18154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18155 "2000 Failed to allocate mbox for "
18156 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018157 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018158 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018159 }
James Smartecfd03c2010-02-12 14:41:27 -050018160 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018161 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018162 if (rc) {
18163 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018164 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018165 }
James Smartecfd03c2010-02-12 14:41:27 -050018166 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018167 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018168 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018169
18170 spin_lock_irq(&phba->hbalock);
18171 phba->hba_flag |= FCF_TS_INPROG;
18172 spin_unlock_irq(&phba->hbalock);
18173
James Smart6fb120a2009-05-22 14:52:59 -040018174 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018175 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018176 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018177 else {
James Smart38b92ef2010-08-04 16:11:39 -040018178 /* Reset eligible FCF count for new scan */
18179 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018180 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018181 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018182 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018183fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018184 if (error) {
18185 if (mboxq)
18186 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018187 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018188 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018189 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018190 spin_unlock_irq(&phba->hbalock);
18191 }
James Smart6fb120a2009-05-22 14:52:59 -040018192 return error;
18193}
James Smarta0c87cb2009-07-19 10:01:10 -040018194
18195/**
James Smarta93ff372010-10-22 11:06:08 -040018196 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018197 * @phba: pointer to lpfc hba data structure.
18198 * @fcf_index: FCF table entry offset.
18199 *
18200 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018201 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018202 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018203 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018204 * otherwise.
18205 **/
18206int
18207lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18208{
18209 int rc = 0, error;
18210 LPFC_MBOXQ_t *mboxq;
18211
18212 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18213 if (!mboxq) {
18214 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18215 "2763 Failed to allocate mbox for "
18216 "READ_FCF cmd\n");
18217 error = -ENOMEM;
18218 goto fail_fcf_read;
18219 }
18220 /* Construct the read FCF record mailbox command */
18221 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18222 if (rc) {
18223 error = -EINVAL;
18224 goto fail_fcf_read;
18225 }
18226 /* Issue the mailbox command asynchronously */
18227 mboxq->vport = phba->pport;
18228 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18229 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18230 if (rc == MBX_NOT_FINISHED)
18231 error = -EIO;
18232 else
18233 error = 0;
18234
18235fail_fcf_read:
18236 if (error && mboxq)
18237 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18238 return error;
18239}
18240
18241/**
18242 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18243 * @phba: pointer to lpfc hba data structure.
18244 * @fcf_index: FCF table entry offset.
18245 *
18246 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018247 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018248 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018249 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018250 * otherwise.
18251 **/
18252int
18253lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18254{
18255 int rc = 0, error;
18256 LPFC_MBOXQ_t *mboxq;
18257
18258 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18259 if (!mboxq) {
18260 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18261 "2758 Failed to allocate mbox for "
18262 "READ_FCF cmd\n");
18263 error = -ENOMEM;
18264 goto fail_fcf_read;
18265 }
18266 /* Construct the read FCF record mailbox command */
18267 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18268 if (rc) {
18269 error = -EINVAL;
18270 goto fail_fcf_read;
18271 }
18272 /* Issue the mailbox command asynchronously */
18273 mboxq->vport = phba->pport;
18274 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18275 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18276 if (rc == MBX_NOT_FINISHED)
18277 error = -EIO;
18278 else
18279 error = 0;
18280
18281fail_fcf_read:
18282 if (error && mboxq)
18283 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18284 return error;
18285}
18286
18287/**
James Smartf5cb5302015-12-16 18:11:52 -050018288 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018289 * phba pointer to the lpfc_hba struct for this port.
18290 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18291 * routine when the rr_bmask is empty. The FCF indecies are put into the
18292 * rr_bmask based on their priority level. Starting from the highest priority
18293 * to the lowest. The most likely FCF candidate will be in the highest
18294 * priority group. When this routine is called it searches the fcf_pri list for
18295 * next lowest priority group and repopulates the rr_bmask with only those
18296 * fcf_indexes.
18297 * returns:
18298 * 1=success 0=failure
18299 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018300static int
James Smart7d791df2011-07-22 18:37:52 -040018301lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18302{
18303 uint16_t next_fcf_pri;
18304 uint16_t last_index;
18305 struct lpfc_fcf_pri *fcf_pri;
18306 int rc;
18307 int ret = 0;
18308
18309 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18310 LPFC_SLI4_FCF_TBL_INDX_MAX);
18311 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18312 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018313
18314 /* Verify the priority list has 2 or more entries */
18315 spin_lock_irq(&phba->hbalock);
18316 if (list_empty(&phba->fcf.fcf_pri_list) ||
18317 list_is_singular(&phba->fcf.fcf_pri_list)) {
18318 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018319 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18320 "3061 Last IDX %d\n", last_index);
18321 return 0; /* Empty rr list */
18322 }
James Smart25626692013-03-01 16:36:54 -050018323 spin_unlock_irq(&phba->hbalock);
18324
James Smart7d791df2011-07-22 18:37:52 -040018325 next_fcf_pri = 0;
18326 /*
18327 * Clear the rr_bmask and set all of the bits that are at this
18328 * priority.
18329 */
18330 memset(phba->fcf.fcf_rr_bmask, 0,
18331 sizeof(*phba->fcf.fcf_rr_bmask));
18332 spin_lock_irq(&phba->hbalock);
18333 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18334 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18335 continue;
18336 /*
18337 * the 1st priority that has not FLOGI failed
18338 * will be the highest.
18339 */
18340 if (!next_fcf_pri)
18341 next_fcf_pri = fcf_pri->fcf_rec.priority;
18342 spin_unlock_irq(&phba->hbalock);
18343 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18344 rc = lpfc_sli4_fcf_rr_index_set(phba,
18345 fcf_pri->fcf_rec.fcf_index);
18346 if (rc)
18347 return 0;
18348 }
18349 spin_lock_irq(&phba->hbalock);
18350 }
18351 /*
18352 * if next_fcf_pri was not set above and the list is not empty then
18353 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018354 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018355 */
18356 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18357 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18358 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18359 /*
18360 * the 1st priority that has not FLOGI failed
18361 * will be the highest.
18362 */
18363 if (!next_fcf_pri)
18364 next_fcf_pri = fcf_pri->fcf_rec.priority;
18365 spin_unlock_irq(&phba->hbalock);
18366 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18367 rc = lpfc_sli4_fcf_rr_index_set(phba,
18368 fcf_pri->fcf_rec.fcf_index);
18369 if (rc)
18370 return 0;
18371 }
18372 spin_lock_irq(&phba->hbalock);
18373 }
18374 } else
18375 ret = 1;
18376 spin_unlock_irq(&phba->hbalock);
18377
18378 return ret;
18379}
18380/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018381 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18382 * @phba: pointer to lpfc hba data structure.
18383 *
18384 * This routine is to get the next eligible FCF record index in a round
18385 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018386 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018387 * shall be returned, otherwise, the next eligible FCF record's index
18388 * shall be returned.
18389 **/
18390uint16_t
18391lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18392{
18393 uint16_t next_fcf_index;
18394
James Smart421c6622013-01-03 15:44:16 -050018395initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018396 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018397 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18398
James Smart7d791df2011-07-22 18:37:52 -040018399next_priority:
James Smart421c6622013-01-03 15:44:16 -050018400 /* Determine the next fcf index to check */
18401 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018402 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18403 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018404 next_fcf_index);
18405
James Smart0c9ab6f2010-02-26 14:15:57 -050018406 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018407 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18408 /*
18409 * If we have wrapped then we need to clear the bits that
18410 * have been tested so that we can detect when we should
18411 * change the priority level.
18412 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018413 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18414 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018415 }
18416
James Smart0c9ab6f2010-02-26 14:15:57 -050018417
James Smart3804dc82010-07-14 15:31:37 -040018418 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018419 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18420 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18421 /*
18422 * If next fcf index is not found check if there are lower
18423 * Priority level fcf's in the fcf_priority list.
18424 * Set up the rr_bmask with all of the avaiable fcf bits
18425 * at that level and continue the selection process.
18426 */
18427 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018428 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018429 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18430 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040018431 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18432 return LPFC_FCOE_FCF_NEXT_NONE;
18433 else {
18434 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18435 "3063 Only FCF available idx %d, flag %x\n",
18436 next_fcf_index,
18437 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18438 return next_fcf_index;
18439 }
James Smart3804dc82010-07-14 15:31:37 -040018440 }
18441
James Smart7d791df2011-07-22 18:37:52 -040018442 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18443 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018444 LPFC_FCF_FLOGI_FAILED) {
18445 if (list_is_singular(&phba->fcf.fcf_pri_list))
18446 return LPFC_FCOE_FCF_NEXT_NONE;
18447
James Smart7d791df2011-07-22 18:37:52 -040018448 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018449 }
James Smart7d791df2011-07-22 18:37:52 -040018450
James Smart3804dc82010-07-14 15:31:37 -040018451 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018452 "2845 Get next roundrobin failover FCF (x%x)\n",
18453 next_fcf_index);
18454
James Smart0c9ab6f2010-02-26 14:15:57 -050018455 return next_fcf_index;
18456}
18457
18458/**
18459 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18460 * @phba: pointer to lpfc hba data structure.
18461 *
18462 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018463 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018464 * does not go beyond the range of the driver allocated bmask dimension
18465 * before setting the bit.
18466 *
18467 * Returns 0 if the index bit successfully set, otherwise, it returns
18468 * -EINVAL.
18469 **/
18470int
18471lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18472{
18473 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18474 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018475 "2610 FCF (x%x) reached driver's book "
18476 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018477 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18478 return -EINVAL;
18479 }
18480 /* Set the eligible FCF record index bmask */
18481 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18482
James Smart3804dc82010-07-14 15:31:37 -040018483 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018484 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018485 "bmask\n", fcf_index);
18486
James Smart0c9ab6f2010-02-26 14:15:57 -050018487 return 0;
18488}
18489
18490/**
James Smart3804dc82010-07-14 15:31:37 -040018491 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018492 * @phba: pointer to lpfc hba data structure.
18493 *
18494 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018495 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018496 * does not go beyond the range of the driver allocated bmask dimension
18497 * before clearing the bit.
18498 **/
18499void
18500lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18501{
James Smart9a803a72013-09-06 12:17:56 -040018502 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018503 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18504 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018505 "2762 FCF (x%x) reached driver's book "
18506 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018507 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18508 return;
18509 }
18510 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018511 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018512 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18513 list) {
James Smart7d791df2011-07-22 18:37:52 -040018514 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18515 list_del_init(&fcf_pri->list);
18516 break;
18517 }
18518 }
18519 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018520 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018521
18522 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018523 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018524 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018525}
18526
18527/**
James Smartecfd03c2010-02-12 14:41:27 -050018528 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18529 * @phba: pointer to lpfc hba data structure.
18530 *
18531 * This routine is the completion routine for the rediscover FCF table mailbox
18532 * command. If the mailbox command returned failure, it will try to stop the
18533 * FCF rediscover wait timer.
18534 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018535static void
James Smartecfd03c2010-02-12 14:41:27 -050018536lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18537{
18538 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18539 uint32_t shdr_status, shdr_add_status;
18540
18541 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18542
18543 shdr_status = bf_get(lpfc_mbox_hdr_status,
18544 &redisc_fcf->header.cfg_shdr.response);
18545 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18546 &redisc_fcf->header.cfg_shdr.response);
18547 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018548 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018549 "2746 Requesting for FCF rediscovery failed "
18550 "status x%x add_status x%x\n",
18551 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018552 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018553 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018554 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018555 spin_unlock_irq(&phba->hbalock);
18556 /*
18557 * CVL event triggered FCF rediscover request failed,
18558 * last resort to re-try current registered FCF entry.
18559 */
18560 lpfc_retry_pport_discovery(phba);
18561 } else {
18562 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018563 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018564 spin_unlock_irq(&phba->hbalock);
18565 /*
18566 * DEAD FCF event triggered FCF rediscover request
18567 * failed, last resort to fail over as a link down
18568 * to FCF registration.
18569 */
18570 lpfc_sli4_fcf_dead_failthrough(phba);
18571 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018572 } else {
18573 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018574 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018575 /*
18576 * Start FCF rediscovery wait timer for pending FCF
18577 * before rescan FCF record table.
18578 */
18579 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018580 }
James Smartecfd03c2010-02-12 14:41:27 -050018581
18582 mempool_free(mbox, phba->mbox_mem_pool);
18583}
18584
18585/**
James Smart3804dc82010-07-14 15:31:37 -040018586 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018587 * @phba: pointer to lpfc hba data structure.
18588 *
18589 * This routine is invoked to request for rediscovery of the entire FCF table
18590 * by the port.
18591 **/
18592int
18593lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18594{
18595 LPFC_MBOXQ_t *mbox;
18596 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18597 int rc, length;
18598
James Smart0c9ab6f2010-02-26 14:15:57 -050018599 /* Cancel retry delay timers to all vports before FCF rediscover */
18600 lpfc_cancel_all_vport_retry_delay_timer(phba);
18601
James Smartecfd03c2010-02-12 14:41:27 -050018602 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18603 if (!mbox) {
18604 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18605 "2745 Failed to allocate mbox for "
18606 "requesting FCF rediscover.\n");
18607 return -ENOMEM;
18608 }
18609
18610 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18611 sizeof(struct lpfc_sli4_cfg_mhdr));
18612 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18613 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18614 length, LPFC_SLI4_MBX_EMBED);
18615
18616 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18617 /* Set count to 0 for invalidating the entire FCF database */
18618 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18619
18620 /* Issue the mailbox command asynchronously */
18621 mbox->vport = phba->pport;
18622 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18623 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18624
18625 if (rc == MBX_NOT_FINISHED) {
18626 mempool_free(mbox, phba->mbox_mem_pool);
18627 return -EIO;
18628 }
18629 return 0;
18630}
18631
18632/**
James Smartfc2b9892010-02-26 14:15:29 -050018633 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18634 * @phba: pointer to lpfc hba data structure.
18635 *
18636 * This function is the failover routine as a last resort to the FCF DEAD
18637 * event when driver failed to perform fast FCF failover.
18638 **/
18639void
18640lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18641{
18642 uint32_t link_state;
18643
18644 /*
18645 * Last resort as FCF DEAD event failover will treat this as
18646 * a link down, but save the link state because we don't want
18647 * it to be changed to Link Down unless it is already down.
18648 */
18649 link_state = phba->link_state;
18650 lpfc_linkdown(phba);
18651 phba->link_state = link_state;
18652
18653 /* Unregister FCF if no devices connected to it */
18654 lpfc_unregister_unused_fcf(phba);
18655}
18656
18657/**
James Smart026abb82011-12-13 13:20:45 -050018658 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018659 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018660 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018661 *
James Smart026abb82011-12-13 13:20:45 -050018662 * This function gets SLI3 port configure region 23 data through memory dump
18663 * mailbox command. When it successfully retrieves data, the size of the data
18664 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018665 **/
James Smart026abb82011-12-13 13:20:45 -050018666static uint32_t
18667lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018668{
18669 LPFC_MBOXQ_t *pmb = NULL;
18670 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018671 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018672 int rc;
18673
James Smart026abb82011-12-13 13:20:45 -050018674 if (!rgn23_data)
18675 return 0;
18676
James Smarta0c87cb2009-07-19 10:01:10 -040018677 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18678 if (!pmb) {
18679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018680 "2600 failed to allocate mailbox memory\n");
18681 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018682 }
18683 mb = &pmb->u.mb;
18684
James Smarta0c87cb2009-07-19 10:01:10 -040018685 do {
18686 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18687 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18688
18689 if (rc != MBX_SUCCESS) {
18690 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018691 "2601 failed to read config "
18692 "region 23, rc 0x%x Status 0x%x\n",
18693 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018694 mb->un.varDmp.word_cnt = 0;
18695 }
18696 /*
18697 * dump mem may return a zero when finished or we got a
18698 * mailbox error, either way we are done.
18699 */
18700 if (mb->un.varDmp.word_cnt == 0)
18701 break;
18702 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18703 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18704
18705 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018706 rgn23_data + offset,
18707 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018708 offset += mb->un.varDmp.word_cnt;
18709 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18710
James Smart026abb82011-12-13 13:20:45 -050018711 mempool_free(pmb, phba->mbox_mem_pool);
18712 return offset;
18713}
18714
18715/**
18716 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18717 * @phba: pointer to lpfc hba data structure.
18718 * @rgn23_data: pointer to configure region 23 data.
18719 *
18720 * This function gets SLI4 port configure region 23 data through memory dump
18721 * mailbox command. When it successfully retrieves data, the size of the data
18722 * will be returned, otherwise, 0 will be returned.
18723 **/
18724static uint32_t
18725lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18726{
18727 LPFC_MBOXQ_t *mboxq = NULL;
18728 struct lpfc_dmabuf *mp = NULL;
18729 struct lpfc_mqe *mqe;
18730 uint32_t data_length = 0;
18731 int rc;
18732
18733 if (!rgn23_data)
18734 return 0;
18735
18736 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18737 if (!mboxq) {
18738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18739 "3105 failed to allocate mailbox memory\n");
18740 return 0;
18741 }
18742
18743 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18744 goto out;
18745 mqe = &mboxq->u.mqe;
18746 mp = (struct lpfc_dmabuf *) mboxq->context1;
18747 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18748 if (rc)
18749 goto out;
18750 data_length = mqe->un.mb_words[5];
18751 if (data_length == 0)
18752 goto out;
18753 if (data_length > DMP_RGN23_SIZE) {
18754 data_length = 0;
18755 goto out;
18756 }
18757 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18758out:
18759 mempool_free(mboxq, phba->mbox_mem_pool);
18760 if (mp) {
18761 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18762 kfree(mp);
18763 }
18764 return data_length;
18765}
18766
18767/**
18768 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18769 * @phba: pointer to lpfc hba data structure.
18770 *
18771 * This function read region 23 and parse TLV for port status to
18772 * decide if the user disaled the port. If the TLV indicates the
18773 * port is disabled, the hba_flag is set accordingly.
18774 **/
18775void
18776lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18777{
18778 uint8_t *rgn23_data = NULL;
18779 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18780 uint32_t offset = 0;
18781
18782 /* Get adapter Region 23 data */
18783 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18784 if (!rgn23_data)
18785 goto out;
18786
18787 if (phba->sli_rev < LPFC_SLI_REV4)
18788 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18789 else {
18790 if_type = bf_get(lpfc_sli_intf_if_type,
18791 &phba->sli4_hba.sli_intf);
18792 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18793 goto out;
18794 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18795 }
James Smarta0c87cb2009-07-19 10:01:10 -040018796
18797 if (!data_size)
18798 goto out;
18799
18800 /* Check the region signature first */
18801 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18803 "2619 Config region 23 has bad signature\n");
18804 goto out;
18805 }
18806 offset += 4;
18807
18808 /* Check the data structure version */
18809 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18811 "2620 Config region 23 has bad version\n");
18812 goto out;
18813 }
18814 offset += 4;
18815
18816 /* Parse TLV entries in the region */
18817 while (offset < data_size) {
18818 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18819 break;
18820 /*
18821 * If the TLV is not driver specific TLV or driver id is
18822 * not linux driver id, skip the record.
18823 */
18824 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18825 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18826 (rgn23_data[offset + 3] != 0)) {
18827 offset += rgn23_data[offset + 1] * 4 + 4;
18828 continue;
18829 }
18830
18831 /* Driver found a driver specific TLV in the config region */
18832 sub_tlv_len = rgn23_data[offset + 1] * 4;
18833 offset += 4;
18834 tlv_offset = 0;
18835
18836 /*
18837 * Search for configured port state sub-TLV.
18838 */
18839 while ((offset < data_size) &&
18840 (tlv_offset < sub_tlv_len)) {
18841 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18842 offset += 4;
18843 tlv_offset += 4;
18844 break;
18845 }
18846 if (rgn23_data[offset] != PORT_STE_TYPE) {
18847 offset += rgn23_data[offset + 1] * 4 + 4;
18848 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18849 continue;
18850 }
18851
18852 /* This HBA contains PORT_STE configured */
18853 if (!rgn23_data[offset + 2])
18854 phba->hba_flag |= LINK_DISABLED;
18855
18856 goto out;
18857 }
18858 }
James Smart026abb82011-12-13 13:20:45 -050018859
James Smarta0c87cb2009-07-19 10:01:10 -040018860out:
James Smarta0c87cb2009-07-19 10:01:10 -040018861 kfree(rgn23_data);
18862 return;
18863}
James Smart695a8142010-01-26 23:08:03 -050018864
18865/**
James Smart52d52442011-05-24 11:42:45 -040018866 * lpfc_wr_object - write an object to the firmware
18867 * @phba: HBA structure that indicates port to create a queue on.
18868 * @dmabuf_list: list of dmabufs to write to the port.
18869 * @size: the total byte value of the objects to write to the port.
18870 * @offset: the current offset to be used to start the transfer.
18871 *
18872 * This routine will create a wr_object mailbox command to send to the port.
18873 * the mailbox command will be constructed using the dma buffers described in
18874 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18875 * BDEs that the imbedded mailbox can support. The @offset variable will be
18876 * used to indicate the starting offset of the transfer and will also return
18877 * the offset after the write object mailbox has completed. @size is used to
18878 * determine the end of the object and whether the eof bit should be set.
18879 *
18880 * Return 0 is successful and offset will contain the the new offset to use
18881 * for the next write.
18882 * Return negative value for error cases.
18883 **/
18884int
18885lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18886 uint32_t size, uint32_t *offset)
18887{
18888 struct lpfc_mbx_wr_object *wr_object;
18889 LPFC_MBOXQ_t *mbox;
18890 int rc = 0, i = 0;
18891 uint32_t shdr_status, shdr_add_status;
18892 uint32_t mbox_tmo;
18893 union lpfc_sli4_cfg_shdr *shdr;
18894 struct lpfc_dmabuf *dmabuf;
18895 uint32_t written = 0;
18896
18897 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18898 if (!mbox)
18899 return -ENOMEM;
18900
18901 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18902 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18903 sizeof(struct lpfc_mbx_wr_object) -
18904 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18905
18906 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18907 wr_object->u.request.write_offset = *offset;
18908 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18909 wr_object->u.request.object_name[0] =
18910 cpu_to_le32(wr_object->u.request.object_name[0]);
18911 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18912 list_for_each_entry(dmabuf, dmabuf_list, list) {
18913 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18914 break;
18915 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18916 wr_object->u.request.bde[i].addrHigh =
18917 putPaddrHigh(dmabuf->phys);
18918 if (written + SLI4_PAGE_SIZE >= size) {
18919 wr_object->u.request.bde[i].tus.f.bdeSize =
18920 (size - written);
18921 written += (size - written);
18922 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18923 } else {
18924 wr_object->u.request.bde[i].tus.f.bdeSize =
18925 SLI4_PAGE_SIZE;
18926 written += SLI4_PAGE_SIZE;
18927 }
18928 i++;
18929 }
18930 wr_object->u.request.bde_count = i;
18931 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18932 if (!phba->sli4_hba.intr_enable)
18933 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18934 else {
James Smarta183a152011-10-10 21:32:43 -040018935 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018936 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18937 }
18938 /* The IOCTL status is embedded in the mailbox subheader. */
18939 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18940 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18941 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18942 if (rc != MBX_TIMEOUT)
18943 mempool_free(mbox, phba->mbox_mem_pool);
18944 if (shdr_status || shdr_add_status || rc) {
18945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18946 "3025 Write Object mailbox failed with "
18947 "status x%x add_status x%x, mbx status x%x\n",
18948 shdr_status, shdr_add_status, rc);
18949 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080018950 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040018951 } else
18952 *offset += wr_object->u.response.actual_write_length;
18953 return rc;
18954}
18955
18956/**
James Smart695a8142010-01-26 23:08:03 -050018957 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18958 * @vport: pointer to vport data structure.
18959 *
18960 * This function iterate through the mailboxq and clean up all REG_LOGIN
18961 * and REG_VPI mailbox commands associated with the vport. This function
18962 * is called when driver want to restart discovery of the vport due to
18963 * a Clear Virtual Link event.
18964 **/
18965void
18966lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18967{
18968 struct lpfc_hba *phba = vport->phba;
18969 LPFC_MBOXQ_t *mb, *nextmb;
18970 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018971 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018972 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018973 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018974 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018975 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018976
James Smartd439d282010-09-29 11:18:45 -040018977 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018978 spin_lock_irq(&phba->hbalock);
18979 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18980 if (mb->vport != vport)
18981 continue;
18982
18983 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18984 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18985 continue;
18986
James Smartd439d282010-09-29 11:18:45 -040018987 list_del(&mb->list);
18988 list_add_tail(&mb->list, &mbox_cmd_list);
18989 }
18990 /* Clean up active mailbox command with the vport */
18991 mb = phba->sli.mbox_active;
18992 if (mb && (mb->vport == vport)) {
18993 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18994 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18995 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18996 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18997 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18998 /* Put reference count for delayed processing */
18999 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19000 /* Unregister the RPI when mailbox complete */
19001 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19002 }
19003 }
James Smart63e801c2010-11-20 23:14:19 -050019004 /* Cleanup any mailbox completions which are not yet processed */
19005 do {
19006 restart_loop = 0;
19007 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19008 /*
19009 * If this mailox is already processed or it is
19010 * for another vport ignore it.
19011 */
19012 if ((mb->vport != vport) ||
19013 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19014 continue;
19015
19016 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19017 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19018 continue;
19019
19020 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19021 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19022 ndlp = (struct lpfc_nodelist *)mb->context2;
19023 /* Unregister the RPI when mailbox complete */
19024 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19025 restart_loop = 1;
19026 spin_unlock_irq(&phba->hbalock);
19027 spin_lock(shost->host_lock);
19028 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19029 spin_unlock(shost->host_lock);
19030 spin_lock_irq(&phba->hbalock);
19031 break;
19032 }
19033 }
19034 } while (restart_loop);
19035
James Smartd439d282010-09-29 11:18:45 -040019036 spin_unlock_irq(&phba->hbalock);
19037
19038 /* Release the cleaned-up mailbox commands */
19039 while (!list_empty(&mbox_cmd_list)) {
19040 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050019041 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19042 mp = (struct lpfc_dmabuf *) (mb->context1);
19043 if (mp) {
19044 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19045 kfree(mp);
19046 }
James Smart78730cf2010-04-06 15:06:30 -040019047 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040019048 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019049 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019050 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019051 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019052 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019053 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019054 }
James Smart695a8142010-01-26 23:08:03 -050019055 }
James Smart695a8142010-01-26 23:08:03 -050019056 mempool_free(mb, phba->mbox_mem_pool);
19057 }
James Smartd439d282010-09-29 11:18:45 -040019058
19059 /* Release the ndlp with the cleaned-up active mailbox command */
19060 if (act_mbx_ndlp) {
19061 spin_lock(shost->host_lock);
19062 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19063 spin_unlock(shost->host_lock);
19064 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019065 }
James Smart695a8142010-01-26 23:08:03 -050019066}
19067
James Smart2a9bf3d2010-06-07 15:24:45 -040019068/**
19069 * lpfc_drain_txq - Drain the txq
19070 * @phba: Pointer to HBA context object.
19071 *
19072 * This function attempt to submit IOCBs on the txq
19073 * to the adapter. For SLI4 adapters, the txq contains
19074 * ELS IOCBs that have been deferred because the there
19075 * are no SGLs. This congestion can occur with large
19076 * vport counts during node discovery.
19077 **/
19078
19079uint32_t
19080lpfc_drain_txq(struct lpfc_hba *phba)
19081{
19082 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019083 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019084 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019085 unsigned long iflags = 0;
19086 char *fail_msg = NULL;
19087 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019088 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019089 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070019090 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040019091
James Smartdc19e3b2018-05-24 21:08:57 -070019092 if (phba->link_flag & LS_MDS_LOOPBACK) {
19093 /* MDS WQE are posted only to first WQ*/
19094 wq = phba->sli4_hba.fcp_wq[0];
19095 if (unlikely(!wq))
19096 return 0;
19097 pring = wq->pring;
19098 } else {
19099 wq = phba->sli4_hba.els_wq;
19100 if (unlikely(!wq))
19101 return 0;
19102 pring = lpfc_phba_elsring(phba);
19103 }
19104
19105 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019106 return 0;
James Smart895427b2017-02-12 13:52:30 -080019107
James Smart398d81c2013-05-31 17:04:19 -040019108 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019109 list_for_each_entry(piocbq, &pring->txq, list) {
19110 txq_cnt++;
19111 }
19112
19113 if (txq_cnt > pring->txq_max)
19114 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019115
James Smart398d81c2013-05-31 17:04:19 -040019116 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019117
James Smart0e9bb8d2013-03-01 16:35:12 -050019118 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019119 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019120
James Smart19ca7602010-11-20 23:11:55 -050019121 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019122 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019123 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019124 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19125 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019126 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019127 break;
19128 }
James Smart895427b2017-02-12 13:52:30 -080019129 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019130 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019131 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019132 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019133 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019134 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019135 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019136
19137 /* The xri and iocb resources secured,
19138 * attempt to issue request
19139 */
James Smart6d368e52011-05-24 11:44:12 -040019140 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019141 piocbq->sli4_xritag = sglq->sli4_xritag;
19142 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19143 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019144 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019145 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070019146 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019147 fail_msg = " - Wq is full";
19148 else
19149 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19150
19151 if (fail_msg) {
19152 /* Failed means we can't issue and need to cancel */
19153 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19154 "2822 IOCB failed %s iotag 0x%x "
19155 "xri 0x%x\n",
19156 fail_msg,
19157 piocbq->iotag, piocbq->sli4_xritag);
19158 list_add_tail(&piocbq->list, &completions);
19159 }
James Smart398d81c2013-05-31 17:04:19 -040019160 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019161 }
19162
James Smart2a9bf3d2010-06-07 15:24:45 -040019163 /* Cancel all the IOCBs that cannot be issued */
19164 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19165 IOERR_SLI_ABORTED);
19166
James Smart0e9bb8d2013-03-01 16:35:12 -050019167 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019168}
James Smart895427b2017-02-12 13:52:30 -080019169
19170/**
19171 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19172 * @phba: Pointer to HBA context object.
19173 * @pwqe: Pointer to command WQE.
19174 * @sglq: Pointer to the scatter gather queue object.
19175 *
19176 * This routine converts the bpl or bde that is in the WQE
19177 * to a sgl list for the sli4 hardware. The physical address
19178 * of the bpl/bde is converted back to a virtual address.
19179 * If the WQE contains a BPL then the list of BDE's is
19180 * converted to sli4_sge's. If the WQE contains a single
19181 * BDE then it is converted to a single sli_sge.
19182 * The WQE is still in cpu endianness so the contents of
19183 * the bpl can be used without byte swapping.
19184 *
19185 * Returns valid XRI = Success, NO_XRI = Failure.
19186 */
19187static uint16_t
19188lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19189 struct lpfc_sglq *sglq)
19190{
19191 uint16_t xritag = NO_XRI;
19192 struct ulp_bde64 *bpl = NULL;
19193 struct ulp_bde64 bde;
19194 struct sli4_sge *sgl = NULL;
19195 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080019196 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080019197 int numBdes = 0;
19198 int i = 0;
19199 uint32_t offset = 0; /* accumulated offset in the sg request list */
19200 int inbound = 0; /* number of sg reply entries inbound from firmware */
19201 uint32_t cmd;
19202
19203 if (!pwqeq || !sglq)
19204 return xritag;
19205
19206 sgl = (struct sli4_sge *)sglq->sgl;
19207 wqe = &pwqeq->wqe;
19208 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19209
19210 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19211 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19212 return sglq->sli4_xritag;
19213 numBdes = pwqeq->rsvd2;
19214 if (numBdes) {
19215 /* The addrHigh and addrLow fields within the WQE
19216 * have not been byteswapped yet so there is no
19217 * need to swap them back.
19218 */
19219 if (pwqeq->context3)
19220 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19221 else
19222 return xritag;
19223
19224 bpl = (struct ulp_bde64 *)dmabuf->virt;
19225 if (!bpl)
19226 return xritag;
19227
19228 for (i = 0; i < numBdes; i++) {
19229 /* Should already be byte swapped. */
19230 sgl->addr_hi = bpl->addrHigh;
19231 sgl->addr_lo = bpl->addrLow;
19232
19233 sgl->word2 = le32_to_cpu(sgl->word2);
19234 if ((i+1) == numBdes)
19235 bf_set(lpfc_sli4_sge_last, sgl, 1);
19236 else
19237 bf_set(lpfc_sli4_sge_last, sgl, 0);
19238 /* swap the size field back to the cpu so we
19239 * can assign it to the sgl.
19240 */
19241 bde.tus.w = le32_to_cpu(bpl->tus.w);
19242 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19243 /* The offsets in the sgl need to be accumulated
19244 * separately for the request and reply lists.
19245 * The request is always first, the reply follows.
19246 */
19247 switch (cmd) {
19248 case CMD_GEN_REQUEST64_WQE:
19249 /* add up the reply sg entries */
19250 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19251 inbound++;
19252 /* first inbound? reset the offset */
19253 if (inbound == 1)
19254 offset = 0;
19255 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19256 bf_set(lpfc_sli4_sge_type, sgl,
19257 LPFC_SGE_TYPE_DATA);
19258 offset += bde.tus.f.bdeSize;
19259 break;
19260 case CMD_FCP_TRSP64_WQE:
19261 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19262 bf_set(lpfc_sli4_sge_type, sgl,
19263 LPFC_SGE_TYPE_DATA);
19264 break;
19265 case CMD_FCP_TSEND64_WQE:
19266 case CMD_FCP_TRECEIVE64_WQE:
19267 bf_set(lpfc_sli4_sge_type, sgl,
19268 bpl->tus.f.bdeFlags);
19269 if (i < 3)
19270 offset = 0;
19271 else
19272 offset += bde.tus.f.bdeSize;
19273 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19274 break;
19275 }
19276 sgl->word2 = cpu_to_le32(sgl->word2);
19277 bpl++;
19278 sgl++;
19279 }
19280 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19281 /* The addrHigh and addrLow fields of the BDE have not
19282 * been byteswapped yet so they need to be swapped
19283 * before putting them in the sgl.
19284 */
19285 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19286 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19287 sgl->word2 = le32_to_cpu(sgl->word2);
19288 bf_set(lpfc_sli4_sge_last, sgl, 1);
19289 sgl->word2 = cpu_to_le32(sgl->word2);
19290 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19291 }
19292 return sglq->sli4_xritag;
19293}
19294
19295/**
19296 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19297 * @phba: Pointer to HBA context object.
19298 * @ring_number: Base sli ring number
19299 * @pwqe: Pointer to command WQE.
19300 **/
19301int
19302lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19303 struct lpfc_iocbq *pwqe)
19304{
James Smart205e8242018-03-05 12:04:03 -080019305 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019306 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019307 struct lpfc_queue *wq;
19308 struct lpfc_sglq *sglq;
19309 struct lpfc_sli_ring *pring;
19310 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019311 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019312
19313 /* NVME_LS and NVME_LS ABTS requests. */
19314 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19315 pring = phba->sli4_hba.nvmels_wq->pring;
19316 spin_lock_irqsave(&pring->ring_lock, iflags);
19317 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19318 if (!sglq) {
19319 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19320 return WQE_BUSY;
19321 }
19322 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19323 pwqe->sli4_xritag = sglq->sli4_xritag;
19324 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19325 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19326 return WQE_ERROR;
19327 }
19328 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19329 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019330 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19331 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019332 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019333 return ret;
James Smart895427b2017-02-12 13:52:30 -080019334 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019335
James Smart895427b2017-02-12 13:52:30 -080019336 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19337 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19338 return 0;
19339 }
19340
19341 /* NVME_FCREQ and NVME_ABTS requests */
19342 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19343 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19344 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19345
19346 spin_lock_irqsave(&pring->ring_lock, iflags);
19347 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19348 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19349 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019350 ret = lpfc_sli4_wq_put(wq, wqe);
19351 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019352 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019353 return ret;
James Smart895427b2017-02-12 13:52:30 -080019354 }
19355 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19356 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19357 return 0;
19358 }
19359
James Smartf358dd02017-02-12 13:52:34 -080019360 /* NVMET requests */
19361 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19362 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19363 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19364
19365 spin_lock_irqsave(&pring->ring_lock, iflags);
19366 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019367 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019368 if (pwqe->sli4_xritag == NO_XRI) {
19369 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19370 pwqe->sli4_xritag = sglq->sli4_xritag;
19371 }
19372 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19373 pwqe->sli4_xritag);
19374 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19375 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19376 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019377 ret = lpfc_sli4_wq_put(wq, wqe);
19378 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019379 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019380 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019381 }
19382 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19383 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19384 return 0;
19385 }
James Smart895427b2017-02-12 13:52:30 -080019386 return WQE_ERROR;
19387}